Skip to content

Commit

Permalink
Close PR if it has not had any activity in the past 2 weeks and has `…
Browse files Browse the repository at this point in the history
…pr/last-call`

As stated in https://github.com/go-gitea/gitea/blob/main/CONTRIBUTING.md#final-call

Signed-off-by: Yarden Shoham <[email protected]>
  • Loading branch information
yardenshoham committed Jan 26, 2024
1 parent 1580e25 commit 615324c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ has passed since they were updated.
The script can execute some actions like updating a PR's branch if requested by
a maintainer through a `giteabot/*` label.

### Last call

The script will close PRs with the label `pr/last-call` if two weeks have passed
since they were updated.

## Usage

Set the following environment variables:
Expand Down
26 changes: 26 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ export const fetchOpenIssuesWithLabel = async (label: string) => {
return json;
};

// returns a list of open PRs with the given label
export const fetchOpenPrsWithLabel = async (label: string) => {
const response = await fetch(
`${GITHUB_API}/search/issues?q=` +
encodeURIComponent(
`is:pr is:open label:${label} repo:go-gitea/gitea`,
),
{ headers: HEADERS },
);
const json = await response.json();
return json;
};

// returns a list of PRs pending merge (have the label reviewed/wait-merge)
export const fetchPendingMerge = async () => {
const response = await fetch(
Expand Down Expand Up @@ -502,3 +515,16 @@ export const closeIssue = async (issueNumber: number) => {
);
return response.json();
};

// closes the given PR
export const closePr = async (prNumber: number) => {
const response = await fetch(
`${GITHUB_API}/repos/go-gitea/gitea/pulls/${prNumber}`,
{
method: "PATCH",
headers: HEADERS,
body: JSON.stringify({ state: "closed" }),
},
);
return response.json();
};
28 changes: 28 additions & 0 deletions src/lastCall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { addComment, closePr, fetchOpenPrsWithLabel } from "./github.ts";

export const run = async () => {
// get all issues with the label "pr/last-call"
const issuesWithStatusPrLastCall = await fetchOpenPrsWithLabel(
"pr/last-call",
);
return Promise.all(issuesWithStatusPrLastCall.items.map(handlePr));
};

// close PR if two weeks have passed since it was last updated
const handlePr = async (pr: {
number: number;
updated_at: string;
}) => {
const twoWeeksAgo = (new Date(Date.now() - 1000 * 60 * 60 * 24 * 14))
.getTime();
if ((new Date(pr.updated_at)).getTime() < twoWeeksAgo) {
console.log(`Closing PR #${pr.number} due to pr/last-call timeout`);
await addComment(
pr.number,
"This pull request has not had any activity in the past two weeks and is in https://github.com/go-gitea/gitea/labels/pr%2Flast-call state. Therefore, [it is politely refused](https://github.com/go-gitea/gitea/blob/main/CONTRIBUTING.md#final-call). :tea:",
);

// close PR
await closePr(pr.number);
}
};
4 changes: 3 additions & 1 deletion src/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as comments from "./comments.ts";
import * as lock from "./lock.ts";
import * as prActions from "./prActions.ts";
import * as feedback from "./feedback.ts";
import * as lastCall from "./lastCall.ts";

const secret = Deno.env.get("BACKPORTER_GITHUB_SECRET");

Expand All @@ -32,11 +33,12 @@ webhook.on("push", ({ payload }) => {
backport.run();
}

// we should take this opportunity to run the label, merge queue, lock, and feedback maintenance
// we should take this opportunity to run the label, merge queue, lock, feedback, and last call maintenance
labels.run();
mergeQueue.run();
lock.run();
feedback.run();
lastCall.run();
});

// on pull request labeling events, run the label and merge queue maintenance
Expand Down

0 comments on commit 615324c

Please sign in to comment.