diff --git a/README.md b/README.md index fecd4035..40c3935a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Automatically labels issues, by adding new labels to the issue. You define the labels you'd like to add to your issues in the YAML file. +Automatically adds or removes labels from issues. You define the labels you'd like to add and/or remove in the YAML file. To add it to your workflow: @@ -6,7 +6,7 @@ To add it to your workflow: - uses: andymckay/labeler@1.0.0 with: repo-token: "${{ secrets.GITHUB_TOKEN }}" - labels: "needs-triage, bug" + add-labels: "needs-triage, bug" ``` This adds the `needs-triage` and `bug` labels to the issue. The most common approach is to do this when issues are created, you can do this with the following in your workflow file: @@ -17,4 +17,19 @@ on: types: [opened] ``` -That's it. +This action can also be used to remove labels from an issue. Just pass the label(s) to be removed separated by commas. + +``` + - uses: andymckay/labeler@1.0.0 + with: + repo-token: "${{ secrets.GITHUB_TOKEN }}" + remove-labels: "help-wanted" +``` + +An example use-case would be, to remove the `help-wanted` label when an issue is assigned to someone. For this, the workflow file would look like: + +``` +on: + issues: + types: [assigned] +``` diff --git a/action.yml b/action.yml index cc84e2d2..8136a203 100644 --- a/action.yml +++ b/action.yml @@ -1,12 +1,15 @@ name: 'Simple Issue Labeler' -description: 'Adds labels to new issues.' +description: 'Adds and removes labels from issues.' inputs: repo-token: description: 'Token for the repo. Can be passed in using {{ secrets.GITHUB_TOKEN }}' required: true - labels: + add-labels: description: 'Labels to add to an issue, seperated by commas.' - required: true + required: false + remove-labels: + description: 'Labels to be removed from an issue, seperated by commas.' + required: false branding: icon: zap-off color: orange diff --git a/label.js b/label.js index 75bf1fa5..a450c19c 100644 --- a/label.js +++ b/label.js @@ -1,8 +1,13 @@ const github = require("@actions/github"); const core = require("@actions/core"); -const requested = core - .getInput("labels") +const labelsToAdd = core + .getInput("add-labels") + .split(",") + .map(x => x.trim()); + +const labelsToRemove = core + .getInput("remove-labels") .split(",") .map(x => x.trim()); @@ -12,11 +17,14 @@ async function label() { const context = github.context; let labels = context.payload.issue.labels.map(label => label.name); - for (let requestedLabel of requested) { - if (!labels.includes(requestedLabel)) { - labels.push(requestedLabel); + for (let labelToAdd of labelsToAdd) { + if (!labels.includes(labelToAdd)) { + labels.push(labelToAdd); } } + labels = labels.filter((value) => { + return !labelsToRemove.includes(value); + }); await octokit.issues.update({ owner: context.payload.repository.owner.login, @@ -31,7 +39,7 @@ label() .then( result => { // eslint-disable-next-line no-console - console.log(`Labelled ${result} with ${requested}.`); + console.log(`Updated labels in ${result}. Added: ${labelsToAdd}. Removed: ${labelsToRemove}.`); }, err => { // eslint-disable-next-line no-console