Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: github/issue-labeler
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.6
Choose a base ref
...
head repository: github/issue-labeler
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v3.0
Choose a head ref
  • 2 commits
  • 4 files changed
  • 4 contributors

Commits on Feb 8, 2023

  1. feat: add sync-labels option (#59)

    * 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]>
    3 people authored Feb 8, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    be327c6 View commit details

Commits on Feb 10, 2023

  1. fix for 3.0

    stephanmiehe committed Feb 10, 2023

    Verified

    This commit was signed with the committer’s verified signature.
    stephanmiehe Stephan Miehe
    Copy the full SHA
    cd54a96 View commit details
Showing with 55 additions and 28 deletions.
  1. +18 −0 README.md
  2. +4 −0 action.yml
  3. +2 −2 lib/index.js
  4. +31 −26 src/main.ts
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/issue-labeler@v2.0
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
@@ -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'
4 changes: 2 additions & 2 deletions lib/index.js

Large diffs are not rendered by default.

57 changes: 31 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";

@@ -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) {
@@ -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) {
@@ -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]);
}
@@ -88,21 +89,26 @@ 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);

if (rejected.length) {
throw new AggregateError(rejected)
}
}

function getIssueOrPRNumber() {
@@ -197,7 +203,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,
@@ -213,22 +220,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);