diff --git a/src/main/java/com/dabsquared/gitlabjenkins/util/CommitStatusUpdater.java b/src/main/java/com/dabsquared/gitlabjenkins/util/CommitStatusUpdater.java index 25ae5711a..1a1459ba2 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/util/CommitStatusUpdater.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/util/CommitStatusUpdater.java @@ -1,6 +1,5 @@ package com.dabsquared.gitlabjenkins.util; - import com.dabsquared.gitlabjenkins.cause.CauseData; import com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause; import com.dabsquared.gitlabjenkins.connection.GitLabConnection; @@ -42,10 +41,9 @@ public class CommitStatusUpdater { private final static Logger LOGGER = Logger.getLogger(CommitStatusUpdater.class.getName()); - public static void updateCommitStatus(Run build, TaskListener listener, BuildState state, String name, List gitLabBranchBuilds, GitLabConnectionProperty connection) { GitLabClient client; - if(connection != null) { + if (connection != null) { client = connection.getClient(); } else { client = getClient(build); @@ -65,28 +63,30 @@ public static void updateCommitStatus(Run build, TaskListener listener, Bu } final String buildUrl = getBuildUrl(build); - for (final GitLabBranchBuild gitLabBranchBuild : gitLabBranchBuilds) { - try { - GitLabClient current_client = client; - if(gitLabBranchBuild.getConnection() != null ) { - GitLabClient build_specific_client = gitLabBranchBuild.getConnection().getClient(); - if (build_specific_client != null) { - current_client = build_specific_client; + if (gitLabBranchBuilds != null) { + for (final GitLabBranchBuild gitLabBranchBuild : gitLabBranchBuilds) { + try { + GitLabClient current_client = client; + if (gitLabBranchBuild.getConnection() != null) { + GitLabClient build_specific_client = gitLabBranchBuild.getConnection().getClient(); + if (build_specific_client != null) { + current_client = build_specific_client; + } } - } - String current_build_name = name; - if(gitLabBranchBuild.getName() != null ) { - current_build_name = gitLabBranchBuild.getName(); - } + String current_build_name = name; + if (gitLabBranchBuild.getName() != null) { + current_build_name = gitLabBranchBuild.getName(); + } - if (existsCommit(current_client, gitLabBranchBuild.getProjectId(), gitLabBranchBuild.getRevisionHash())) { - LOGGER.log(Level.INFO, String.format("Updating build '%s' to '%s'", gitLabBranchBuild.getProjectId(),state)); - current_client.changeBuildStatus(gitLabBranchBuild.getProjectId(), gitLabBranchBuild.getRevisionHash(), state, getBuildBranchOrTag(build), current_build_name, buildUrl, state.name()); + if (existsCommit(current_client, gitLabBranchBuild.getProjectId(), gitLabBranchBuild.getRevisionHash())) { + LOGGER.log(Level.INFO, String.format("Updating build '%s' to '%s'", gitLabBranchBuild.getProjectId(), state)); + current_client.changeBuildStatus(gitLabBranchBuild.getProjectId(), gitLabBranchBuild.getRevisionHash(), state, getBuildBranchOrTag(build), current_build_name, buildUrl, state.name()); + } + } catch (WebApplicationException | ProcessingException e) { + printf(listener, "Failed to update Gitlab commit status for project '%s': %s%n", gitLabBranchBuild.getProjectId(), e.getMessage()); + LOGGER.log(Level.SEVERE, String.format("Failed to update Gitlab commit status for project '%s'", gitLabBranchBuild.getProjectId()), e); } - } catch (WebApplicationException | ProcessingException e) { - printf(listener, "Failed to update Gitlab commit status for project '%s': %s%n", gitLabBranchBuild.getProjectId(), e.getMessage()); - LOGGER.log(Level.SEVERE, String.format("Failed to update Gitlab commit status for project '%s'", gitLabBranchBuild.getProjectId()), e); } } } @@ -94,7 +94,7 @@ public static void updateCommitStatus(Run build, TaskListener listener, Bu public static void updateCommitStatus(Run build, TaskListener listener, BuildState state, String name) { try { - updateCommitStatus(build,listener,state,name,null,null); + updateCommitStatus(build, listener, state, name, null, null); } catch (IllegalStateException e) { printf(listener, "Failed to update Gitlab commit status: %s%n", e.getMessage()); } @@ -271,6 +271,4 @@ private static List findBuildsFromUpstreamCauses(List } return Collections.emptyList(); } - - } diff --git a/src/main/java/com/dabsquared/gitlabjenkins/workflow/GitLabBranchBuild.java b/src/main/java/com/dabsquared/gitlabjenkins/workflow/GitLabBranchBuild.java index 7ba226d79..95bb07c47 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/workflow/GitLabBranchBuild.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/workflow/GitLabBranchBuild.java @@ -1,6 +1,7 @@ package com.dabsquared.gitlabjenkins.workflow; import com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty; +import edu.umd.cs.findbugs.annotations.NonNull; import hudson.Extension; import hudson.model.AbstractDescribableImpl; import hudson.model.Descriptor; @@ -13,7 +14,6 @@ public class GitLabBranchBuild extends AbstractDescribableImpl { private static final Logger LOGGER = LoggerFactory.getLogger(GitLabBranchBuild.class); - private String name; private String projectId; private String revisionHash; @@ -70,10 +70,9 @@ public GitLabConnectionProperty getConnection() { return connection; } - - @Extension public static class DescriptorImpl extends Descriptor { + @NonNull @Override public String getDisplayName() { return "Gitlab Branch Build"; diff --git a/src/main/java/com/dabsquared/gitlabjenkins/workflow/GitLabBuildsStep.java b/src/main/java/com/dabsquared/gitlabjenkins/workflow/GitLabBuildsStep.java index b387ad1cb..fe7f9f7a5 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/workflow/GitLabBuildsStep.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/workflow/GitLabBuildsStep.java @@ -37,8 +37,7 @@ public class GitLabBuildsStep extends Step { @DataBoundConstructor public GitLabBuildsStep() { } - - + @Override public StepExecution start(StepContext context) throws Exception { return new GitLabBuildStepExecution(context, this); diff --git a/src/main/java/com/dabsquared/gitlabjenkins/workflow/GitLabCommitStatusStep.java b/src/main/java/com/dabsquared/gitlabjenkins/workflow/GitLabCommitStatusStep.java index 1a7b65a04..c97f0bdfd 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/workflow/GitLabCommitStatusStep.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/workflow/GitLabCommitStatusStep.java @@ -129,7 +129,7 @@ public void stop(@NonNull Throwable cause) throws Exception { // should be no need to do anything special (but verify in JENKINS-26148) if (body != null) { String name = StringUtils.isEmpty(step.name) ? "jenkins" : step.name; - CommitStatusUpdater.updateCommitStatus(run, null, BuildState.canceled, name, step.builds, step.connection); + CommitStatusUpdater.updateCommitStatus(run, null, BuildState.canceled, name, step.builds, step.connection); body.cancel(cause); } } @@ -149,6 +149,7 @@ private TaskListener getTaskListener(StepContext context) { @Extension public static final class DescriptorImpl extends StepDescriptor { + @NonNull @Override public String getDisplayName() { return "Update the commit status in GitLab depending on the build status"; @@ -166,9 +167,9 @@ public boolean takesImplicitBlockArgument() { @Override public Set> getRequiredContext() { - Set> context = new HashSet<>(); - Collections.addAll(context, TaskListener.class, Run.class); - return Collections.unmodifiableSet(context); + Set> context = new HashSet<>(); + Collections.addAll(context, TaskListener.class, Run.class); + return Collections.unmodifiableSet(context); } } } diff --git a/src/test/java/com/dabsquared/gitlabjenkins/workflow/GitLabCommitStatusStepTest.java b/src/test/java/com/dabsquared/gitlabjenkins/workflow/GitLabCommitStatusStepTest.java index 5c280c7a2..c062651d7 100644 --- a/src/test/java/com/dabsquared/gitlabjenkins/workflow/GitLabCommitStatusStepTest.java +++ b/src/test/java/com/dabsquared/gitlabjenkins/workflow/GitLabCommitStatusStepTest.java @@ -19,7 +19,7 @@ public void bare_gitlabCommitStatus() throws Exception { String pipelineText = IOUtils.toString(getClass().getResourceAsStream( "pipeline/bare-gitlabCommitStatus-pipeline.groovy")); project.setDefinition(new CpsFlowDefinition(pipelineText, false)); - Run build = j.buildAndAssertSuccess(project); + Run build = j.buildAndAssertSuccess(project); j.assertLogContains("this is simple jenkins-build", build); } @@ -29,7 +29,7 @@ public void named_simple_pipeline_builds_as_LString() throws Exception { String pipelineText = IOUtils.toString(getClass().getResourceAsStream( "pipeline/named-simple-pipeline-builds-as-LString.groovy")); project.setDefinition(new CpsFlowDefinition(pipelineText, false)); - Run build = j.buildAndAssertSuccess(project); + Run build = j.buildAndAssertSuccess(project); j.assertLogContains("this is pre-build stage", build); } @@ -39,7 +39,7 @@ public void named_simple_pipeline_builds_as_String() throws Exception { String pipelineText = IOUtils.toString(getClass().getResourceAsStream( "pipeline/named-simple-pipeline-builds-as-String.groovy")); project.setDefinition(new CpsFlowDefinition(pipelineText, false)); - Run build = j.buildAndAssertSuccess(project); + Run build = j.buildAndAssertSuccess(project); j.assertLogContains("this is pre-build stage", build); } @@ -49,7 +49,7 @@ public void multisite() throws Exception { String pipelineText = IOUtils.toString(getClass().getResourceAsStream( "pipeline/multisite-pipeline.groovy")); project.setDefinition(new CpsFlowDefinition(pipelineText, false)); - Run build = j.buildAndAssertSuccess(project); + Run build = j.buildAndAssertSuccess(project); j.assertLogContains("this is stage3", build); } @@ -59,7 +59,7 @@ public void multiproject_specific_connection() throws Exception { String pipelineText = IOUtils.toString(getClass().getResourceAsStream( "pipeline/multiproject-specific-connection-pipeline.groovy")); project.setDefinition(new CpsFlowDefinition(pipelineText, false)); - Run build = j.buildAndAssertSuccess(project); + Run build = j.buildAndAssertSuccess(project); j.assertLogContains("this is pre-build stage", build); } @@ -69,7 +69,7 @@ public void multiproject() throws Exception { String pipelineText = IOUtils.toString(getClass().getResourceAsStream( "pipeline/multiproject-pipeline.groovy")); project.setDefinition(new CpsFlowDefinition(pipelineText, false)); - Run build = j.buildAndAssertSuccess(project); + Run build = j.buildAndAssertSuccess(project); j.assertLogContains("this is pre-build stage", build); } }