-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
141 lines (125 loc) · 4.07 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
"time"
"github.com/google/go-github/v51/github"
"github.com/tidwall/gjson"
"golang.org/x/oauth2"
)
func newWorkflowStatusLoopChannel(ctx context.Context, wrkflw *workflowEnvironment, client *github.Client) chan int {
channel := make(chan int)
go func() {
for {
workflowRuns, _, err := client.Actions.ListRepositoryWorkflowRuns(ctx, wrkflw.repoOwner, wrkflw.repo, &github.ListWorkflowRunsOptions{
HeadSHA: wrkflw.headSha,
})
if err != nil {
fmt.Printf("error listing workflow for a repository: %v\n", err)
channel <- 1
close(channel)
}
if workflowRuns.GetTotalCount() == 0 {
fmt.Printf("No workflows found for the specified commit sha")
channel <- 0
close(channel)
}
parsedOutput, err := json.Marshal(workflowRuns)
if err != nil {
fmt.Printf("error parsing output to json: %v\n", err)
channel <- 1
close(channel)
}
headBranchWorkflowExists := gjson.Get(string(parsedOutput), "workflow_runs.#(head_branch==\""+wrkflw.baseBranch+"\")").Exists()
if headBranchWorkflowExists {
headBranchWorkflow := gjson.Get(string(parsedOutput), "workflow_runs.#(head_branch==\""+wrkflw.baseBranch+"\")")
headBranchWorkflowStatus := gjson.Get(headBranchWorkflow.String(), "status").String()
fmt.Printf("Base branch workflow status: %s\n", headBranchWorkflowStatus)
switch headBranchWorkflowStatus {
case string(workflowStatusCompleted):
fmt.Println("Base branch workflow status is completed, verifying base branch workflow conclusion...")
headBranchWorkflowConclusion := gjson.Get(headBranchWorkflow.String(), "conclusion").String()
switch headBranchWorkflowConclusion {
case string(workflowConclusionSuccess):
fmt.Printf("Base branch workflow status is success")
channel <- 0
close(channel)
case string(workflowConclusionFailed):
fmt.Printf("Base branch workflow status is failed")
channel <- 1
close(channel)
case string(workflowConclusionSkipped):
fmt.Printf("Base branch workflow status is skipped")
channel <- 1
close(channel)
case string(workflowConclusionTimeOut):
fmt.Printf("Base branch workflow status is timeout")
channel <- 1
close(channel)
case string(workflowConclusionCancelled):
fmt.Printf("Base branch workflow status is cancelled")
channel <- 1
close(channel)
}
case string(workflowStatusFailed):
fmt.Printf("Base branch workflow status is failed")
channel <- 1
close(channel)
}
time.Sleep(wrkflw.pollingInterval)
}
}
}()
return channel
}
func newGithubClient(ctx context.Context) (*github.Client, error) {
token := os.Getenv(envVarToken)
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
serverUrl, serverUrlPresent := os.LookupEnv("GITHUB_SERVER_URL")
apiUrl, apiUrlPresent := os.LookupEnv("GITHUB_API_URL")
if serverUrlPresent {
if !apiUrlPresent {
apiUrl = serverUrl
}
return github.NewEnterpriseClient(apiUrl, serverUrl, tc)
}
return github.NewClient(tc), nil
}
func main() {
if err := validateInput(); err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
repo := os.Getenv(envVarRepoName)
repoOwner := os.Getenv(envVarRepoOwner)
ctx := context.Background()
client, err := newGithubClient(ctx)
if err != nil {
fmt.Printf("error connecting to server: %v\n", err)
os.Exit(1)
}
headSha := os.Getenv(envVarHeadSha)
baseBranch := os.Getenv(envVarBaseBranch)
pollingInterval := os.Getenv(envVarPollingInterval)
wrkflw, err := newWorkflowEnvironment(repo, repoOwner, headSha, baseBranch, pollingInterval)
if err != nil {
fmt.Printf("error creating workflow environment: %v\n", err)
os.Exit(1)
}
killSignalChannel := make(chan os.Signal, 1)
signal.Notify(killSignalChannel, os.Interrupt)
workflowStatusLoopChannel := newWorkflowStatusLoopChannel(ctx, wrkflw, client)
select {
case exitCode := <-workflowStatusLoopChannel:
os.Exit(exitCode)
case <-killSignalChannel:
handleInterrupt(ctx)
os.Exit(1)
}
}