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

Commit

Permalink
SONARGITUB-26 set status even when unexpected errors (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Gyerik authored Jan 18, 2017
1 parent c5ae81b commit d953e97
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@
import java.util.HashMap;
import java.util.Map;
import java.util.stream.StreamSupport;
import org.kohsuke.github.GHCommitState;
import org.sonar.api.batch.fs.InputComponent;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.postjob.PostJob;
import org.sonar.api.batch.postjob.PostJobContext;
import org.sonar.api.batch.postjob.PostJobDescriptor;
import org.sonar.api.batch.postjob.issue.PostJobIssue;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

/**
* Compute comments to be added on the pull request.
*/
public class PullRequestIssuePostJob implements PostJob {
private static final Logger LOG = Loggers.get(PullRequestFacade.class);

private static final Comparator<PostJobIssue> ISSUE_COMPARATOR = new IssueComparator();

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

updateReviewComments(commentsToBeAddedByLine);
updateReviewComments(commentsToBeAddedByLine);

pullRequestFacade.deleteOutdatedComments();
pullRequestFacade.deleteOutdatedComments();

pullRequestFacade.createOrUpdateGlobalComments(report.hasNewIssue() ? report.formatForMarkdown() : null);
pullRequestFacade.createOrUpdateGlobalComments(report.hasNewIssue() ? report.formatForMarkdown() : null);

pullRequestFacade.createOrUpdateSonarQubeStatus(report.getStatus(), report.getStatusDescription());
pullRequestFacade.createOrUpdateSonarQubeStatus(report.getStatus(), report.getStatusDescription());
} catch (Exception e) {
String msg = "SonarQube failed to complete the review of this pull request";
LOG.error(msg, e);
pullRequestFacade.createOrUpdateSonarQubeStatus(GHCommitState.ERROR, msg + ": " + e.getMessage());
}
}

private Map<InputFile, Map<Integer, StringBuilder>> processIssues(GlobalReport report, Iterable<PostJobIssue> issues) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,15 @@ public void testPullRequestAnalysisWithNewBlockerAndCriticalIssues() {

verify(pullRequestFacade).createOrUpdateSonarQubeStatus(GHCommitState.ERROR, "SonarQube reported 2 issues, with 1 critical and 1 blocker");
}

@Test
public void should_update_sonarqube_status_even_if_unexpected_errors_were_raised() {
String innerMsg = "Failed to get issues";
// not really realistic unexpected error, but good enough for this test
when(context.issues()).thenThrow(new IllegalStateException(innerMsg));
pullRequestIssuePostJob.execute(context);

String msg = "SonarQube failed to complete the review of this pull request: " + innerMsg;
verify(pullRequestFacade).createOrUpdateSonarQubeStatus(GHCommitState.ERROR, msg);
}
}

0 comments on commit d953e97

Please sign in to comment.