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

Commit 969bc36

Browse files
committed
Change pull request status to NEEDS_WORK instead of resetting approval.
1 parent 0fc7f94 commit 969bc36

File tree

5 files changed

+45
-18
lines changed

5 files changed

+45
-18
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private void postInfoAndPRsActions(
142142
if (shouldApprovePullRequest(config.getApprovalSeverityThreshold(), issueReport)) {
143143
stashRequestFacade.approvePullRequest(pr, stashClient);
144144
} else {
145-
stashRequestFacade.resetPullRequestApproval(pr, stashClient);
145+
stashRequestFacade.markPullRequestNeedsWork(pr, stashClient);
146146
}
147147
}
148148
}

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

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

96+
9697
/**
97-
* Reset pull-request approval
98+
* Mark that pull-request NEEDS WORK
9899
*/
99-
public void resetPullRequestApproval(PullRequestRef pr, StashClient stashClient) {
100+
public void markPullRequestNeedsWork(PullRequestRef pr, StashClient stashClient) {
100101
try {
101-
stashClient.resetPullRequestApproval(pr);
102+
stashClient.markPullRequestNeedsWork(pr);
102103

103104
// squid:S2629 : no evaluation required if the logging level is not activated
104105
if (LOGGER.isInfoEnabled()) {
105-
LOGGER.info("Pull-request {} ({}/{}) NOT APPROVED by user \"{}\"",
106-
pr.pullRequestId(), pr.project(), pr.repository(), stashClient.getLogin());
106+
LOGGER.info("Pull-request {} ({}/{}) marked NEEDS WORK by user \"{}\"",
107+
pr.pullRequestId(), pr.project(), pr.repository(), stashClient.getLogin());
107108
}
108109

109110
} catch (StashClientException e) {
110-
LOGGER.error("Unable to reset pull-request approval", e);
111+
LOGGER.error("Unable to set NEEDS WORK status on pull-request", e);
111112
}
112113
}
113114

115+
114116
/**
115117
* Add a reviewer to the current pull-request.
116118
*/

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ 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+
68+
private static final String API_ONE_PR_NEEDS_WORK = API_ONE_PR + "/participants/{4}";
69+
6770
private static final String API_ONE_PR_COMMENT_PATH = API_ONE_PR + "/comments?path={4}&start={5,number,#}";
6871

6972
private static final String API_ONE_PR_ONE_COMMENT = API_ONE_PR_ALL_COMMENTS + "/{4}?version={5}";
@@ -97,6 +100,11 @@ public String getLogin() {
97100
return credentials.getLogin();
98101
}
99102

103+
104+
public String getUserSlug() {
105+
return credentials.getUserSlug();
106+
}
107+
100108
public void postCommentOnPullRequest(PullRequestRef pr, String report)
101109
throws StashClientException {
102110

@@ -267,6 +275,23 @@ public void approvePullRequest(PullRequestRef pr) throws StashClientException {
267275
MessageFormat.format(PULL_REQUEST_APPROVAL_POST_ERROR_MESSAGE, pr.repository(), pr.pullRequestId()));
268276
}
269277

278+
279+
public void markPullRequestNeedsWork(PullRequestRef pr ) throws StashClientException {
280+
String request = MessageFormat.format(API_ONE_PR_NEEDS_WORK,
281+
baseUrl,
282+
pr.project(),
283+
pr.repository(),
284+
pr.pullRequestId(),
285+
credentials.getUserSlug());
286+
JsonObject json = new JsonObject();
287+
json.put("status","NEEDS_WORK");
288+
289+
put(request,
290+
json,
291+
MessageFormat.format(PULL_REQUEST_APPROVAL_POST_ERROR_MESSAGE, pr.repository(), pr.pullRequestId()));
292+
}
293+
294+
270295
public void resetPullRequestApproval(PullRequestRef pr) throws StashClientException {
271296
String request = MessageFormat.format(API_ONE_PR_APPROVAL,
272297
baseUrl,

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void testExecuteOn() throws Exception {
121121
verify(stashRequestFacade, times(0))
122122
.approvePullRequest(eq(pr), (StashClient) Mockito.anyObject());
123123
verify(stashRequestFacade, times(0))
124-
.resetPullRequestApproval(eq(pr), (StashClient) Mockito.anyObject());
124+
.markPullRequestNeedsWork(eq(pr), (StashClient) Mockito.anyObject());
125125
}
126126

127127
@Test
@@ -162,7 +162,7 @@ public void testExecuteOnWithNoPluginActivation() throws Exception {
162162
verify(stashRequestFacade, times(0))
163163
.approvePullRequest(eq(pr), (StashClient) Mockito.anyObject());
164164
verify(stashRequestFacade, times(0))
165-
.resetPullRequestApproval(eq(pr), (StashClient) Mockito.anyObject());
165+
.markPullRequestNeedsWork(eq(pr), (StashClient) Mockito.anyObject());
166166
}
167167

168168
@Test
@@ -183,11 +183,11 @@ public void testExecuteOnWithNoStashUserDefined() throws Exception {
183183
verify(stashRequestFacade, times(0))
184184
.approvePullRequest(eq(pr), (StashClient) Mockito.anyObject());
185185
verify(stashRequestFacade, times(0))
186-
.resetPullRequestApproval(eq(pr), (StashClient) Mockito.anyObject());
186+
.markPullRequestNeedsWork(eq(pr), (StashClient) Mockito.anyObject());
187187
}
188188

189189
@Test
190-
public void testExecuteOnWithResetCommentActivated() throws Exception {
190+
public void testExecuteOnWithResetmvnCommentActivated() throws Exception {
191191
when(config.resetComments()).thenReturn(true);
192192

193193
myJob = new StashIssueReportingPostJob(config, stashRequestFacade, server);
@@ -221,7 +221,7 @@ public void testExecuteOnWithNoDiffReport() throws Exception {
221221
verify(stashRequestFacade, times(0))
222222
.approvePullRequest(eq(pr), (StashClient) Mockito.anyObject());
223223
verify(stashRequestFacade, times(0))
224-
.resetPullRequestApproval(eq(pr), (StashClient) Mockito.anyObject());
224+
.markPullRequestNeedsWork(eq(pr), (StashClient) Mockito.anyObject());
225225
}
226226

227227
@Test

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -783,20 +783,20 @@ public void testApprovePullRequestException() throws Exception {
783783

784784

785785
@Test
786-
public void addResetPullRequestApproval() throws Exception {
786+
public void addMarkNeedsWorkPullRequestApproval() throws Exception {
787787

788-
myFacade.resetPullRequestApproval(pr, stashClient);
789-
verify(stashClient, times(1)).resetPullRequestApproval(pr);
788+
myFacade.markPullRequestNeedsWork(pr, stashClient);
789+
verify(stashClient, times(1)).markPullRequestNeedsWork(pr);
790790
}
791791

792792

793793
@Test
794-
public void addResetPullRequestApprovalException() throws Exception {
794+
public void markPullRequestNeedsWorkException() throws Exception {
795795

796796
StashClient StaCli = mock(StashClient.class);
797-
doThrow(StashClientException.class).when(StaCli).resetPullRequestApproval(any());
797+
doThrow(StashClientException.class).when(StaCli).markPullRequestNeedsWork(any());
798798

799-
myFacade.resetPullRequestApproval(pr, StaCli);
799+
myFacade.markPullRequestNeedsWork(pr, StaCli);
800800
}
801801

802802

0 commit comments

Comments
 (0)