Skip to content

Commit

Permalink
feat: branch new api methods (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ committed Jul 13, 2024
1 parent 515a01d commit ee7efe8
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/sourceFiles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PatchRequest,
ResponseList,
ResponseObject,
Status,
} from '../core';

/**
Expand All @@ -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<ResponseObject<SourceFilesModel.Branch>> {
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<ResponseObject<Status<{}>>> {
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<ResponseObject<Status<{}>>> {
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
Expand Down Expand Up @@ -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 {
Expand Down
57 changes: 57 additions & 0 deletions tests/sourceFiles/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ee7efe8

Please sign in to comment.