Skip to content

Commit

Permalink
feat: branch clone and merge api (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
katerina20 authored Apr 22, 2024
1 parent a24d715 commit bc53819
Show file tree
Hide file tree
Showing 18 changed files with 366 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/crowdin/client/Client.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.crowdin.client;

import com.crowdin.client.applications.ApplicationsApi;
import com.crowdin.client.branches.BranchesApi;
import com.crowdin.client.bundles.BundlesApi;
import com.crowdin.client.clients.ClientsApi;
import com.crowdin.client.core.CrowdinApi;
Expand Down Expand Up @@ -68,6 +69,7 @@ public class Client extends CrowdinApi {
private final NotificationsApi notificationsApi;
private final ApplicationsApi applicationsApi;
private final ClientsApi clientsApi;
private final BranchesApi branchesApi;

public Client(Credentials credentials) {
super(credentials);
Expand Down Expand Up @@ -101,6 +103,7 @@ public Client(Credentials credentials) {
this.notificationsApi = new NotificationsApi(credentials);
this.applicationsApi = new ApplicationsApi(credentials);
this.clientsApi = new ClientsApi(credentials);
this.branchesApi = new BranchesApi(credentials);
}

public Client(Credentials credentials, ClientConfig clientConfig) {
Expand Down Expand Up @@ -135,6 +138,7 @@ public Client(Credentials credentials, ClientConfig clientConfig) {
this.notificationsApi = new NotificationsApi(credentials, clientConfig);
this.applicationsApi = new ApplicationsApi(credentials, clientConfig);
this.clientsApi = new ClientsApi(credentials, clientConfig);
this.branchesApi = new BranchesApi(credentials, clientConfig);
}

}
105 changes: 105 additions & 0 deletions src/main/java/com/crowdin/client/branches/BranchesApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.crowdin.client.branches;

import com.crowdin.client.branches.model.*;
import com.crowdin.client.core.CrowdinApi;
import com.crowdin.client.core.http.HttpRequestConfig;
import com.crowdin.client.core.http.exceptions.HttpBadRequestException;
import com.crowdin.client.core.http.exceptions.HttpException;
import com.crowdin.client.core.model.ClientConfig;
import com.crowdin.client.core.model.Credentials;
import com.crowdin.client.core.model.ResponseObject;

public class BranchesApi extends CrowdinApi {

public BranchesApi(Credentials credentials) {
super(credentials);
}

public BranchesApi(Credentials credentials, ClientConfig clientConfig) {
super(credentials, clientConfig);
}

/**
* @param projectId project identifier
* @param branchId branch identifier
* @param request request object
* @return clone status
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/string-based/#operation/api.projects.branches.clones.post" target="_blank"><b>API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<BranchCloneStatus> cloneBranch(Long projectId, Long branchId, CloneBranchRequest request) throws HttpException, HttpBadRequestException {
BranchCloneStatusResponseObject branchResponseObject = this.httpClient.post(this.url + "/projects/" + projectId + "/branches/" + branchId + "/clones", request, new HttpRequestConfig(), BranchCloneStatusResponseObject.class);
return ResponseObject.of(branchResponseObject.getData());
}

/**
* @param projectId project identifier
* @param branchId branch identifier
* @param cloneId clone identifier
* @return clone status
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/string-based/#operation/api.projects.branches.clones.get" target="_blank"><b>API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<BranchCloneStatus> checkCloneBranchStatus(Long projectId, Long branchId, String cloneId) throws HttpException, HttpBadRequestException {
BranchCloneStatusResponseObject branchResponseObject = this.httpClient.get(this.url + "/projects/" + projectId + "/branches/" + branchId + "/clones/" + cloneId, new HttpRequestConfig(), BranchCloneStatusResponseObject.class);
return ResponseObject.of(branchResponseObject.getData());
}

/**
* @param projectId project identifier
* @param branchId branch identifier
* @param cloneId clone identifier
* @return cloned branch
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/string-based/#operation/api.projects.branches.clones.branch.get" target="_blank"><b>API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<ClonedBranch> getClonedBranch(Long projectId, Long branchId, String cloneId) throws HttpException, HttpBadRequestException {
ClonedBranchResponseObject branchResponseObject = this.httpClient.get(this.url + "/projects/" + projectId + "/branches/" + branchId + "/clones/" + cloneId + "/branch", new HttpRequestConfig(), ClonedBranchResponseObject.class);
return ResponseObject.of(branchResponseObject.getData());
}

/**
* @param projectId project identifier
* @param branchId branch identifier
* @param request request object
* @return merge status
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/string-based/#operation/api.projects.branches.merges.post" target="_blank"><b>API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<BranchMergeStatus> mergeBranch(Long projectId, Long branchId, MergeBranchRequest request) throws HttpException, HttpBadRequestException {
BranchMergeStatusResponseObject branchResponseObject = this.httpClient.post(this.url + "/projects/" + projectId + "/branches/" + branchId + "/merges", request, new HttpRequestConfig(), BranchMergeStatusResponseObject.class);
return ResponseObject.of(branchResponseObject.getData());
}

/**
* @param projectId project identifier
* @param branchId branch identifier
* @param mergeId merge identifier
* @return merge status
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/string-based/#operation/api.projects.branches.clones.get" target="_blank"><b>API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<BranchMergeStatus> checkMergeBranchStatus(Long projectId, Long branchId, String mergeId) throws HttpException, HttpBadRequestException {
BranchMergeStatusResponseObject branchResponseObject = this.httpClient.get(this.url + "/projects/" + projectId + "/branches/" + branchId + "/merges/" + mergeId, new HttpRequestConfig(), BranchMergeStatusResponseObject.class);
return ResponseObject.of(branchResponseObject.getData());
}

/**
* @param projectId project identifier
* @param branchId branch identifier
* @param mergeId merge identifier
* @return merge summary
* @see <ul>
* <li><a href="https://developer.crowdin.com/api/v2/string-based/#operation/api.projects.branches.merges.summary.get" target="_blank"><b>API Documentation</b></a></li>
* </ul>
*/
public ResponseObject<BranchMergeSummary> getMergeBranchSummary(Long projectId, Long branchId, String mergeId) throws HttpException, HttpBadRequestException {
BranchMergeSummaryResponseObject branchResponseObject = this.httpClient.get(this.url + "/projects/" + projectId + "/branches/" + branchId + "/merges/" + mergeId + "/summary", new HttpRequestConfig(), BranchMergeSummaryResponseObject.class);
return ResponseObject.of(branchResponseObject.getData());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.crowdin.client.branches.model;

import lombok.Data;

import java.util.Date;

@Data
public class BranchCloneStatus {

private String identifier;
private String status;
private Integer progress;
private Object attributes;
private Date createdAt;
private Date updatedAt;
private String startedAt;
private String finishedAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.crowdin.client.branches.model;

import lombok.Data;

@Data
public class BranchCloneStatusResponseObject {

private BranchCloneStatus data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.crowdin.client.branches.model;

import lombok.Data;

import java.util.Date;

@Data
public class BranchMergeStatus {

private String identifier;
private String status;
private Integer progress;
private BranchMergeStatus.Attributes attributes;
private Date createdAt;
private Date updatedAt;
private String startedAt;
private String finishedAt;

@Data
public static class Attributes {

private Integer sourceBranchId;
private Boolean deleteAfterMerge;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.crowdin.client.branches.model;

import lombok.Data;

@Data
public class BranchMergeStatusResponseObject {

private BranchMergeStatus data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.crowdin.client.branches.model;

import lombok.Data;

import java.util.Map;

@Data
public class BranchMergeSummary {

private String status;
private Long sourceBranchId;
private Long targetBranchId;
private Boolean dryRun;
private Map<String, Long> details;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.crowdin.client.branches.model;

import lombok.Data;

@Data
public class BranchMergeSummaryResponseObject {

private BranchMergeSummary data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.crowdin.client.branches.model;

import lombok.Data;

@Data
public class CloneBranchRequest {

private String name;
private String title;
}
16 changes: 16 additions & 0 deletions src/main/java/com/crowdin/client/branches/model/ClonedBranch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.crowdin.client.branches.model;

import lombok.Data;

import java.util.Date;

@Data
public class ClonedBranch {

private Long id;
private Long projectId;
private String name;
private String title;
private Date createdAt;
private Date updatedAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.crowdin.client.branches.model;

import lombok.Data;

@Data
public class ClonedBranchResponseObject {

private ClonedBranch data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.crowdin.client.branches.model;

import lombok.Data;

@Data
public class MergeBranchRequest {

private Boolean deleteAfterMerge;
private Long sourceBranchId;
private Boolean dryRun;
}
76 changes: 76 additions & 0 deletions src/test/java/com/crowdin/client/branches/BranchesApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.crowdin.client.branches;

import com.crowdin.client.branches.model.*;
import com.crowdin.client.core.model.ResponseObject;
import com.crowdin.client.framework.RequestMock;
import com.crowdin.client.framework.TestClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

class BranchesApiTest extends TestClient {

private final Long projectId = 1L;
private final Long id = 14L;
private final Long sourceBranchId = 8L;
private final String cloneId = "aaee1685-c92a-4da6-8a08-c28b4db5cd4a";
private final String mergeId = "50fb3506-4127-4ba8-8296-f97dc7e3e0c3";
private final String name = "cloned";
private final String title = "Cloned Branch";

@Override
public List<RequestMock> getMocks() {
return Arrays.asList(
RequestMock.build(this.url + "/projects/" + projectId + "/branches/" + id + "/clones", HttpPost.METHOD_NAME, "api/branches/cloneBranchRequest.json", "api/branches/branchCloneStatus.json"),
RequestMock.build(this.url + "/projects/" + projectId + "/branches/" + id + "/clones/" + cloneId, HttpGet.METHOD_NAME, "api/branches/branchCloneStatus.json"),
RequestMock.build(this.url + "/projects/" + projectId + "/branches/" + id + "/merges", HttpPost.METHOD_NAME, "api/branches/mergeBranchRequest.json", "api/branches/branchMergeStatus.json"),
RequestMock.build(this.url + "/projects/" + projectId + "/branches/" + id + "/merges/" + mergeId, HttpGet.METHOD_NAME, "api/branches/branchMergeStatus.json"),
RequestMock.build(this.url + "/projects/" + projectId + "/branches/" + id + "/merges/" + mergeId + "/summary", HttpGet.METHOD_NAME, "api/branches/branchMergeSummary.json")
);
}

@Test
public void cloneBranchTest() {
CloneBranchRequest request = new CloneBranchRequest();
request.setName(name);
request.setTitle(title);

ResponseObject<BranchCloneStatus> response = this.getBranchesApi().cloneBranch(projectId, id, request);
assertEquals(response.getData().getIdentifier(), cloneId);
}

@Test
public void checkCloneBranchStatusTest() {
ResponseObject<BranchCloneStatus> response = this.getBranchesApi().checkCloneBranchStatus(projectId, id, cloneId);
assertEquals(response.getData().getIdentifier(), cloneId);
}

@Test
public void mergeBranchTest() {
MergeBranchRequest request = new MergeBranchRequest();
request.setDeleteAfterMerge(true);
request.setSourceBranchId(sourceBranchId);
request.setDryRun(false);

ResponseObject<BranchMergeStatus> response = this.getBranchesApi().mergeBranch(projectId, id, request);
assertEquals(response.getData().getIdentifier(), mergeId);
}

@Test
public void checkMergeBranchStatusTest() {
ResponseObject<BranchMergeStatus> response = this.getBranchesApi().checkMergeBranchStatus(projectId, id, mergeId);
assertEquals(response.getData().getIdentifier(), mergeId);
}

@Test
public void getMergeBranchSummaryTest() {
ResponseObject<BranchMergeSummary> response = this.getBranchesApi().getMergeBranchSummary(projectId, id, mergeId);
assertEquals(response.getData().getTargetBranchId(), id);
assertEquals(response.getData().getSourceBranchId(), sourceBranchId);
}
}
12 changes: 12 additions & 0 deletions src/test/resources/api/branches/branchCloneStatus.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"data": {
"identifier": "aaee1685-c92a-4da6-8a08-c28b4db5cd4a",
"status": "finished",
"progress": 100,
"attributes": {},
"createdAt": "2024-04-16T16:26:44+00:00",
"updatedAt": "2024-04-16T16:26:44+00:00",
"startedAt": "2024-04-16T16:26:44+00:00",
"finishedAt": "2024-04-16T16:26:44+00:00"
}
}
15 changes: 15 additions & 0 deletions src/test/resources/api/branches/branchMergeStatus.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"data": {
"identifier": "50fb3506-4127-4ba8-8296-f97dc7e3e0c3",
"status": "finished",
"progress": 100,
"attributes": {
"sourceBranchId": 8,
"deleteAfterMerge": false
},
"createdAt": "2019-09-23T11:26:54+00:00",
"updatedAt": "2019-09-23T11:26:54+00:00",
"startedAt": "2019-09-23T11:26:54+00:00",
"finishedAt": "2019-09-23T11:26:54+00:00"
}
}
14 changes: 14 additions & 0 deletions src/test/resources/api/branches/branchMergeSummary.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"data": {
"status": "merged",
"sourceBranchId": 8,
"targetBranchId": 14,
"dryRun": false,
"details": {
"added": 1,
"deleted": 2,
"updated": 3,
"conflicted": 7
}
}
}
Loading

0 comments on commit bc53819

Please sign in to comment.