6
6
"errors"
7
7
"gopkg.in/resty.v1"
8
8
"strconv"
9
+ "time"
9
10
)
10
11
11
12
type GithubActionsApi struct {
@@ -23,6 +24,8 @@ type WorkflowRun struct {
23
24
HtmlUrl string `json:"html_url"`
24
25
Status string `json:"status"`
25
26
Conclusion string `json:"conclusion"`
27
+ HeadBranch string `json:"head_branch"`
28
+ Event string `json:"event"`
26
29
}
27
30
28
31
type WorkflowsResult struct {
@@ -46,13 +49,11 @@ func (p *GithubActionsApi) SetToken(token string) {
46
49
p .token = token
47
50
}
48
51
49
- func (p * GithubActionsApi ) GetBuildInfo (branchName string ) (CiBuildInfo , error ) {
50
- var githubActionsBuildInfo CiBuildInfo
52
+ func (p * GithubActionsApi ) getBuilds (branchName string ) (WorkflowRunsResult , error ) {
53
+ var res WorkflowRunsResult
51
54
52
55
url := "https://api.github.com/repos/" + p .repoOwner + "/" + p .repo + "/actions/runs"
53
56
54
- var res WorkflowRunsResult
55
-
56
57
resp , err := resty .R ().
57
58
SetHeader ("Content-Type" , "application/json" ).
58
59
SetHeader ("Accept" , "application/json" ).
@@ -63,21 +64,31 @@ func (p *GithubActionsApi) GetBuildInfo(branchName string) (CiBuildInfo, error)
63
64
SetResult (& res ).
64
65
Get (url )
65
66
if err != nil {
66
- return githubActionsBuildInfo , err
67
+ return res , err
67
68
}
68
69
69
70
if ! ((resp .StatusCode () >= 200 ) && (resp .StatusCode () <= 209 )) {
70
- return githubActionsBuildInfo , errors .New (resp .String ())
71
+ return res , errors .New (resp .String ())
71
72
}
73
+ return res ,nil
74
+ }
72
75
73
- if res .TotalCount == 0 {
76
+ func (p * GithubActionsApi ) GetBuildInfo (branchName string ) (CiBuildInfo , error ) {
77
+ var githubActionsBuildInfo CiBuildInfo
78
+
79
+ builds , err := p .getBuilds (branchName )
80
+ if err != nil {
81
+ return githubActionsBuildInfo , err
82
+ }
83
+
84
+ if builds .TotalCount == 0 {
74
85
return githubActionsBuildInfo , errors .New ("No workflow run found" )
75
86
}
76
87
77
- githubActionsBuildInfo .JobUrl = res .WorkflowRuns [0 ].HtmlUrl
88
+ githubActionsBuildInfo .JobUrl = builds .WorkflowRuns [0 ].HtmlUrl
78
89
79
- status := res .WorkflowRuns [0 ].Status
80
- conclusion := res .WorkflowRuns [0 ].Conclusion
90
+ status := builds .WorkflowRuns [0 ].Status
91
+ conclusion := builds .WorkflowRuns [0 ].Conclusion
81
92
82
93
if status == "queued" {
83
94
githubActionsBuildInfo .LastBuild .State = "created"
@@ -92,7 +103,7 @@ func (p *GithubActionsApi) GetBuildInfo(branchName string) (CiBuildInfo, error)
92
103
} else {
93
104
githubActionsBuildInfo .LastBuild .State = "failed"
94
105
}
95
- githubActionsBuildInfo .LastBuild .Id = res .WorkflowRuns [0 ].Id
106
+ githubActionsBuildInfo .LastBuild .Id = builds .WorkflowRuns [0 ].Id
96
107
return githubActionsBuildInfo , nil
97
108
}
98
109
@@ -132,6 +143,13 @@ func (p *GithubActionsApi) StartBuild(branchName string) error {
132
143
return err
133
144
}
134
145
146
+ buildsBefore , err := p .getBuilds (branchName )
147
+ if err != nil {
148
+ return err
149
+ }
150
+
151
+ numBuildsBefore := buildsBefore .TotalCount
152
+
135
153
var req GithubActionsRequest
136
154
req .Ref = branchName
137
155
@@ -152,5 +170,24 @@ func (p *GithubActionsApi) StartBuild(branchName string) error {
152
170
return errors .New (resp .String ())
153
171
}
154
172
155
- return nil
173
+ //there seems to be no way (yet) to get the build number of the job we manually start.
174
+ //so in order to determine the build number, we look at the build that was created at last.
175
+ //that's not bulletproof, but it should work well enough.
176
+ for i := 0 ; i < 5 ; i ++ { //it might take a bit until the build number is available
177
+ buildsAfter , err := p .getBuilds (branchName )
178
+ if err != nil {
179
+ time .Sleep (1 * time .Second )
180
+ continue
181
+ }
182
+ numBuildsAfter := buildsAfter .TotalCount
183
+ if numBuildsAfter == numBuildsBefore + 1 {
184
+ if buildsAfter .WorkflowRuns [0 ].HeadBranch == branchName && buildsAfter .WorkflowRuns [0 ].Event == "workflow_dispatch" {
185
+ return nil
186
+ }
187
+ }
188
+
189
+ time .Sleep (1 * time .Second )
190
+ }
191
+
192
+ return errors .New ("Couldn't get build info after 5 attempts" )
156
193
}
0 commit comments