Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to pass a different repo-owner and repo-name #1081

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Every argument is optional.
| Input | Description | Default |
| ------------------------------------------------------------------- | --------------------------------------------------------------------------- | --------------------- |
| [repo-token](#repo-token) | PAT for GitHub API authentication | `${{ github.token }}` |
| [repo-owner](#repo-owner) | Owner of the repository | `${{ github.repository_owner }}` |
| [repo-name](#repo-name) | Name of the repository | `${{ github.event.repository.name }}` |
| [days-before-stale](#days-before-stale) | Idle number of days before marking issues/PRs stale | `60` |
| [days-before-issue-stale](#days-before-issue-stale) | Override [days-before-stale](#days-before-stale) for issues only | |
| [days-before-pr-stale](#days-before-pr-stale) | Override [days-before-stale](#days-before-stale) for PRs only | |
Expand Down Expand Up @@ -109,11 +111,23 @@ Every argument is optional.

#### repo-token

Personal Access Token (PAT) that allows the stale workflow to authenticate and perform API calls to GitHub.
Personal Access Token (PAT) that allows the stale workflow to authenticate and perform API calls to GitHub in the specified repository.
Under the hood, it uses the [@actions/github](https://www.npmjs.com/package/@actions/github) package.

Default value: `${{ github.token }}`

#### repo-owner

The owner / organization of the repository that you wish to run the stale workflow on.

Default value: `${{ github.repository_owner }}`

#### repo-name

The name of the repository that you wish to run the stale workflow on.

Default value: `${{ github.event.repository.name }}`

#### days-before-stale

The idle number of days before marking the issues or the pull requests as stale (by adding a label).
Expand Down
2 changes: 2 additions & 0 deletions __tests__/constants/default-processor-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {IIssuesProcessorOptions} from '../../src/interfaces/issues-processor-opt
// Mirrors the defaults defined in action.yml
export const DefaultProcessorOptions: IIssuesProcessorOptions = Object.freeze({
repoToken: 'none',
repoOwner: '',
repoName: '',
staleIssueMessage: 'This issue is stale',
stalePrMessage: 'This PR is stale',
closeIssueMessage: 'This issue is being closed',
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ inputs:
description: 'Token for the repository. Can be passed in using `{{ secrets.GITHUB_TOKEN }}`.'
required: false
default: ${{ github.token }}
repo-owner:
description: 'Name of the organization / owner of the repository that you want to manage'
required: false
repo-name:
description: 'Name of the repository that you want to apply stale bot'
required: false
stale-issue-message:
description: 'The message to post on the issue when tagging it. If none provided, will not mark issues stale.'
required: false
Expand Down
60 changes: 35 additions & 25 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,8 +659,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: this.options.repoOwner,
repo: this.options.repoName,
issue_number: issue.number,
since: sinceDate
});
Expand All @@ -676,11 +676,12 @@ class IssuesProcessor {
getIssues(page) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
this._logger.info(logger_service_1.LoggerService.green(`Processing: ${this.options.repoOwner}/${this.options.repoName}`));
try {
this.operations.consumeOperation();
const issueResult = yield this.client.rest.issues.listForRepo({
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
owner: this.options.repoOwner,
repo: this.options.repoName,
state: 'open',
per_page: 100,
direction: this.options.ascending ? 'asc' : 'desc',
Expand All @@ -704,8 +705,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: this.options.repoOwner,
repo: this.options.repoName,
per_page: 100,
issue_number: issue.number
});
Expand All @@ -728,8 +729,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: this.options.repoOwner,
repo: this.options.repoName,
pull_number: issue.number
});
return pullRequest.data;
Expand Down Expand Up @@ -848,8 +849,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: this.options.repoOwner,
repo: this.options.repoName,
issue_number: issue.number,
body: staleMessage
});
Expand All @@ -865,8 +866,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: this.options.repoOwner,
repo: this.options.repoName,
issue_number: issue.number,
labels: [staleLabel]
});
Expand All @@ -891,8 +892,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: this.options.repoOwner,
repo: this.options.repoName,
issue_number: issue.number,
body: closeMessage
});
Expand All @@ -908,8 +909,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: this.options.repoOwner,
repo: this.options.repoName,
issue_number: issue.number,
labels: [closeLabel]
});
Expand All @@ -924,8 +925,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: this.options.repoOwner,
repo: this.options.repoName,
issue_number: issue.number,
state: 'closed',
state_reason: this.options.closeIssueReason || undefined
Expand Down Expand Up @@ -955,15 +956,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}`) {
`${this.options.repoOwner}/${this.options.repoName}`) {
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: this.options.repoOwner,
repo: this.options.repoName,
ref: `heads/${branch}`
});
}
Expand All @@ -989,8 +990,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: this.options.repoOwner,
repo: this.options.repoName,
issue_number: issue.number,
name: label
});
Expand Down Expand Up @@ -1089,8 +1090,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: this.options.repoOwner,
repo: this.options.repoName,
issue_number: issue.number,
labels: labelsToAdd
});
Expand Down Expand Up @@ -2173,6 +2174,8 @@ exports.Option = void 0;
var Option;
(function (Option) {
Option["RepoToken"] = "repo-token";
Option["RepoOwner"] = "repo-owner";
Option["RepoName"] = "repo-name";
Option["StaleIssueMessage"] = "stale-issue-message";
Option["StalePrMessage"] = "stale-pr-message";
Option["CloseIssueMessage"] = "close-issue-message";
Expand Down Expand Up @@ -2480,6 +2483,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const core = __importStar(__nccwpck_require__(2186));
const github_1 = __nccwpck_require__(5438);
const issues_processor_1 = __nccwpck_require__(3292);
const is_valid_date_1 = __nccwpck_require__(891);
const state_service_1 = __nccwpck_require__(6330);
Expand Down Expand Up @@ -2514,6 +2518,12 @@ function _run() {
function _getAndValidateArgs() {
const args = {
repoToken: core.getInput('repo-token'),
repoOwner: core.getInput('repo-owner') === ''
? github_1.context.repo.owner
: core.getInput('repo-owner'),
repoName: core.getInput('repo-name') === ''
? github_1.context.repo.repo
: core.getInput('repo-name'),
staleIssueMessage: core.getInput('stale-issue-message'),
stalePrMessage: core.getInput('stale-pr-message'),
closeIssueMessage: core.getInput('close-issue-message'),
Expand Down
2 changes: 2 additions & 0 deletions src/classes/issue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ describe('Issue', (): void => {
removeIssueStaleWhenUpdated: undefined,
removePrStaleWhenUpdated: undefined,
repoToken: '',
repoOwner: '',
repoName: '',
staleIssueMessage: '',
stalePrMessage: '',
startDate: undefined,
Expand Down
57 changes: 31 additions & 26 deletions src/classes/issues-processor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as core from '@actions/core';
import {context, getOctokit} from '@actions/github';
import {getOctokit} from '@actions/github';
import {GitHub} from '@actions/github/lib/utils';
import {Option} from '../enums/option';
import {getHumanizedDate} from '../functions/dates/get-humanized-date';
Expand Down Expand Up @@ -548,8 +548,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: this.options.repoOwner,
repo: this.options.repoName,
issue_number: issue.number,
since: sinceDate
});
Expand All @@ -562,11 +562,16 @@ export class IssuesProcessor {

// grab issues from github in batches of 100
async getIssues(page: number): Promise<Issue[]> {
this._logger.info(
LoggerService.green(
`Processing: ${this.options.repoOwner}/${this.options.repoName}`
)
);
try {
this.operations.consumeOperation();
const issueResult = await this.client.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
owner: this.options.repoOwner,
repo: this.options.repoName,
state: 'open',
per_page: 100,
direction: this.options.ascending ? 'asc' : 'desc',
Expand Down Expand Up @@ -596,8 +601,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: this.options.repoOwner,
repo: this.options.repoName,
per_page: 100,
issue_number: issue.number
});
Expand Down Expand Up @@ -627,8 +632,8 @@ export class IssuesProcessor {
this.statistics?.incrementFetchedPullRequestsCount();

const pullRequest = await this.client.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
owner: this.options.repoOwner,
repo: this.options.repoName,
pull_number: issue.number
});

Expand Down Expand Up @@ -847,8 +852,8 @@ export class IssuesProcessor {

if (!this.options.debugOnly) {
await this.client.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
owner: this.options.repoOwner,
repo: this.options.repoName,
issue_number: issue.number,
body: staleMessage
});
Expand All @@ -865,8 +870,8 @@ export class IssuesProcessor {

if (!this.options.debugOnly) {
await this.client.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
owner: this.options.repoOwner,
repo: this.options.repoName,
issue_number: issue.number,
labels: [staleLabel]
});
Expand Down Expand Up @@ -895,8 +900,8 @@ export class IssuesProcessor {

if (!this.options.debugOnly) {
await this.client.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
owner: this.options.repoOwner,
repo: this.options.repoName,
issue_number: issue.number,
body: closeMessage
});
Expand All @@ -913,8 +918,8 @@ export class IssuesProcessor {

if (!this.options.debugOnly) {
await this.client.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
owner: this.options.repoOwner,
repo: this.options.repoName,
issue_number: issue.number,
labels: [closeLabel]
});
Expand All @@ -930,8 +935,8 @@ export class IssuesProcessor {

if (!this.options.debugOnly) {
await this.client.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
owner: this.options.repoOwner,
repo: this.options.repoName,
issue_number: issue.number,
state: 'closed',
state_reason: this.options.closeIssueReason || undefined
Expand Down Expand Up @@ -967,7 +972,7 @@ export class IssuesProcessor {
if (
pullRequest.head.repo === null ||
pullRequest.head.repo.full_name ===
`${context.repo.owner}/${context.repo.repo}`
`${this.options.repoOwner}/${this.options.repoName}`
) {
issueLogger.info(
`Deleting the branch "${LoggerService.cyan(branch)}" from closed $$type`
Expand All @@ -979,8 +984,8 @@ export class IssuesProcessor {

if (!this.options.debugOnly) {
await this.client.rest.git.deleteRef({
owner: context.repo.owner,
repo: context.repo.repo,
owner: this.options.repoOwner,
repo: this.options.repoName,
ref: `heads/${branch}`
});
}
Expand Down Expand Up @@ -1023,8 +1028,8 @@ export class IssuesProcessor {

if (!this.options.debugOnly) {
await this.client.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
owner: this.options.repoOwner,
repo: this.options.repoName,
issue_number: issue.number,
name: label
});
Expand Down Expand Up @@ -1161,8 +1166,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: this.options.repoOwner,
repo: this.options.repoName,
issue_number: issue.number,
labels: labelsToAdd
});
Expand Down
Loading