Move issue closer rules into action code
This commit is contained in:
parent
329912b7b1
commit
21d1c750cc
|
@ -1,5 +1,5 @@
|
||||||
name: Issue matching auto-closer
|
name: Issue matching auto-closer
|
||||||
description: Automatically close issues based on regexs matching.
|
description: Automatically close issues.
|
||||||
author: arkon
|
author: arkon
|
||||||
branding:
|
branding:
|
||||||
icon: alert-circle
|
icon: alert-circle
|
||||||
|
@ -8,15 +8,6 @@ inputs:
|
||||||
repo-token:
|
repo-token:
|
||||||
required: true
|
required: true
|
||||||
description: GitHub token
|
description: GitHub token
|
||||||
type:
|
|
||||||
required: true
|
|
||||||
description: Either "body" or "title", indicating what to run the regex against.
|
|
||||||
regex:
|
|
||||||
required: true
|
|
||||||
description: Regular expression pattern which if matched closes the issue.
|
|
||||||
message:
|
|
||||||
required: true
|
|
||||||
description: Message to post when closing the issue.
|
|
||||||
runs:
|
runs:
|
||||||
using: node12
|
using: node12
|
||||||
main: dist/index.js
|
main: dist/index.js
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "issue-closer",
|
"name": "issue-closer",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "GitHub action to automatically close issues based on regexs matching",
|
"description": "GitHub action to automatically close issues",
|
||||||
"main": "lib/action.js",
|
"main": "lib/action.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"postinstall": "ncc build src/main.ts"
|
"postinstall": "ncc build src/main.ts"
|
||||||
|
|
|
@ -1,71 +1,101 @@
|
||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
import * as github from '@actions/github';
|
import * as github from '@actions/github';
|
||||||
|
|
||||||
|
interface Rule {
|
||||||
|
type: 'title' | 'body';
|
||||||
|
regex: string;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
async function run() {
|
async function run() {
|
||||||
try {
|
try {
|
||||||
const type: string = core.getInput('type', {required: true});
|
// Get client and context
|
||||||
const regex: string = core.getInput('regex', {required: true});
|
const client: github.GitHub = new github.GitHub(
|
||||||
const message: string = core.getInput('message', {required: true});
|
core.getInput('repo-token', {required: true})
|
||||||
|
);
|
||||||
|
const context = github.context;
|
||||||
|
const payload = context.payload;
|
||||||
|
|
||||||
if (type !== 'title' && type !== 'body') {
|
// Do nothing if it's wasn't being opened or it's not an issue
|
||||||
throw new Error(
|
if (payload.action !== 'opened' || !payload.issue) {
|
||||||
'`type` must be either "title" or "body".'
|
return;
|
||||||
);
|
}
|
||||||
|
|
||||||
|
if (!payload.sender) {
|
||||||
|
throw new Error('Internal error, no sender provided by GitHub');
|
||||||
|
}
|
||||||
|
|
||||||
|
const issue: {owner: string; repo: string; number: number} = context.issue;
|
||||||
|
|
||||||
|
const rules: Rule[] = [
|
||||||
|
// No source name or short description provided in title
|
||||||
|
{
|
||||||
|
type: 'title',
|
||||||
|
regex: ".*<(Source Name|short description)>*",
|
||||||
|
message: "You did not fill out the description in the title"
|
||||||
|
},
|
||||||
|
|
||||||
|
// Body acknowledgement section not removed
|
||||||
|
{
|
||||||
|
type: 'body',
|
||||||
|
regex: ".*DELETE THIS SECTION IF YOU HAVE READ AND ACKNOWLEDGED IT.*",
|
||||||
|
message: "The acknowledgment section was not removed"
|
||||||
|
},
|
||||||
|
|
||||||
|
// Body requested information not filled out
|
||||||
|
{
|
||||||
|
type: 'body',
|
||||||
|
regex: ".*\\* (Tachiyomi version|Android version|Device|Name|Link|Extension version): \\?.*",
|
||||||
|
message: "The requested information was not filled out"
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const results = rules
|
||||||
|
.map(rule => {
|
||||||
|
const text = rule.type === 'title' ? payload?.issue?.title : payload?.issue?.body;
|
||||||
|
const regexMatches: boolean = check(rule.regex, text);
|
||||||
|
|
||||||
|
if (regexMatches) {
|
||||||
|
return rule.message;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.filter(Boolean);
|
||||||
|
|
||||||
|
if (results.length > 0) {
|
||||||
|
// Comment and close
|
||||||
|
const message = ['@${issue.user.login} this issue was automatically closed because:\n', ...results].join('\n- ');
|
||||||
|
|
||||||
|
await client.issues.createComment({
|
||||||
|
owner: issue.owner,
|
||||||
|
repo: issue.repo,
|
||||||
|
issue_number: issue.number,
|
||||||
|
body: evalTemplate(message, payload)
|
||||||
|
});
|
||||||
|
|
||||||
|
await client.issues.update({
|
||||||
|
owner: issue.owner,
|
||||||
|
repo: issue.repo,
|
||||||
|
issue_number: issue.number,
|
||||||
|
state: 'closed'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
core.setFailed(error.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get client and context
|
|
||||||
const client: github.GitHub = new github.GitHub(
|
|
||||||
core.getInput('repo-token', {required: true})
|
|
||||||
);
|
|
||||||
const context = github.context;
|
|
||||||
const payload = context.payload;
|
|
||||||
|
|
||||||
// Do nothing if it's wasn't being opened or it's not an issue
|
|
||||||
if (payload.action !== 'opened' || !payload.issue) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!payload.sender) {
|
|
||||||
throw new Error('Internal error, no sender provided by GitHub');
|
|
||||||
}
|
|
||||||
|
|
||||||
const issue: {owner: string; repo: string; number: number} = context.issue;
|
|
||||||
|
|
||||||
const text = type === 'title' ? payload?.issue?.title : payload?.issue?.body;
|
|
||||||
const regexMatches: boolean = check(regex, text);
|
|
||||||
|
|
||||||
if (regexMatches) {
|
|
||||||
// Comment and close
|
|
||||||
await client.issues.createComment({
|
|
||||||
owner: issue.owner,
|
|
||||||
repo: issue.repo,
|
|
||||||
issue_number: issue.number,
|
|
||||||
body: evalTemplate(message, payload)
|
|
||||||
});
|
|
||||||
await client.issues.update({
|
|
||||||
owner: issue.owner,
|
|
||||||
repo: issue.repo,
|
|
||||||
issue_number: issue.number,
|
|
||||||
state: 'closed'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
core.setFailed(error.message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function check(patternString: string, text: string | undefined): boolean {
|
function check(patternString: string, text: string | undefined): boolean {
|
||||||
const pattern: RegExp = new RegExp(patternString);
|
const pattern: RegExp = new RegExp(patternString);
|
||||||
|
|
||||||
if (text?.match(pattern)) {
|
if (text?.match(pattern)) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function evalTemplate(template: string, params: any) {
|
function evalTemplate(template: string, params: any) {
|
||||||
return Function(...Object.keys(params), `return \`${template}\``)(...Object.values(params));
|
return Function(...Object.keys(params), `return \`${template}\``)(...Object.values(params));
|
||||||
}
|
}
|
||||||
|
|
||||||
run();
|
run();
|
||||||
|
|
|
@ -12,25 +12,7 @@ jobs:
|
||||||
node-version: 14
|
node-version: 14
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: cd ./.github/actions/issue-closer && npm install
|
run: cd ./.github/actions/issue-closer && npm install
|
||||||
|
- name: Autoclose issue
|
||||||
- name: Autoclose when no source name or short description provided in title
|
|
||||||
uses: ./.github/actions/issue-closer
|
uses: ./.github/actions/issue-closer
|
||||||
with:
|
with:
|
||||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
type: title
|
|
||||||
regex: ".*<(Source Name|short description)>*"
|
|
||||||
message: "@${issue.user.login} this issue was automatically closed because you did not fill out the description in the title."
|
|
||||||
- name: Autoclose when body acknowledgement section not removed
|
|
||||||
uses: ./.github/actions/issue-closer
|
|
||||||
with:
|
|
||||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
type: body
|
|
||||||
regex: ".*DELETE THIS SECTION IF YOU HAVE READ AND ACKNOWLEDGED IT.*"
|
|
||||||
message: "@${issue.user.login} this issue was automatically closed because the acknowledgment section was not removed."
|
|
||||||
- name: Autoclose when body requested information not filled out
|
|
||||||
uses: ./.github/actions/issue-closer
|
|
||||||
with:
|
|
||||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
type: body
|
|
||||||
regex: ".*\\* (Tachiyomi version|Android version|Device|Name|Link|Extension version): \\?.*"
|
|
||||||
message: "@${issue.user.login} this issue was automatically closed because the requested information was not filled out."
|
|
Loading…
Reference in New Issue