From 6dd1e7bab11700a198c19eb828e7ff4c855612c6 Mon Sep 17 00:00:00 2001 From: Bhargav Date: Wed, 26 Nov 2025 15:03:08 +0530 Subject: [PATCH] fix: Stale pr workflow v1 (#9298) --- .github/workflows/pr-slack.yml | 53 ++++++++++++++++++++++------------ .github/workflows/stale.yml | 2 +- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/.github/workflows/pr-slack.yml b/.github/workflows/pr-slack.yml index 41c2819b2c..723307ba02 100644 --- a/.github/workflows/pr-slack.yml +++ b/.github/workflows/pr-slack.yml @@ -3,7 +3,7 @@ permissions: contents: read on: schedule: - - cron: "20 17 * * *" # everyday at 4PM + - cron: "0 11 * * *" # everyday at 11AM workflow_dispatch: jobs: @@ -11,39 +11,54 @@ jobs: runs-on: ubuntu-latest steps: - - name: Get stale PRs - - uses: actions/github-script@v6 + - name: Get PRs older than 7 days + uses: actions/github-script@v6 id: stale-prs with: script: | + const sevenDaysAgo = new Date(); + sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7); + const dateString = sevenDaysAgo.toISOString().split('T')[0]; + const resp = await github.rest.search.issuesAndPullRequests({ - q: `repo:${{ github.repository }} is:pr is:open label:stale` + q: `repo:${{ github.repository }} is:pr is:open created:<${dateString}`, + sort: 'created', + order: 'asc', + per_page: 100 }); - const prs = resp.data.items.map(pr => ({ - number: pr.number, - title: pr.title, - user: pr.user.login - })); + const prs = resp.data.items.map(pr => { + const createdDate = new Date(pr.created_at); + const daysOld = Math.floor((new Date() - createdDate) / (1000 * 60 * 60 * 24)); + return { + number: pr.number, + title: pr.title, + user: pr.user.login, + daysOld: daysOld + }; + }); core.setOutput("prs", JSON.stringify(prs)); - name: Build Slack message id: slack-msg run: | - PRS=$(echo '${{ steps.stale-prs.outputs.prs }}' | jq -r '.[] | "- — @\(.user)"') - MESSAGE="🔔 *Stale Pull Requests in ${{ github.repository }}*\n\n$PRS" - echo "message<> $GITHUB_OUTPUT - echo "$MESSAGE" >> $GITHUB_OUTPUT + PRS=$(echo '${{ steps.stale-prs.outputs.prs }}' | jq -r '.[] | "- — @\(.user) (_\(.daysOld) days old_)"') + PR_COUNT=$(echo '${{ steps.stale-prs.outputs.prs }}' | jq '. | length') + if [ "$PR_COUNT" -eq 0 ]; then + MESSAGE="✅ *No stale PRs in ${{ github.repository }}*\n\nAll PRs are less than 7 days old!" + else + MESSAGE="🔔 *Pull Requests older than 7 days in ${{ github.repository }}*\n\n$PRS" + fi + # Escape for JSON and create the payload + PAYLOAD=$(jq -n --arg msg "$MESSAGE" '{text: $msg}') + echo "payload<> $GITHUB_OUTPUT + echo "$PAYLOAD" >> $GITHUB_OUTPUT echo "EOF" >> $GITHUB_OUTPUT - name: Send Slack notification - if: ${{ steps.stale-prs.outputs.prs != '' }} uses: slackapi/slack-github-action@v1.25.0 with: - payload: | - { - "text": "${{ steps.slack-msg.outputs.message }}" - } + payload: ${{ steps.slack-msg.outputs.payload }} env: - SLACK_WEBHOOK_URL: ${{ secrets.STALE_PR_SLACK_NOTIFICATION }} + SLACK_WEBHOOK_URL: ${{ secrets.STALE_PR_SLACK_NOTIFICATION }} \ No newline at end of file diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml index a3d3ac1857..b4ea99a66a 100644 --- a/.github/workflows/stale.yml +++ b/.github/workflows/stale.yml @@ -4,7 +4,7 @@ permissions: pull-requests: write on: schedule: - - cron: "20 17 * * *" # runs daily + - cron: "0 0 * * *" # runs daily workflow_dispatch: jobs: