Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Fix task deletion for branch delete command #63

Merged
merged 1 commit into from
Jul 15, 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
16 changes: 10 additions & 6 deletions src/create-tasks.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {
CommonErrorResponse,
TasksModel,
ValidationErrorResponse,
ResponseObject,
} from "@crowdin/crowdin-api-client";
import {
CrowdinType,
createTask,
deleteTask,
isCommonErrorResponse,
listBranches,
listTasks,
unwrapErrorResponse,
} from "./lib/crowdin";
Expand All @@ -20,21 +21,24 @@ export interface CreateTasksOptions {
type: "proofread" | "translate";
}

const taskLimit: number = 500;

export default async (options: CreateTasksOptions) => {
log.info("Creating tasks...");

let branches, branchId, tasks;
let tasks: ResponseObject<TasksModel.Task>[] = [];
try {
branches = await listBranches(options.branchName);
branchId = branches.data[0].data.id;
tasks = await listTasks({ branchId });
for (let i = 0; tasks.length >= i * taskLimit; i++) {
const moreTasks = await listTasks(i * taskLimit, taskLimit);
tasks = tasks.concat(moreTasks.data);
}
} catch (error) {
log.error(JSON.stringify(error));
throw error;
}

await Promise.allSettled(
tasks.data
tasks
.filter(task => task.data.fileIds.includes(options.fileId))
.map(task => deleteTask(task.data.id))
);
Expand Down
30 changes: 24 additions & 6 deletions src/delete-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ import {
} from "./lib/crowdin";
import { getCrowdinBranchName } from "./utils/get-crowdin-branch-name";
import log from "./utils/logging";
import { TasksModel, ResponseObject } from "@crowdin/crowdin-api-client";

export interface DeleteBranchOptions {
branchName: string;
deleteTasks?: boolean;
}

const taskLimit: number = 500;

export default async ({
branchName: gitBranchName,
deleteTasks,
}: DeleteBranchOptions) => {
log.info("Deleting branch from Crowdin");
const branchName = getCrowdinBranchName(gitBranchName);
if (branchName === "master") {
return log.error("Cannot delete the master branch");
}

let branches = null;

try {
Expand All @@ -39,17 +46,28 @@ export default async ({

try {
if (deleteTasks) {
log.info("Deleting tasks");
const files = await listFiles(branchId);
const tasks = await listTasks({ branchId });
if (files?.data.length !== 1) {
return log.error(`Expected 1 file, found ${files?.data.length}`);
}

let tasks: ResponseObject<TasksModel.Task>[] = [];
for (let i = 0; tasks.length >= i * taskLimit; i++) {
const moreTasks = await listTasks(i * taskLimit, taskLimit);
tasks = tasks.concat(moreTasks.data);
}

await Promise.allSettled(
tasks.data
.filter(task => {
return task.data.fileIds.some(fileId =>
files.data.some(file => file.data.id === fileId)
tasks
.filter(task => task.data.fileIds.includes(files.data[0].data.id))
.map(task => {
log.info(
`Deleting task: ${task.data.title} - ${task.data.targetLanguageId}`
);

return deleteTask(task.data.id);
})
.map(task => deleteTask(task.data.id))
).then(results =>
results.forEach(result => {
if (result.status === "rejected") {
Expand Down
12 changes: 6 additions & 6 deletions src/lib/crowdin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,13 @@ export const exportFile = async (
return axios.get(translation.data.url);
};

export const listTasks = (options?: {
branchId: number;
}): Promise<ResponseList<TasksModel.Task>> => {
export const listTasks = (
offset: number = 0,
limit: number = 500
): Promise<ResponseList<TasksModel.Task>> => {
return tasksApi.listTasks(CROWDIN_PROJECT_ID, {
limit: 500,
status: TasksModel.Status.TODO,
...(options || {}),
limit: limit,
offset: offset,
});
};

Expand Down
Loading