11package org .kohsuke .github ;
22
3- import com .github .tomakehurst .wiremock .core .WireMockConfiguration ;
43import org .awaitility .Awaitility ;
54import org .junit .Assert ;
65import org .junit .Before ;
1110import java .io .IOException ;
1211import java .time .Duration ;
1312import java .util .List ;
13+ import java .util .Optional ;
14+ import java .util .function .Function ;
1415
1516public class GHWorkflowRunTest extends AbstractGitHubWireMockTest {
1617
@@ -26,31 +27,9 @@ public class GHWorkflowRunTest extends AbstractGitHubWireMockTest {
2627
2728 private GHRepository repo ;
2829
29- private Duration atLeast ;
30- private Duration pollInterval ;
31- private Duration atMost ;
32-
33- private long cancelledWorkflowRunId ;
34- private long workflowRunIdToDelete ;
35-
36- @ Override
37- protected WireMockConfiguration getWireMockOptions () {
38- return super .getWireMockOptions ().extensions (templating .newResponseTransformer ());
39- }
40-
4130 @ Before
4231 public void setUp () throws Exception {
4332 repo = gitHub .getRepository (REPO_NAME );
44-
45- if (mockGitHub .isUseProxy ()) {
46- atLeast = Duration .ofSeconds (5 );
47- pollInterval = Duration .ofSeconds (5 );
48- atMost = Duration .ofSeconds (60 );
49- } else {
50- atLeast = Duration .ofMillis (20 );
51- pollInterval = Duration .ofMillis (20 );
52- atMost = Duration .ofMillis (240 );
53- }
5433 }
5534
5635 @ Test
@@ -61,39 +40,39 @@ public void testManualRunAndBasicInformation() throws IOException {
6140
6241 workflow .dispatch (MAIN_BRANCH );
6342
64- // now that we have triggered a workflow run, we can try to get the latest info from the run
65- Awaitility . await (). atLeast ( atLeast ). pollInterval ( pollInterval ). atMost ( atMost ). until (() -> {
66- List < GHWorkflowRun > workflowRuns = getLatestWorkflowRuns ( MAIN_BRANCH , Status . COMPLETED );
67- for ( GHWorkflowRun workflowRun : workflowRuns ) {
68- if ( workflowRun . getName (). equals ( FAST_WORKFLOW_NAME )
69- && workflowRun . getId () > latestPreexistingWorkflowRunId ) {
70- assertEquals ( workflow . getId (), workflowRun . getWorkflowId ());
71- assertTrue ( workflowRun . getUrl (). getPath (). contains ( "/actions/runs/" ));
72- assertTrue ( workflowRun . getHtmlUrl (). getPath (). contains ( "/actions/runs/" ));
73- assertTrue ( workflowRun . getJobsUrl (). getPath (). endsWith ( "/jobs" ));
74- assertTrue ( workflowRun . getLogsUrl (). getPath (). endsWith ( "/logs " ));
75- assertTrue ( workflowRun . getCheckSuiteUrl (). getPath (). contains ( "/check-suites/" ));
76- assertTrue ( workflowRun . getArtifactsUrl (). getPath (). endsWith ( "/artifacts" ));
77- assertTrue (workflowRun .getCancelUrl ().getPath ().endsWith ("/cancel " ));
78- assertTrue (workflowRun .getRerunUrl ().getPath ().endsWith ("/rerun " ));
79- assertTrue (workflowRun .getWorkflowUrl ().getPath ().contains ("/actions/workflows/ " ));
80- assertEquals ( MAIN_BRANCH , workflowRun .getHeadBranch ( ));
81- assertNotNull (workflowRun .getHeadCommit ().getId ( ));
82- assertNotNull (workflowRun .getHeadCommit ().getTreeId ( ));
83- assertNotNull (workflowRun .getHeadCommit ().getMessage ( ));
84- assertNotNull (workflowRun .getHeadCommit ().getTimestamp ( ));
85- assertNotNull (workflowRun .getHeadCommit ().getAuthor ().getEmail ( ));
86- assertNotNull ( workflowRun .getHeadCommit (). getCommitter (). getEmail ());
87- assertEquals ( GHEvent . WORKFLOW_DISPATCH , workflowRun .getEvent ());
88- assertEquals ( Status . COMPLETED , workflowRun .getStatus ());
89- assertEquals ( Conclusion . SUCCESS , workflowRun .getConclusion ());
90- assertNotNull (workflowRun .getHeadSha ());
91-
92- return true ;
93- }
94- }
95- return false ;
96- } );
43+ await (( nonRecordingRepo ) -> getWorkflowRun ( nonRecordingRepo ,
44+ FAST_WORKFLOW_NAME ,
45+ MAIN_BRANCH ,
46+ Status . COMPLETED ,
47+ latestPreexistingWorkflowRunId ). isPresent ());
48+
49+ GHWorkflowRun workflowRun = getWorkflowRun ( FAST_WORKFLOW_NAME ,
50+ MAIN_BRANCH ,
51+ Status . COMPLETED ,
52+ latestPreexistingWorkflowRunId ). orElseThrow (
53+ () -> new IllegalStateException ( "We must have a valid workflow run starting from here " ));
54+
55+ assertEquals ( workflow . getId (), workflowRun . getWorkflowId ( ));
56+ assertTrue (workflowRun .getUrl ().getPath ().contains ("/actions/runs/ " ));
57+ assertTrue (workflowRun .getHtmlUrl ().getPath ().contains ("/actions/runs/ " ));
58+ assertTrue (workflowRun .getJobsUrl ().getPath ().endsWith ("/jobs " ));
59+ assertTrue ( workflowRun .getLogsUrl (). getPath (). endsWith ( "/logs" ));
60+ assertTrue (workflowRun .getCheckSuiteUrl ().getPath (). contains ( "/check-suites/" ));
61+ assertTrue (workflowRun .getArtifactsUrl ().getPath (). endsWith ( "/artifacts" ));
62+ assertTrue (workflowRun .getCancelUrl ().getPath (). endsWith ( "/cancel" ));
63+ assertTrue (workflowRun .getRerunUrl ().getPath (). endsWith ( "/rerun" ));
64+ assertTrue (workflowRun .getWorkflowUrl ().getPath ().contains ( "/actions/workflows/" ));
65+ assertEquals ( MAIN_BRANCH , workflowRun .getHeadBranch ());
66+ assertNotNull ( workflowRun .getHeadCommit (). getId ());
67+ assertNotNull ( workflowRun .getHeadCommit (). getTreeId ());
68+ assertNotNull ( workflowRun .getHeadCommit (). getMessage ());
69+ assertNotNull (workflowRun .getHeadCommit (). getTimestamp ());
70+ assertNotNull ( workflowRun . getHeadCommit (). getAuthor (). getEmail ());
71+ assertNotNull ( workflowRun . getHeadCommit (). getCommitter (). getEmail ()) ;
72+ assertEquals ( GHEvent . WORKFLOW_DISPATCH , workflowRun . getEvent ());
73+ assertEquals ( Status . COMPLETED , workflowRun . getStatus ());
74+ assertEquals ( Conclusion . SUCCESS , workflowRun . getConclusion ()) ;
75+ assertNotNull ( workflowRun . getHeadSha () );
9776 }
9877
9978 @ Test
@@ -105,43 +84,39 @@ public void testCancelAndRerun() throws IOException {
10584 workflow .dispatch (MAIN_BRANCH );
10685
10786 // now that we have triggered the workflow run, we will wait until it's in progress and then cancel it
108- Awaitility .await ().atLeast (atLeast ).pollInterval (pollInterval ).atMost (atMost ).until (() -> {
109- List <GHWorkflowRun > workflowRuns = getLatestWorkflowRuns (MAIN_BRANCH , Status .IN_PROGRESS );
110- for (GHWorkflowRun workflowRun : workflowRuns ) {
111- if (workflowRun .getName ().equals (SLOW_WORKFLOW_NAME )
112- && workflowRun .getId () > latestPreexistingWorkflowRunId ) {
113- assertNotNull (workflowRun .getId ());
114-
115- workflowRun .cancel ();
116- cancelledWorkflowRunId = workflowRun .getId ();
117- return true ;
118- }
119- }
120- return false ;
121- });
87+ await ((nonRecordingRepo ) -> getWorkflowRun (nonRecordingRepo ,
88+ SLOW_WORKFLOW_NAME ,
89+ MAIN_BRANCH ,
90+ Status .IN_PROGRESS ,
91+ latestPreexistingWorkflowRunId ).isPresent ());
12292
123- // let's check that it has been properly cancelled
124- Awaitility .await ().atLeast (atLeast ).pollInterval (pollInterval ).atMost (atMost ).until (() -> {
125- GHWorkflowRun workflowRun = repo .getWorkflowRun (cancelledWorkflowRunId );
126- if (workflowRun .getStatus () == Status .COMPLETED && workflowRun .getConclusion () == Conclusion .CANCELLED ) {
127- return true ;
128- }
93+ GHWorkflowRun workflowRun = getWorkflowRun (SLOW_WORKFLOW_NAME ,
94+ MAIN_BRANCH ,
95+ Status .IN_PROGRESS ,
96+ latestPreexistingWorkflowRunId ).orElseThrow (
97+ () -> new IllegalStateException ("We must have a valid workflow run starting from here" ));
12998
130- return false ;
131- });
99+ assertNotNull (workflowRun .getId ());
100+
101+ workflowRun .cancel ();
102+ long cancelledWorkflowRunId = workflowRun .getId ();
103+
104+ // let's wait until it's completed
105+ await ((nonRecordingRepo ) -> getWorkflowRunStatus (nonRecordingRepo , cancelledWorkflowRunId ) == Status .COMPLETED );
106+
107+ // let's check that it has been properly cancelled
108+ workflowRun = repo .getWorkflowRun (cancelledWorkflowRunId );
109+ assertEquals (Conclusion .CANCELLED , workflowRun .getConclusion ());
132110
133111 // now let's rerun it
134- GHWorkflowRun cancelledWorkflowRun = repo .getWorkflowRun (cancelledWorkflowRunId );
135- cancelledWorkflowRun .rerun ();
112+ workflowRun .rerun ();
136113
137114 // let's check that it has been rerun
138- Awaitility .await ().atLeast (atLeast ).pollInterval (pollInterval ).atMost (atMost ).until (() -> {
139- GHWorkflowRun rerunWorkflowRun = repo .getWorkflowRun (cancelledWorkflowRunId );
140- return rerunWorkflowRun .getStatus () == Status .IN_PROGRESS ;
141- });
115+ await ((nonRecordingRepo ) -> getWorkflowRunStatus (nonRecordingRepo ,
116+ cancelledWorkflowRunId ) == Status .IN_PROGRESS );
142117
143118 // cancel it again
144- cancelledWorkflowRun .cancel ();
119+ workflowRun .cancel ();
145120 }
146121
147122 @ Test
@@ -152,28 +127,25 @@ public void testDelete() throws IOException {
152127
153128 workflow .dispatch (MAIN_BRANCH );
154129
155- // now that we have triggered a workflow run, we can try to get the latest info from the run
156- Awaitility .await ().atLeast (atLeast ).pollInterval (pollInterval ).atMost (atMost ).until (() -> {
157- List <GHWorkflowRun > workflowRuns = getLatestWorkflowRuns (MAIN_BRANCH , Status .COMPLETED );
158- for (GHWorkflowRun workflowRun : workflowRuns ) {
159- if (workflowRun .getName ().equals (FAST_WORKFLOW_NAME )
160- && workflowRun .getId () > latestPreexistingWorkflowRunId ) {
161- assertNotNull (workflowRun .getId ());
130+ await ((nonRecordingRepo ) -> getWorkflowRun (nonRecordingRepo ,
131+ FAST_WORKFLOW_NAME ,
132+ MAIN_BRANCH ,
133+ Status .COMPLETED ,
134+ latestPreexistingWorkflowRunId ).isPresent ());
162135
163- workflowRunIdToDelete = workflowRun .getId ();
136+ GHWorkflowRun workflowRunToDelete = getWorkflowRun (FAST_WORKFLOW_NAME ,
137+ MAIN_BRANCH ,
138+ Status .COMPLETED ,
139+ latestPreexistingWorkflowRunId ).orElseThrow (
140+ () -> new IllegalStateException ("We must have a valid workflow run starting from here" ));
164141
165- return true ;
166- }
167- }
168- return false ;
169- });
142+ assertNotNull (workflowRunToDelete .getId ());
170143
171- GHWorkflowRun workflowRunToDelete = repo .getWorkflowRun (workflowRunIdToDelete );
172144 workflowRunToDelete .delete ();
173145
174146 try {
175- repo .getWorkflowRun (workflowRunIdToDelete );
176- Assert .fail ("The workflow " + workflowRunIdToDelete + " should have been deleted." );
147+ repo .getWorkflowRun (workflowRunToDelete . getId () );
148+ Assert .fail ("The workflow " + workflowRunToDelete . getId () + " should have been deleted." );
177149 } catch (GHFileNotFoundException e ) {
178150 // success
179151 }
@@ -187,37 +159,75 @@ public void testSearchOnBranch() throws IOException {
187159
188160 workflow .dispatch (SECOND_BRANCH );
189161
190- // now that we have triggered a workflow run, we can try to get the latest info from the run
191- Awaitility .await ().atLeast (atLeast ).pollInterval (pollInterval ).atMost (atMost ).until (() -> {
192- List <GHWorkflowRun > workflowRuns = getLatestWorkflowRuns (SECOND_BRANCH , Status .COMPLETED );
193- for (GHWorkflowRun workflowRun : workflowRuns ) {
194- if (workflowRun .getName ().equals (FAST_WORKFLOW_NAME )
195- && workflowRun .getId () > latestPreexistingWorkflowRunId ) {
196- assertEquals (workflow .getId (), workflowRun .getWorkflowId ());
197- assertEquals (SECOND_BRANCH , workflowRun .getHeadBranch ());
198- assertEquals (GHEvent .WORKFLOW_DISPATCH , workflowRun .getEvent ());
199- assertEquals (Status .COMPLETED , workflowRun .getStatus ());
200- assertEquals (Conclusion .SUCCESS , workflowRun .getConclusion ());
201-
202- return true ;
203- }
204- }
205- return false ;
162+ await ((nonRecordingRepo ) -> getWorkflowRun (nonRecordingRepo ,
163+ FAST_WORKFLOW_NAME ,
164+ SECOND_BRANCH ,
165+ Status .COMPLETED ,
166+ latestPreexistingWorkflowRunId ).isPresent ());
167+
168+ GHWorkflowRun workflowRun = getWorkflowRun (FAST_WORKFLOW_NAME ,
169+ SECOND_BRANCH ,
170+ Status .COMPLETED ,
171+ latestPreexistingWorkflowRunId ).orElseThrow (
172+ () -> new IllegalStateException ("We must have a valid workflow run starting from here" ));
173+
174+ assertEquals (workflow .getId (), workflowRun .getWorkflowId ());
175+ assertEquals (SECOND_BRANCH , workflowRun .getHeadBranch ());
176+ assertEquals (GHEvent .WORKFLOW_DISPATCH , workflowRun .getEvent ());
177+ assertEquals (Status .COMPLETED , workflowRun .getStatus ());
178+ assertEquals (Conclusion .SUCCESS , workflowRun .getConclusion ());
179+ }
180+
181+ private void await (Function <GHRepository , Boolean > condition ) throws IOException {
182+ if (!mockGitHub .isUseProxy ()) {
183+ return ;
184+ }
185+
186+ GHRepository nonRecordingRepo = getGitHubBeforeAfter ().getRepository (REPO_NAME );
187+
188+ Awaitility .await ().pollInterval (Duration .ofSeconds (5 )).atMost (Duration .ofSeconds (60 )).until (() -> {
189+ return condition .apply (nonRecordingRepo );
206190 });
207191 }
208192
209193 private long getLatestPreexistingWorkflowRunId () {
210194 return repo .queryWorkflowRuns ().list ().withPageSize (1 ).iterator ().next ().getId ();
211195 }
212196
213- private List <GHWorkflowRun > getLatestWorkflowRuns (String branch , Status status ) {
214- return repo .queryWorkflowRuns ()
197+ private static Optional <GHWorkflowRun > getWorkflowRun (GHRepository repository ,
198+ String workflowName ,
199+ String branch ,
200+ Status status ,
201+ long latestPreexistingWorkflowRunId ) {
202+ List <GHWorkflowRun > workflowRuns = repository .queryWorkflowRuns ()
215203 .branch (branch )
216204 .status (status )
217205 .event (GHEvent .WORKFLOW_DISPATCH )
218206 .list ()
219207 .withPageSize (20 )
220208 .iterator ()
221209 .nextPage ();
210+
211+ for (GHWorkflowRun workflowRun : workflowRuns ) {
212+ if (workflowRun .getName ().equals (workflowName ) && workflowRun .getId () > latestPreexistingWorkflowRunId ) {
213+ return Optional .of (workflowRun );
214+ }
215+ }
216+ return Optional .empty ();
217+ }
218+
219+ private Optional <GHWorkflowRun > getWorkflowRun (String workflowName ,
220+ String branch ,
221+ Status status ,
222+ long latestPreexistingWorkflowRunId ) {
223+ return getWorkflowRun (this .repo , workflowName , branch , status , latestPreexistingWorkflowRunId );
224+ }
225+
226+ private static Status getWorkflowRunStatus (GHRepository repository , long workflowRunId ) {
227+ try {
228+ return repository .getWorkflowRun (workflowRunId ).getStatus ();
229+ } catch (IOException e ) {
230+ throw new IllegalStateException ("Unable to get workflow run status" , e );
231+ }
222232 }
223233}
0 commit comments