Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: branch new api methods #410

Merged
merged 1 commit into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading