feat: add toggle-labels action

This commit is contained in:
wuxh
2023-01-12 22:26:46 +08:00
parent f4eba4debf
commit 8f847c01c2
5 changed files with 40 additions and 0 deletions

View File

@@ -138,6 +138,11 @@ export async function doSetLabels(labels: string[]) {
core.info(`[doSetLabels] [${labels}] success!`);
}
export async function doToggleLabels(labels: string[]) {
await ICE.toggleLabels(labels);
core.info(`[doToggleLabels] [${labels}] success!`);
}
export async function doUnlockIssue() {
await ICE.unlockIssue();
core.info(`[doUnlockIssue] success!`);

View File

@@ -31,6 +31,7 @@ import {
doRemoveAssignees,
doRemoveLabels,
doSetLabels,
doToggleLabels,
doUnlockIssue,
doUpdateComment,
doUpdateIssue,
@@ -208,6 +209,14 @@ export class IssueHelperEngine implements IIssueHelperEngine {
await doUpdateIssue(0, state, title, body, updateMode, labels, assignees);
break;
}
case 'toggle-labels': {
if (labels && labels.length) {
await doToggleLabels(labels);
} else {
core.warning(`[doToggleLabels] labels is empty!`);
}
break;
}
// ---[ Base End ]--->>>
// ^_^ ============= ^_^
// -[ Advanced Begin ]->

View File

@@ -273,6 +273,30 @@ export class IssueCoreEngine implements IIssueCoreEngine {
}
}
public async toggleLabels(labels: string[]) {
const issue = await this.getIssue();
const baseLabels: string[] = issue.labels.map(({ name }: any) => name);
let addLabels = [];
let removeLabels = [];
for (const label of labels) {
if (baseLabels.includes(label)) {
removeLabels.push(label);
} else {
addLabels.push(label);
}
}
if (removeLabels.length) {
await this.removeLabels(removeLabels);
}
if (addLabels.length) {
await this.addLabels(addLabels);
}
}
public async unlockIssue() {
const { owner, repo, octokit, issueNumber } = this;
await octokit.issues.unlock({

View File

@@ -102,6 +102,7 @@ export interface IIssueCoreEngine {
removeLabels: (labels: string[]) => Promise<void>;
setLabels: (labels: string[]) => Promise<void>;
toggleLabels: (labels: string[]) => Promise<void>;
unlockIssue: () => Promise<void>;

View File

@@ -44,6 +44,7 @@ export type TAction =
| 'unlock-issue'
| 'update-comment'
| 'update-issue'
| 'toggle-labels'
// [ Base End ]
// ^_^ ========== ^_^
// [ Advanced Begin ]