@@ -2,6 +2,7 @@ package dispatch
2
2
3
3
import (
4
4
"fmt"
5
+ "net/http"
5
6
"net/url"
6
7
"testing"
7
8
@@ -11,7 +12,13 @@ import (
11
12
)
12
13
13
14
func TestRepositoryDispatchRun (t * testing.T ) {
14
- repo := "OWNER/REPO"
15
+ ghRepo := & ghRepo {
16
+ Name : "REPO" ,
17
+ Owner : "OWNER" ,
18
+ }
19
+ repo := ghRepo .RepoFullName ()
20
+ event := "repository_dispatch"
21
+
15
22
tests := []struct {
16
23
name string
17
24
opts * repositoryDispatchOptions
@@ -35,36 +42,76 @@ func TestRepositoryDispatchRun(t *testing.T) {
35
42
httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/actions/workflows" , repo )),
36
43
httpmock .StringResponse (getWorkflowsResponse ))
37
44
45
+ reg .Register (
46
+ httpmock .GraphQL ("query UserCurrent{viewer{login}}" ),
47
+ httpmock .StringResponse (currentUserResponse ))
48
+
38
49
v := url.Values {}
39
- v .Set ("event" , "repository_dispatch" )
50
+ v .Set ("per_page" , "50" )
51
+
52
+ reg .Register (
53
+ httpmock .QueryMatcher ("GET" , fmt .Sprintf ("repos/%s/actions/workflows/456/runs" , repo ), v ),
54
+ httpmock .StringResponse (fmt .Sprintf (getWorkflowRunsResponse , event , repo )))
55
+
56
+ q := url.Values {}
57
+ q .Set ("per_page" , "100" )
58
+ q .Set ("page" , "1" )
59
+
60
+ reg .Register (
61
+ httpmock .QueryMatcher ("GET" , fmt .Sprintf ("repos/%s/actions/workflows" , repo ), q ),
62
+ httpmock .StringResponse (fmt .Sprintf (getWorkflowRunsResponse , event , repo )))
40
63
41
64
reg .Register (
42
- httpmock .QueryMatcher ("GET" , fmt .Sprintf ("repos/%s/actions/runs" , repo ), v ),
43
- httpmock .StringResponse (getWorkflowRunsResponse ))
65
+ httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/actions/workflows/456" , repo )),
66
+ httpmock .StringResponse (getWorkflowResponse ))
67
+
68
+ reg .Register (
69
+ httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/actions/workflows/456" , repo )),
70
+ httpmock .StringResponse (getWorkflowResponse ))
44
71
45
72
reg .Register (
46
73
httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/actions/runs/123" , repo )),
47
74
httpmock .StringResponse (`{
48
- "id": 123
75
+ "id": 123,
76
+ "workflow_id": 456,
77
+ "event": "repository_dispatch"
49
78
}` ))
50
79
80
+ reg .Register (
81
+ httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/actions/workflows/456" , repo )),
82
+ httpmock .StringResponse (getWorkflowResponse ))
83
+
51
84
reg .Register (
52
85
httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/actions/runs/123" , repo )),
53
- httpmock .StringResponse (`{
86
+ httpmock .StringResponse (fmt . Sprintf ( `{
54
87
"id": 123,
88
+ "workflow_id": 456,
55
89
"status": "completed",
56
- "conclusion": "success"
57
- }` ))
90
+ "event": "repository_dispatch",
91
+ "conclusion": "success",
92
+ "jobs_url": "https://api.github.com/repos/%s/actions/runs/123/jobs"
93
+ }` , repo )))
58
94
59
95
reg .Register (
60
- httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/actions/runs/123/attempts/1/ jobs" , repo )),
96
+ httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/actions/runs/123/jobs" , repo )),
61
97
httpmock .StringResponse (getJobsResponse ))
62
98
63
99
reg .Register (
64
100
httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/check-runs/123/annotations" , repo )),
65
101
httpmock .StringResponse ("[]" ))
66
102
},
67
- wantOut : "Refreshing run status every 2 seconds. Press Ctrl+C to quit.\n \n https://github.com/OWNER/REPO/actions/runs/123\n \n \n JOBS\n ✓ build in 1m59s (ID 123)\n ✓ Run actions/checkout@v2\n ✓ Test\n " ,
103
+ wantOut : `Refreshing run status every 2 seconds. Press Ctrl+C to quit.
104
+
105
+ https://github.com/OWNER/REPO/actions/runs/123
106
+
107
+ ✓ foo · 123
108
+ Triggered via repository_dispatch
109
+
110
+ JOBS
111
+ ✓ build in 1m59s (ID 123)
112
+ ✓ Run actions/checkout@v2
113
+ ✓ Test
114
+ ` ,
68
115
}, {
69
116
name : "unsuccessful workflow run" ,
70
117
opts : & repositoryDispatchOptions {
@@ -81,36 +128,79 @@ func TestRepositoryDispatchRun(t *testing.T) {
81
128
httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/actions/workflows" , repo )),
82
129
httpmock .StringResponse (getWorkflowsResponse ))
83
130
131
+ reg .Register (
132
+ httpmock .GraphQL ("query UserCurrent{viewer{login}}" ),
133
+ httpmock .StringResponse (currentUserResponse ))
134
+
84
135
v := url.Values {}
85
- v .Set ("event" , "repository_dispatch" )
136
+ v .Set ("per_page" , "50" )
137
+
138
+ reg .Register (
139
+ httpmock .QueryMatcher ("GET" , fmt .Sprintf ("repos/%s/actions/workflows/456/runs" , repo ), v ),
140
+ httpmock .StringResponse (fmt .Sprintf (getWorkflowRunsResponse , event , repo )))
141
+
142
+ q := url.Values {}
143
+ q .Set ("per_page" , "100" )
144
+ q .Set ("page" , "1" )
145
+
146
+ // TODO: is this correct? is it the correct response?
147
+ reg .Register (
148
+ httpmock .QueryMatcher ("GET" , fmt .Sprintf ("repos/%s/actions/workflows" , repo ), q ),
149
+ httpmock .StringResponse (fmt .Sprintf (getWorkflowRunsResponse , event , repo )))
150
+
151
+ // TODO: is this correct? is it the correct response?
152
+ reg .Register (
153
+ httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/actions/workflows/456" , repo )),
154
+ httpmock .StringResponse (getWorkflowResponse ))
86
155
156
+ // TODO: is this correct? is it the correct response?
87
157
reg .Register (
88
- httpmock .QueryMatcher ("GET" , fmt .Sprintf ("repos/%s/actions/runs " , repo ), v ),
89
- httpmock .StringResponse (getWorkflowRunsResponse ))
158
+ httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/actions/workflows/456 " , repo )),
159
+ httpmock .StringResponse (getWorkflowResponse ))
90
160
91
161
reg .Register (
92
162
httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/actions/runs/123" , repo )),
93
163
httpmock .StringResponse (`{
94
- "id": 123
164
+ "id": 123,
165
+ "workflow_id": 456,
166
+ "event": "repository_dispatch"
95
167
}` ))
96
168
169
+ reg .Register (
170
+ httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/actions/workflows/456" , repo )),
171
+ httpmock .StringResponse (getWorkflowResponse ))
172
+
97
173
reg .Register (
98
174
httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/actions/runs/123" , repo )),
99
- httpmock .StringResponse (`{
175
+ httpmock .StringResponse (fmt . Sprintf ( `{
100
176
"id": 123,
177
+ "workflow_id": 456,
101
178
"status": "completed",
102
- "conclusion": "failure"
103
- }` ))
179
+ "event": "repository_dispatch",
180
+ "conclusion": "failure",
181
+ "jobs_url": "https://api.github.com/repos/%s/actions/runs/123/jobs"
182
+ }` , repo )))
104
183
105
184
reg .Register (
106
- httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/actions/runs/123/attempts/1/ jobs" , repo )),
185
+ httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/actions/runs/123/jobs" , repo )),
107
186
httpmock .StringResponse (getFailingJobsResponse ))
108
187
109
188
reg .Register (
110
189
httpmock .REST ("GET" , fmt .Sprintf ("repos/%s/check-runs/123/annotations" , repo )),
111
190
httpmock .StringResponse ("[]" ))
112
191
},
113
- wantOut : "Refreshing run status every 2 seconds. Press Ctrl+C to quit.\n \n https://github.com/OWNER/REPO/actions/runs/123\n \n \n JOBS\n ✓ build in 1m59s (ID 123)\n ✓ Run actions/checkout@v2\n X Test\n " ,
192
+ wantOut : `Refreshing run status every 2 seconds. Press Ctrl+C to quit.
193
+
194
+ https://github.com/OWNER/REPO/actions/runs/123
195
+
196
+ X foo · 123
197
+ Triggered via repository_dispatch
198
+
199
+ JOBS
200
+ ✓ build in 1m59s (ID 123)
201
+ ✓ Run actions/checkout@v2
202
+ X Test
203
+ ` ,
114
204
wantErr : true ,
115
205
errMsg : "SilentError" ,
116
206
}, {
@@ -136,10 +226,11 @@ func TestRepositoryDispatchRun(t *testing.T) {
136
226
ios .SetStdoutTTY (false )
137
227
ios .SetAlternateScreenBufferEnabled (false )
138
228
139
- tt .opts .repo = repo
229
+ tt .opts .repo = ghRepo
140
230
tt .opts .io = ios
141
- tt .opts .httpTransport = reg
142
- tt .opts .authToken = "123"
231
+ tt .opts .httpClient = & http.Client {
232
+ Transport : reg ,
233
+ }
143
234
144
235
t .Run (tt .name , func (t * testing.T ) {
145
236
err := repositoryDispatchRun (tt .opts )
0 commit comments