Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subscriber for check run event in order to handle rerun request #25

Merged
merged 14 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ private void scheduleRerun(final GHEventPayload.CheckRun checkRun, final String
if (source.isPresent() && source.get().getRepoOwner().equals(repository.getOwnerName())
&& source.get().getRepository().equals(repository.getName())
&& job.getName().equals(branchName)) {
if (!gitHubSCMFacade.findHeadCommit(source.get(), job.getLastBuild())
.equals(checkRun.getCheckRun().getHeadSha())) {
XiongKezhi marked this conversation as resolved.
Show resolved Hide resolved
LOGGER.log(Level.INFO, "Ignored the rerun request since it's not requested for the head commit.");
return;
}

Cause cause = new GitHubChecksRerunActionCause(checkRun.getSender().getLogin());
ParameterizedJobMixIn.scheduleBuild2(job, 0, new CauseAction(cause));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import hudson.model.Job;
import hudson.model.Run;
import hudson.security.ACL;
import jenkins.plugins.git.AbstractGitSCMSource;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMRevisionAction;
import jenkins.scm.api.SCMSource;
import org.jenkinsci.plugins.github_branch_source.GitHubAppCredentials;
import org.jenkinsci.plugins.github_branch_source.GitHubSCMSource;
import org.jenkinsci.plugins.github_branch_source.PullRequestSCMRevision;

import java.io.IOException;
import java.util.Collections;
Expand Down Expand Up @@ -78,4 +82,24 @@ public Optional<SCMRevision> findRevision(final GitHubSCMSource source, final SC
source.getRepoOwner() + "/" + source.getRepository(), head.getName()), e);
}
}

/**
* Find the commit from given {@code build}.
*
* @param source
* the scm source the {@code build} is using
* @param build
* the target build
* @return the found commit
*/
public String findHeadCommit(final GitHubSCMSource source, final Run<?, ?> build) {
SCMRevision revision = SCMRevisionAction.getRevision(source, build);
if (revision instanceof AbstractGitSCMSource.SCMRevisionImpl) {
return ((AbstractGitSCMSource.SCMRevisionImpl)revision).getHash();
} else if (revision instanceof PullRequestSCMRevision) {
return ((PullRequestSCMRevision) revision).getPullHash();
} else {
throw new IllegalArgumentException("Unsupported revision " + revision);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.jenkins.plugins.checks.github;

import hudson.model.Item;
import hudson.model.Job;
import hudson.model.*;
import io.jenkins.plugins.util.JenkinsFacade;
import org.apache.commons.io.FileUtils;
import org.jenkinsci.plugins.github.extension.GHSubscriberEvent;
Expand Down Expand Up @@ -122,6 +121,34 @@ void shouldNotScheduleRerunWhenNoProperJobFound() throws IOException {
+ "branch: PR-1");
}

@Test
void shouldNotScheduleRerunWhenWhenRerunIsNotRequestedForHeadCommit() throws IOException {
Job job = mock(Job.class);
Run lastBuild = mock(Run.class);
JenkinsFacade jenkinsFacade = mock(JenkinsFacade.class);
GitHubSCMFacade scmFacade = mock(GitHubSCMFacade.class);
GitHubSCMSource source = mock(GitHubSCMSource.class);

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

loggerRule.record(CheckRunGHEventSubscriber.class.getName(), Level.INFO).capture(1);
new CheckRunGHEventSubscriber(jenkinsFacade, scmFacade)
.onEvent(new GHSubscriberEvent("shouldNotScheduleRerunWhenWhenRerunIsNotRequestedForHeadCommit",
GHEvent.CHECK_RUN, FileUtils.readFileToString(new File(getClass().getResource(
getClass().getSimpleName() + "/check-run-event-with-rerun-action.json").getFile()),
StandardCharsets.UTF_8)));
assertThat(loggerRule.getMessages())
.contains("Ignored the rerun request since it's not requested for the head commit.");
}

@Test
void shouldContainsUserInShortDescriptionOfGitHubChecksRerunActionCause() {
CheckRunGHEventSubscriber.GitHubChecksRerunActionCause cause =
Expand Down