Removed the build number from target url when the state is pending

(job is waiting for the next available executor).

Build number in the url causes problems when there are more than one job
in the build queue. In those cases job.getNextBuildNumber returns the same
number for every build in the queue which leads to wrong urls reported to Gitlab.

The build number is assigned when the execution starts so the best option
for pending builds is probably just to set the target url point to the job page without
the build number.
This commit is contained in:
Tommi Salonen 2017-12-03 19:17:15 +02:00
parent df7432a760
commit 4ecec17236
2 changed files with 4 additions and 5 deletions

View File

@ -17,12 +17,12 @@ import hudson.model.Job;
import hudson.plugins.git.GitSCM; import hudson.plugins.git.GitSCM;
import hudson.plugins.git.RevisionParameterAction; import hudson.plugins.git.RevisionParameterAction;
import hudson.scm.SCM; import hudson.scm.SCM;
import jenkins.model.Jenkins;
import jenkins.model.ParameterizedJobMixIn; import jenkins.model.ParameterizedJobMixIn;
import jenkins.triggers.SCMTriggerItem; import jenkins.triggers.SCMTriggerItem;
import net.karneim.pojobuilder.GeneratePojoBuilder; import net.karneim.pojobuilder.GeneratePojoBuilder;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.transport.URIish; import org.eclipse.jgit.transport.URIish;
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;
import javax.ws.rs.ProcessingException; import javax.ws.rs.ProcessingException;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
@ -69,8 +69,7 @@ public abstract class AbstractWebHookTriggerHandler<H extends WebHook> implement
if (client == null) { if (client == null) {
LOGGER.log(Level.SEVERE, "No GitLab connection configured"); LOGGER.log(Level.SEVERE, "No GitLab connection configured");
} else { } else {
String targetUrl = String targetUrl = DisplayURLProvider.get().getJobURL(job);
Jenkins.getInstance().getRootUrl() + job.getUrl() + job.getNextBuildNumber() + "/";
client.changeBuildStatus(buildStatusUpdate.getProjectId(), buildStatusUpdate.getSha(), client.changeBuildStatus(buildStatusUpdate.getProjectId(), buildStatusUpdate.getSha(),
BuildState.pending, buildStatusUpdate.getRef(), buildName, targetUrl, BuildState.pending.name()); BuildState.pending, buildStatusUpdate.getRef(), buildName, targetUrl, BuildState.pending.name());
} }

View File

@ -21,11 +21,11 @@ import hudson.model.CauseAction;
import hudson.model.Job; import hudson.model.Job;
import hudson.plugins.git.RevisionParameterAction; import hudson.plugins.git.RevisionParameterAction;
import hudson.triggers.Trigger; import hudson.triggers.Trigger;
import jenkins.model.Jenkins;
import jenkins.model.ParameterizedJobMixIn; import jenkins.model.ParameterizedJobMixIn;
import jenkins.model.ParameterizedJobMixIn.ParameterizedJob; import jenkins.model.ParameterizedJobMixIn.ParameterizedJob;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.transport.URIish; import org.eclipse.jgit.transport.URIish;
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;
import javax.ws.rs.ProcessingException; import javax.ws.rs.ProcessingException;
import javax.ws.rs.WebApplicationException; import javax.ws.rs.WebApplicationException;
@ -159,7 +159,7 @@ class OpenMergeRequestPushHookTriggerHandler implements PushHookTriggerHandler {
if (StringUtils.isNotBlank(buildName)) { if (StringUtils.isNotBlank(buildName)) {
GitLabClient client = job.getProperty(GitLabConnectionProperty.class).getClient(); GitLabClient client = job.getProperty(GitLabConnectionProperty.class).getClient();
try { try {
String targetUrl = Jenkins.getInstance().getRootUrl() + job.getUrl() + job.getNextBuildNumber() + "/"; String targetUrl = DisplayURLProvider.get().getJobURL(job);
client.changeBuildStatus(projectId, commit, BuildState.pending, ref, buildName, targetUrl, BuildState.pending.name()); client.changeBuildStatus(projectId, commit, BuildState.pending, ref, buildName, targetUrl, BuildState.pending.name());
} catch (WebApplicationException | ProcessingException e) { } catch (WebApplicationException | ProcessingException e) {
LOGGER.log(Level.SEVERE, "Failed to set build state to pending", e); LOGGER.log(Level.SEVERE, "Failed to set build state to pending", e);