-
Notifications
You must be signed in to change notification settings - Fork 168
/
notification.go
217 lines (184 loc) · 6.2 KB
/
notification.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
package v1alpha1
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
"github.com/caicloud/cyclone/pkg/apis/cyclone/v1alpha1"
"github.com/caicloud/cyclone/pkg/meta"
s_v1alpha1 "github.com/caicloud/cyclone/pkg/server/apis/v1alpha1"
"github.com/caicloud/cyclone/pkg/server/biz/scm"
"github.com/caicloud/cyclone/pkg/server/common"
"github.com/caicloud/cyclone/pkg/server/config"
"github.com/caicloud/cyclone/pkg/server/handler"
utilhttp "github.com/caicloud/cyclone/pkg/util/http"
"github.com/caicloud/cyclone/pkg/workflow/values/ref"
)
// HandleWorkflowRunNotification handles workflowrun finished notification from workflow engine.
func HandleWorkflowRunNotification(ctx context.Context, wfr *v1alpha1.WorkflowRun) (interface{}, error) {
err := updatePullRequestStatus(wfr)
if err != nil {
log.WithField("wfr", wfr.Name).Error("Failed to update SCM status: ", err)
}
err = sendNotifications(wfr)
if err != nil {
log.WithField("wfr", wfr.Name).Error("Failed to send notifications: ", err)
}
// Always return success as have received the workflowrun notifications.
// No need to return the errors when handle it.
return nil, nil
}
// sendNotifications send notifications to subscribe systems, and record the results in status.
func sendNotifications(wfr *v1alpha1.WorkflowRun) error {
// If already there are notification status, no need to send notifications again.
if wfr.Status.Notifications != nil {
return nil
}
// Send notifications with workflowrun.
bodyBytes, err := json.Marshal(wfr)
if err != nil {
log.WithField("wfr", wfr.Name).Error("Failed to marshal workflowrun: ", err)
return err
}
body := bytes.NewReader(bodyBytes)
status := make(map[string]v1alpha1.NotificationStatus)
for _, endpoint := range config.Config.Notifications {
req, err := http.NewRequest(http.MethodPost, endpoint.URL, body)
if err != nil {
err = fmt.Errorf("Failed to new notification request: %v", err)
log.WithField("wfr", wfr.Name).Error(err)
status[endpoint.Name] = v1alpha1.NotificationStatus{
Result: v1alpha1.NotificationResultFailed,
Message: err.Error(),
}
continue
}
// Set Json content type in Http header.
req.Header.Set(utilhttp.HeaderContentType, utilhttp.HeaderContentTypeJSON)
resp, err := http.DefaultClient.Do(req)
if err != nil {
s := v1alpha1.NotificationStatus{
Result: v1alpha1.NotificationResultFailed,
}
log.WithField("wfr", wfr.Name).Errorf("Failed to send notification for %s: %v", endpoint.Name, err)
if resp != nil {
defer func() {
if err := resp.Body.Close(); err != nil {
log.WithField("wfr", wfr.Name).Errorf("Fail to close response body as: %v", err)
}
}()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error(err)
s.Message = err.Error()
} else {
s.Message = fmt.Sprintf("Status code: %d, error: %s", resp.StatusCode, body)
}
}
status[endpoint.Name] = s
continue
}
log.WithField("wfr", wfr.Name).Infof("Status code of notification for %s: %d", endpoint.Name, resp.StatusCode)
status[endpoint.Name] = v1alpha1.NotificationStatus{
Result: v1alpha1.NotificationResultSucceeded,
Message: fmt.Sprintf("Status code: %d", resp.StatusCode),
}
}
// Update WorkflowRun notification status with retry.
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
// Get latest WorkflowRun.
latest, err := handler.K8sClient.CycloneV1alpha1().WorkflowRuns(wfr.Namespace).Get(wfr.Name, metav1.GetOptions{})
if err != nil {
return err
}
if latest.Status.Notifications == nil {
latest.Status.Notifications = status
_, err = handler.K8sClient.CycloneV1alpha1().WorkflowRuns(wfr.Namespace).Update(latest)
return err
}
return nil
})
if err != nil {
log.WithField("name", wfr.Name).Error("Update workflowrun notification status error: ", err)
}
return err
}
func updatePullRequestStatus(wfr *v1alpha1.WorkflowRun) error {
wfrName := wfr.Name
if wfr.Annotations == nil {
return fmt.Errorf("Annotations of workflowrun %s can not be empty", wfrName)
}
trigger, ok := wfr.Annotations[meta.AnnotationWorkflowRunTrigger]
if !ok {
// Skip workflowruns without trigger.
return nil
}
// Skip workflowruns whose trigger are not about pull request.
if trigger != string(scm.PullRequestCommentEventType) && trigger != string(scm.PullRequestEventType) {
return nil
}
labels := wfr.Labels
if labels == nil {
return fmt.Errorf("Labels of workflowrun %s can not be empty", wfrName)
}
var tenant, project, wfName string
tenant = common.NamespaceTenant(wfr.Namespace)
if project, ok = labels[meta.LabelProjectName]; !ok {
return fmt.Errorf("Fail to get project name from labels of workflowrun %s", wfrName)
}
if wfName, ok = labels[meta.LabelWorkflowName]; !ok {
return fmt.Errorf("Fail to get workflow name from labels of workflowrun %s", wfrName)
}
scm, err := getSCMSourceFromWorkflowRun(wfr)
if err != nil {
return err
}
event, err := getSCMEventData(wfr.Annotations)
if err != nil {
return err
}
recordURL, err := generateRecordURL(tenant, project, wfName, wfrName)
if err != nil {
return err
}
return createSCMStatus(scm, wfr.Status.Overall.Phase, recordURL, event)
}
func getSCMSourceFromWorkflowRun(wfr *v1alpha1.WorkflowRun) (*s_v1alpha1.SCMSource, error) {
// Parse "SCM_AUTH" from resource parameters. ONLY support one SCM git resource.
var gitToken string
found := false
for _, r := range wfr.Spec.Resources {
for i, p := range r.Parameters {
if p.Name == "SCM_AUTH" {
if r.Parameters[i].Value != nil {
gitToken = *r.Parameters[i].Value
}
found = true
break
}
}
if found {
break
}
}
if found {
secretRef := ref.NewSecretRefValue()
if err := secretRef.Parse(gitToken); err != nil {
return nil, err
}
in, err := getIntegration(secretRef.Namespace, secretRef.Secret)
if err != nil {
return nil, err
}
if in.Spec.Type != s_v1alpha1.SCM {
return nil, fmt.Errorf("Type of integration %s is %s, should be %s", in.Name, in.Spec.Type, s_v1alpha1.SCM)
}
return in.Spec.SCM, nil
}
return nil, fmt.Errorf("Can not get SCM source from workflowrun %s", wfr.Name)
}