From 13181e0342ce8d21b2d8eb05dd938051f6dfdcee Mon Sep 17 00:00:00 2001 From: Phi Date: Wed, 22 Jan 2025 13:19:14 +0100 Subject: [PATCH] feat: workflow for marking old draft PRs as stale feat: workflow for marking old draft PRs as stale --- .github/workflows/stale-draft-prs.yml | 77 +++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 .github/workflows/stale-draft-prs.yml diff --git a/.github/workflows/stale-draft-prs.yml b/.github/workflows/stale-draft-prs.yml new file mode 100644 index 0000000000..3228ed7ab3 --- /dev/null +++ b/.github/workflows/stale-draft-prs.yml @@ -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'] + }); + } + } \ No newline at end of file