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: v3.1
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.2
Choose a head ref
  • 9 commits
  • 4 files changed
  • 5 contributors

Commits on Apr 1, 2023

  1. Update README.md

    stephanmiehe authored Apr 1, 2023
    Copy the full SHA
    e874594 View commit details

Commits on May 15, 2023

  1. fix: read default token (#67)

    * fix: read default token
    
    * docs: update README
    EndBug authored May 15, 2023
    Copy the full SHA
    ddcff90 View commit details
  2. feat: allow the use of custom issue numbers (#66)

    This allows users to run the action on any event, like
    `workflow_dispatch`.
    The default value is still the number from the issue/PR that triggered
    the workflow run, if any.
    
    Closes #64
    EndBug authored May 15, 2023
    Copy the full SHA
    30eb925 View commit details

Commits on May 23, 2023

  1. Update README.md (#69)

    stephanmiehe authored May 23, 2023
    Copy the full SHA
    3d981b0 View commit details

Commits on May 26, 2023

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    083c480 View commit details
  2. feat: Add outputs of the labels added and removed (#71)

    * feat: Add outputs of the labels added and removed
    
    * chore: Regenerate action
    phated authored May 26, 2023

    Verified

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

Commits on Jun 29, 2023

  1. Verified

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

Commits on Jul 4, 2023

  1. feat: add option to include/exclude body as regex target (#70)

    * feat: add option to include/exclude body as regex target
    
    * build: create build
    amerikan authored Jul 4, 2023

    Verified

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

Commits on Jul 5, 2023

  1. Update README.md

    stephanmiehe authored Jul 5, 2023
    Copy the full SHA
    98b5412 View commit details
Showing with 101 additions and 43 deletions.
  1. +59 −17 README.md
  2. +18 −2 action.yml
  3. +1 −1 lib/index.js
  4. +23 −23 src/main.ts
76 changes: 59 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -18,40 +18,49 @@ critical:
- '(critical|urgent)'
```
#### Label All Issues
```yml
# Add 'critical' label to any issue that gets opened
critical:
- '/.*/'
```
### Create Workflow
Create a workflow (eg: `.github/workflows/labeler.yml` see [Creating a Workflow file](https://help.github.com/en/articles/configuring-a-workflow#creating-a-workflow-file)) to utilize the labeler action with content:

```
```yml
name: "Issue Labeler"
on:
issues:
types: [opened, edited]
permissions:
issues: write
contents: read
jobs:
triage:
runs-on: ubuntu-latest
steps:
- uses: github/issue-labeler@v2.5 #May not be the latest version
- uses: github/issue-labeler@v3.1 #May not be the latest version
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
configuration-path: .github/labeler.yml
not-before: 2020-01-15T02:54:32Z
enable-versioned-regex: 0
repo-token: ${{ github.token }}
```

`not-before` is optional and will result in any issues prior to this timestamp to be ignored.

_Note: The above workflow grants access to the `GITHUB_TOKEN` so the action can make calls to GitHub's REST API._
### Example using versioned issue templates

As you iterate on your regular expressions, since maybe your issue template gets updated, this can have an impact on existing issues. The below allows you to version your regular expression definitions and pair them with issue templates.

Below is the body of an example issue which has the version identifier `issue_labeler_regex_version` embedded.

```
```md
<!--
issue_labeler_regex_version=1
--!>
@@ -61,24 +70,28 @@ I have an urgent issue that requires someone's attention.

Below is the workflow file

```
```yml
name: "Issue Labeler"
on:
issues:
types: [opened, edited]
permissions:
issues: write
contents: read
jobs:
triage:
runs-on: ubuntu-latest
steps:
- uses: github/issue-labeler@v2.5 #May not be the latest version
- uses: github/issue-labeler@v3.1 #May not be the latest version
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
configuration-path: .github/labeler.yml
not-before: 2020-01-15T02:54:32Z
enable-versioned-regex: 1
versioned-regex: 'issue_labeler_regex_version=(\d+)'
body-missing-regex-label: 'broken-template'
repo-token: ${{ github.token }}
```

When the issue is evaluated it'll look for `.github/labeler-v1.yml` based on the `configuration-path` and the version number set in the issue.
@@ -93,7 +106,7 @@ Set `body-missing-regex-label` to the name of the label that should be added to

The labeler action is also available for pull requests. Make sure the workflow is triggered by pull requests.

```
```yml
on:
pull_request:
types: [opened, edited]
@@ -103,38 +116,67 @@ on:

Set `include-title` to `1` to include the issue title in addition to the body in the regular expression target.

```
```yml
name: "Issue Labeler"
on:
issues:
types: [opened, edited]
permissions:
issues: write
contents: read
jobs:
triage:
runs-on: ubuntu-latest
steps:
- uses: github/issue-labeler@v2.5 #May not be the latest version
- uses: github/issue-labeler@v3.1 #May not be the latest version
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
configuration-path: .github/labeler.yml
enable-versioned-regex: 0
include-title: 1
repo-token: ${{ github.token }}
```

### Example of *only* including the issue title, but not the body in the regex target

Set `include-title: 1` and `include-body: 0`.

```yml
name: "Issue Labeler"
on:
issues:
types: [opened, edited]
permissions:
issues: write
contents: read
jobs:
triage:
runs-on: ubuntu-latest
steps:
- uses: github/issue-labeler@v3.1 #May not be the latest version
with:
configuration-path: .github/labeler.yml
include-title: 1
include-body: 0
```

### 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`.

```
```yml
jobs:
triage:
runs-on: ubuntu-latest
steps:
- uses: github/issue-labeler@v2.0
- uses: github/issue-labeler@v3.1
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
configuration-path: .github/labeler.yml
enable-versioned-regex: 0
sync-labels: 1
repo-token: ${{ github.token }}
```
20 changes: 18 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
@@ -4,7 +4,8 @@ author: 'GitHub'
inputs:
repo-token:
description: 'The GITHUB_TOKEN secret'
required: true
required: false
default: '${{ github.token }}'
configuration-path:
description: 'Path to the labeler.yml configuration file'
required: true
@@ -21,13 +22,28 @@ inputs:
description: 'The name of the label that should be added to an issue where the specified `version-regex` can not be found.'
required: false
include-title:
description: 'Include the title in addition to the body in the regex target'
description: 'Include the title in the regex target'
required: false
default: "0"
include-body:
description: 'Include the body in the regex target'
required: false
default: "1"
sync-labels:
description: 'Remove the label from the issue if the label regex does not match'
required: false
default: "0"
issue-number:
description: 'The number of the issue/PR to label'
required: false
default: ${{ github.event.issue.number || github.event.pull_request.number }}

outputs:
labels-added:
description: 'The labels that were added by the action, as a stringified array.'
labels-removed:
description: 'The labels that were removed by the action, as a stringified array.'

runs:
using: 'node16'
main: 'lib/index.js'
2 changes: 1 addition & 1 deletion lib/index.js

Large diffs are not rendered by default.

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

@@ -20,25 +20,10 @@ async function run() {
required: false,
});
const includeTitle = parseInt(getInput("include-title", { required: false }));
const includeBody = parseInt(getInput("include-body", { required: false }));
const syncLabels = parseInt(getInput("sync-labels", { required: false }));

const issue_number = getIssueOrPRNumber();
if (issue_number === undefined) {
console.log("Could not get issue or PR number from context, exiting");
return;
}

const issue_body = getIssueOrPRBody();
if (issue_body === undefined) {
console.log("Could not get issue or PR body from context, exiting");
return;
}

const issue_title = getIssueOrPRTitle();
if (!issue_title) {
console.log("Could not get issue or PR title from context, exiting");
return;
}
const issue_number = parseInt(getInput("issue-number", { required: true }))

// A client to load data from GitHub
const { rest: client } = getOctokit(token);
@@ -54,6 +39,18 @@ async function run() {
issue_number,
});

const issue_body = issue.data.body ?? getIssueOrPRBody();
if (issue_body === undefined) {
console.log("Could not get issue or PR body from API or context, exiting");
return;
}

const issue_title = issue.data.title ?? getIssueOrPRTitle();
if (!issue_title) {
console.log("Could not get issue or PR title from API or context, exiting");
return;
}

const labels: String[] = []
issue.data.labels.forEach((label) => {
if (typeof label === 'string') {
@@ -96,7 +93,7 @@ async function run() {

let issueContent = "";
if (includeTitle === 1) issueContent += `${issue_title}\n\n`;
issueContent += issue_body;
if (includeBody === 1) issueContent += issue_body;

for (const [label, globs] of labelRegexes.entries()) {
if (checkRegexes(issueContent, globs)) {
@@ -123,11 +120,14 @@ async function run() {
if (rejected.length) {
throw new AggregateError(rejected)
}
}

function getIssueOrPRNumber() {
const { issue, pull_request } = context.payload;
return issue?.number ?? pull_request?.number;
if (toAdd.length) {
setOutput("labels-added", toAdd);
}

if (toRemove.length) {
setOutput("labels-removed", toRemove);
}
}

function getIssueOrPRBody() {