-
Notifications
You must be signed in to change notification settings - Fork 358
feature/324 | skip non PR notifications #451
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,12 +42,15 @@ | |
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
import jenkins.model.JenkinsLocationConfiguration; | ||
import jenkins.plugins.git.AbstractGitSCMSource; | ||
import jenkins.scm.api.SCMHeadObserver; | ||
import jenkins.scm.api.SCMRevision; | ||
import jenkins.scm.api.SCMRevisionAction; | ||
import jenkins.scm.api.SCMSource; | ||
import org.apache.commons.codec.digest.DigestUtils; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider; | ||
|
||
|
@@ -62,6 +65,7 @@ public class BitbucketBuildStatusNotifications { | |
private static final String FAILED_STATE = "FAILED"; | ||
private static final String STOPPED_STATE = "STOPPED"; | ||
private static final String INPROGRESS_STATE = "INPROGRESS"; | ||
private static final Pattern PR_BUILD_PATTERN = Pattern.compile("^.*/job/PR-(\\d+)/(\\d+)/.*$"); | ||
|
||
private static String getRootURL(@NonNull Run<?, ?> build) { | ||
JenkinsLocationConfiguration cfg = JenkinsLocationConfiguration.get(); | ||
|
@@ -157,7 +161,7 @@ private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener | |
statusDescription = StringUtils.defaultIfBlank(buildDescription, "The build is in progress..."); | ||
state = INPROGRESS_STATE; | ||
} | ||
status = new BitbucketBuildStatus(hash, statusDescription, state, url, key, name); | ||
status = new BitbucketBuildStatus(hash, statusDescription, state, url, DigestUtils.md5Hex(key), name); | ||
new BitbucketChangesetCommentNotifier(bitbucket).buildStatus(status); | ||
if (result != null) { | ||
listener.getLogger().println("[Bitbucket] Build result notified"); | ||
|
@@ -184,6 +188,7 @@ private static void sendNotifications(BitbucketSCMSource source, Run<?, ?> build | |
if (hash == null) { | ||
return; | ||
} | ||
|
||
boolean shareBuildKeyBetweenBranchAndPR = sourceContext | ||
.filters().stream() | ||
.anyMatch(filter -> filter instanceof ExcludeOriginPRBranchesSCMHeadFilter); | ||
|
@@ -199,6 +204,15 @@ private static void sendNotifications(BitbucketSCMSource source, Run<?, ?> build | |
listener.getLogger().println("[Bitbucket] Notifying commit build result"); | ||
key = getBuildKey(build, r.getHead().getName(), shareBuildKeyBetweenBranchAndPR); | ||
bitbucket = source.buildBitbucketClient(); | ||
if (sourceContext.doNotOverridePrBuildStatuses()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this have a special case for reporting the status of a pull request build? Like, there is a pull request and it's first built once and the build status goes to Bitbucket all right. Then a user of Jenkins replays the build — there is no new commit. When the second build finishes, does the plugin decide not to report the status to Bitbucket because that commit already has one build status from a pull request — even though it's from the same pull request? It seems to me that the previous build status should be overwritten in that case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for this case it shouldn't be a problem as notification for PRs being processed in the previous |
||
List<BitbucketBuildStatus> buildStatuses = bitbucket.getBuildStatus(hash).getValues(); | ||
for (final BitbucketBuildStatus bs : buildStatuses) { | ||
if (DigestUtils.md5Hex(key).equals(bs.getKey()) && PR_BUILD_PATTERN.matcher(bs.getUrl()).matches()) { | ||
listener.getLogger().println("[Bitbucket] Skip branch notification as this revision has PR build status"); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
createStatus(build, listener, bitbucket, key, hash); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2016, CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package com.cloudbees.jenkins.plugins.bitbucket.api; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class BitbucketBuildStatuses { | ||
private List<BitbucketBuildStatus> values; | ||
|
||
public List<BitbucketBuildStatus> getValues() { | ||
return values == null ? Collections.<BitbucketBuildStatus>emptyList() : Collections.unmodifiableList(values); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<div> | ||
Prevents a prematurely successful state of not fully tested PRs. | ||
<br/> | ||
<strong>"Exclude branches that are also filed as PRs"</strong> strategy implies that branch and PR builds | ||
do share the same key while publishing the build status to Bitbucket, thus can overwrite statuses of each other. | ||
Usually, it happens when the branch build finishes in the middle of a running PR build | ||
so Bitbucket has been already notified about the branch status and | ||
(in case this status was a success) we can merge the PR until the PR build is completed. | ||
|
||
<br/> | ||
<strong>NOTE:</strong> this option is only applicable when <strong>"Exclude branches that are also filed as PRs"</strong> strategy in multibranch configuration is used. | ||
</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The added md5Hex call looked surprising but I now see you just moved it from the BitbucketBuildStatus constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like it was added to avoid issues with longer names #15