From a906e47092cf5f86cd068dbad8c7bcd3cd66e263 Mon Sep 17 00:00:00 2001 From: kt Date: Fri, 30 Jan 2026 12:48:58 -0800 Subject: [PATCH 1/3] new workflow: close stale prs after 60 days --- .github/workflows/close-stale-draft-prs.yaml | 18 ++ scripts/close-stale-draft-prs.sh | 177 +++++++++++++++++++ 2 files changed, 195 insertions(+) create mode 100644 .github/workflows/close-stale-draft-prs.yaml create mode 100755 scripts/close-stale-draft-prs.sh diff --git a/.github/workflows/close-stale-draft-prs.yaml b/.github/workflows/close-stale-draft-prs.yaml new file mode 100644 index 000000000000..68008a88c9bf --- /dev/null +++ b/.github/workflows/close-stale-draft-prs.yaml @@ -0,0 +1,18 @@ +name: 'Stale draft PRs' +on: + workflow_dispatch: + schedule: + - cron: '0 6 * * MON' + +permissions: + issues: write + pull-requests: write + +jobs: + stale-draft-prs: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + - name: Process stale draft PRs + run: ./scripts/close-stale-draft-prs.sh -o "${{ github.repository_owner }}" -r "${{ github.event.repository.name }}" -t "${{ secrets.GITHUB_TOKEN }}" + diff --git a/scripts/close-stale-draft-prs.sh b/scripts/close-stale-draft-prs.sh new file mode 100755 index 000000000000..cabb10f84287 --- /dev/null +++ b/scripts/close-stale-draft-prs.sh @@ -0,0 +1,177 @@ +#!/usr/bin/env bash +# Copyright IBM Corp. 2014, 2025 +# SPDX-License-Identifier: MPL-2.0 + +# shellcheck disable=SC2086 + +# as the normal stale bot does not work for draft PRs, we need to do it manually until this pr is merged https://github.com/actions/stale/pull/1314 + +set -euo pipefail + +DRY_RUN=false + +while getopts o:r:t:d flag +do + case "${flag}" in + o) owner=${OPTARG};; + r) repo=${OPTARG};; + t) token=${OPTARG};; + d) DRY_RUN=true;; + *) echo "Usage: $0 -o owner -r repo [-t token] [-d]"; exit 1;; + esac +done + +# Use token from env if not provided via flag +token="${token:-${GH_TOKEN:-${GITHUB_TOKEN:-}}}" +if [[ -z "$token" ]]; then + echo "Error: No token provided. Use -t flag or set GH_TOKEN/GITHUB_TOKEN env var." + exit 1 +fi + +API_BASE="https://api.github.com/repos/${owner}/${repo}" +# Handle both GNU date (Linux) and BSD date (macOS) +THIRTY_DAYS_AGO=$(date -u -d "30 days ago" +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || date -u -v-30d +%Y-%m-%dT%H:%M:%SZ) +SIXTY_DAYS_AGO=$(date -u -d "60 days ago" +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || date -u -v-60d +%Y-%m-%dT%H:%M:%SZ) + +echo "=== Stale Draft PR Processor ===" +echo "Repository: ${owner}/${repo}" +echo "Stale threshold: 30 days (before ${THIRTY_DAYS_AGO})" +echo "Close threshold: 60 days (before ${SIXTY_DAYS_AGO})" +if [[ "$DRY_RUN" == "true" ]]; then + echo "Mode: DRY RUN (no changes will be made)" +else + echo "Mode: LIVE" +fi +echo "" + +# First, collect all open PRs to get total count +echo "Fetching open PRs..." +all_prs=() +page=1 +while :; do + prs_json=$(curl -s -L \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $token" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "${API_BASE}/pulls?state=open&per_page=100&page=${page}") + + count=$(echo "$prs_json" | jq length) + if [[ "$count" -eq 0 ]]; then + break + fi + + while IFS= read -r pr; do + all_prs+=("$pr") + done < <(echo "$prs_json" | jq -c '.[]') + + page=$((page + 1)) +done + +total_prs=${#all_prs[@]} +echo "Found ${total_prs} open PR(s)" +echo "" + +# Process each PR +current=0 +drafts_found=0 +stale_count=0 +close_count=0 + +for pr in "${all_prs[@]}"; do + current=$((current + 1)) + + pr_number=$(echo "$pr" | jq -r '.number') + pr_title=$(echo "$pr" | jq -r '.title' | head -c 60) + draft=$(echo "$pr" | jq -r '.draft') + updated_at=$(echo "$pr" | jq -r '.updated_at') + labels=$(echo "$pr" | jq -r '.labels[].name' 2>/dev/null || echo "") + + echo "PR ${current}/${total_prs} - #${pr_number} \"${pr_title}\"" + + # Check if draft + if [[ "$draft" != "true" ]]; then + echo " ↳ Not a draft, skipping" + continue + fi + + drafts_found=$((drafts_found + 1)) + echo " ↳ Is draft, last updated: ${updated_at}" + + # Check for keep-draft label + if echo "$labels" | grep -q "^keep-draft$"; then + echo " ↳ Has 'keep-draft' label, skipping" + continue + fi + + # Check for existing stale-draft label + has_stale_label=false + if echo "$labels" | grep -q "^stale-draft$"; then + has_stale_label=true + echo " ↳ Already has 'stale-draft' label" + fi + + # Close if 60+ days stale + if [[ "$updated_at" < "$SIXTY_DAYS_AGO" ]]; then + close_count=$((close_count + 1)) + echo " ↳ Inactive for 60+ days → CLOSING" + + if [[ "$DRY_RUN" == "false" ]]; then + curl -s -L -X PATCH \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $token" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "${API_BASE}/pulls/${pr_number}" \ + -d '{"state":"closed"}' > /dev/null + + curl -s -L -X POST \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $token" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "${API_BASE}/issues/${pr_number}/comments" \ + -d '{"body":"I'\''m going to close this draft pull request because it has been inactive for _60 days_ ⏳. This helps our maintainers find and focus on the active contributions.\n\nIf you would like to continue working on this, please reopen the pull request and mark it as ready for review when complete. Thank you!"}' > /dev/null + echo " ↳ Closed and commented" + else + echo " ↳ (dry run - would close and comment)" + fi + + # Comment and label if 30+ days stale without stale-draft label + elif [[ "$updated_at" < "$THIRTY_DAYS_AGO" && "$has_stale_label" == "false" ]]; then + stale_count=$((stale_count + 1)) + echo " ↳ Inactive for 30+ days → MARKING STALE" + + if [[ "$DRY_RUN" == "false" ]]; then + curl -s -L -X POST \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $token" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "${API_BASE}/issues/${pr_number}/labels" \ + -d '{"labels":["stale-draft"]}' > /dev/null + + curl -s -L -X POST \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $token" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "${API_BASE}/issues/${pr_number}/comments" \ + -d '{"body":"This draft pull request is being labeled as \"stale-draft\" because it has not been updated for _30 days_ ⏳.\n\nIf this draft is still valid, please remove the \"stale-draft\" label. To prevent automatic closure after 60 days, add the `keep-draft` label or mark it as ready for review.\n\nIf you need some help completing this draft, please leave a comment letting us know. Thank you!"}' > /dev/null + echo " ↳ Labeled and commented" + else + echo " ↳ (dry run - would label and comment)" + fi + + elif [[ "$has_stale_label" == "true" ]]; then + echo " ↳ Already stale, not yet 60 days old" + else + echo " ↳ Not stale yet (updated within 30 days)" + fi +done + +echo "" +echo "=== Summary ===" +echo "Total PRs checked: ${total_prs}" +echo "Draft PRs found: ${drafts_found}" +echo "Newly marked stale: ${stale_count}" +echo "Closed: ${close_count}" +if [[ "$DRY_RUN" == "true" ]]; then + echo "(dry run - no actual changes made)" +fi +echo "Done." From 56f6149cf56d3d6043950c8fc5ed500cddec005e Mon Sep 17 00:00:00 2001 From: kt Date: Fri, 30 Jan 2026 13:02:12 -0800 Subject: [PATCH 2/3] adjust name --- .github/workflows/close-stale-draft-prs.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/close-stale-draft-prs.yaml b/.github/workflows/close-stale-draft-prs.yaml index 68008a88c9bf..5f99460e2ab3 100644 --- a/.github/workflows/close-stale-draft-prs.yaml +++ b/.github/workflows/close-stale-draft-prs.yaml @@ -1,4 +1,4 @@ -name: 'Stale draft PRs' +name: 'Mark Stale/Close Inactive Draft PRs' on: workflow_dispatch: schedule: From b4d80bc67d2d0972806cb41929e14b744b69647e Mon Sep 17 00:00:00 2001 From: kt Date: Mon, 9 Feb 2026 19:35:37 -0800 Subject: [PATCH 3/3] clean up output & only close no longer label --- .github/workflows/document-lint.yaml | 48 -------------------- scripts/close-stale-draft-prs.sh | 65 +++++----------------------- 2 files changed, 10 insertions(+), 103 deletions(-) delete mode 100644 .github/workflows/document-lint.yaml diff --git a/.github/workflows/document-lint.yaml b/.github/workflows/document-lint.yaml deleted file mode 100644 index 4df4c7ea893a..000000000000 --- a/.github/workflows/document-lint.yaml +++ /dev/null @@ -1,48 +0,0 @@ ---- -name: Resource Document Linting - -permissions: - contents: read - pull-requests: read - -on: - pull_request: - types: ["opened", "synchronize"] - paths: - - ".github/workflows/document-lint.yaml" - - "internal/services/**" - - "website/**" - branches: ["main"] - -jobs: - document-lint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - fetch-depth: 2 - - uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0 - with: - go-version-file: ./.go-version - - run: bash scripts/gogetcookie.sh - - name: Get changed files - id: changed-files - run: | - changed=$(git diff --name-only HEAD^1 HEAD -- internal/services | sed "s|^|${{ github.workspace }}/|" | tr '\n' ',' | sed 's/,$//') - changed=${changed:-} - echo "::notice::check with files: ${changed}" - echo "FILE_LIST=$changed" >> $GITHUB_ENV - echo "has-files=$([ -n "$changed" ] && echo 'true' || echo 'false')" >> $GITHUB_OUTPUT - - - name: run document lint checker - if: steps.changed-files.outputs.has-files == 'true' - run: | - if make document-lint; then - echo "::notice::Document lint success." - else - echo "::warning::Document lint failed. Please fix the issues." - fi - - - name: skip document lint checker - if: steps.changed-files.outputs.has-files == 'false' - run: echo "::notice::No internal/services files changed, skipping resource document lint check" diff --git a/scripts/close-stale-draft-prs.sh b/scripts/close-stale-draft-prs.sh index cabb10f84287..b74a85ab2572 100755 --- a/scripts/close-stale-draft-prs.sh +++ b/scripts/close-stale-draft-prs.sh @@ -30,12 +30,10 @@ fi API_BASE="https://api.github.com/repos/${owner}/${repo}" # Handle both GNU date (Linux) and BSD date (macOS) -THIRTY_DAYS_AGO=$(date -u -d "30 days ago" +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || date -u -v-30d +%Y-%m-%dT%H:%M:%SZ) SIXTY_DAYS_AGO=$(date -u -d "60 days ago" +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || date -u -v-60d +%Y-%m-%dT%H:%M:%SZ) -echo "=== Stale Draft PR Processor ===" +echo "=== Close Inactive Draft PRs ===" echo "Repository: ${owner}/${repo}" -echo "Stale threshold: 30 days (before ${THIRTY_DAYS_AGO})" echo "Close threshold: 60 days (before ${SIXTY_DAYS_AGO})" if [[ "$DRY_RUN" == "true" ]]; then echo "Mode: DRY RUN (no changes will be made)" @@ -44,7 +42,7 @@ else fi echo "" -# First, collect all open PRs to get total count +# Collect all open PRs echo "Fetching open PRs..." all_prs=() page=1 @@ -72,30 +70,24 @@ echo "Found ${total_prs} open PR(s)" echo "" # Process each PR -current=0 drafts_found=0 -stale_count=0 close_count=0 for pr in "${all_prs[@]}"; do - current=$((current + 1)) - - pr_number=$(echo "$pr" | jq -r '.number') - pr_title=$(echo "$pr" | jq -r '.title' | head -c 60) draft=$(echo "$pr" | jq -r '.draft') - updated_at=$(echo "$pr" | jq -r '.updated_at') - labels=$(echo "$pr" | jq -r '.labels[].name' 2>/dev/null || echo "") - echo "PR ${current}/${total_prs} - #${pr_number} \"${pr_title}\"" - - # Check if draft + # Skip non-draft PRs silently if [[ "$draft" != "true" ]]; then - echo " ↳ Not a draft, skipping" continue fi + pr_number=$(echo "$pr" | jq -r '.number') + pr_title=$(echo "$pr" | jq -r '.title') + updated_at=$(echo "$pr" | jq -r '.updated_at') + labels=$(echo "$pr" | jq -r '.labels[].name' 2>/dev/null || echo "") + drafts_found=$((drafts_found + 1)) - echo " ↳ Is draft, last updated: ${updated_at}" + echo "Draft PR #${pr_number} \"${pr_title}\" (updated: ${updated_at})" # Check for keep-draft label if echo "$labels" | grep -q "^keep-draft$"; then @@ -103,14 +95,7 @@ for pr in "${all_prs[@]}"; do continue fi - # Check for existing stale-draft label - has_stale_label=false - if echo "$labels" | grep -q "^stale-draft$"; then - has_stale_label=true - echo " ↳ Already has 'stale-draft' label" - fi - - # Close if 60+ days stale + # Close if 60+ days inactive if [[ "$updated_at" < "$SIXTY_DAYS_AGO" ]]; then close_count=$((close_count + 1)) echo " ↳ Inactive for 60+ days → CLOSING" @@ -133,35 +118,6 @@ for pr in "${all_prs[@]}"; do else echo " ↳ (dry run - would close and comment)" fi - - # Comment and label if 30+ days stale without stale-draft label - elif [[ "$updated_at" < "$THIRTY_DAYS_AGO" && "$has_stale_label" == "false" ]]; then - stale_count=$((stale_count + 1)) - echo " ↳ Inactive for 30+ days → MARKING STALE" - - if [[ "$DRY_RUN" == "false" ]]; then - curl -s -L -X POST \ - -H "Accept: application/vnd.github+json" \ - -H "Authorization: Bearer $token" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "${API_BASE}/issues/${pr_number}/labels" \ - -d '{"labels":["stale-draft"]}' > /dev/null - - curl -s -L -X POST \ - -H "Accept: application/vnd.github+json" \ - -H "Authorization: Bearer $token" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "${API_BASE}/issues/${pr_number}/comments" \ - -d '{"body":"This draft pull request is being labeled as \"stale-draft\" because it has not been updated for _30 days_ ⏳.\n\nIf this draft is still valid, please remove the \"stale-draft\" label. To prevent automatic closure after 60 days, add the `keep-draft` label or mark it as ready for review.\n\nIf you need some help completing this draft, please leave a comment letting us know. Thank you!"}' > /dev/null - echo " ↳ Labeled and commented" - else - echo " ↳ (dry run - would label and comment)" - fi - - elif [[ "$has_stale_label" == "true" ]]; then - echo " ↳ Already stale, not yet 60 days old" - else - echo " ↳ Not stale yet (updated within 30 days)" fi done @@ -169,7 +125,6 @@ echo "" echo "=== Summary ===" echo "Total PRs checked: ${total_prs}" echo "Draft PRs found: ${drafts_found}" -echo "Newly marked stale: ${stale_count}" echo "Closed: ${close_count}" if [[ "$DRY_RUN" == "true" ]]; then echo "(dry run - no actual changes made)"