From 6a55b3a9f474d8a01b0014217755598f5d8065b8 Mon Sep 17 00:00:00 2001 From: Wuxh Date: Tue, 31 Jan 2023 12:56:43 +0800 Subject: [PATCH] feat: add `toggle-labels` action (#132) * feat: add `toggle-labels` action * docs: update docs * chore: npm run package * Revert "docs: update docs" This reverts commit b08e8f300e0caafa362239f4e678cfdeac81449a. * docs: update docs * Update README.zh-CN.md Co-authored-by: xrkffgg * Update README.md Co-authored-by: xrkffgg * Update README.zh-CN.md Co-authored-by: xrkffgg * Update README.md * move * update * lint code * build use npm run all * update * build use npm run all * chore: order * update docs * format code use npm run format * build use npm run all --------- Co-authored-by: xrkffgg --- README.md | 34 ++++++++++++++++++++++++++++++++++ README.zh-CN.md | 34 ++++++++++++++++++++++++++++++++++ dist/index.js | 30 +++++++++++++++++++++++++++++- src/helper/advanced.ts | 27 +++++++++++++++++++++++++++ src/helper/helper.ts | 5 +++++ src/types.ts | 3 ++- web/docs/advanced.md | 31 +++++++++++++++++++++++++++++++ web/docs/advanced.zh-CN.md | 31 +++++++++++++++++++++++++++++++ 8 files changed, 193 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 035b9d2..22ddecb 100644 --- a/README.md +++ b/README.md @@ -256,6 +256,7 @@ When the following list does not have the features you want, you can submit it i - [`mark-assignees`](#mark-assignees) - [`mark-duplicate`](#mark-duplicate) - [`welcome`](#welcome) + - [`toggle-labels`](#toggle-labels) ## 🚀 Usage @@ -1150,6 +1151,39 @@ jobs: ⏫ [Back to list](#List) +#### `toggle-labels` + +When an issue is reopened, the set labels are removed if they already exist, otherwise they are added. + +```yml +name: Toggle Labels + +on: + issues: + types: [reopened] + +jobs: + toggle-labels: + runs-on: ubuntu-latest + steps: + - name: Toggle labels + uses: actions-cool/issues-helper@v3 + with: + actions: 'toggle-labels' + token: ${{ secrets.GITHUB_TOKEN }} + issue-number: ${{ github.event.issue.number }} + labels: 'unread,outdated' +``` + +| Param | Desc | Type | Required | +| -- | -- | -- | -- | +| actions | Action type | string | ✔ | +| token | [Token explain](#token) | string | ✖ | +| issue-number | The number of issue. When not input, it will be obtained from the trigger event | number | ✖ | +| labels | The toggle labels. Delete if the label already exists, add if it does not exist | string | ✖ | + +⏫ [Back to list](#List) + ## 🎁 Reference ### token diff --git a/README.zh-CN.md b/README.zh-CN.md index a917377..a03836e 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -260,6 +260,7 @@ - [`mark-assignees`](#mark-assignees) - [`mark-duplicate`](#mark-duplicate) - [`welcome`](#welcome) + - [`toggle-labels`](#toggle-labels) ## 🚀 使 用 @@ -1148,6 +1149,39 @@ jobs: ⏫ [返回列表](#列-表) +#### `toggle-labels` + +当一个 issue 被重新打开,判断设置的 labels 如果已经存在则进行删除,否则进行添加。 + +```yml +name: Toggle Labels + +on: + issues: + types: [reopened] + +jobs: + toggle-labels: + runs-on: ubuntu-latest + steps: + - name: Toggle labels + uses: actions-cool/issues-helper@v3 + with: + actions: 'toggle-labels' + token: ${{ secrets.GITHUB_TOKEN }} + issue-number: ${{ github.event.issue.number }} + labels: 'unread,outdated' +``` + +| 参数 | 描述 | 类型 | 必填 | +| -- | -- | -- | -- | +| actions | 操作类型 | string | ✔ | +| token | [token 说明](#token) | string | ✖ | +| issue-number | 指定的 issue,当不传时会从触发事件中获取 | number | ✖ | +| labels | 切换 labels。如果 label 已存在则删除,不存在则添加 | string | ✖ | + +⏫ [返回列表](#列-表) + ## 🎁 参 考 ### token diff --git a/dist/index.js b/dist/index.js index 54ec15b..263f54b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -16060,7 +16060,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.doWelcome = exports.doMarkDuplicate = exports.doMarkAssignees = exports.doLockIssues = exports.doFindIssues = exports.doFindComments = exports.doCloseIssues = exports.doCheckIssue = exports.doCheckInactive = exports.doQueryIssues = exports.initAdvancedICE = void 0; +exports.doToggleLabels = exports.doWelcome = exports.doMarkDuplicate = exports.doMarkAssignees = exports.doLockIssues = exports.doFindIssues = exports.doFindComments = exports.doCloseIssues = exports.doCheckIssue = exports.doCheckInactive = exports.doQueryIssues = exports.initAdvancedICE = void 0; const actions_util_1 = __nccwpck_require__(6972); const dayjs_1 = __importDefault(__nccwpck_require__(7401)); const isSameOrBefore_1 = __importDefault(__nccwpck_require__(9517)); @@ -16437,6 +16437,30 @@ function doWelcome(auth, issueNumber, body, labels, assignees, emoji) { }); } exports.doWelcome = doWelcome; +function doToggleLabels(labels = []) { + return __awaiter(this, void 0, void 0, function* () { + const issue = yield ICE.getIssue(); + const baseLabels = issue.labels.map(({ name }) => name); + const addLabels = []; + const removeLabels = []; + for (const label of labels) { + if (baseLabels.includes(label)) { + removeLabels.push(label); + } + else { + addLabels.push(label); + } + } + if (removeLabels.length) { + yield (0, base_1.doRemoveLabels)(removeLabels); + } + if (addLabels.length) { + yield (0, base_1.doAddLabels)(addLabels); + } + core.info(`[doToggleLabels] Done!`); + }); +} +exports.doToggleLabels = doToggleLabels; /***/ }), @@ -16923,6 +16947,10 @@ class IssueHelperEngine { } break; } + case 'toggle-labels': { + yield (0, advanced_1.doToggleLabels)(labels); + break; + } // -[ Advanced End ]-> default: { core.warning(`The ${action} is not allowed.`); diff --git a/src/helper/advanced.ts b/src/helper/advanced.ts index 08055f1..dd4f66f 100644 --- a/src/helper/advanced.ts +++ b/src/helper/advanced.ts @@ -16,6 +16,7 @@ import { doCreateComment, doCreateCommentEmoji, doLockIssue, + doRemoveLabels, doSetLabels, doUpdateComment, } from './base'; @@ -392,3 +393,29 @@ export async function doWelcome( core.info(`[doWelcome] ${auth} is not first time!`); } } + +export async function doToggleLabels(labels: string[] = []) { + const issue = await ICE.getIssue(); + const baseLabels: string[] = issue.labels.map(({ name }: any) => name); + + const addLabels = []; + const removeLabels = []; + + for (const label of labels) { + if (baseLabels.includes(label)) { + removeLabels.push(label); + } else { + addLabels.push(label); + } + } + + if (removeLabels.length) { + await doRemoveLabels(removeLabels); + } + + if (addLabels.length) { + await doAddLabels(addLabels); + } + + core.info(`[doToggleLabels] Done!`); +} diff --git a/src/helper/helper.ts b/src/helper/helper.ts index 0dc3177..1cd0740 100644 --- a/src/helper/helper.ts +++ b/src/helper/helper.ts @@ -14,6 +14,7 @@ import { doLockIssues, doMarkAssignees, doMarkDuplicate, + doToggleLabels, doWelcome, initAdvancedICE, } from './advanced'; @@ -259,6 +260,10 @@ export class IssueHelperEngine implements IIssueHelperEngine { } break; } + case 'toggle-labels': { + await doToggleLabels(labels); + break; + } // -[ Advanced End ]-> default: { core.warning(`The ${action} is not allowed.`); diff --git a/src/types.ts b/src/types.ts index 27f29c2..d2ae897 100644 --- a/src/types.ts +++ b/src/types.ts @@ -56,5 +56,6 @@ export type TAction = | 'lock-issues' | 'mark-assignees' | 'mark-duplicate' - | 'welcome'; + | 'welcome' + | 'toggle-labels'; //// [ Advanced End ] diff --git a/web/docs/advanced.md b/web/docs/advanced.md index aa435b2..d09c9a2 100644 --- a/web/docs/advanced.md +++ b/web/docs/advanced.md @@ -389,3 +389,34 @@ jobs: | issue-emoji | Add [emoji](/guide/ref#-emoji-type) to this issue| string | ✖ | - If these 4 options are not filled, no operation + +## `toggle-labels` + +When an issue is reopened, the set labels are removed if they already exist, otherwise they are added. + +```yml +name: Toggle Labels + +on: + issues: + types: [reopened] + +jobs: + toggle-labels: + runs-on: ubuntu-latest + steps: + - name: Toggle labels + uses: actions-cool/issues-helper@v3 + with: + actions: 'toggle-labels' + token: ${{ secrets.GITHUB_TOKEN }} + issue-number: ${{ github.event.issue.number }} + labels: 'unread,outdated' +``` + +| Param | Desc | Type | Required | +| -- | -- | -- | -- | +| actions | Action type | string | ✔ | +| token | [Token explain](/guide/ref#-token) | string | ✖ | +| issue-number | The number of issue. When not input, it will be obtained from the trigger event | number | ✖ | +| labels | The toggle labels. Delete if the label already exists, add if it does not exist | string | ✖ | diff --git a/web/docs/advanced.zh-CN.md b/web/docs/advanced.zh-CN.md index 1cf141f..f60aaf3 100644 --- a/web/docs/advanced.zh-CN.md +++ b/web/docs/advanced.zh-CN.md @@ -384,3 +384,34 @@ jobs: | issue-emoji | 为该 issue 增加 [emoji](/zh-CN/guide/ref#-emoji-类型) | string | ✖ | - 若这 4 个可选项都不填,则无操作 + +## `toggle-labels` + +当一个 issue 被重新打开,判断设置的 labels 如果已经存在则进行删除,否则进行添加。 + +```yml +name: Toggle Labels + +on: + issues: + types: [reopened] + +jobs: + toggle-labels: + runs-on: ubuntu-latest + steps: + - name: Toggle labels + uses: actions-cool/issues-helper@v3 + with: + actions: 'toggle-labels' + token: ${{ secrets.GITHUB_TOKEN }} + issue-number: ${{ github.event.issue.number }} + labels: 'unread,outdated' +``` + +| 参数 | 描述 | 类型 | 必填 | +| -- | -- | -- | -- | +| actions | 操作类型 | string | ✔ | +| token | [token 说明](/zh-CN/guide/ref#-token-说明) | string | ✖ | +| issue-number | 指定的 issue,当不传时会从触发事件中获取 | number | ✖ | +| labels | 切换 labels。如果 label 已存在则删除,不存在则添加 | string | ✖ |