Skip to content

Commit

Permalink
Merge pull request #19 from slawekGoszcz/ignore-branches-with-open-prs
Browse files Browse the repository at this point in the history
Ignore branches with open PRs
  • Loading branch information
fpicalausa authored Jul 12, 2024
2 parents a59aa10 + d500de8 commit 16ded70
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 18 deletions.
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@ Without setting `dry_run: true`, this action will remove branches. Consider sett

## Inputs

| Input | Defaults | Description |
|-----------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `github-token` | `${{ secrets.GITHUB_TOKEN }}` | PAT for GitHub API authentication. |
| `dry-run` | `false` | Flag that prevents this action from doing any modification to the repository. |
| `exempt-organization` | (not set) | Name of a Github organization. Branches for which the latest commiter belongs to this organization will be exempt from cleanup. |
| `exempt-branches-regex` | `^(main\|master)$` | Regular expression defining branches name that are exempt from cleanup. |
| `exempt-authors-regex` | (not set) | Regular expression defining authors who are exempt from cleanup. |
| `exempt-protected-branches` | `true` | Whether protected branches are exempted |
| `stale-branch-message` | `@{author} Your branch [{branchName}]({branchUrl}) hasn't been updated in the last 60 days and is marked as stale. It will be removed in a week.\r\nIf you want to keep this branch around, delete this comment or add new commits to this branch.` | Template for commit comments notifying the author that their branch will be removed. |
| `days-before-branch-stale` | `90` | Number of days since the last commit before a branch is considered stale. Once stale, this action will leave a comment on the last commit, marking the branch as stale. |
| `days-before-branch-delete` | `7` | Number of days before a stale branch is removed. Set to 0 to remove immediately. |
| `operations-per-run` | `10` | Maximum number of stale branches to look at in any run of this action. |
| `ignore-unknown-authors` | `false` | Whether to abort early when a commit author cannot be identified. By default, stop early since this may indicate that the token used to run the action doesn't have the right privileges. Set to true and define a default recipient instead if not a concern. |
| `default-recipient` | (not set) | When `ignore-unknown-authors` is `true`, use this login as the author to notify when the branch becomes stale. |
| Input | Defaults | Description |
|---------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `github-token` | `${{ secrets.GITHUB_TOKEN }}` | PAT for GitHub API authentication. |
| `dry-run` | `false` | Flag that prevents this action from doing any modification to the repository. |
| `exempt-organization` | (not set) | Name of a Github organization. Branches for which the latest commiter belongs to this organization will be exempt from cleanup. |
| `exempt-branches-regex` | `^(main\|master)$` | Regular expression defining branches name that are exempt from cleanup. |
| `exempt-authors-regex` | (not set) | Regular expression defining authors who are exempt from cleanup. |
| `exempt-protected-branches` | `true` | Whether protected branches are exempted |
| `stale-branch-message` | `@{author} Your branch [{branchName}]({branchUrl}) hasn't been updated in the last 60 days and is marked as stale. It will be removed in a week.\r\nIf you want to keep this branch around, delete this comment or add new commits to this branch.` | Template for commit comments notifying the author that their branch will be removed. |
| `days-before-branch-stale` | `90` | Number of days since the last commit before a branch is considered stale. Once stale, this action will leave a comment on the last commit, marking the branch as stale. |
| `days-before-branch-delete` | `7` | Number of days before a stale branch is removed. Set to 0 to remove immediately. |
| `operations-per-run` | `10` | Maximum number of stale branches to look at in any run of this action. |
| `ignore-unknown-authors` | `false` | Whether to abort early when a commit author cannot be identified. By default, stop early since this may indicate that the token used to run the action doesn't have the right privileges. Set to true and define a default recipient instead if not a concern. |
| `default-recipient` | (not set) | When `ignore-unknown-authors` is `true`, use this login as the author to notify when the branch becomes stale. |
| `ignore-branches-with-open-prs` | `false` | When `ignore-branches-with-open-prs` is `true`, branches with open PRs will be ignored. |

### Tokens replaced in `stale-branch-message`

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ inputs:
description: "A string corresponding to the username to be tagged on stale branches from unknown authors"
default: ""
required: false
ignore-branches-with-open-prs:
description: "Whether to ignore branches with open pull requests."
default: "false"
required: false
22 changes: 20 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34248,6 +34248,7 @@ function run() {
const ignoreUnknownAuthors = core.getBooleanInput("ignore-unknown-authors", {
required: false,
});
const ignoreBranchesWithOpenPRs = core.getBooleanInput("ignore-branches-with-open-prs", { required: false });
return (0, removeStaleBranches_1.removeStaleBranches)(octokit, {
isDryRun,
repo: github.context.repo,
Expand All @@ -34261,6 +34262,7 @@ function run() {
operationsPerRun,
defaultRecipient,
ignoreUnknownAuthors,
ignoreBranchesWithOpenPRs,
});
});
}
Expand Down Expand Up @@ -34299,6 +34301,11 @@ const GRAPHQL_QUERY = `query ($repo: String!, $owner: String!, $after: String) {
edges {
node {
name
associatedPullRequests(first: 1, states: OPEN) {
nodes {
state
}
}
prefix
... on Ref {
refUpdateRule {
Expand Down Expand Up @@ -34337,6 +34344,11 @@ const GRAPHQL_QUERY_WITH_ORG = `query ($repo: String!, $owner: String!, $organiz
edges {
node {
name
associatedPullRequests(first: 1, states: OPEN) {
nodes {
state
}
}
prefix
... on Ref {
refUpdateRule {
Expand Down Expand Up @@ -34382,7 +34394,7 @@ function readBranches(octokit, headers, repo, organization) {
const { repository: { refs: { edges, pageInfo }, }, } = yield __await(octokit.graphql(organization ? GRAPHQL_QUERY_WITH_ORG : GRAPHQL_QUERY, params));
for (let i = 0; i < edges.length; ++i) {
const ref = edges[i];
const { name, prefix, refUpdateRule } = ref.node;
const { name, prefix, refUpdateRule, associatedPullRequests } = ref.node;
const { oid, authoredDate, author } = ref.node.target;
let branchAuthor = null;
if (author) {
Expand All @@ -34399,6 +34411,7 @@ function readBranches(octokit, headers, repo, organization) {
commitId: oid,
author: branchAuthor,
isProtected: refUpdateRule !== null,
openPrs: associatedPullRequests.nodes.length > 0,
});
}
pagination = pageInfo;
Expand Down Expand Up @@ -34478,7 +34491,9 @@ function processBranch(plan, branch, commitComments, params) {
}
if (plan.action === "mark stale") {
console.log("-> branch will be removed on " + (0, formatISO_1.default)(plan.cutoffTime));
console.log("-> marking branch as stale (notifying: " + (((_c = branch.author) === null || _c === void 0 ? void 0 : _c.username) || params.defaultRecipient) + ")");
console.log("-> marking branch as stale (notifying: " +
(((_c = branch.author) === null || _c === void 0 ? void 0 : _c.username) || params.defaultRecipient) +
")");
if (params.isDryRun) {
console.log("-> (doing nothing because of dry run flag)");
return;
Expand Down Expand Up @@ -34532,6 +34547,9 @@ function planBranchAction(now, branch, filters, commitComments, params) {
branch.author.belongsToOrganization) {
return skip(`author ${branch.author.username} belongs to protected organization ${params.protectedOrganizationName}`);
}
if (branch.openPrs && params.ignoreBranchesWithOpenPRs) {
return skip(`branch ${branch.branchName} has open PRs`);
}
if (filters.authorsRegex &&
((_a = branch.author) === null || _a === void 0 ? void 0 : _a.username) &&
filters.authorsRegex.test(branch.author.username)) {
Expand Down
2 changes: 2 additions & 0 deletions dist/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export declare type Branch = {
branchName: string;
prefix: string;
commitId: string;
openPrs: boolean;
author: {
username: string | null;
email: string | null;
Expand All @@ -28,4 +29,5 @@ export declare type Params = {
repo: Repo;
ignoreUnknownAuthors: boolean;
defaultRecipient: string | null;
ignoreBranchesWithOpenPRs: boolean;
};
1 change: 1 addition & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ removeStaleBranches(octokit, {
owner: "github",
repo: "octocat",
},
ignoreBranchesWithOpenPRs: false,
})
.then(() => clearTimeout(timer))
.catch((e) => console.log(e));
Loading

0 comments on commit 16ded70

Please sign in to comment.