Skip to content

Commit

Permalink
Add 'include-title' input to include the issue title in the regex tar…
Browse files Browse the repository at this point in the history
…get (#43)

* Add support for pull requests

Signed-off-by: Kazuki Suda <kazuki.suda@gmail.com>

* Add 'include-title' input to include the issue title in the regex target

Signed-off-by: Kazuki Suda <kazuki.suda@gmail.com>

Co-authored-by: Stephan Miehe <49914758+stephanmiehe@users.noreply.github.com>
  • Loading branch information
superbrothers and stephanmiehe authored May 14, 2022
1 parent 29e775b commit ddb9716
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,25 @@ on:
pull_requests:
types: [opened, edited]
```

### Example including the issue title in the regex target

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

```
name: "Issue Labeler"
on:
issues:
types: [opened, edited]

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
include-title: 1
```
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ inputs:
body-missing-regex-label:
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'
required: false
default: "0"
runs:
using: 'node12'
main: 'lib/main.js'
Expand Down
24 changes: 23 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function run() {
const versionedRegex = new RegExp(core.getInput('versioned-regex', { required: false }));
const notBefore = Date.parse(core.getInput('not-before', { required: false }));
const bodyMissingRegexLabel = core.getInput('body-missing-regex-label', { required: false });
const includeTitle = parseInt(core.getInput('include-title', { required: false }));
const issue_number = getIssueOrPullRequestNumber();
if (issue_number === undefined) {
console.log('Could not get issue or pull request number from context, exiting');
Expand All @@ -51,6 +52,11 @@ function run() {
console.log('Could not get issue or pull request body from context, exiting');
return;
}
const issue_title = getIssueOrPullRequestTitle();
if (issue_title === undefined) {
console.log('Could not get issue or pull request title from context, exiting');
return;
}
// A client to load data from GitHub
const client = new github.GitHub(token);
const addLabel = [];
Expand Down Expand Up @@ -86,8 +92,13 @@ function run() {
}
// Load our regex rules from the configuration path
const labelRegexes = yield getLabelRegexes(client, configPath);
let issueContent = "";
if (includeTitle === 1) {
issueContent += `${issue_title}\n\n`;
}
issueContent += issue_body;
for (const [label, globs] of labelRegexes.entries()) {
if (checkRegexes(issue_body, globs)) {
if (checkRegexes(issueContent, globs)) {
addLabel.push(label);
}
else {
Expand Down Expand Up @@ -131,6 +142,17 @@ function getIssueOrPullRequestBody() {
}
return;
}
function getIssueOrPullRequestTitle() {
const issue = github.context.payload.issue;
if (issue) {
return issue.title;
}
const pull_request = github.context.payload.pull_request;
if (pull_request) {
return pull_request.title;
}
return;
}
function regexifyConfigPath(configPath, version) {
var lastIndex = configPath.lastIndexOf('.');
return `${configPath.substring(0, lastIndex)}-v${version}.yml`;
Expand Down
31 changes: 29 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ async function run() {
const versionedRegex = new RegExp(core.getInput('versioned-regex', { required: false }));
const notBefore = Date.parse(core.getInput('not-before', { required: false }));
const bodyMissingRegexLabel = core.getInput('body-missing-regex-label', { required: false });
const includeTitle = parseInt(core.getInput('include-title', { required: false }));

const issue_number = getIssueOrPullRequestNumber();
if (issue_number === undefined) {
console.log('Could not get issue or pull request number from context, exiting');
Expand All @@ -23,6 +25,12 @@ async function run() {
return;
}

const issue_title = getIssueOrPullRequestTitle();
if (issue_title === undefined) {
console.log('Could not get issue or pull request title from context, exiting');
return;
}

// A client to load data from GitHub
const client = new github.GitHub(token);

Expand All @@ -46,7 +54,6 @@ async function run() {
configPath = regexifyConfigPath(configPath, regexVersion[1])
}


// If the notBefore parameter has been set to a valid timestamp, exit if the current issue was created before notBefore
if (notBefore) {
const issue = client.issues.get({
Expand All @@ -67,8 +74,14 @@ async function run() {
configPath
);

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

for (const [label, globs] of labelRegexes.entries()) {
if (checkRegexes(issue_body, globs)) {
if (checkRegexes(issueContent, globs)) {
addLabel.push(label)
}
else {
Expand Down Expand Up @@ -118,6 +131,20 @@ function getIssueOrPullRequestBody(): string | undefined {
return;
}

function getIssueOrPullRequestTitle(): string | undefined {
const issue = github.context.payload.issue;
if (issue) {
return issue.title;
}

const pull_request = github.context.payload.pull_request;
if (pull_request) {
return pull_request.title;
}

return;
}

function regexifyConfigPath(configPath: string, version: string) {
var lastIndex = configPath.lastIndexOf('.')
return `${configPath.substring(0, lastIndex)}-v${version}.yml`
Expand Down

0 comments on commit ddb9716

Please sign in to comment.