Skip to content

Commit

Permalink
branch api updates
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ committed Dec 7, 2024
1 parent d790170 commit 66499c0
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/sourceFiles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,51 @@ export class SourceFiles extends CrowdinApi {
return this.patch(url, request, this.defaultConfig());
}

/**
* @param projectId project identifier
* @param branchId branch identifier
* @param request request body
* @see https://support.crowdin.com/developer/api/v2/string-based/#tag/Branches/operation/api.projects.branches.merges.post
*/
mergeBranch(
projectId: number,
branchId: number,
request: SourceFilesModel.MergeBranchRequest,
): Promise<ResponseObject<Status<SourceFilesModel.MergeBranchAttributes>>> {
const url = `${this.url}/projects/${projectId}/branches/${branchId}/merges`;
return this.post(url, request, this.defaultConfig());
}

/**
* @param projectId project identifier
* @param branchId branch identifier
* @param mergeId merge branch identifier
* @see https://support.crowdin.com/developer/api/v2/string-based/#tag/Branches/operation/api.projects.branches.merges.get
*/
checkBranchMergeStatus(
projectId: number,
branchId: number,
mergeId: string,
): Promise<ResponseObject<Status<SourceFilesModel.MergeBranchAttributes>>> {
const url = `${this.url}/projects/${projectId}/branches/${branchId}/merges/${mergeId}`;
return this.get(url, this.defaultConfig());
}

/**
* @param projectId project identifier
* @param branchId branch identifier
* @param mergeId merge branch identifier
* @see https://support.crowdin.com/developer/api/v2/string-based/#tag/Branches/operation/api.projects.branches.merges.summary.get
*/
getBranchMergeSummary(
projectId: number,
branchId: number,
mergeId: string,
): Promise<ResponseObject<SourceFilesModel.MergeBranchSummary>> {
const url = `${this.url}/projects/${projectId}/branches/${branchId}/merges/${mergeId}/summary`;
return this.get(url, this.defaultConfig());
}

/**
* @param projectId project identifier
* @param options optional parameters for the request
Expand Down Expand Up @@ -540,6 +585,30 @@ export namespace SourceFilesModel {
title?: string;
}

export interface MergeBranchRequest {
deleteAfterMerge?: boolean;
sourceBranchId: number;
dryRun?: boolean;
}

export interface MergeBranchAttributes {
sourceBranchId: number;
deleteAfterMerge: boolean;
}

export interface MergeBranchSummary {
status: string;
sourceBranchId: number;
targetBranchId: number;
dryRun: boolean;
details: {
added: number;
deleted: number;
updated: number;
conflicted: number;
};
}

export type Priority = 'low' | 'normal' | 'high';

export interface ListProjectDirectoriesOptions extends PaginationOptions {
Expand Down
56 changes: 56 additions & 0 deletions tests/sourceFiles/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ describe('Source Files API', () => {
const fileRevisionId = 888;

const branchId = 12;
const sourceBranchId = 14;
const mergeBranchId = 'merge-123';
const mergeBranchStatus = 'merged';
const branchName = 'master';
const branchTitle = 'testTitle';
const storageId = 123;
Expand Down Expand Up @@ -160,6 +163,42 @@ describe('Source Files API', () => {
title: branchTitle,
},
})
.post(
`/projects/${projectId}/branches/${branchId}/merges`,
{
sourceBranchId,
},
{
reqheaders: {
Authorization: `Bearer ${api.token}`,
},
},
)
.reply(200, {
data: {
identifier: mergeBranchId,
},
})
.get(`/projects/${projectId}/branches/${branchId}/merges/${mergeBranchId}`, undefined, {
reqheaders: {
Authorization: `Bearer ${api.token}`,
},
})
.reply(200, {
data: {
identifier: mergeBranchId,
},
})
.get(`/projects/${projectId}/branches/${branchId}/merges/${mergeBranchId}/summary`, undefined, {
reqheaders: {
Authorization: `Bearer ${api.token}`,
},
})
.reply(200, {
data: {
status: mergeBranchStatus,
},
})
.get(`/projects/${projectId}/directories`, undefined, {
reqheaders: {
Authorization: `Bearer ${api.token}`,
Expand Down Expand Up @@ -506,6 +545,23 @@ describe('Source Files API', () => {
expect(branch.data.title).toBe(branchTitle);
});

it('Merge branch', async () => {
const mergeStatus = await api.mergeBranch(projectId, branchId, {
sourceBranchId,
});
expect(mergeStatus.data.identifier).toBe(mergeBranchId);
});

it('Check Branch Merge Status', async () => {
const mergeStatus = await api.checkBranchMergeStatus(projectId, branchId, mergeBranchId);
expect(mergeStatus.data.identifier).toBe(mergeBranchId);
});

it('Get Branch Merge Summary', async () => {
const mergeStatus = await api.getBranchMergeSummary(projectId, branchId, mergeBranchId);
expect(mergeStatus.data.status).toBe(mergeBranchStatus);
});

it('List project directories', async () => {
const directories = await api.listProjectDirectories(projectId);
expect(directories.data.length).toBe(1);
Expand Down

0 comments on commit 66499c0

Please sign in to comment.