Skip to content

Commit

Permalink
Respect GitLab's delete source branch checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew McMillan committed Jan 13, 2022
1 parent 2dddc2c commit 943dbb5
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface GitLabClient {

void getCommit(String projectId, String sha);

void acceptMergeRequest(MergeRequest mr, String mergeCommitMessage, boolean shouldRemoveSourceBranch);
void acceptMergeRequest(MergeRequest mr, String mergeCommitMessage, Boolean shouldRemoveSourceBranch);

void createMergeRequestNote(MergeRequest mr, String body);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Void execute(GitLabClient client) {
}

@Override
public void acceptMergeRequest(final MergeRequest mr, final String mergeCommitMessage, final boolean shouldRemoveSourceBranch) {
public void acceptMergeRequest(final MergeRequest mr, final String mergeCommitMessage, final Boolean shouldRemoveSourceBranch) {
execute(
new GitLabOperation<Void>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface GitLabApiProxy {

void getCommit(String projectId, String sha);

void acceptMergeRequest(Integer projectId, Integer mergeRequestId, String mergeCommitMessage, boolean shouldRemoveSourceBranch);
void acceptMergeRequest(Integer projectId, Integer mergeRequestId, String mergeCommitMessage, Boolean shouldRemoveSourceBranch);

void createMergeRequestNote(Integer projectId, Integer mergeRequestId, String body);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void getCommit(String projectId, String sha) {
}

@Override
public void acceptMergeRequest(MergeRequest mr, String mergeCommitMessage, boolean shouldRemoveSourceBranch) {
public void acceptMergeRequest(MergeRequest mr, String mergeCommitMessage, Boolean shouldRemoveSourceBranch) {
api.acceptMergeRequest(mr.getProjectId(), mergeRequestIdProvider.apply(mr), mergeCommitMessage, shouldRemoveSourceBranch);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void changeBuildStatus(@PathParam("projectId") @Encoded Integer projectId,
void acceptMergeRequest(@PathParam("projectId") @Encoded Integer projectId,
@PathParam("mergeRequestId") @Encoded Integer mergeRequestId,
@FormParam("merge_commit_message") String mergeCommitMessage,
@FormParam("should_remove_source_branch") boolean shouldRemoveSourceBranch);
@FormParam("should_remove_source_branch") Boolean shouldRemoveSourceBranch);

@POST
@Produces(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void changeBuildStatus(@PathParam("projectId") @Encoded Integer projectId,
void acceptMergeRequest(@PathParam("projectId") @Encoded Integer projectId,
@PathParam("mergeRequestIid") @Encoded Integer mergeRequestIid,
@FormParam("merge_commit_message") String mergeCommitMessage,
@FormParam("should_remove_source_branch") boolean shouldRemoveSourceBranch);
@FormParam("should_remove_source_branch") Boolean shouldRemoveSourceBranch);

@POST
@Produces(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class GitLabAcceptMergeRequestPublisher extends MergeRequestNotifier {
private static final Logger LOGGER = Logger.getLogger(GitLabAcceptMergeRequestPublisher.class.getName());

private boolean deleteSourceBranch = false;
private Boolean deleteSourceBranch;

@DataBoundConstructor
public GitLabAcceptMergeRequestPublisher() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public void success() throws IOException, InterruptedException {
publish(mockSimpleBuild(GITLAB_CONNECTION_V4, Result.SUCCESS));

mockServerClient.verify(
prepareAcceptMergeRequestWithSuccessResponse("v3", MERGE_REQUEST_ID),
prepareAcceptMergeRequestWithSuccessResponse("v4", MERGE_REQUEST_IID));
prepareAcceptMergeRequestWithSuccessResponse("v3", MERGE_REQUEST_ID, null),
prepareAcceptMergeRequestWithSuccessResponse("v4", MERGE_REQUEST_IID, null));
}

@Test
Expand All @@ -86,17 +86,22 @@ private void publish(AbstractBuild build) throws InterruptedException, IOExcepti
publisher.perform(build, null, listener);
}

private HttpRequest prepareAcceptMergeRequestWithSuccessResponse(String apiLevel, int mergeRequestId) throws UnsupportedEncodingException {
HttpRequest updateCommitStatus = prepareAcceptMergeRequest(apiLevel, mergeRequestId);
private HttpRequest prepareAcceptMergeRequestWithSuccessResponse(String apiLevel, int mergeRequestId, Boolean shouldRemoveSourceBranch) throws UnsupportedEncodingException {
HttpRequest updateCommitStatus = prepareAcceptMergeRequest(apiLevel, mergeRequestId, shouldRemoveSourceBranch);
mockServerClient.when(updateCommitStatus).respond(response().withStatusCode(200));
return updateCommitStatus;
}

private HttpRequest prepareAcceptMergeRequest(String apiLevel, int mergeRequestId) throws UnsupportedEncodingException {
private HttpRequest prepareAcceptMergeRequest(String apiLevel, int mergeRequestId, Boolean removeSourceBranch) throws UnsupportedEncodingException {
String body = "merge_commit_message=Merge+Request+accepted+by+jenkins+build+success";
if (removeSourceBranch != null)
{
body += "&should_remove_source_branch=" + removeSourceBranch;
}
return request()
.withPath("/gitlab/api/" + apiLevel + "/projects/" + PROJECT_ID + "/merge_requests/" + mergeRequestId + "/merge")
.withMethod("PUT")
.withHeader("PRIVATE-TOKEN", "secret")
.withBody("merge_commit_message=Merge+Request+accepted+by+jenkins+build+success&should_remove_source_branch=false");
.withBody(body);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void getCommit(String projectId, String sha) {
}

@Override
public void acceptMergeRequest(MergeRequest mr, String mergeCommitMessage, boolean shouldRemoveSourceBranch) {
public void acceptMergeRequest(MergeRequest mr, String mergeCommitMessage, Boolean shouldRemoveSourceBranch) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void getCommit(String projectId, String sha) {
}

@Override
public void acceptMergeRequest(MergeRequest mr, String mergeCommitMessage, boolean shouldRemoveSourceBranch) {
public void acceptMergeRequest(MergeRequest mr, String mergeCommitMessage, Boolean shouldRemoveSourceBranch) {

}

Expand Down

0 comments on commit 943dbb5

Please sign in to comment.