Skip to content
This repository was archived by the owner on Apr 2, 2019. It is now read-only.

Commit d953e97

Browse files
author
Janos Gyerik
authored
SONARGITUB-26 set status even when unexpected errors (#27)
1 parent c5ae81b commit d953e97

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/main/java/org/sonar/plugins/github/PullRequestIssuePostJob.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,22 @@
2323
import java.util.HashMap;
2424
import java.util.Map;
2525
import java.util.stream.StreamSupport;
26+
import org.kohsuke.github.GHCommitState;
2627
import org.sonar.api.batch.fs.InputComponent;
2728
import org.sonar.api.batch.fs.InputFile;
2829
import org.sonar.api.batch.postjob.PostJob;
2930
import org.sonar.api.batch.postjob.PostJobContext;
3031
import org.sonar.api.batch.postjob.PostJobDescriptor;
3132
import org.sonar.api.batch.postjob.issue.PostJobIssue;
33+
import org.sonar.api.utils.log.Logger;
34+
import org.sonar.api.utils.log.Loggers;
3235

3336
/**
3437
* Compute comments to be added on the pull request.
3538
*/
3639
public class PullRequestIssuePostJob implements PostJob {
40+
private static final Logger LOG = Loggers.get(PullRequestFacade.class);
41+
3742
private static final Comparator<PostJobIssue> ISSUE_COMPARATOR = new IssueComparator();
3843

3944
private final PullRequestFacade pullRequestFacade;
@@ -56,15 +61,21 @@ public void describe(PostJobDescriptor descriptor) {
5661
@Override
5762
public void execute(PostJobContext context) {
5863
GlobalReport report = new GlobalReport(markDownUtils, gitHubPluginConfiguration.tryReportIssuesInline());
59-
Map<InputFile, Map<Integer, StringBuilder>> commentsToBeAddedByLine = processIssues(report, context.issues());
64+
try {
65+
Map<InputFile, Map<Integer, StringBuilder>> commentsToBeAddedByLine = processIssues(report, context.issues());
6066

61-
updateReviewComments(commentsToBeAddedByLine);
67+
updateReviewComments(commentsToBeAddedByLine);
6268

63-
pullRequestFacade.deleteOutdatedComments();
69+
pullRequestFacade.deleteOutdatedComments();
6470

65-
pullRequestFacade.createOrUpdateGlobalComments(report.hasNewIssue() ? report.formatForMarkdown() : null);
71+
pullRequestFacade.createOrUpdateGlobalComments(report.hasNewIssue() ? report.formatForMarkdown() : null);
6672

67-
pullRequestFacade.createOrUpdateSonarQubeStatus(report.getStatus(), report.getStatusDescription());
73+
pullRequestFacade.createOrUpdateSonarQubeStatus(report.getStatus(), report.getStatusDescription());
74+
} catch (Exception e) {
75+
String msg = "SonarQube failed to complete the review of this pull request";
76+
LOG.error(msg, e);
77+
pullRequestFacade.createOrUpdateSonarQubeStatus(GHCommitState.ERROR, msg + ": " + e.getMessage());
78+
}
6879
}
6980

7081
private Map<InputFile, Map<Integer, StringBuilder>> processIssues(GlobalReport report, Iterable<PostJobIssue> issues) {

src/test/java/org/sonar/plugins/github/PullRequestIssuePostJobTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,15 @@ public void testPullRequestAnalysisWithNewBlockerAndCriticalIssues() {
225225

226226
verify(pullRequestFacade).createOrUpdateSonarQubeStatus(GHCommitState.ERROR, "SonarQube reported 2 issues, with 1 critical and 1 blocker");
227227
}
228+
229+
@Test
230+
public void should_update_sonarqube_status_even_if_unexpected_errors_were_raised() {
231+
String innerMsg = "Failed to get issues";
232+
// not really realistic unexpected error, but good enough for this test
233+
when(context.issues()).thenThrow(new IllegalStateException(innerMsg));
234+
pullRequestIssuePostJob.execute(context);
235+
236+
String msg = "SonarQube failed to complete the review of this pull request: " + innerMsg;
237+
verify(pullRequestFacade).createOrUpdateSonarQubeStatus(GHCommitState.ERROR, msg);
238+
}
228239
}

0 commit comments

Comments
 (0)