2019-08-04 09:34:59 +08:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as github from '@actions/github';
|
2020-04-15 01:28:00 +08:00
|
|
|
import {Octokit} from '@octokit/rest';
|
2019-08-04 09:34:59 +08:00
|
|
|
|
2019-08-07 22:14:20 +08:00
|
|
|
type Issue = Octokit.IssuesListForRepoResponseItem;
|
2019-08-07 22:36:13 +08:00
|
|
|
type IssueLabel = Octokit.IssuesListForRepoResponseItemLabelsItem;
|
2019-08-07 22:14:20 +08:00
|
|
|
|
2020-04-15 01:28:00 +08:00
|
|
|
interface Args {
|
2019-08-07 04:25:08 +08:00
|
|
|
repoToken: string;
|
|
|
|
staleIssueMessage: string;
|
|
|
|
stalePrMessage: string;
|
|
|
|
daysBeforeStale: number;
|
|
|
|
daysBeforeClose: number;
|
|
|
|
staleIssueLabel: string;
|
2019-08-28 03:58:14 +08:00
|
|
|
exemptIssueLabel: string;
|
2019-08-07 04:25:08 +08:00
|
|
|
stalePrLabel: string;
|
2019-08-28 03:58:14 +08:00
|
|
|
exemptPrLabel: string;
|
2019-09-27 21:33:20 +08:00
|
|
|
onlyLabels: string;
|
2019-08-07 04:25:08 +08:00
|
|
|
operationsPerRun: number;
|
2020-04-15 01:28:00 +08:00
|
|
|
}
|
2019-08-04 09:34:59 +08:00
|
|
|
|
2020-04-15 01:28:00 +08:00
|
|
|
async function run(): Promise<void> {
|
2019-08-04 09:34:59 +08:00
|
|
|
try {
|
|
|
|
const args = getAndValidateArgs();
|
|
|
|
|
2019-08-07 04:25:08 +08:00
|
|
|
const client = new github.GitHub(args.repoToken);
|
|
|
|
await processIssues(client, args, args.operationsPerRun);
|
|
|
|
} catch (error) {
|
|
|
|
core.error(error);
|
|
|
|
core.setFailed(error.message);
|
|
|
|
}
|
|
|
|
}
|
2019-08-04 09:34:59 +08:00
|
|
|
|
2019-08-07 04:25:08 +08:00
|
|
|
async function processIssues(
|
|
|
|
client: github.GitHub,
|
|
|
|
args: Args,
|
|
|
|
operationsLeft: number,
|
|
|
|
page: number = 1
|
|
|
|
): Promise<number> {
|
|
|
|
const issues = await client.issues.listForRepo({
|
|
|
|
owner: github.context.repo.owner,
|
|
|
|
repo: github.context.repo.repo,
|
|
|
|
state: 'open',
|
2019-09-27 21:33:20 +08:00
|
|
|
labels: args.onlyLabels,
|
2019-08-07 04:25:08 +08:00
|
|
|
per_page: 100,
|
2020-04-15 01:28:00 +08:00
|
|
|
page
|
2019-08-07 04:25:08 +08:00
|
|
|
});
|
2019-08-04 09:34:59 +08:00
|
|
|
|
2019-08-07 04:25:08 +08:00
|
|
|
operationsLeft -= 1;
|
2019-08-04 09:34:59 +08:00
|
|
|
|
2019-08-07 04:25:08 +08:00
|
|
|
if (issues.data.length === 0 || operationsLeft === 0) {
|
|
|
|
return operationsLeft;
|
|
|
|
}
|
2019-08-04 09:34:59 +08:00
|
|
|
|
2020-04-15 01:28:00 +08:00
|
|
|
for (const issue of issues.data.values()) {
|
2019-08-07 04:25:08 +08:00
|
|
|
core.debug(`found issue: ${issue.title} last updated ${issue.updated_at}`);
|
2020-04-15 01:28:00 +08:00
|
|
|
const isPr = !!issue.pull_request;
|
2019-08-07 06:26:47 +08:00
|
|
|
|
2020-04-15 01:28:00 +08:00
|
|
|
const staleMessage = isPr ? args.stalePrMessage : args.staleIssueMessage;
|
2019-08-07 06:26:47 +08:00
|
|
|
if (!staleMessage) {
|
2019-08-07 22:36:13 +08:00
|
|
|
core.debug(`skipping ${isPr ? 'pr' : 'issue'} due to empty message`);
|
2019-08-07 06:26:47 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-04-15 01:28:00 +08:00
|
|
|
const staleLabel = isPr ? args.stalePrLabel : args.staleIssueLabel;
|
|
|
|
const exemptLabel = isPr ? args.exemptPrLabel : args.exemptIssueLabel;
|
2019-08-07 04:25:08 +08:00
|
|
|
|
2019-08-28 03:58:14 +08:00
|
|
|
if (exemptLabel && isLabeled(issue, exemptLabel)) {
|
|
|
|
continue;
|
|
|
|
} else if (isLabeled(issue, staleLabel)) {
|
2020-04-15 01:28:00 +08:00
|
|
|
if (
|
|
|
|
args.daysBeforeClose >= 0 &&
|
|
|
|
wasLastUpdatedBefore(issue, args.daysBeforeClose)
|
|
|
|
) {
|
2019-09-05 21:57:15 +08:00
|
|
|
operationsLeft -= await closeIssue(client, issue);
|
|
|
|
} else {
|
2019-08-07 04:25:08 +08:00
|
|
|
continue;
|
2019-08-04 09:34:59 +08:00
|
|
|
}
|
2019-08-07 04:25:08 +08:00
|
|
|
} else if (wasLastUpdatedBefore(issue, args.daysBeforeStale)) {
|
|
|
|
operationsLeft -= await markStale(
|
|
|
|
client,
|
|
|
|
issue,
|
|
|
|
staleMessage,
|
|
|
|
staleLabel
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operationsLeft <= 0) {
|
|
|
|
core.warning(
|
|
|
|
`performed ${args.operationsPerRun} operations, exiting to avoid rate limit`
|
|
|
|
);
|
|
|
|
return 0;
|
2019-08-04 09:34:59 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-07 04:25:08 +08:00
|
|
|
return await processIssues(client, args, operationsLeft, page + 1);
|
2019-08-04 09:34:59 +08:00
|
|
|
}
|
|
|
|
|
2019-08-28 03:58:14 +08:00
|
|
|
function isLabeled(issue: Issue, label: string): boolean {
|
2019-08-07 22:36:13 +08:00
|
|
|
const labelComparer: (l: IssueLabel) => boolean = l =>
|
2019-08-07 22:14:20 +08:00
|
|
|
label.localeCompare(l.name, undefined, {sensitivity: 'accent'}) === 0;
|
2019-08-07 06:29:11 +08:00
|
|
|
return issue.labels.filter(labelComparer).length > 0;
|
2019-08-04 09:34:59 +08:00
|
|
|
}
|
|
|
|
|
2019-08-07 22:36:13 +08:00
|
|
|
function wasLastUpdatedBefore(issue: Issue, num_days: number): boolean {
|
2019-08-20 02:28:41 +08:00
|
|
|
const daysInMillis = 1000 * 60 * 60 * 24 * num_days;
|
2019-08-04 09:34:59 +08:00
|
|
|
const millisSinceLastUpdated =
|
|
|
|
new Date().getTime() - new Date(issue.updated_at).getTime();
|
|
|
|
return millisSinceLastUpdated >= daysInMillis;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function markStale(
|
2019-08-07 04:25:08 +08:00
|
|
|
client: github.GitHub,
|
2019-08-07 22:14:20 +08:00
|
|
|
issue: Issue,
|
2019-08-07 04:25:08 +08:00
|
|
|
staleMessage: string,
|
|
|
|
staleLabel: string
|
|
|
|
): Promise<number> {
|
2019-08-04 09:34:59 +08:00
|
|
|
core.debug(`marking issue${issue.title} as stale`);
|
|
|
|
|
2019-08-07 04:25:08 +08:00
|
|
|
await client.issues.createComment({
|
|
|
|
owner: github.context.repo.owner,
|
|
|
|
repo: github.context.repo.repo,
|
2019-08-04 09:34:59 +08:00
|
|
|
issue_number: issue.number,
|
2019-08-07 04:25:08 +08:00
|
|
|
body: staleMessage
|
2019-08-04 09:34:59 +08:00
|
|
|
});
|
|
|
|
|
2019-08-07 04:25:08 +08:00
|
|
|
await client.issues.addLabels({
|
|
|
|
owner: github.context.repo.owner,
|
|
|
|
repo: github.context.repo.repo,
|
2019-08-04 09:34:59 +08:00
|
|
|
issue_number: issue.number,
|
2019-08-07 04:25:08 +08:00
|
|
|
labels: [staleLabel]
|
2019-08-04 09:34:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return 2; // operations performed
|
|
|
|
}
|
|
|
|
|
|
|
|
async function closeIssue(
|
2019-08-07 04:25:08 +08:00
|
|
|
client: github.GitHub,
|
2019-08-07 22:14:20 +08:00
|
|
|
issue: Issue
|
2019-08-07 04:25:08 +08:00
|
|
|
): Promise<number> {
|
2019-08-04 09:34:59 +08:00
|
|
|
core.debug(`closing issue ${issue.title} for being stale`);
|
|
|
|
|
2019-08-07 04:25:08 +08:00
|
|
|
await client.issues.update({
|
|
|
|
owner: github.context.repo.owner,
|
|
|
|
repo: github.context.repo.repo,
|
2019-08-04 09:34:59 +08:00
|
|
|
issue_number: issue.number,
|
|
|
|
state: 'closed'
|
|
|
|
});
|
|
|
|
|
|
|
|
return 1; // operations performed
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAndValidateArgs(): Args {
|
|
|
|
const args = {
|
2019-08-07 04:25:08 +08:00
|
|
|
repoToken: core.getInput('repo-token', {required: true}),
|
|
|
|
staleIssueMessage: core.getInput('stale-issue-message'),
|
2019-08-07 06:26:47 +08:00
|
|
|
stalePrMessage: core.getInput('stale-pr-message'),
|
2019-08-07 04:25:08 +08:00
|
|
|
daysBeforeStale: parseInt(
|
|
|
|
core.getInput('days-before-stale', {required: true})
|
|
|
|
),
|
|
|
|
daysBeforeClose: parseInt(
|
|
|
|
core.getInput('days-before-close', {required: true})
|
|
|
|
),
|
|
|
|
staleIssueLabel: core.getInput('stale-issue-label', {required: true}),
|
2019-08-28 03:58:14 +08:00
|
|
|
exemptIssueLabel: core.getInput('exempt-issue-label'),
|
2019-08-07 04:25:08 +08:00
|
|
|
stalePrLabel: core.getInput('stale-pr-label', {required: true}),
|
2019-08-28 03:58:14 +08:00
|
|
|
exemptPrLabel: core.getInput('exempt-pr-label'),
|
2019-09-27 21:33:20 +08:00
|
|
|
onlyLabels: core.getInput('only-labels'),
|
2019-08-07 04:25:08 +08:00
|
|
|
operationsPerRun: parseInt(
|
|
|
|
core.getInput('operations-per-run', {required: true})
|
|
|
|
)
|
2019-08-04 09:34:59 +08:00
|
|
|
};
|
|
|
|
|
2020-04-15 01:28:00 +08:00
|
|
|
for (const numberInput of [
|
2019-08-07 04:25:08 +08:00
|
|
|
'days-before-stale',
|
|
|
|
'days-before-close',
|
|
|
|
'operations-per-run'
|
|
|
|
]) {
|
|
|
|
if (isNaN(parseInt(core.getInput(numberInput)))) {
|
2019-08-04 09:34:59 +08:00
|
|
|
throw Error(`input ${numberInput} did not parse to a valid integer`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
run();
|