From 4ecec17236326c7198d18d9a0e25a31b5a7596f3 Mon Sep 17 00:00:00 2001 From: Tommi Salonen Date: Sun, 3 Dec 2017 19:17:15 +0200 Subject: [PATCH] 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. --- .../trigger/handler/AbstractWebHookTriggerHandler.java | 5 ++--- .../handler/push/OpenMergeRequestPushHookTriggerHandler.java | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/AbstractWebHookTriggerHandler.java b/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/AbstractWebHookTriggerHandler.java index 13b1470..6dfbe23 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/AbstractWebHookTriggerHandler.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/AbstractWebHookTriggerHandler.java @@ -17,12 +17,12 @@ import hudson.model.Job; import hudson.plugins.git.GitSCM; import hudson.plugins.git.RevisionParameterAction; import hudson.scm.SCM; -import jenkins.model.Jenkins; import jenkins.model.ParameterizedJobMixIn; import jenkins.triggers.SCMTriggerItem; import net.karneim.pojobuilder.GeneratePojoBuilder; import org.apache.commons.lang.StringUtils; import org.eclipse.jgit.transport.URIish; +import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider; import javax.ws.rs.ProcessingException; import javax.ws.rs.WebApplicationException; @@ -69,8 +69,7 @@ public abstract class AbstractWebHookTriggerHandler implement if (client == null) { LOGGER.log(Level.SEVERE, "No GitLab connection configured"); } else { - String targetUrl = - Jenkins.getInstance().getRootUrl() + job.getUrl() + job.getNextBuildNumber() + "/"; + String targetUrl = DisplayURLProvider.get().getJobURL(job); client.changeBuildStatus(buildStatusUpdate.getProjectId(), buildStatusUpdate.getSha(), BuildState.pending, buildStatusUpdate.getRef(), buildName, targetUrl, BuildState.pending.name()); } diff --git a/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/push/OpenMergeRequestPushHookTriggerHandler.java b/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/push/OpenMergeRequestPushHookTriggerHandler.java index 30eef62..6a71b31 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/push/OpenMergeRequestPushHookTriggerHandler.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/push/OpenMergeRequestPushHookTriggerHandler.java @@ -21,11 +21,11 @@ import hudson.model.CauseAction; import hudson.model.Job; import hudson.plugins.git.RevisionParameterAction; import hudson.triggers.Trigger; -import jenkins.model.Jenkins; import jenkins.model.ParameterizedJobMixIn; import jenkins.model.ParameterizedJobMixIn.ParameterizedJob; import org.apache.commons.lang.StringUtils; import org.eclipse.jgit.transport.URIish; +import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider; import javax.ws.rs.ProcessingException; import javax.ws.rs.WebApplicationException; @@ -159,7 +159,7 @@ class OpenMergeRequestPushHookTriggerHandler implements PushHookTriggerHandler { if (StringUtils.isNotBlank(buildName)) { GitLabClient client = job.getProperty(GitLabConnectionProperty.class).getClient(); 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()); } catch (WebApplicationException | ProcessingException e) { LOGGER.log(Level.SEVERE, "Failed to set build state to pending", e);