Retrieve Issues and PRs using a search filter

_Context:_
Normally, all open issues/PRs in the repository that's running this
action are retrieved then all the label, assignee, milestone, etc. criteria
you provide to the action are applied. Unfortunately, this limits the action
to only those issues and PRs in this repository.  It also prevents operating
on only a subset of issues or PRs that can't be filtered by the existing
action criteria.  A good example of this are PRs that are in a
`review:changes_requested` state.  While additional filtering criteria could
be added to the action, it would result in additional callbacks to GitHub
which could trigger rate-limits to be applied.

_Purpose:_
This option is an array of one or more standard [GitHub Issues and
Pull Requests search queries]
(https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests)
which will be used to retrieve the set of issues/PRs to test and take action
on. These queries will be used in place of the default retrieval of all open
issues and PRs for the context's owner/repo.  It can be used to expand or
limit the set of issues and PRs operated on beyond what is retuned by the
standard query.  When the retrieval is complete, all the other label,
assignee, milestone, etc. criteria will be applied.

You may also use this option to improve performance when you have a large number
of open issues or PRs but only a small subset might be eligible for action. For
instance, let's say you have 1000 open issues in your repository but only those
with label `auto-closable` should ever be automatically marked as stale or
closed.  By default, this action would retrieve all 1000 issues _and all open
PRs_ and iterate over them looking for the label you specified in the
`only-issues-label` parameter. If you use the `only-matching-filter` parameter
with `repo:myorg/myrepo is:issue label:auto-closable` this would limit the
download to just those issues you  _know_ should  have further criteria
applied.

_Syntax:_

```
only-matching-filter: [ "<query_string>", ... ]
```

Or if there's only one query string...

```
only-matching-filter: '<query_string>'
```

_Examples:_
To operate only on the open PRs in your organization that have a review state of
`changes_requested`:

```
only-matching-filter: 'org:myorg is:pr is:open review:changes_requested'
```

Since there's only one query specified, you can omit the array syntax and just
specify the string directly.

GitHub only allows boolean logic and grouping in a Code Searches not in Issues
and Pull Requests searches so there's no way to do an "OR" operation but you
can get around this to a limited degree by specifying multiple search queries
in the form of a string array. Each query is run separately and the results are
accumulated and duplicates removed before any further processing is done.

To retrieve all of the open PRs in your organization that have a review state of
`changes_requested` or a label named `submitter-action-required`, you'd use:

```
only-matching-filter: '[ "org:myorg is:pr is:open review:changes_requested", "org:myorg is:pr is:open label:submitter-action-required" ]'
```

_Notes:_
* Each query is checked to ensure it contains an `owner:`, `org:`, `user:` or
`repo:` search term.  If it doesn't, the search will automatically be scoped
to the owner and repository in the context to prevent accidental global
searches.  If the request doesn't already contain an `is:open` search term,
it will automatically be added as well.
* If using the array form, the value of this parameter MUST be valid JSON
which means using double quotes around each query string, not single quotes.

Default value: '[]'

Resolves: #1143
This commit is contained in:
George Joseph 2024-03-14 13:16:24 -06:00
parent 3f3b0175e8
commit 1b80269ed4
15 changed files with 344 additions and 50 deletions

View File

@ -60,6 +60,7 @@ Every argument is optional.
| [close-issue-reason](#close-issue-reason) | Reason to use when closing issues | `not_planned` |
| [stale-pr-label](#stale-pr-label) | Label to apply on staled PRs | `Stale` |
| [close-pr-label](#close-pr-label) | Label to apply on closed PRs | |
| [only-matching-filter](#only-matching-filter) | Only issues/PRs matching the search filter(s) will be retrieved and tested | `[]` |
| [exempt-issue-labels](#exempt-issue-labels) | Labels on issues exempted from stale | |
| [exempt-pr-labels](#exempt-pr-labels) | Labels on PRs exempted from stale | |
| [only-labels](#only-labels) | Only issues/PRs with ALL these labels are checked | |
@ -258,6 +259,51 @@ It will be automatically removed if the pull requests are no longer closed nor l
Default value: unset
Required Permission: `pull-requests: write`
#### only-matching-filter
_Context:_
Normally, all open issues/PRs in the repository that's running this action are retrieved then all the label, assignee, milestone, etc. criteria you provide to the action are applied. Unfortunately, this limits the action to only those issues and PRs in this repository. It also prevents operating on only a subset of issues or PRs that can't be filtered by the existing action criteria. A good example of this are PRs that are in a `review:changes_requested` state. While additional filtering criteria could be added to the action, it would result in additional callbacks to GitHub which could trigger rate-limits to be applied.
_Purpose:_
This option is an array of one or more standard [GitHub Issues and Pull Requests search queries](https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests) which will be used to retrieve the set of issues/PRs to test and take action on. These queries will be used in place of the default retrieval of all open issues and PRs for the context's owner/repo. It can be used to expand or limit the set of issues and PRs operated on beyond what is retuned by the standard query. When the retrieval is complete, all the other label, assignee, milestone, etc. criteria will be applied.
You may also use this option to improve performance when you have a large number of open issues or PRs but only a small subset might be eligible for action. For instance, let's say you have 1000 open issues in your repository but only those with label `auto-closable` should ever be automatically marked as stale or closed. By default, this action would retrieve all 1000 issues _and all open PRs_ and iterate over them looking for the label you specified in the `only-issues-label` parameter. If you use the `only-matching-filter` parameter with `repo:myorg/myrepo is:issue label:auto-closable` this would limit the download to just those issues you _know_ should have further criteria applied.
_Syntax:_
```
only-matching-filter: [ "<query_string>", ... ]
```
Or if there's only one query string...
```
only-matching-filter: '<query_string>'
```
_Examples:_
To operate only on the open PRs in your organization that have a review state of `changes_requested`:
```
only-matching-filter: 'org:myorg is:pr is:open review:changes_requested'
```
Since there's only one query specified, you can omit the array syntax and just specify the string directly.
GitHub only allows boolean logic and grouping in a Code Searches not in Issues and Pull Requests searches so there's no way to do an "OR" operation but you can get around this to a limited degree by specifying multiple search queries in the form of a string array. Each query is run separately and the results are accumulated and duplicates removed before any further processing is done.
To retrieve all of the open PRs in your organization that have a review state of `changes_requested` or a label named `submitter-action-required`, you'd use:
```
only-matching-filter: '[ "org:myorg is:pr is:open review:changes_requested", "org:myorg is:pr is:open label:submitter-action-required" ]'
```
_Notes:_
* Each query is checked to ensure it contains an `owner:`, `org:`, `user:` or `repo:` search term. If it doesn't, the search will automatically be scoped to the owner and repository in the context to prevent accidental global searches. If the request doesn't already contain an `is:open` search term, it will automatically be added as well.
* If using the array form, the value of this parameter MUST be valid JSON which means using double quotes around each query string, not single quotes.
Default value: '[]'
#### exempt-issue-labels
Comma separated list of labels that can be assigned to issues to exclude them from being marked as stale

View File

@ -19,6 +19,7 @@ export const DefaultProcessorOptions: IIssuesProcessorOptions = Object.freeze({
exemptIssueLabels: '',
stalePrLabel: 'Stale',
closePrLabel: '',
onlyMatchingFilter: [],
exemptPrLabels: '',
onlyLabels: '',
onlyIssueLabels: '',

View File

@ -39,6 +39,7 @@ export function generateIssue(
login: assignee,
type: 'User'
};
})
}),
repository_url: 'https://api.github.com/repos/dummy/dummy'
});
}

View File

@ -45,6 +45,10 @@ inputs:
close-issue-label:
description: 'The label to apply when an issue is closed.'
required: false
only-matching-filter:
description: 'Only issues/PRs matching the search filter(s) will be retrieved and tested'
default: '[]'
required: false
exempt-issue-labels:
description: 'The labels that mean an issue is exempt from being marked stale. Separate multiple labels with commas (eg. "label1,label2").'
default: ''

162
dist/index.js vendored
View File

@ -272,6 +272,7 @@ exports.Issue = void 0;
const is_labeled_1 = __nccwpck_require__(6792);
const is_pull_request_1 = __nccwpck_require__(5400);
const operations_1 = __nccwpck_require__(7957);
const owner_repo_1 = __nccwpck_require__(6226);
class Issue {
constructor(options, issue) {
this.operations = new operations_1.Operations();
@ -287,8 +288,10 @@ class Issue {
this.locked = issue.locked;
this.milestone = issue.milestone;
this.assignees = issue.assignees || [];
this.repository_url = issue.repository_url;
this.isStale = (0, is_labeled_1.isLabeled)(this, this.staleLabel);
this.markedStaleThisRun = false;
this.owner_repo = new owner_repo_1.OwnerRepo(issue.repository_url || '');
}
get isPullRequest() {
return (0, is_pull_request_1.isPullRequest)(this);
@ -426,7 +429,7 @@ class IssuesProcessor {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
// get the next batch of issues
const issues = yield this.getIssues(page);
const issues = yield this.getIssuesWrapper(page);
if (issues.length <= 0) {
this._logger.info(logger_service_1.LoggerService.green(`No more issues found to process. Exiting...`));
(_a = this.statistics) === null || _a === void 0 ? void 0 : _a.setOperationsCount(this.operations.getConsumedOperationsCount()).logStats();
@ -659,8 +662,8 @@ class IssuesProcessor {
this._consumeIssueOperation(issue);
(_a = this.statistics) === null || _a === void 0 ? void 0 : _a.incrementFetchedItemsCommentsCount();
const comments = yield this.client.rest.issues.listComments({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
issue_number: issue.number,
since: sinceDate
});
@ -687,6 +690,7 @@ class IssuesProcessor {
page
});
(_a = this.statistics) === null || _a === void 0 ? void 0 : _a.incrementFetchedItemsCount(issueResult.data.length);
this._logger.info(logger_service_1.LoggerService.green(`Retrieved ${issueResult.data.length} issues/PRs for repo ${github_1.context.repo.owner}/${github_1.context.repo.repo}`));
return issueResult.data.map((issue) => new issue_1.Issue(this.options, issue));
}
catch (error) {
@ -694,6 +698,47 @@ class IssuesProcessor {
}
});
}
// grab issues and/or prs from github in batches of 100 using search filter
getIssuesByFilter(page, search) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
this.operations.consumeOperation();
const issueResult = yield this.client.rest.search.issuesAndPullRequests({
q: search,
per_page: 100,
direction: this.options.ascending ? 'asc' : 'desc',
page
});
(_a = this.statistics) === null || _a === void 0 ? void 0 : _a.incrementFetchedItemsCount(issueResult.data.total_count);
this._logger.info(logger_service_1.LoggerService.green(`Retrieved ${issueResult.data.total_count} issues/PRs for search '${search}'`));
return issueResult.data.items.map((issue) => new issue_1.Issue(this.options, issue));
}
catch (error) {
throw Error(`Getting issues was blocked by the error: ${error.message}`);
}
});
}
_removeDupIssues(issues) {
return issues.reduce(function (a, b) {
if (!a.find(o => o.number == b.number))
a.push(b);
return a;
}, []);
}
getIssuesWrapper(page) {
return __awaiter(this, void 0, void 0, function* () {
if (this.options.onlyMatchingFilter.length == 0) {
return this.getIssues(page);
}
const results = [];
for (const term of this.options.onlyMatchingFilter) {
const r = yield this.getIssuesByFilter(page, term);
results.push(...r);
}
return this._removeDupIssues(results);
});
}
// returns the creation date of a given label on an issue (or nothing if no label existed)
///see https://developer.github.com/v3/activity/events/
getLabelCreationDate(issue, label) {
@ -704,8 +749,8 @@ class IssuesProcessor {
this._consumeIssueOperation(issue);
(_a = this.statistics) === null || _a === void 0 ? void 0 : _a.incrementFetchedItemsEventsCount();
const options = this.client.rest.issues.listEvents.endpoint.merge({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
per_page: 100,
issue_number: issue.number
});
@ -728,8 +773,8 @@ class IssuesProcessor {
this._consumeIssueOperation(issue);
(_a = this.statistics) === null || _a === void 0 ? void 0 : _a.incrementFetchedPullRequestsCount();
const pullRequest = yield this.client.rest.pulls.get({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
pull_number: issue.number
});
return pullRequest.data;
@ -848,8 +893,8 @@ class IssuesProcessor {
(_a = this.statistics) === null || _a === void 0 ? void 0 : _a.incrementAddedItemsComment(issue);
if (!this.options.debugOnly) {
yield this.client.rest.issues.createComment({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
issue_number: issue.number,
body: staleMessage
});
@ -865,8 +910,8 @@ class IssuesProcessor {
(_c = this.statistics) === null || _c === void 0 ? void 0 : _c.incrementStaleItemsCount(issue);
if (!this.options.debugOnly) {
yield this.client.rest.issues.addLabels({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
issue_number: issue.number,
labels: [staleLabel]
});
@ -891,8 +936,8 @@ class IssuesProcessor {
this.addedCloseCommentIssues.push(issue);
if (!this.options.debugOnly) {
yield this.client.rest.issues.createComment({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
issue_number: issue.number,
body: closeMessage
});
@ -908,8 +953,8 @@ class IssuesProcessor {
(_b = this.statistics) === null || _b === void 0 ? void 0 : _b.incrementAddedItemsLabel(issue);
if (!this.options.debugOnly) {
yield this.client.rest.issues.addLabels({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
issue_number: issue.number,
labels: [closeLabel]
});
@ -924,8 +969,8 @@ class IssuesProcessor {
(_c = this.statistics) === null || _c === void 0 ? void 0 : _c.incrementClosedItemsCount(issue);
if (!this.options.debugOnly) {
yield this.client.rest.issues.update({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
issue_number: issue.number,
state: 'closed',
state_reason: this.options.closeIssueReason || undefined
@ -955,15 +1000,15 @@ class IssuesProcessor {
const branch = pullRequest.head.ref;
if (pullRequest.head.repo === null ||
pullRequest.head.repo.full_name ===
`${github_1.context.repo.owner}/${github_1.context.repo.repo}`) {
`${issue.owner_repo.owner}/${issue.owner_repo.repo}`) {
issueLogger.info(`Deleting the branch "${logger_service_1.LoggerService.cyan(branch)}" from closed $$type`);
try {
this._consumeIssueOperation(issue);
(_a = this.statistics) === null || _a === void 0 ? void 0 : _a.incrementDeletedBranchesCount();
if (!this.options.debugOnly) {
yield this.client.rest.git.deleteRef({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
ref: `heads/${branch}`
});
}
@ -989,8 +1034,8 @@ class IssuesProcessor {
(_a = this.statistics) === null || _a === void 0 ? void 0 : _a.incrementDeletedItemsLabelsCount(issue);
if (!this.options.debugOnly) {
yield this.client.rest.issues.removeLabel({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
issue_number: issue.number,
name: label
});
@ -1089,8 +1134,8 @@ class IssuesProcessor {
(_a = this.statistics) === null || _a === void 0 ? void 0 : _a.incrementAddedItemsLabel(issue);
if (!this.options.debugOnly) {
yield this.client.rest.issues.addLabels({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
issue_number: issue.number,
labels: labelsToAdd
});
@ -1499,6 +1544,31 @@ class Operations {
exports.Operations = Operations;
/***/ }),
/***/ 6226:
/***/ ((__unused_webpack_module, exports) => {
"use strict";
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.OwnerRepo = void 0;
class OwnerRepo {
constructor(repo_url) {
const m = repo_url.match(/.*\/([^/]+)\/(.+)$/);
if (!m) {
this.owner = '';
this.repo = '';
}
else {
this.owner = m[1];
this.repo = m[2];
}
}
}
exports.OwnerRepo = OwnerRepo;
/***/ }),
/***/ 7069:
@ -2185,6 +2255,7 @@ var Option;
Option["DaysBeforePrClose"] = "days-before-pr-close";
Option["StaleIssueLabel"] = "stale-issue-label";
Option["CloseIssueLabel"] = "close-issue-label";
Option["OnlyMatchingFilter"] = "only-matching-filter";
Option["ExemptIssueLabels"] = "exempt-issue-labels";
Option["StalePrLabel"] = "stale-pr-label";
Option["ClosePrLabel"] = "close-pr-label";
@ -2483,6 +2554,7 @@ const core = __importStar(__nccwpck_require__(2186));
const issues_processor_1 = __nccwpck_require__(3292);
const is_valid_date_1 = __nccwpck_require__(891);
const state_service_1 = __nccwpck_require__(6330);
const github_1 = __nccwpck_require__(5438);
function _run() {
return __awaiter(this, void 0, void 0, function* () {
try {
@ -2526,6 +2598,7 @@ function _getAndValidateArgs() {
daysBeforePrClose: parseInt(core.getInput('days-before-pr-close')),
staleIssueLabel: core.getInput('stale-issue-label', { required: true }),
closeIssueLabel: core.getInput('close-issue-label'),
onlyMatchingFilter: _toStringArray('only-matching-filter'),
exemptIssueLabels: core.getInput('exempt-issue-labels'),
stalePrLabel: core.getInput('stale-pr-label', { required: true }),
closePrLabel: core.getInput('close-pr-label'),
@ -2569,6 +2642,25 @@ function _getAndValidateArgs() {
closeIssueReason: core.getInput('close-issue-reason'),
includeOnlyAssigned: core.getInput('include-only-assigned') === 'true'
};
/*
* When using onlyMatchingFilter, we don't want an accidental search of
* all of GitHub so we make sure each filter in the list has at least
* one of repo: owner: org: or user:. If not, we'll set repo: to the
* current owner/repo.
*
* We'll also include is:open if it wasn't already specified.
*/
const new_omf = [];
for (let term of args.onlyMatchingFilter) {
if (term.search(/repo:|owner:|org:|user:/) < 0) {
term = `repo:${github_1.context.repo.owner}/${github_1.context.repo.repo} ${term}`;
}
if (term.search(/is:open/) < 0) {
term += ' is:open';
}
new_omf.push(term);
}
args.onlyMatchingFilter = new_omf;
for (const numberInput of ['days-before-stale']) {
if (isNaN(parseFloat(core.getInput(numberInput)))) {
const errorMessage = `Option "${numberInput}" did not parse to a valid float`;
@ -2628,6 +2720,28 @@ function _toOptionalBoolean(argumentName) {
}
return undefined;
}
/**
* @description
* From an argument name, get the value as an optional string array
* This is very useful for all the arguments that override others
* It will allow us to easily use the original one when the return value is `undefined`
*
* @param {Readonly<string>} argumentName The name of the argument to check
*
* @returns {string[]} The value matching the given argument name
*/
function _toStringArray(argumentName) {
const val = core.getInput(argumentName);
if (!val) {
return [];
}
try {
return JSON.parse(val);
}
catch (err) {
return [val];
}
}
void _run();

View File

@ -2,6 +2,7 @@ import {IUserAssignee} from '../interfaces/assignee';
import {IIssue} from '../interfaces/issue';
import {IIssuesProcessorOptions} from '../interfaces/issues-processor-options';
import {ILabel} from '../interfaces/label';
import {IOwnerRepo} from '../interfaces/owner-repo';
import {IMilestone} from '../interfaces/milestone';
import {Issue} from './issue';
@ -29,6 +30,7 @@ describe('Issue', (): void => {
exemptPrLabels: '',
onlyLabels: '',
onlyIssueLabels: '',
onlyMatchingFilter: [],
onlyPrLabels: '',
anyOfLabels: '',
anyOfIssueLabels: '',
@ -88,7 +90,8 @@ describe('Issue', (): void => {
login: 'dummy-login',
type: 'User'
}
]
],
repository_url: 'https://api.github.com/repos/dummy/dummy'
};
issue = new Issue(optionsInterface, issueInterface);
});

View File

@ -4,9 +4,11 @@ import {Assignee} from '../interfaces/assignee';
import {IIssue, OctokitIssue} from '../interfaces/issue';
import {IIssuesProcessorOptions} from '../interfaces/issues-processor-options';
import {ILabel} from '../interfaces/label';
import {IOwnerRepo} from '../interfaces/owner-repo';
import {IMilestone} from '../interfaces/milestone';
import {IsoDateString} from '../types/iso-date-string';
import {Operations} from './operations';
import {OwnerRepo} from './owner-repo';
export class Issue implements IIssue {
readonly title: string;
@ -20,8 +22,10 @@ export class Issue implements IIssue {
readonly locked: boolean;
readonly milestone?: IMilestone | null;
readonly assignees: Assignee[];
readonly repository_url?: string;
isStale: boolean;
markedStaleThisRun: boolean;
readonly owner_repo: IOwnerRepo;
operations = new Operations();
private readonly _options: IIssuesProcessorOptions;
@ -41,8 +45,10 @@ export class Issue implements IIssue {
this.locked = issue.locked;
this.milestone = issue.milestone;
this.assignees = issue.assignees || [];
this.repository_url = issue.repository_url;
this.isStale = isLabeled(this, this.staleLabel);
this.markedStaleThisRun = false;
this.owner_repo = new OwnerRepo(issue.repository_url || '');
}
get isPullRequest(): boolean {

View File

@ -106,7 +106,7 @@ export class IssuesProcessor {
async processIssues(page: Readonly<number> = 1): Promise<number> {
// get the next batch of issues
const issues: Issue[] = await this.getIssues(page);
const issues: Issue[] = await this.getIssuesWrapper(page);
if (issues.length <= 0) {
this._logger.info(
@ -549,8 +549,8 @@ export class IssuesProcessor {
this._consumeIssueOperation(issue);
this.statistics?.incrementFetchedItemsCommentsCount();
const comments = await this.client.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
issue_number: issue.number,
since: sinceDate
});
@ -574,6 +574,11 @@ export class IssuesProcessor {
page
});
this.statistics?.incrementFetchedItemsCount(issueResult.data.length);
this._logger.info(
LoggerService.green(
`Retrieved ${issueResult.data.length} issues/PRs for repo ${context.repo.owner}/${context.repo.repo}`
)
);
return issueResult.data.map(
(issue): Issue =>
@ -584,6 +589,51 @@ export class IssuesProcessor {
}
}
// grab issues and/or prs from github in batches of 100 using search filter
async getIssuesByFilter(page: number, search: string): Promise<Issue[]> {
try {
this.operations.consumeOperation();
const issueResult = await this.client.rest.search.issuesAndPullRequests({
q: search,
per_page: 100,
direction: this.options.ascending ? 'asc' : 'desc',
page
});
this.statistics?.incrementFetchedItemsCount(issueResult.data.total_count);
this._logger.info(
LoggerService.green(
`Retrieved ${issueResult.data.total_count} issues/PRs for search '${search}'`
)
);
return issueResult.data.items.map(
(issue): Issue =>
new Issue(this.options, issue as Readonly<OctokitIssue>)
);
} catch (error) {
throw Error(`Getting issues was blocked by the error: ${error.message}`);
}
}
private _removeDupIssues(issues: Issue[]): Issue[] {
return issues.reduce(function (a: Issue[], b: Issue) {
if (!a.find(o => o.number == b.number)) a.push(b);
return a;
}, []);
}
async getIssuesWrapper(page: number): Promise<Issue[]> {
if (this.options.onlyMatchingFilter.length == 0) {
return this.getIssues(page);
}
const results: Issue[] = [];
for (const term of this.options.onlyMatchingFilter) {
const r: Issue[] = await this.getIssuesByFilter(page, term);
results.push(...r);
}
return this._removeDupIssues(results);
}
// returns the creation date of a given label on an issue (or nothing if no label existed)
///see https://developer.github.com/v3/activity/events/
async getLabelCreationDate(
@ -597,8 +647,8 @@ export class IssuesProcessor {
this._consumeIssueOperation(issue);
this.statistics?.incrementFetchedItemsEventsCount();
const options = this.client.rest.issues.listEvents.endpoint.merge({
owner: context.repo.owner,
repo: context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
per_page: 100,
issue_number: issue.number
});
@ -628,8 +678,8 @@ export class IssuesProcessor {
this.statistics?.incrementFetchedPullRequestsCount();
const pullRequest = await this.client.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
pull_number: issue.number
});
@ -848,8 +898,8 @@ export class IssuesProcessor {
if (!this.options.debugOnly) {
await this.client.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
issue_number: issue.number,
body: staleMessage
});
@ -866,8 +916,8 @@ export class IssuesProcessor {
if (!this.options.debugOnly) {
await this.client.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
issue_number: issue.number,
labels: [staleLabel]
});
@ -896,8 +946,8 @@ export class IssuesProcessor {
if (!this.options.debugOnly) {
await this.client.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
issue_number: issue.number,
body: closeMessage
});
@ -914,8 +964,8 @@ export class IssuesProcessor {
if (!this.options.debugOnly) {
await this.client.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
issue_number: issue.number,
labels: [closeLabel]
});
@ -931,8 +981,8 @@ export class IssuesProcessor {
if (!this.options.debugOnly) {
await this.client.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
issue_number: issue.number,
state: 'closed',
state_reason: this.options.closeIssueReason || undefined
@ -968,7 +1018,7 @@ export class IssuesProcessor {
if (
pullRequest.head.repo === null ||
pullRequest.head.repo.full_name ===
`${context.repo.owner}/${context.repo.repo}`
`${issue.owner_repo.owner}/${issue.owner_repo.repo}`
) {
issueLogger.info(
`Deleting the branch "${LoggerService.cyan(branch)}" from closed $$type`
@ -980,8 +1030,8 @@ export class IssuesProcessor {
if (!this.options.debugOnly) {
await this.client.rest.git.deleteRef({
owner: context.repo.owner,
repo: context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
ref: `heads/${branch}`
});
}
@ -1024,8 +1074,8 @@ export class IssuesProcessor {
if (!this.options.debugOnly) {
await this.client.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
issue_number: issue.number,
name: label
});
@ -1162,8 +1212,8 @@ export class IssuesProcessor {
this.statistics?.incrementAddedItemsLabel(issue);
if (!this.options.debugOnly) {
await this.client.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
owner: issue.owner_repo.owner,
repo: issue.owner_repo.repo,
issue_number: issue.number,
labels: labelsToAdd
});

17
src/classes/owner-repo.ts Normal file
View File

@ -0,0 +1,17 @@
import {IOwnerRepo} from '../interfaces/owner-repo';
export class OwnerRepo implements IOwnerRepo {
readonly owner: string;
readonly repo: string;
constructor(repo_url: string) {
const m = repo_url.match(/.*\/([^/]+)\/(.+)$/);
if (!m) {
this.owner = '';
this.repo = '';
} else {
this.owner = m[1];
this.repo = m[2];
}
}
}

View File

@ -12,6 +12,7 @@ export enum Option {
DaysBeforePrClose = 'days-before-pr-close',
StaleIssueLabel = 'stale-issue-label',
CloseIssueLabel = 'close-issue-label',
OnlyMatchingFilter = 'only-matching-filter',
ExemptIssueLabels = 'exempt-issue-labels',
StalePrLabel = 'stale-pr-label',
ClosePrLabel = 'close-pr-label',

View File

@ -3,6 +3,7 @@ import {Assignee} from './assignee';
import {ILabel} from './label';
import {IMilestone} from './milestone';
import {components} from '@octokit/openapi-types';
export interface IIssue {
title: string;
number: number;
@ -15,6 +16,7 @@ export interface IIssue {
locked: boolean;
milestone?: IMilestone | null;
assignees?: Assignee[] | null;
repository_url?: string;
}
export type OctokitIssue = components['schemas']['issue'];

View File

@ -14,6 +14,7 @@ export interface IIssuesProcessorOptions {
daysBeforePrClose: number; // Could be NaN
staleIssueLabel: string;
closeIssueLabel: string;
onlyMatchingFilter: string[];
exemptIssueLabels: string;
stalePrLabel: string;
closePrLabel: string;

View File

@ -0,0 +1,4 @@
export interface IOwnerRepo {
owner: string;
repo: string;
}

View File

@ -4,6 +4,7 @@ import {isValidDate} from './functions/dates/is-valid-date';
import {IIssuesProcessorOptions} from './interfaces/issues-processor-options';
import {Issue} from './classes/issue';
import {getStateInstance} from './services/state.service';
import {context} from '@actions/github';
async function _run(): Promise<void> {
try {
@ -73,6 +74,7 @@ function _getAndValidateArgs(): IIssuesProcessorOptions {
daysBeforePrClose: parseInt(core.getInput('days-before-pr-close')),
staleIssueLabel: core.getInput('stale-issue-label', {required: true}),
closeIssueLabel: core.getInput('close-issue-label'),
onlyMatchingFilter: _toStringArray('only-matching-filter'),
exemptIssueLabels: core.getInput('exempt-issue-labels'),
stalePrLabel: core.getInput('stale-pr-label', {required: true}),
closePrLabel: core.getInput('close-pr-label'),
@ -126,6 +128,26 @@ function _getAndValidateArgs(): IIssuesProcessorOptions {
includeOnlyAssigned: core.getInput('include-only-assigned') === 'true'
};
/*
* When using onlyMatchingFilter, we don't want an accidental search of
* all of GitHub so we make sure each filter in the list has at least
* one of repo: owner: org: or user:. If not, we'll set repo: to the
* current owner/repo.
*
* We'll also include is:open if it wasn't already specified.
*/
const new_omf: string[] = [];
for (let term of args.onlyMatchingFilter) {
if (term.search(/repo:|owner:|org:|user:/) < 0) {
term = `repo:${context.repo.owner}/${context.repo.repo} ${term}`;
}
if (term.search(/is:open/) < 0) {
term += ' is:open';
}
new_omf.push(term);
}
args.onlyMatchingFilter = new_omf;
for (const numberInput of ['days-before-stale']) {
if (isNaN(parseFloat(core.getInput(numberInput)))) {
const errorMessage = `Option "${numberInput}" did not parse to a valid float`;
@ -198,4 +220,26 @@ function _toOptionalBoolean(
return undefined;
}
/**
* @description
* From an argument name, get the value as an optional string array
* This is very useful for all the arguments that override others
* It will allow us to easily use the original one when the return value is `undefined`
*
* @param {Readonly<string>} argumentName The name of the argument to check
*
* @returns {string[]} The value matching the given argument name
*/
function _toStringArray(argumentName: Readonly<string>): string[] {
const val = core.getInput(argumentName);
if (!val) {
return [];
}
try {
return JSON.parse(val);
} catch (err) {
return [val];
}
}
void _run();

BIN
stale-action-9.0.0.tgz Normal file

Binary file not shown.