Skip to content

Commit

Permalink
Use rerequested action created by github instead of user requested ac…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
XiongKezhi committed Aug 17, 2020
1 parent 6942cb0 commit ea060be
Show file tree
Hide file tree
Showing 5 changed files with 548 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,18 @@ protected Set<GHEvent> events() {

@Override
protected void onEvent(final GHSubscriberEvent event) {
// TODO: open a PR in Checks API to expose properties in GHRequestedAction
final String payload = event.getPayload();
JSONObject json = JSONObject.fromObject(payload);
if (!json.getString("action").equals("requested_action")
|| !json.getJSONObject("requested_action").get("identifier").equals("rerun")) {
LOGGER.log(Level.FINE, "Unsupported check run event: " + payload.replaceAll("[\r\n]", ""));
return;
}

GHEventPayload.CheckRun checkRun;
try {
checkRun = GitHub.offline().parseEventPayload(new StringReader(payload), GHEventPayload.CheckRun.class);
}
catch (IOException e) {
throw new IllegalStateException("Could not parse check run event: " + payload.replaceAll("\r\n", ""), e);
throw new IllegalStateException("Could not parse check run event: " + payload.replaceAll("[\r\n]", ""), e);
}

if (!checkRun.getAction().equals("rerequested")) {
LOGGER.log(Level.FINE, "Unsupported check run action: " + checkRun.getAction().replaceAll("[\r\n]", ""));
return;
}

LOGGER.log(Level.INFO, "Received rerun request through GitHub checks API.");
Expand Down Expand Up @@ -108,7 +105,7 @@ private void scheduleRerun(final GHEventPayload.CheckRun checkRun, final String
}

LOGGER.log(Level.WARNING, String.format("No proper job found for the rerun request from repository: %s and "
+ "branch: %s", repository.getFullName(), branchName).replaceAll("\r\n", ""));
+ "branch: %s", repository.getFullName(), branchName).replaceAll("[\r\n]", ""));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,19 @@ void shouldSubscribeToCheckRunEvent() {
}

@Test
void shouldProcessCheckRunEventWithRerunAction() throws IOException {
void shouldProcessCheckRunEventWithRerequestedAction() throws IOException {
loggerRule.record(CheckRunGHEventSubscriber.class.getName(), Level.INFO).capture(1);
new CheckRunGHEventSubscriber(mock(JenkinsFacade.class), mock(GitHubSCMFacade.class))
.onEvent(createEventWithRerunRequest(RERUN_REQUEST_JSON_FOR_PR));
assertThat(loggerRule.getMessages().get(0)).contains("Received rerun request through GitHub checks API.");
}

@Test
void shouldIgnoreCheckRunEventWithoutRequestedAction() {
GHSubscriberEvent event = mock(GHSubscriberEvent.class);
when(event.getPayload()).thenReturn("{\"action\":\"created\"}");

void shouldIgnoreCheckRunEventWithoutRerequestedAction() throws IOException {
loggerRule.record(CheckRunGHEventSubscriber.class.getName(), Level.FINE).capture(1);
new CheckRunGHEventSubscriber(mock(JenkinsFacade.class), mock(GitHubSCMFacade.class)).onEvent(event);
assertThat(loggerRule.getMessages()).contains("Unsupported check run event: {\"action\":\"created\"}");
}

@Test
void shouldIgnoreCheckRunEVentWithOtherAction() {
GHSubscriberEvent event = mock(GHSubscriberEvent.class);
when(event.getPayload()).thenReturn("{\"action\":\"requested_action\",\"requested_action\":" +
"{\"identifier\":\"fix\"}}");

loggerRule.record(CheckRunGHEventSubscriber.class.getName(), Level.FINE).capture(1);
new CheckRunGHEventSubscriber(mock(JenkinsFacade.class), mock(GitHubSCMFacade.class)).onEvent(event);
assertThat(loggerRule.getMessages()).contains("Unsupported check run event: " +
"{\"action\":\"requested_action\",\"requested_action\":{\"identifier\":\"fix\"}}");
new CheckRunGHEventSubscriber(mock(JenkinsFacade.class), mock(GitHubSCMFacade.class))
.onEvent(createEventWithRerunRequest("check-run-event-with-created-action.json"));
assertThat(loggerRule.getMessages()).contains("Unsupported check run action: created");
}

@Test
Expand All @@ -100,18 +86,18 @@ void shouldScheduleRerunForPR() throws IOException {
GitHubSCMSource source = mock(GitHubSCMSource.class);

when(jenkinsFacade.getAllJobs()).thenReturn(Collections.singletonList(job));
when(jenkinsFacade.getFullNameOf(job)).thenReturn("Sandbox/PR-1");
when(jenkinsFacade.getFullNameOf(job)).thenReturn("codingstyle/PR-1");
when(scmFacade.findGitHubSCMSource(job)).thenReturn(Optional.of(source));
when(source.getRepoOwner()).thenReturn("XiongKezhi");
when(source.getRepository()).thenReturn("Sandbox");
when(source.getRepository()).thenReturn("codingstyle");
when(job.getNextBuildNumber()).thenReturn(1);
when(job.getName()).thenReturn("PR-1");

loggerRule.record(CheckRunGHEventSubscriber.class.getName(), Level.INFO).capture(1);
new CheckRunGHEventSubscriber(jenkinsFacade, scmFacade)
.onEvent(createEventWithRerunRequest(RERUN_REQUEST_JSON_FOR_PR));
assertThat(loggerRule.getMessages())
.contains("Scheduled rerun (build #1) for job Sandbox/PR-1, requested by XiongKezhi");
.contains("Scheduled rerun (build #1) for job codingstyle/PR-1, requested by XiongKezhi");
}

@Test
Expand All @@ -122,18 +108,18 @@ void shouldScheduleRerunForMaster() throws IOException {
GitHubSCMSource source = mock(GitHubSCMSource.class);

when(jenkinsFacade.getAllJobs()).thenReturn(Collections.singletonList(job));
when(jenkinsFacade.getFullNameOf(job)).thenReturn("Sandbox/master");
when(jenkinsFacade.getFullNameOf(job)).thenReturn("codingstyle/master");
when(scmFacade.findGitHubSCMSource(job)).thenReturn(Optional.of(source));
when(source.getRepoOwner()).thenReturn("XiongKezhi");
when(source.getRepository()).thenReturn("Sandbox");
when(source.getRepository()).thenReturn("codingstyle");
when(job.getNextBuildNumber()).thenReturn(1);
when(job.getName()).thenReturn("master");

loggerRule.record(CheckRunGHEventSubscriber.class.getName(), Level.INFO).capture(1);
new CheckRunGHEventSubscriber(jenkinsFacade, scmFacade)
.onEvent(createEventWithRerunRequest(RERUN_REQUEST_JSON_FOR_MASTER));
assertThat(loggerRule.getMessages())
.contains("Scheduled rerun (build #1) for job Sandbox/master, requested by XiongKezhi");
.contains("Scheduled rerun (build #1) for job codingstyle/master, requested by XiongKezhi");
}

@Test
Expand Down Expand Up @@ -196,7 +182,7 @@ void shouldNotScheduleRerunWhenBranchNamesAreDifferent() throws IOException {
when(jenkinsFacade.getAllJobs()).thenReturn(Collections.singletonList(job));
when(scmFacade.findGitHubSCMSource(job)).thenReturn(Optional.of(source));
when(source.getRepoOwner()).thenReturn("XiongKezhi");
when(source.getRepository()).thenReturn("Sandbox");
when(source.getRepository()).thenReturn("codingstyle");
when(job.getName()).thenReturn("PR-2");

assertNoBuildIsScheduled(jenkinsFacade, scmFacade, createEventWithRerunRequest(RERUN_REQUEST_JSON_FOR_PR));
Expand All @@ -215,7 +201,7 @@ private void assertNoBuildIsScheduled(final JenkinsFacade jenkinsFacade, final G
loggerRule.record(CheckRunGHEventSubscriber.class.getName(), Level.WARNING).capture(1);
new CheckRunGHEventSubscriber(jenkinsFacade, scmFacade).onEvent(event);
assertThat(loggerRule.getMessages())
.contains("No proper job found for the rerun request from repository: XiongKezhi/Sandbox and "
.contains("No proper job found for the rerun request from repository: XiongKezhi/codingstyle and "
+ "branch: PR-1");
}

Expand Down
Loading

0 comments on commit ea060be

Please sign in to comment.