-
Notifications
You must be signed in to change notification settings - Fork 1
/
githubFunctions.go
83 lines (70 loc) · 2.12 KB
/
githubFunctions.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
)
func checkDependencyPr(depUrl string, prUrl string, statusUrl string) {
client := &http.Client{}
req, err := http.NewRequest("GET", depUrl, nil)
if err != nil {
panic(err)
}
req.Header.Add("Authorization", `Basic dGNyYW5kczpiYWlsZXkxMjM=`)
resp, err := client.Do(req)
defer resp.Body.Close()
if resp.StatusCode == 200 {
githubDataDependency := &GithubDataDependency{}
decoder := json.NewDecoder(resp.Body)
err := decoder.Decode(&githubDataDependency)
if err != nil {
panic(err)
}
state := githubDataDependency.State
prRepoName := githubDataDependency.Head.Repo.Name
if state == "closed" {
status := "success"
changePrStatus(depUrl, status, prRepoName, statusUrl)
} else {
status := "failure"
newUrl := strings.Replace(depUrl, "api.github.com/repos", "github.com", 1)
newDepUrl := strings.Replace(newUrl, "pulls", "pull", 1)
storedPRData := &StoredPRData{
PrUrl: prUrl,
DepUrl: newDepUrl,
StatusUrl: statusUrl,
}
go updateDatabase(storedPRData, newDepUrl)
go changePrStatus(newDepUrl, status, prRepoName, statusUrl)
}
}
}
func changePrStatus(depUrl string, status string, prRepoName string, statusUrl string) {
client := &http.Client{}
message := prepareMessage(prRepoName, status)
var jsonStr = []byte(`{"state": "` + status + `", "target_url": "` + depUrl + `", "description": "` + message + `", "context": "Dependency Manager"}`)
fmt.Println(bytes.NewBuffer(jsonStr))
req, err := http.NewRequest("POST", statusUrl, bytes.NewBuffer(jsonStr))
if err != nil {
panic(err)
}
req.Header.Add("Authorization", `Basic dGNyYW5kczpiYWlsZXkxMjM=`)
resp, err := client.Do(req)
defer resp.Body.Close()
if resp.StatusCode == 201 {
fmt.Println("Pull Request Status Updated")
} else {
fmt.Println(resp.StatusCode)
}
}
func prepareMessage(prRepoName string, status string) string {
if status == "failure" {
return "Requires: " + strings.ToUpper(prRepoName) + " (Click details ->)"
} else if status == "success" {
return "No Dependencies Found"
} else {
return "An Error has occured"
}
}