Skip to content

Commit

Permalink
[JENKINS-23641] - Handle possible nulls if Git checkout fails
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Nenashev <o.v.nenashev@gmail.com>
  • Loading branch information
oleg-nenashev committed Oct 12, 2014
1 parent 0f29b1d commit c4347f8
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/main/java/com/cloudbees/jenkins/GitHubCommitNotifier.java
Expand Up @@ -29,6 +29,7 @@
import java.io.IOException;

import static hudson.model.Result.*;
import hudson.plugins.git.Revision;

/**
* Create commit status notifications on the commits based on the outcome of the build.
Expand All @@ -50,8 +51,15 @@ public BuildStepMonitor getRequiredMonitorService() {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {

BuildData buildData = build.getAction(BuildData.class);
String sha1 = ObjectId.toString(buildData.getLastBuiltRevision().getSha1());

if (buildData == null) {
throw new IOException("Cannot retrieve Git metadata for the build");
}
final Revision lastBuildRevision = buildData.getLastBuiltRevision();
final ObjectId sha1 = lastBuildRevision != null ? lastBuildRevision.getSha1() : null;
if (sha1 == null) { // Nowhere to report => fail the build
throw new IOException("Cannot determine sha1 of the commit. The status cannot be reported");
}

for (GitHubRepositoryName name : GitHubRepositoryNameContributor.parseAssociatedNames(build.getProject())) {
for (GHRepository repository : name.resolve()) {
GHCommitState state;
Expand All @@ -73,7 +81,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
}

listener.getLogger().println(Messages.GitHubCommitNotifier_SettingCommitStatus(repository.getUrl() + "/commit/" + sha1));
repository.createCommitStatus(sha1, state, build.getAbsoluteUrl(), msg);
repository.createCommitStatus(ObjectId.toString(sha1), state, build.getAbsoluteUrl(), msg);
}
}
return true;
Expand Down

0 comments on commit c4347f8

Please sign in to comment.