-
Notifications
You must be signed in to change notification settings - Fork 0
/
servicehook.go
185 lines (158 loc) · 7.62 KB
/
servicehook.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
package azuredevops
import (
"fmt"
"time"
)
// ServiceHook defines the JSON body of service hooks
// https://docs.microsoft.com/en-us/azure/devops/service-hooks/events?toc=%2Fazure%2Fdevops%2Fintegrate%2Ftoc.json&bc=%2Fazure%2Fdevops%2Fintegrate%2Fbreadcrumb%2Ftoc.json&view=azure-devops
type ServiceHook struct {
ID string `json:"id"`
EventType string `json:"eventType"`
PublisherID string `json:"publisherId"`
Scope string `json:"scope"`
Message ServiceHookMessage `json:"message"`
DetailedMessage ServiceHookMessage `json:"detailedMessage"`
Resource ServiceHookResource `json:"resource"`
ResourceVersion string `json:"resourceVersion"`
ResourceContainers ServiceHookResourceContainers `json:"resourceContainers"`
CreatedDate time.Time `json:"createdDate"`
}
// Describe returns a user-friendly descriptor for this Service Hook
func (sh ServiceHook) Describe() string {
return fmt.Sprintf("(%s %s) %s", sh.EventType, sh.ID, sh.Message.Text)
}
// GetStatus returns the status of the project from the Service Hook
func (sh ServiceHook) GetStatus() *string {
if sh.Resource.Status != nil {
// build.complete
// git.pullrequest.created
// git.pullrequest.merged
// git.pullrequest.updated
return sh.Resource.Status
}
if sh.Resource.Approval != nil {
// ms.vss-release.deployment-approval-completed-event
// ms.vss-release.deployment-approval-pending-event
status := string(sh.Resource.Approval.Status)
return &status
}
if sh.Resource.Release != nil && sh.Resource.Release.Status != "" {
// ms.vss-release.release-abandoned-event
// ms.vss-release.release-created-event
return &sh.Resource.Release.Status
}
if sh.Resource.Environment != nil && sh.Resource.Environment.Status != "" {
// ms.vss-release.deployment-completed-event
// ms.vss-release.deployment-started-event
return &sh.Resource.Environment.Status
}
return nil
}
// GetReason returns the reason of the project from the Service Hook
func (sh ServiceHook) GetReason() *string {
if sh.EventType == string(ServiceHookEventTypeBuildComplete) {
// build.complete
return sh.Resource.Reason
}
if sh.Resource.Release != nil && sh.Resource.Release.Reason != "" {
// ms.vss-release.release-abandoned-event
// ms.vss-release.release-created-event
// ms.vss-release.deployment-approval-completed-event
// ms.vss-release.deployment-approval-pending-event
return &sh.Resource.Release.Reason
}
return nil
}
// GetProjectName returns the name of the project from the Service Hook
func (sh ServiceHook) GetProjectName() *string {
if sh.Resource.Project != nil && sh.Resource.Project.Name != "" {
// ms.vss-release.release-abandoned-event
// ms.vss-release.release-created-event
// ms.vss-release.deployment-approval-completed-event
// ms.vss-release.deployment-approval-pending-event
// ms.vss-release.deployment-completed-event
// ms.vss-release.deployment-started-event
return &sh.Resource.Project.Name
}
if sh.Resource.Repository != nil && sh.Resource.Repository.Project.Name != "" {
// git.push
// git.pullrequest.created
// git.pullrequest.merged
// git.pullrequest.updated
return &sh.Resource.Repository.Project.Name
}
if sh.Resource.URL != nil {
// build.complete
// tfvc.checkin
// git.push
// git.pullrequest.created
// git.pullrequest.merged
// git.pullrequest.updated
return GetProjectFromURL(sh.Resource.URL)
}
return nil
}
// GetEnvironments returns all of the environment names attached to this ServiceHook
func (sh ServiceHook) GetEnvironments() []string {
if sh.Resource.Environment != nil {
// ms.vss-release.deployment-completed-event
// ms.vss-release.deployment-started-event
return []string{sh.Resource.Environment.Name}
}
if sh.Resource.Release != nil {
// ms.vss-release.release-abandoned-event
// ms.vss-release.release-created-event
// ms.vss-release.deployment-approval-completed-event
// ms.vss-release.deployment-approval-pending-event
var environments []string
for _, environment := range sh.Resource.Release.Environments {
environments = append(environments, environment.Name)
}
return environments
}
return nil
}
// ServiceHookMessage is used to hold the message and defaultMessage fields in service hooks
type ServiceHookMessage struct {
Text string `json:"text"`
HTML string `json:"html"`
Markdown string `json:"markdown"`
}
// ServiceHookEventType represents all possible Event Type values for a Service Hook configuration
type ServiceHookEventType string
const (
// ServiceHookEventTypeBuildComplete represents the Build Completed event
ServiceHookEventTypeBuildComplete ServiceHookEventType = "build.complete"
// ServiceHookEventTypeReleaseAbandoned represents the Release Abandoned event
ServiceHookEventTypeReleaseAbandoned ServiceHookEventType = "ms.vss-release.release-abandoned-event"
// ServiceHookEventTypeReleaseCreated represents the Release Created event
ServiceHookEventTypeReleaseCreated ServiceHookEventType = "ms.vss-release.release-created-event"
// ServiceHookEventTypeReleaseDeploymentApprovalCompleted represents the Release Deployment Approval Completed event
ServiceHookEventTypeReleaseDeploymentApprovalCompleted ServiceHookEventType = "ms.vss-release.deployment-approval-completed-event"
// ServiceHookEventTypeReleaseDeploymestringntApprovalPending represents the Release Deployment Approval Pending event
ServiceHookEventTypeReleaseDeploymentApprovalPending ServiceHookEventType = "ms.vss-release.deployment-approval-pending-event"
// ServiceHookEventTypeReleaseDeploymentCompleted represents the Release Deployment Completed event
ServiceHookEventTypeReleaseDeploymentCompleted ServiceHookEventType = "ms.vss-release.deployment-completed-event"
// ServiceHookEventTypeReleaseDeploymentStarted represents the Release Deployment Started event
ServiceHookEventTypeReleaseDeploymentStarted ServiceHookEventType = "ms.vss-release.deployment-started-event"
// ServiceHookEventTypeCodeCheckedIn represents the Code Checked In event
ServiceHookEventTypeCodeCheckedIn ServiceHookEventType = "tfvc.checkin"
// ServiceHookEventTypeCodePushed represents the Code Pushed event
ServiceHookEventTypeCodePushed ServiceHookEventType = "git.push"
// ServiceHookEventTypePullRequestCreated represents the Pull Request Created event
ServiceHookEventTypePullRequestCreated ServiceHookEventType = "git.pullrequest.created"
// ServiceHookEventTypePullRequestMerged represents the Pull Request Merged event
ServiceHookEventTypePullRequestMerged ServiceHookEventType = "git.pullrequest.merged"
// ServiceHookEventTypePullRequestUpdated represents the Pull Request Updated event
ServiceHookEventTypePullRequestUpdated ServiceHookEventType = "git.pullrequest.updated"
// ServiceHookEventTypeWorkItemCreated represents the Work Item Created event
ServiceHookEventTypeWorkItemCreated ServiceHookEventType = "workitem.created"
// ServiceHookEventTypeWorkItemDeleted represents the Work Item Deleted event
ServiceHookEventTypeWorkItemDeleted ServiceHookEventType = "workitem.deleted"
// ServiceHookEventTypeWorkItemRestored represents the Work Item Restored event
ServiceHookEventTypeWorkItemRestored ServiceHookEventType = "workitem.restored"
// ServiceHookEventTypeWorkItemUpdated represents the Work Item Updated event
ServiceHookEventTypeWorkItemUpdated ServiceHookEventType = "workitem.updated"
// ServiceHookEventTypeWorkItemCommented represents the Work Item Commented event
ServiceHookEventTypeWorkItemCommented ServiceHookEventType = "workitem.commented"
)