-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpratus.go
121 lines (98 loc) · 2.61 KB
/
pratus.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
package main
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
func getPRState(owner string, repo string, number int, token string) (state string, statuses []github.RepoStatus, err error) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
g := github.NewClient(tc)
pr, _, err := g.PullRequests.Get(ctx, owner, repo, number)
if err != nil {
return "", nil, err
}
commit := pr.GetHead().GetSHA()
status, _, err := g.Repositories.GetCombinedStatus(ctx, owner, repo, commit, nil)
if err != nil {
return "", nil, err
}
state = status.GetState()
statuses = status.Statuses
return state, statuses, nil
}
func stillPending(statuses []github.RepoStatus) (pending bool) {
for _, status := range statuses {
if status.GetState() == "pending" {
return true
}
}
return false
}
func getFailedURLs(statuses []github.RepoStatus) (failed []string) {
for _, status := range statuses {
if status.GetState() != "success" {
failed = append(failed, status.GetTargetURL())
}
}
return failed
}
func parseGitHubURL(baseURL string, URL string) (owner string, repo string, number int) {
strippedURL := strings.Replace(URL, baseURL, "", 1)
splitURL := strings.Split(strippedURL, "/")
owner = splitURL[0]
repo = splitURL[1]
number, _ = strconv.Atoi(splitURL[3])
return
}
func main() {
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
fmt.Println("GITHUB_TOKEN must be set, aborting.")
os.Exit(1)
}
URL := os.Args[1]
gitHubBaseURL := "https://github.com/"
timer := time.Duration(60)
if _, ok := os.LookupEnv("PRATUS_SLEEP_TIMER"); ok {
customTimer, _ := strconv.Atoi(os.Getenv("PRATUS_SLEEP_TIMER"))
timer = time.Duration(customTimer)
}
sleepTimer := timer * time.Second
owner, repo, number := parseGitHubURL(gitHubBaseURL, URL)
fmt.Printf("Checking status of pull request %d in %s/%s every %d seconds\n", number, owner, repo, timer)
for {
state, statuses, err := getPRState(owner, repo, number, token)
if err != nil {
fmt.Println(err)
time.Sleep(sleepTimer)
continue
}
if state == "pending" || stillPending(statuses) {
fmt.Print(".")
time.Sleep(sleepTimer)
continue
}
failedURLs := getFailedURLs(statuses)
switch state {
case "success":
fmt.Println("\nPR succeeded :)")
os.Exit(0)
case "failure", "error":
fmt.Println("\nPR failed :( Failed URLs:")
fmt.Println(strings.Join(failedURLs, "\n"))
os.Exit(1)
default:
fmt.Printf("\nPR contained an unknown status: %q", state)
os.Exit(1)
}
}
}