Skip to content

Send build completion notification for pull requests merged after build start #472

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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 @@ -31,6 +31,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.ItemGroup;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
Expand All @@ -48,6 +49,8 @@
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMRevisionAction;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.SCMSourceOwner;
import jenkins.scm.impl.NullSCMSource;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;

Expand Down Expand Up @@ -100,8 +103,8 @@ private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener
@NonNull BitbucketApi bitbucket, @NonNull String key, @NonNull String hash)
throws IOException, InterruptedException {

final SCMSource source = SCMSource.SourceByItem.findSource(build.getParent());
if (!(source instanceof BitbucketSCMSource)) {
final BitbucketSCMSource source = findBitbucketSCMSource(build);
if (source == null) {
return;
}

Expand Down Expand Up @@ -167,6 +170,13 @@ private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener

private static @CheckForNull BitbucketSCMSource findBitbucketSCMSource(Run<?, ?> build) {
SCMSource s = SCMSource.SourceByItem.findSource(build.getParent());
if (s instanceof NullSCMSource) {
// for instance PR merged on Bitbucket since the build has been started
ItemGroup<?> grandFather = build.getParent().getParent();
if (grandFather instanceof SCMSourceOwner) {
s = ((SCMSourceOwner) grandFather).getSCMSources().get(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a Jenkins multibranch job has multiple SCM sources (e.g. an original repository and a fork with distinct branch names), I think this might not select the correct one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, because this plugin is using the older REST API that does not distinguish between repositories, it will work anyway, as long as the repositories are on the same server.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But a similar fix might not be valid for jenkinsci/github-checks-plugin#196 because the GitHub API Create a check run is at POST /repos/{owner}/{repo}/check-runs and presumably has to be told the correct repository.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix worked for me and was quite easy to implement. A solution to be really safe would be probably to save the url to which the INPROGRESS event has been sent to reuse it at build completion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not only the URL but also the credential ID.

I think this change is fine for bitbucket-branch-source-plugin, but we need something more reliable for the equivalent issue in github-checks-plugin and in the future bitbucket-server-checks-plugin.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can someone say whether the proposed fix can be merged (and do it) or otherwise provide feedback about what should be changed? Thanks.

}
}
return s instanceof BitbucketSCMSource ? (BitbucketSCMSource) s : null;
}

Expand Down