-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsession.go
220 lines (166 loc) · 6.15 KB
/
session.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package scribe
import (
"fmt"
"strings"
"time"
"github.com/payvision-development/scribe/freshservice"
"github.com/payvision-development/scribe/vss"
)
type state struct {
LastEvent *vss.Event
RequestItilChange *freshservice.RequestItilChange
ResponseItilChange *freshservice.ResponseItilChange
}
// Session func
func Session(tc uint32, ch chan *vss.Event, fs *freshservice.Freshservice, v *vss.TFS) {
s := state{}
for {
select {
case event := <-ch:
switch et := event.EventType; et {
case vss.DeploymentStartedEvent:
fmt.Printf("[Release: %v] Event received: %v\n", tc, vss.DeploymentStartedEvent)
desc, err := composeDescription(event, v)
if err != nil {
fmt.Printf("[Release: %v] [ERR] Work Items not found: %v\n", tc, err)
}
err = createChange(event.ReleaseName, event.EnvironmentName, desc, event.Timestamp, &s, fs)
if err != nil {
fmt.Printf("[Release: %v] [ERR] There was an error during the creation of the change: %v\n", tc, err)
} else {
err := updateChange(event.DetailedMessageHTML, freshservice.StatusOpen, &s, fs)
if err != nil {
fmt.Printf("[Release: %v] [ERR] There was an error updating the change: %v\n", tc, err)
} else {
if nil != s.LastEvent && vss.DeploymentApprovalPendingEvent == s.LastEvent.EventType {
var status int
if "preDeploy" == s.LastEvent.ApprovalType {
status = freshservice.StatusAwaitingApproval
} else {
status = freshservice.StatusPendingReview
}
err := updateChange(s.LastEvent.DetailedMessageHTML, status, &s, fs)
if err != nil {
fmt.Printf("[Release: %v] [ERR] There was an error updating the change: %v\n", tc, err)
}
}
}
}
case vss.DeploymentApprovalPendingEvent:
fmt.Printf("[Release: %v] Event received: %v\n", tc, vss.DeploymentApprovalPendingEvent)
if s.ResponseItilChange != nil && 0 != s.ResponseItilChange.Item.ItilChange.DisplayID {
var status int
if "preDeploy" == event.ApprovalType {
status = freshservice.StatusAwaitingApproval
} else {
status = freshservice.StatusPendingReview
}
err := updateChange(event.DetailedMessageHTML, status, &s, fs)
if err != nil {
fmt.Printf("[Release: %v] [ERR] There was an error updating the change: %v\n", tc, err)
}
}
case vss.DeploymentApprovalCompletedEvent:
fmt.Printf("[Release: %v] Event received: %v\n", tc, vss.DeploymentApprovalCompletedEvent)
if s.ResponseItilChange != nil && 0 != s.ResponseItilChange.Item.ItilChange.DisplayID {
var status int
if "preDeploy" == event.ApprovalType {
status = freshservice.StatusPendingRelease
} else {
status = freshservice.StatusOpen
}
err := updateChange(event.DetailedMessageHTML, status, &s, fs)
if err != nil {
fmt.Printf("[Release: %v] [ERR] There was an error updating the change: %v\n", tc, err)
}
}
case vss.DeploymentCompletedEvent:
fmt.Printf("[Release: %v] Event received: %v\n", tc, vss.DeploymentCompletedEvent)
if s.ResponseItilChange != nil && 0 != s.ResponseItilChange.Item.ItilChange.DisplayID {
err := updateChange(event.DetailedMessageHTML, freshservice.StatusClosed, &s, fs)
if err != nil {
fmt.Printf("[Release: %v] [ERR] There was an error updating the change: %v\n", tc, err)
}
}
}
s.LastEvent = event
case <-time.After(30 * time.Minute):
if s.ResponseItilChange != nil && 0 != s.ResponseItilChange.Item.ItilChange.DisplayID && s.LastEvent.EventType != vss.DeploymentCompletedEvent {
fmt.Printf("[Release: %v] Event: %v\n", tc, "Deployment timeout")
err := updateChange("Deployment timeout<br>Status: Failed", freshservice.StatusClosed, &s, fs)
if err != nil {
fmt.Printf("[Release: %v] [ERR] There was an error updating the change: %v\n", tc, err)
}
}
return
}
}
}
func composeDescription(event *vss.Event, v *vss.TFS) (string, error) {
nowi := "<br><b>No Work Items associated to this deploy</b><br>"
if v == nil {
return nowi, nil
}
var s strings.Builder
r, err := v.GetRelease(event.ProjectID, event.ReleaseID)
if err != nil {
return "", err
}
if r.Artifacts == nil || len(r.Artifacts) == 0 {
return nowi, nil
}
for _, a := range r.Artifacts {
if a.Type == vss.ArtifactBuildType {
ws, err := v.GetWorkItems(event.ProjectID, a.DefinitionReference.Version.ID)
if err != nil {
return "", err
}
if ws.Count != 0 {
s.WriteString("<br><b>Work Items to deploy from " + a.Alias + " (" + a.Type + ")</b><br>")
for _, item := range ws.Value {
w, err := v.GetWorkItem(item.ID)
if err != nil {
return "", err
}
s.WriteString("<br>[" + w.Fields.SystemWorkItemType + "] <a href='" + w.Links.HTML.Href + "'>" + w.Fields.SystemTitle + "</a>")
}
}
}
}
return s.String(), nil
}
func createChange(name string, environment string, msg string, date string, s *state, fs *freshservice.Freshservice) error {
c := &freshservice.RequestItilChange{}
c.ItilChange.Subject = "[Release Management] Deployment of release " + name + " to environment " + environment
c.ItilChange.DescriptionHTML = msg
c.ItilChange.Status = freshservice.StatusPendingRelease
c.ItilChange.Priority = freshservice.PriorityMedium
c.ItilChange.ChangeType = freshservice.TypeStandard
c.ItilChange.Risk = freshservice.RiskMedium
c.ItilChange.Impact = freshservice.ImpactMedium
c.ItilChange.PlannedStartDate = date
c.ItilChange.PlannedEndDate = date
s.RequestItilChange = c
resItilChange, err := fs.CreateChange(c)
if err != nil {
return err
}
s.ResponseItilChange = resItilChange
return nil
}
func updateChange(msg string, status int, s *state, fs *freshservice.Freshservice) error {
n := &freshservice.RequestNote{}
n.Note.BodyHTML = msg
_, err := fs.AddChangeNote(s.ResponseItilChange.Item.ItilChange.DisplayID, n)
if err != nil {
return err
}
s.RequestItilChange.ItilChange.Status = status
s.RequestItilChange.ItilChange.PlannedStartDate = ""
s.RequestItilChange.ItilChange.PlannedEndDate = ""
_, err = fs.UpdateChange(s.ResponseItilChange.Item.ItilChange.DisplayID, s.RequestItilChange)
if err != nil {
return err
}
return nil
}