Skip to content

Commit

Permalink
Patch remaining NullPointerException bugs in GitLabCommitStatusStep (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
krisstern authored Feb 5, 2023
1 parent d8e6e55 commit e45da60
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<GitLabBranchBuild> gitLabBranchBuilds, GitLabConnectionProperty connection) {
GitLabClient client;
if(connection != null) {
if (connection != null) {
client = connection.getClient();
} else {
client = getClient(build);
Expand All @@ -65,36 +63,38 @@ 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);
}
}
}


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());
}
Expand Down Expand Up @@ -271,6 +271,4 @@ private static List<GitLabBranchBuild> findBuildsFromUpstreamCauses(List<Cause>
}
return Collections.emptyList();
}


}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,7 +14,6 @@
public class GitLabBranchBuild extends AbstractDescribableImpl<GitLabBranchBuild> {
private static final Logger LOGGER = LoggerFactory.getLogger(GitLabBranchBuild.class);


private String name;
private String projectId;
private String revisionHash;
Expand Down Expand Up @@ -70,10 +70,9 @@ public GitLabConnectionProperty getConnection() {
return connection;
}



@Extension
public static class DescriptorImpl extends Descriptor<GitLabBranchBuild> {
@NonNull
@Override
public String getDisplayName() {
return "Gitlab Branch Build";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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";
Expand All @@ -166,9 +167,9 @@ public boolean takesImplicitBlockArgument() {

@Override
public Set<Class<?>> getRequiredContext() {
Set<Class<?>> context = new HashSet<>();
Collections.addAll(context, TaskListener.class, Run.class);
return Collections.unmodifiableSet(context);
Set<Class<?>> context = new HashSet<>();
Collections.addAll(context, TaskListener.class, Run.class);
return Collections.unmodifiableSet(context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}
}

0 comments on commit e45da60

Please sign in to comment.