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

Commit cbbe8f4

Browse files
committed
Change pull request status to NEEDS_WORK instead of resetting approval.
1 parent a8ca0e8 commit cbbe8f4

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed

src/main/java/org/sonar/plugins/stash/StashIssueReportingPostJob.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ private void postInfoAndPRsActions(
145145
stashRequestFacade.resetPullRequestApproval(pr, stashClient);
146146
}
147147
}
148+
149+
if (config.canMarkPullRequestNeedsWork()) {
150+
if (shouldApprovePullRequest(config.getApprovalSeverityThreshold(), issueReport)) {
151+
stashRequestFacade.resetPullRequestApproval(pr, stashClient);
152+
} else {
153+
stashRequestFacade.markPullRequestNeedsWork(pr, stashClient);
154+
}
155+
}
148156
}
149157

150158
static boolean shouldApprovePullRequest(Optional<Severity> approvalSeverityThreshold, List<PostJobIssue> report) {

src/main/java/org/sonar/plugins/stash/StashPlugin.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public enum IssueType {
6969
public static final String STASH_PASSWORD = "sonar.stash.password";
7070
public static final String STASH_PASSWORD_ENVIRONMENT_VARIABLE = "sonar.stash.password.variable";
7171
public static final String STASH_REVIEWER_APPROVAL = "sonar.stash.reviewer.approval";
72+
public static final String STASH_REVIEWER_MARK_NEEDS_WORK = "sonar.stash.reviewer.mark.needs.work";
7273
public static final String STASH_REVIEWER_APPROVAL_SEVERITY_THRESHOLD = "sonar.stash.reviewer.approval.severity.threshold";
7374
public static final String STASH_ISSUE_THRESHOLD = "sonar.stash.issue.threshold";
7475
public static final String STASH_ISSUE_SEVERITY_THRESHOLD = "sonar.stash.issue.severity.threshold";
@@ -191,7 +192,14 @@ public void define(Context context) {
191192
.type(PropertyType.STRING)
192193
.subCategory(CONFIG_PAGE_SUB_CATEGORY_STASH)
193194
.onQualifiers(Qualifiers.PROJECT)
194-
.defaultValue(DEFAULT_STASH_EXCLUDE_RULES).build()
195+
.defaultValue(DEFAULT_STASH_EXCLUDE_RULES).build(),
196+
PropertyDefinition.builder(STASH_REVIEWER_MARK_NEEDS_WORK)
197+
.name("Stash reviewer marking NEEDS WORK")
198+
.description("Does SonarQube mark the pull-request NEEDS WORK if there are new issues?")
199+
.subCategory(CONFIG_PAGE_SUB_CATEGORY_STASH)
200+
.onQualifiers(Qualifiers.PROJECT)
201+
.type(PropertyType.BOOLEAN)
202+
.defaultValue("false").build()
195203
);
196204
}
197205
}

src/main/java/org/sonar/plugins/stash/StashPluginConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ public int getStashTimeout() {
8989
public boolean canApprovePullRequest() {
9090
return settings.getBoolean(StashPlugin.STASH_REVIEWER_APPROVAL);
9191
}
92+
public boolean canMarkPullRequestNeedsWork() {
93+
return settings.getBoolean(StashPlugin.STASH_REVIEWER_MARK_NEEDS_WORK);
94+
}
9295

9396
public boolean resetComments() {
9497
return settings.getBoolean(StashPlugin.STASH_RESET_COMMENTS);

src/main/java/org/sonar/plugins/stash/StashRequestFacade.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ public void approvePullRequest(PullRequestRef pr, StashClient stashClient) {
9393
}
9494
}
9595

96+
/**
97+
* Mark pull-request needs work
98+
*/
99+
public void markPullRequestNeedsWork(PullRequestRef pr, StashClient stashClient) {
100+
try {
101+
stashClient.markPullRequestNeedsWork(pr);
102+
103+
// squid:S2629 : no evaluation required if the logging level is not activated
104+
if (LOGGER.isInfoEnabled()) {
105+
LOGGER.info("Pull-request {} ({}/{}) marked NEEDS WORK by user \"{}\"",
106+
pr.pullRequestId(), pr.project(), pr.repository(), stashClient.getLogin());
107+
}
108+
109+
} catch (StashClientException e) {
110+
LOGGER.error("Unable to mark pull-request NEEDS WORK", e);
111+
}
112+
}
113+
96114
/**
97115
* Reset pull-request approval
98116
*/

src/main/java/org/sonar/plugins/stash/client/StashClient.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ public class StashClient implements AutoCloseable {
6464
private static final String API_ONE_PR_ALL_COMMENTS = API_ONE_PR + "/comments";
6565
private static final String API_ONE_PR_DIFF = API_ONE_PR + "/diff?withComments=true";
6666
private static final String API_ONE_PR_APPROVAL = API_ONE_PR + "/approve";
67+
private static final String API_ONE_PR_NEEDS_WORK = API_ONE_PR + "/participants/{4}";
6768
private static final String API_ONE_PR_COMMENT_PATH = API_ONE_PR + "/comments?path={4}&start={5,number,#}";
6869

6970
private static final String API_ONE_PR_ONE_COMMENT = API_ONE_PR_ALL_COMMENTS + "/{4}?version={5}";
7071

72+
private static final String PULL_REQUEST_MARK_NEEDS_WORK_PUT_ERROR_MESSAGE = "Unable to set NEEDS WORK status of pull-request {0}";
7173
private static final String PULL_REQUEST_APPROVAL_POST_ERROR_MESSAGE = "Unable to change status of pull-request {0}"
7274
+ " #{1,number,#}.";
7375
private static final String PULL_REQUEST_GET_ERROR_MESSAGE = "Unable to retrieve pull-request {0} #{1,number,#}.";
@@ -278,6 +280,21 @@ public void resetPullRequestApproval(PullRequestRef pr) throws StashClientExcept
278280
MessageFormat.format(PULL_REQUEST_APPROVAL_POST_ERROR_MESSAGE, pr.repository(), pr.pullRequestId()));
279281
}
280282

283+
public void markPullRequestNeedsWork(PullRequestRef pr ) throws StashClientException {
284+
String request = MessageFormat.format(API_ONE_PR_NEEDS_WORK,
285+
baseUrl,
286+
pr.project(),
287+
pr.repository(),
288+
pr.pullRequestId(),
289+
credentials.getUserSlug());
290+
JsonObject json = new JsonObject();
291+
json.put("status", "NEEDS_WORK");
292+
put(request,
293+
json,
294+
MessageFormat
295+
.format(PULL_REQUEST_MARK_NEEDS_WORK_PUT_ERROR_MESSAGE, pr.repository(), pr.pullRequestId()));
296+
}
297+
281298
public void postTaskOnComment(String message, Long commentId) throws StashClientException {
282299
String request = baseUrl + TASKS_API;
283300

src/test/java/org/sonar/plugins/stash/StashRequestFacadeTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,12 @@ public void testApprovePullRequest() throws Exception {
771771
verify(stashClient, times(1)).approvePullRequest(pr);
772772
}
773773

774+
@Test
775+
public void testmarkPullRequestNeedsWork() throws Exception {
776+
777+
myFacade.markPullRequestNeedsWork(pr, stashClient);
778+
verify(stashClient, times(1)).markPullRequestNeedsWork(pr);
779+
}
774780

775781
@Test
776782
public void testApprovePullRequestException() throws Exception {

0 commit comments

Comments
 (0)