Move issue closer rules into action code

This commit is contained in:
arkon 2020-08-28 23:01:40 -04:00
parent 329912b7b1
commit 21d1c750cc
4 changed files with 89 additions and 86 deletions

View File

@ -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

View File

@ -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"

View File

@ -1,18 +1,14 @@
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});
const regex: string = core.getInput('regex', {required: true});
const message: string = core.getInput('message', {required: true});
if (type !== 'title' && type !== 'body') {
throw new Error(
'`type` must be either "title" or "body".'
);
}
// Get client and context // Get client and context
const client: github.GitHub = new github.GitHub( const client: github.GitHub = new github.GitHub(
core.getInput('repo-token', {required: true}) core.getInput('repo-token', {required: true})
@ -31,17 +27,51 @@ async function run() {
const issue: {owner: string; repo: string; number: number} = context.issue; const issue: {owner: string; repo: string; number: number} = context.issue;
const text = type === 'title' ? payload?.issue?.title : payload?.issue?.body; const rules: Rule[] = [
const regexMatches: boolean = check(regex, text); // 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) { if (regexMatches) {
return rule.message;
}
})
.filter(Boolean);
if (results.length > 0) {
// Comment and close // Comment and close
const message = ['@${issue.user.login} this issue was automatically closed because:\n', ...results].join('\n- ');
await client.issues.createComment({ await client.issues.createComment({
owner: issue.owner, owner: issue.owner,
repo: issue.repo, repo: issue.repo,
issue_number: issue.number, issue_number: issue.number,
body: evalTemplate(message, payload) body: evalTemplate(message, payload)
}); });
await client.issues.update({ await client.issues.update({
owner: issue.owner, owner: issue.owner,
repo: issue.repo, repo: issue.repo,

View File

@ -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."