Skip to content

Commit

Permalink
feat: workflow for marking old draft PRs as stale
Browse files Browse the repository at this point in the history
feat: workflow for marking old draft PRs as stale
  • Loading branch information
rjan90 committed Jan 22, 2025
1 parent 313cd2b commit 13181e0
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions .github/workflows/stale-draft-prs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Mark Stale Draft PRs

on:
schedule:
# Run daily at midnight UTC
- cron: '0 0 * * *'
# Allow manual trigger
workflow_dispatch:

jobs:
stale-draft:
runs-on: ubuntu-latest
permissions:
pull-requests: write

steps:
- name: Check Draft PRs
uses: actions/github-script@v7
with:
script: |
const now = new Date();
const thirtyFiveMonthsAgo = new Date(now.setMonth(now.getMonth() - 35));
const query = `query($cursor: String) {
repository(owner: "${context.repo.owner}", name: "${context.repo.repo}") {
pullRequests(first: 100, after: $cursor, states: OPEN, isDraft: false) {
nodes {
number
createdAt
labels(first: 100) {
nodes {
name
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`;
async function getAllDraftPRs() {
let cursor = null;
let allPRs = [];
while (true) {
const response = await github.graphql(query, { cursor });
const prs = response.repository.pullRequests.nodes;
allPRs = allPRs.concat(prs);
if (!response.repository.pullRequests.pageInfo.hasNextPage) {
break;
}
cursor = response.repository.pullRequests.pageInfo.endCursor;
}
return allPRs;
}
const prs = await getAllDraftPRs();
for (const pr of prs) {
const createdAt = new Date(pr.createdAt);
const hasStaleLabel = pr.labels.nodes.some(label => label.name === 'kind/stale');
if (createdAt < thirtyFiveMonthsAgo && !hasStaleLabel) {
console.log(`Adding stale label to PR #${pr.number}`);
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
labels: ['kind/stale']
});
}
}

0 comments on commit 13181e0

Please sign in to comment.