Skip to content

Commit

Permalink
feat: add sync-labels option (#59)
Browse files Browse the repository at this point in the history
* Add 'sync-labels' to README.md

* feat: add `sync-labels` option

* Update action.yml

* Update docs

---------

Co-authored-by: Kaeden Wile <[email protected]>
Co-authored-by: Stephan Miehe <[email protected]>
  • Loading branch information
3 people authored Feb 8, 2023
1 parent de16e74 commit be327c6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 27 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,21 @@ jobs:
enable-versioned-regex: 0
include-title: 1
```

### Syncing Labels

By default, labels that no longer match are not removed from the issue. To enable this functionality, explicity
set `sync-labels` to `1`.

```
jobs:
triage:
runs-on: ubuntu-latest
steps:
- uses: github/[email protected]
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
configuration-path: .github/labeler.yml
enable-versioned-regex: 0
sync-labels: 1
```
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ inputs:
description: 'Include the title in addition to the body in the regex target'
required: false
default: "0"
sync-labels:
description: 'Remove the label from the issue if the label regex does not match'
required: false
default: "0"
runs:
using: 'node16'
main: 'lib/index.js'
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js

Large diffs are not rendered by default.

55 changes: 29 additions & 26 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getInput, error as setError, setFailed } from "@actions/core";
import { getInput, setFailed, debug } from "@actions/core";
import { context, getOctokit } from "@actions/github";
import { load as loadYaml } from "js-yaml";

Expand All @@ -20,6 +20,7 @@ async function run() {
required: false,
});
const includeTitle = parseInt(getInput("include-title", { required: false }));
const syncLabels = parseInt(getInput("sync-labels", { required: false }));

const issue_number = getIssueOrPRNumber();
if (issue_number === undefined) {
Expand All @@ -42,10 +43,12 @@ async function run() {
// A client to load data from GitHub
const { rest: client } = getOctokit(token);

const addLabel: string[] = [];
const removeLabelItems: string[] = [];
/** List of labels to add */
const toAdd: string[] = [];
/** List of labels to remove */
const toRemove: string[] = [];

if (enableVersionedRegex == 1) {
if (enableVersionedRegex === 1) {
const regexVersion = versionedRegex.exec(issue_body);
if (!regexVersion || !regexVersion[1]) {
if (bodyMissingRegexLabel) {
Expand All @@ -55,10 +58,8 @@ async function run() {
`Issue #${issue_number} does not contain regex version in the body of the issue, exiting.`
);
return;
} else {
if (bodyMissingRegexLabel) {
removeLabelItems.push(bodyMissingRegexLabel);
}
} else if (bodyMissingRegexLabel) {
toRemove.push(bodyMissingRegexLabel);
}
configPath = regexifyConfigPath(configPath, regexVersion[1]);
}
Expand Down Expand Up @@ -88,21 +89,24 @@ async function run() {

for (const [label, globs] of labelRegexes.entries()) {
if (checkRegexes(issueContent, globs)) {
addLabel.push(label);
} else {
removeLabelItems.push(label);
toAdd.push(label);
} else if (syncLabels === 1) {
toRemove.push(label);
}
}

const promises = [];
if (addLabel.length) {
console.log(`Adding labels ${addLabel} to issue #${issue_number}`);
promises.push(addLabels(client, issue_number, addLabel));
let promises = [];
if (toAdd.length) {
promises.push(addLabels(client, issue_number, toAdd));
}

await Promise.all(
promises.concat(removeLabelItems.map(removeLabel(client, issue_number)))
);
promises = promises.concat(toRemove.map(removeLabel(client, issue_number)));

const rejected = (await Promise.allSettled(promises))
.map((p) => p.status === "rejected" && p.reason)
.filter(Boolean);

throw new AggregateError(rejected);
}

function getIssueOrPRNumber() {
Expand Down Expand Up @@ -197,7 +201,8 @@ async function addLabels(
labels: string[]
) {
try {
console.log(`Adding labels ${labels} to issue #${issue_number}`);
const formatted = labels.map((l) => `"${l}"`).join(", ");
debug(`Adding label(s) (${formatted}) to issue #${issue_number}`);
return await client.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
Expand All @@ -213,22 +218,20 @@ async function addLabels(
function removeLabel(client: GitHubClient, issue_number: number) {
return async function (name: string) {
try {
console.log(`Removing label ${name} from issue #${issue_number}`);
debug(`Removing label ${name} from issue #${issue_number}`);
return await client.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number,
name,
});
} catch (error) {
console.log(`Could not remove label ${name} from issue #${issue_number}`);
console.log(
`Could not remove label "${name}" from issue #${issue_number}`
);
throw error;
}
};
}

run().catch((e) => {
const error = e as Error;
setError(error);
setFailed(error.message);
});
run().catch(setFailed);

0 comments on commit be327c6

Please sign in to comment.