Skip to content
This repository has been archived by the owner on Jan 7, 2021. It is now read-only.

Commit

Permalink
Approve PR only when all issues are below threshold severity
Browse files Browse the repository at this point in the history
  • Loading branch information
Kosta Zaikin committed Jul 24, 2017
1 parent a8be7d7 commit 66357a0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ private void postInfoAndPRsActions(PullRequestRef pr, List<Issue> issueReport, i
StashClient stashClient) {

// Some local definitions
boolean canApprovePullrequest = config.canApprovePullRequest();

int issueTotal = issueReport.size();

// if threshold exceeded, do not push issue list to Stash
Expand All @@ -136,8 +134,10 @@ private void postInfoAndPRsActions(PullRequestRef pr, List<Issue> issueReport, i

// if no new issues and coverage is improved,
// plugin approves the pull-request
if (canApprovePullrequest) {
if (issueTotal == 0) {
if (config.canApprovePullRequest()) {
List<String> reportedSeverities = stashRequestFacade.getReportedSeverities();
boolean approve = issueReport.stream().noneMatch(issue -> reportedSeverities.contains(issue.severity()));
if (approve) {
stashRequestFacade.approvePullRequest(pr, stashClient);
} else {
stashRequestFacade.resetPullRequestApproval(pr, stashClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@
import org.sonar.plugins.stash.issue.StashUser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sonar.api.rule.Severity.BLOCKER;
import static org.sonar.api.rule.Severity.CRITICAL;
import static org.sonar.api.rule.Severity.INFO;
import static org.sonar.api.rule.Severity.MAJOR;
import static org.sonar.api.rule.Severity.MINOR;


@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -412,4 +418,54 @@ public void testExecuteOnWithoutAnalysisComment() throws Exception {
verify(stashRequestFacade, times(0)).postAnalysisOverview(eq(pr), eq(STASH_ISSUE_THRESHOLD), eq(report), (StashClient) Mockito.anyObject());
}
*/

@Test
public void whenNoIssues_approve() throws Exception {
when(config.canApprovePullRequest()).thenReturn(true);

List<Issue> report = Collections.emptyList();
when(stashRequestFacade.extractIssueReport(projectIssues)).thenReturn(report);

myJob = new StashIssueReportingPostJob(config, projectIssues, stashRequestFacade);
myJob.executeOn(project, context);

verify(stashRequestFacade, times(1)).approvePullRequest(eq(pr), Mockito.anyObject());
}

@Test
public void whenAllIssuesBelowThreshold_approve() throws Exception {
when(config.canApprovePullRequest()).thenReturn(true);

List<Issue> report = report(INFO, MINOR, MAJOR, CRITICAL);
when(stashRequestFacade.extractIssueReport(projectIssues)).thenReturn(report);

myJob = new StashIssueReportingPostJob(config, projectIssues, stashRequestFacade);
myJob.executeOn(project, context);

verify(stashRequestFacade, times(1)).approvePullRequest(eq(pr), Mockito.anyObject());
}

@Test
public void whenGotIssuesAboveThreshold_resetApproval() throws Exception {
when(config.canApprovePullRequest()).thenReturn(true);

List<Issue> report = report(INFO, MINOR, MAJOR, CRITICAL, BLOCKER);
when(stashRequestFacade.extractIssueReport(projectIssues)).thenReturn(report);
when(stashRequestFacade.getReportedSeverities()).thenReturn(Collections.singletonList(BLOCKER));

myJob = new StashIssueReportingPostJob(config, projectIssues, stashRequestFacade);
myJob.executeOn(project, context);

verify(stashRequestFacade, times(1)).resetPullRequestApproval(eq(pr), Mockito.anyObject());
}

private List<Issue> report(String... severities) {
List<Issue> result = new ArrayList<>();
for (String s : severities) {
Issue issue = mock(Issue.class);
when(issue.severity()).thenReturn(s);
result.add(issue);
}
return result;
}
}

0 comments on commit 66357a0

Please sign in to comment.