10000 Merge pull request #111 from jenkinsci/failed-update · github-cloud/github-plugin@25d7939 · GitHub
[go: up one dir, main page]

Skip to content

Commit 25d7939

Browse files
committed
Merge pull request jenkinsci#111 from jenkinsci/failed-update
Don't let the build fail if the commit doesn't exist in upstream
2 parents 4cf308e + e1728e4 commit 25d7939

File tree

4 files changed

+45
-18
lines changed

4 files changed

+45
-18
lines changed

src/main/java/com/cloudbees/jenkins/GitHubCommitNotifier.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import hudson.tasks.Publisher;
1515
import hudson.util.ListBoxModel;
1616
import jenkins.tasks.SimpleBuildStep;
17-
1817
import org.eclipse.jgit.lib.ObjectId;
1918
import org.jenkinsci.plugins.github.common.ExpandableMessage;
2019
import org.jenkinsci.plugins.github.util.BuildDataHelper;
@@ -24,8 +23,11 @@
2423
import org.kohsuke.github.GHRepository;
2524
import org.kohsuke.stapler.DataBoundConstructor;
2625
import org.kohsuke.stapler.DataBoundSetter;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
2728

2829
import javax.annotation.Nonnull;
30+
import java.io.FileNotFoundException;
2931
import java.io.IOException;
3032

3133
import static com.cloudbees.jenkins.Messages.GitHubCommitNotifier_DisplayName;
@@ -52,6 +54,8 @@ public class GitHubCommitNotifier extends Notifier implements SimpleBuildStep {
5254
private final String resultOnFailure;
5355
private static final Result[] SUPPORTED_RESULTS = {FAILURE, UNSTABLE, SUCCESS};
5456

57+
private static final Logger LOGGER = LoggerFactory.getLogger(GitHubCommitNotifier.class);
58+
5559
@Restricted(NoExternalUse.class)
5660
public GitHubCommitNotifier() {
5761
this(getDefaultResultOnFailure().toString());
@@ -104,9 +108,9 @@ public BuildStepMonitor getRequiredMonitorService() {
104108

105109
@Override
106110
public void perform(Run<?, ?> build,
107-
FilePath ws,
108-
Launcher launcher,
109-
TaskListener listener) throws InterruptedException, IOException {
111+
FilePath ws,
112+
Launcher launcher,
113+
TaskListener listener) throws InterruptedException, IOException {
110114
try {
111115
updateCommitStatus(build, listener);
112116
} catch (IOException error) {
@@ -139,11 +143,20 @@ private void updateCommitStatus(@Nonnull Run<?, ?> build,
139143
GitHubCommitNotifier_SettingCommitStatus(repository.getHtmlUrl() + "/commit/" + sha1)
140144
);
141145

142-
repository.createCommitStatus(
143-
sha1, status.getState(), build.getAbsoluteUrl(),
144-
message,
145-
contextName
146-
);
146+
try {
147+
repository.createCommitStatus(
148+
sha1, status.getState(), build.getAbsoluteUrl(),
149+
message,
150+
contextName
151+
);
152+
} catch (FileNotFoundException e) {
153+
// PR builds and other merge activities can create a merge commit that
154+
// doesn't exist in the upstream. Don't let the build fail
155+
// TODO: ideally we'd like other plugins to designate a commit to put the status update to
156+
LOGGER.debug("Failed to update commit status", e);
157+
listener.getLogger()
158+
.format("Commit doesn't exist in %s. Status is not set%n", repository.getFullName());
159+
}
147160
}
148161
}
149162
}

src/main/java/org/jenkinsci/plugins/github/util/BuildDataHelper.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import hudson.model.Run;
44
import hudson.plugins.git.Revision;
5+
import hudson.plugins.git.util.Build;
56
import hudson.plugins.git.util.BuildData;
67
import org.eclipse.jgit.lib.ObjectId;
78

@@ -32,11 +33,20 @@ public static ObjectId getCommitSHA1(@Nonnull Run<?, ?> build) throws IOExceptio
3233
if (buildData == null) {
3334
throw new IOException(Messages.BuildDataHelper_NoBuildDataError());
3435
}
35-
final Revision lastBuildRevision = buildData.getLastBuiltRevision();
36-
final ObjectId sha1 = lastBuildRevision != null ? lastBuildRevision.getSha1() : null;
37-
if (sha1 == null) { // Nowhere to report => fail the build
38-
throw new IOException(Messages.BuildDataHelper_NoLastRevisionError());
36+
37+
// buildData?.lastBuild?.marked and fall back to .revision with null check everywhere to be defensive
38+
Build b = buildData.lastBuild;
39+
if (b != null) {
40+
Revision r = b.marked;
41+
if (r == null) {
42+
r = b.revision;
43+
}
44+
if (r != null) {
45+
return r.getSha1();
46+
}
3947
}
40-
return sha1;
48+
49+
// Nowhere to report => fail the build
50+
throw new IOException(Messages.BuildDataHelper_NoLastRevisionError());
4151
}
4252
}

src/test/java/com/cloudbees/jenkins/GitHubCommitNotifierTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
3535
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
3636
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
37+
import static org.jenkinsci.plugins.github.util.Messages.BuildDataHelper_NoBuildDataError;
38+
import static org.jenkinsci.plugins.github.util.Messages.BuildDataHelper_NoLastRevisionError;
3739
import static org.mockito.Mockito.when;
3840

3941
/**
@@ -73,6 +75,7 @@ public class GitHubCommitNotifierTest {
7375
@Override
7476
protected void before() throws Throwable {
7577
when(data.getLastBuiltRevision()).thenReturn(rev);
78+
data.lastBuild = new hudson.plugins.git.util.Build(rev, rev, 0, Result.SUCCESS);
7679
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA));
7780
}
7881
};
@@ -84,7 +87,7 @@ public void testNoBuildData() throws Exception {
8487
prj.getPublishersList().add(new GitHubCommitNotifier());
8588
Build b = prj.scheduleBuild2(0).get();
8689
jRule.assertBuildStatus(Result.FAILURE, b);
87-
jRule.assertLogContains(org.jenkinsci.plugins.github.util.Messages.BuildDataHelper_NoBuildDataError(), b);
90+
jRule.assertLogContains(BuildDataHelper_NoBuildDataError(), b);
8891
}
8992

9093
@Test
@@ -95,7 +98,7 @@ public void testNoBuildRevision() throws Exception {
9598
prj.getPublishersList().add(new GitHubCommitNotifier());
9699
Build b = prj.scheduleBuild2(0).get();
97100
jRule.assertBuildStatus(Result.FAILURE, b);
98-
jRule.assertLogContains(org.jenkinsci.plugins.github.util.Messages.BuildDataHelper_NoLastRevisionError(), b);
101+
jRule.assertLogContains(BuildDataHelper_NoLastRevisionError(), b);
99102
}
100103

101104
@Test

src/test/java/com/cloudbees/jenkins/GitHubSetCommitStatusBuilderTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ public class GitHubSetCommitStatusBuilderTest {
5757

5858
@Inject
5959
public GitHubPluginConfig config;
60-
60+
6161
public JenkinsRule jRule = new JenkinsRule();
6262

6363
@Rule
64-
public RuleChain chain = RuleChain.outerRule(jRule).around(new InjectJenkinsMembersRule(jRule, this));
64+
public RuleChain chain = RuleChain.outerRule(jRule).around(new InjectJenkinsMembersRule(jRule, this));
6565

6666
@Rule
6767
public GHMockRule github = new GHMockRule(
@@ -77,6 +77,7 @@ public class GitHubSetCommitStatusBuilderTest {
7777
@Override
7878
protected void before() throws Throwable {
7979
when(data.getLastBuiltRevision()).thenReturn(rev);
80+
data.lastBuild = new hudson.plugins.git.util.Build(rev, rev, 0, Result.SUCCESS);
8081
when(rev.getSha1()).thenReturn(ObjectId.fromString(SOME_SHA));
8182
}
8283
};

0 commit comments

Comments
 (0)
0