-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (122 loc) · 3.34 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"fmt"
"io/ioutil"
"time"
"gitlab.mirv.ninja/rcp/octopus"
"gopkg.in/yaml.v2"
_ "gopkg.in/yaml.v2"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
dat, err := ioutil.ReadFile("settings.yml")
if err != nil {
fmt.Println("Error loading settings.yml")
}
var config octopus.Config
err = yaml.Unmarshal([]byte(dat), &config)
if err != nil {
fmt.Println("Error Unmarshalling settings.yml, do you have a syntax error")
}
for _, o := range config.Servers {
//start := time.Now()
octo := octopus.NewOctopus(&o)
candidates := process(octo)
fmt.Println(fmt.Sprintf("==== Release candidates for %s ====", octo.Config.Name))
for _, c := range candidates {
fmt.Println(c)
}
//elapsed := time.Since(start)
//log.Printf("Execution took %s", elapsed)
}
}
func process(octo octopus.Octopus) []string {
var candidates []string
projects := octo.GetProjects()
for _, project := range projects {
if project.IsDisabled {
continue
}
var prodIds []int
var devIds []int
var devEnvId string //t
var prodEnvId string
var prodChanId string
var devChanid string
var latestProd octopus.Releases
var latestDev octopus.Releases
//Output project ID & Name
//fmt.Println(fmt.Sprintf("%s - %s", project.ID, project.Name))
projects := octo.GetProjectProgress(project.ID)
channels := octo.GetProjectChannels(project.ID)
//project := octo.GetProject("Projects-562")
for _, c := range channels.Items {
switch c.Name {
//case "Development":
case octo.Config.Channels.Dev:
devChanid = c.ID
//case "Production":
case octo.Config.Channels.Prod:
prodChanId = c.ID
}
}
for _, e := range projects.ChannelEnvironments[prodChanId] {
if e.Name == octo.Config.Envs.Test {
prodEnvId = e.ID
}
}
for _, e := range projects.ChannelEnvironments[devChanid] {
if e.Name == octo.Config.Envs.Test {
devEnvId = e.ID
}
}
for id, r := range projects.Releases {
switch r.Channel.Name {
//case "Development":
case octo.Config.Channels.Dev:
if _, ok := r.Deployments[devEnvId]; ok {
devIds = append(devIds, id)
}
//case "Production":
case octo.Config.Channels.Prod:
if _, ok := r.Deployments[prodEnvId]; ok {
prodIds = append(prodIds, id)
}
}
}
if len(prodIds) > 1 {
var lastTime time.Time
for _, id := range prodIds {
rel := projects.Releases[id]
if lastTime.Unix() < rel.Deployments[prodEnvId][0].CompletedTime.Unix() {
lastTime = rel.Deployments[prodEnvId][0].CompletedTime
latestProd = rel
}
}
} else if len(prodIds) > 0 {
latestProd = projects.Releases[prodIds[0]]
}
if len(devIds) > 1 {
var lastTime time.Time
for _, id := range devIds {
rel := projects.Releases[id]
if lastTime.Unix() < rel.Deployments[devEnvId][0].CompletedTime.Unix() {
lastTime = rel.Deployments[devEnvId][0].CompletedTime
latestDev = rel
}
}
}
if len(latestDev.Deployments[devEnvId]) > 0 && len(latestProd.Deployments[prodEnvId]) > 0 {
if latestDev.Deployments[devEnvId][0].CompletedTime.Unix() > latestProd.Deployments[prodEnvId][0].CompletedTime.Unix() {
candidates = append(candidates, fmt.Sprintf("%s %s", project.Name, latestDev.Deployments[devEnvId][0].ReleaseVersion))
} else {
// fmt.Println(fmt.Sprintf("%s Not canidate for release", project.Name))
}
}
}
return candidates
}