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

Commit

Permalink
Fix task deletion for branch delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBrouws committed Jul 11, 2024
1 parent b61dd9a commit 6aacace
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
15 changes: 10 additions & 5 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,15 +21,19 @@ export interface CreateTasksOptions {
type: "proofread" | "translate";
}

const taskLimit: number = 500;

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

const branches = await listBranches(options.branchName);
const branchId = branches.data[0].data.id;
const tasks = await listTasks({ branchId });
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
tasks
.filter(task => task.data.fileIds.includes(options.fileId))
.map(task => deleteTask(task.data.id))
);
Expand Down
26 changes: 20 additions & 6 deletions src/delete-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ 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,
Expand All @@ -39,17 +42,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

0 comments on commit 6aacace

Please sign in to comment.