-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgocd_pipeline_client.go
58 lines (49 loc) · 1.7 KB
/
gocd_pipeline_client.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
package main
import (
"encoding/json"
"fmt"
"net/url"
"strings"
"time"
"github.com/leonmaia/requests"
)
func isPipelineAvailable(pipelineName, statusCheckURL, auth string) (bool, error) {
status := GoCDPipelineResponse{}
req, _ := createGoCDRequest("GET", statusCheckURL, auth, strings.NewReader(""))
resp, err := req.Do()
if err != nil {
return false, fmt.Errorf("error while doing check pipeline request to gocd: %s", err.Error())
}
err = json.NewDecoder(resp.Body).Decode(&status)
if err != nil {
return false, fmt.Errorf("error while decoding gocd response: %s", err.Error())
}
return status.Schedulable, nil
}
func notifyGoCDOfChangeInPR(pipelineName, materialName, notifyURL, statusCheckURL, auth string, ghPayload GitHubPullRequestPayload) error {
form := url.Values{}
form.Add(fmt.Sprintf("materials[%s]", materialName), ghPayload.After)
req, err := createGoCDRequest("POST", notifyURL, auth, strings.NewReader(form.Encode()))
if err != nil {
return err
}
req.Header.Set("Confirm", "true")
if result, err := isPipelineAvailable(pipelineName, statusCheckURL, auth); result && err == nil {
_, err = req.Do()
if err != nil {
return fmt.Errorf("error while doing schedule request to gocd: %s", err.Error())
}
} else {
work := WorkRequest{Request: req, Delay: 10 * time.Second, PipelineName: pipelineName, StatusCheckURL: statusCheckURL, Auth: auth}
Collector(work)
}
return nil
}
func createGoCDRequest(method, url, auth string, body *strings.Reader) (*requests.Request, error) {
req, err := requests.NewRequest(method, url, body)
if err != nil {
return nil, fmt.Errorf("error creating gocd request: %s", err.Error())
}
req.Header.Set("Authorization", auth)
return req, nil
}