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: add toggle-labels action #132

Merged
merged 19 commits into from
Jan 31, 2023
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -1150,6 +1151,39 @@ jobs:

⏫ [Back to list](#List)

#### `toggle-labels`
xrkffgg marked this conversation as resolved.
Show resolved Hide resolved

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
Expand Down
34 changes: 34 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@
- [`mark-assignees`](#mark-assignees)
- [`mark-duplicate`](#mark-duplicate)
- [`welcome`](#welcome)
- [`toggle-labels`](#toggle-labels)

## 🚀 使 用

Expand Down Expand Up @@ -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
Expand Down
30 changes: 29 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;


/***/ }),
Expand Down Expand Up @@ -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.`);
Expand Down
27 changes: 27 additions & 0 deletions src/helper/advanced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
doCreateComment,
doCreateCommentEmoji,
doLockIssue,
doRemoveLabels,
doSetLabels,
doUpdateComment,
} from './base';
Expand Down Expand Up @@ -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!`);
}
5 changes: 5 additions & 0 deletions src/helper/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
doLockIssues,
doMarkAssignees,
doMarkDuplicate,
doToggleLabels,
doWelcome,
initAdvancedICE,
} from './advanced';
Expand Down Expand Up @@ -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.`);
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ export type TAction =
| 'lock-issues'
| 'mark-assignees'
| 'mark-duplicate'
| 'welcome';
| 'welcome'
| 'toggle-labels';
//// [ Advanced End ]
31 changes: 31 additions & 0 deletions web/docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | ✖ |
31 changes: 31 additions & 0 deletions web/docs/advanced.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | ✖ |