From 169fc0aaaa6d0f9bcf5196fac5b3da268601d0ee Mon Sep 17 00:00:00 2001 From: Yevheniy Oliynyk Date: Sat, 13 Jul 2024 18:02:04 +0200 Subject: [PATCH] feat: branch new api methods --- src/sourceFiles/index.ts | 48 ++++++++++++++++++++++++++++- tests/sourceFiles/api.test.ts | 57 +++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/src/sourceFiles/index.ts b/src/sourceFiles/index.ts index 67bf2fa71..23b5ebd56 100644 --- a/src/sourceFiles/index.ts +++ b/src/sourceFiles/index.ts @@ -7,6 +7,7 @@ import { PatchRequest, ResponseList, ResponseObject, + Status, } from '../core'; /** @@ -15,8 +16,48 @@ import { * Use API to keep the source files up to date, check on file revisions, and manage project branches. * Before adding source files to the project, upload each file to the Storage first. */ -//TODO add missing branch endpoints (https://github.com/crowdin/crowdin-api-client-js/issues/380) export class SourceFiles extends CrowdinApi { + /** + * @param projectId project identifier + * @param branchId branch identifier + * @param cloneId clone branch identifier + * @see https://developer.crowdin.com/api/v2/string-based/#operation/api.projects.branches.clones.branch.get + */ + getClonedBranch( + projectId: number, + branchId: number, + cloneId: string, + ): Promise> { + const url = `${this.url}/projects/${projectId}/branches/${branchId}/clones/${cloneId}/branch`; + return this.get(url, this.defaultConfig()); + } + + /** + * @param projectId project identifier + * @param branchId branch identifier + * @param request request body + * @see https://developer.crowdin.com/api/v2/string-based/#operation/api.projects.branches.clones.post + */ + clonedBranch( + projectId: number, + branchId: number, + request: SourceFilesModel.CloneBranchRequest, + ): Promise>> { + const url = `${this.url}/projects/${projectId}/branches/${branchId}/clones`; + return this.post(url, request, this.defaultConfig()); + } + + /** + * @param projectId project identifier + * @param branchId branch identifier + * @param cloneId clone branch identifier + * @see https://developer.crowdin.com/api/v2/string-based/#operation/api.projects.branches.clones.get + */ + checkBranchClonedStatus(projectId: number, branchId: number, cloneId: string): Promise>> { + const url = `${this.url}/projects/${projectId}/branches/${branchId}/clones/${cloneId}`; + return this.get(url, this.defaultConfig()); + } + /** * @param projectId project identifier * @param options optional parameters for the request @@ -494,6 +535,11 @@ export namespace SourceFilesModel { priority?: Priority; } + export interface CloneBranchRequest { + name: string; + title?: string; + } + export type Priority = 'low' | 'normal' | 'high'; export interface ListProjectDirectoriesOptions extends PaginationOptions { diff --git a/tests/sourceFiles/api.test.ts b/tests/sourceFiles/api.test.ts index afb6d3f5d..4f1571f33 100644 --- a/tests/sourceFiles/api.test.ts +++ b/tests/sourceFiles/api.test.ts @@ -27,11 +27,50 @@ describe('Source Files API', () => { const buildId = 121212; + const cloneId = 'test12312'; + const fileId = 321; const limit = 25; beforeAll(() => { scope = nock(api.url) + .get(`/projects/${projectId}/branches/${branchId}/clones/${cloneId}/branch`, undefined, { + reqheaders: { + Authorization: `Bearer ${api.token}`, + }, + }) + .reply(200, { + data: { + id: branchId, + name: branchName, + }, + }) + .post( + `/projects/${projectId}/branches/${branchId}/clones`, + { + name: branchName, + }, + { + reqheaders: { + Authorization: `Bearer ${api.token}`, + }, + }, + ) + .reply(200, { + data: { + identifier: cloneId, + }, + }) + .get(`/projects/${projectId}/branches/${branchId}/clones/${cloneId}`, undefined, { + reqheaders: { + Authorization: `Bearer ${api.token}`, + }, + }) + .reply(200, { + data: { + identifier: cloneId, + }, + }) .get(`/projects/${projectId}/branches`, undefined, { reqheaders: { Authorization: `Bearer ${api.token}`, @@ -408,6 +447,24 @@ describe('Source Files API', () => { scope.done(); }); + it('Get Cloned Branch', async () => { + const branch = await api.getClonedBranch(projectId, branchId, cloneId); + expect(branch.data.id).toBe(branchId); + expect(branch.data.name).toBe(branchName); + }); + + it('Clone branch', async () => { + const clone = await api.clonedBranch(projectId, branchId, { + name: branchName, + }); + expect(clone.data.identifier).toBe(cloneId); + }); + + it('Check Branch Clone Status', async () => { + const clone = await api.checkBranchClonedStatus(projectId, branchId, cloneId); + expect(clone.data.identifier).toBe(cloneId); + }); + it('List project branches', async () => { const branches = await api.listProjectBranches(projectId, { name: branchName }); expect(branches.data.length).toBe(1);