-
Notifications
You must be signed in to change notification settings - Fork 2.7k
chore: add workflow to clean up stale test release branches and PRs #8501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hectormmg
wants to merge
9
commits into
dev
Choose a base branch
from
chore/cleanup-test-release-branches
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
16fd2fc
chore: add workflow to clean up stale test release branches and PRs
hectormmg 1cb9bfb
chore: trigger cleanup workflow in dry-run mode on PR
hectormmg 65ef509
Merge branch 'dev' into chore/cleanup-test-release-branches
hectormmg b2d77fa
chore: remove temporary pull_request test trigger
hectormmg 9c008a0
Merge branch 'dev' into chore/cleanup-test-release-branches
hectormmg f1066ef
Merge branch 'dev' into chore/cleanup-test-release-branches
hectormmg b228a35
chore: tighten cleanup workflow guards and fix shell reliability
hectormmg fe03717
Merge branch 'dev' into chore/cleanup-test-release-branches
hectormmg 6793f4b
Merge branch 'dev' into chore/cleanup-test-release-branches
hectormmg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| # Closes and deletes stale test release PRs and branches created by the MSAL.js release | ||
| # pipeline running in test mode. | ||
| # | ||
| # Background | ||
| # ---------- | ||
| # When the release pipeline runs in test mode it creates: | ||
| # - A staging branch named <TEST_BRANCH_PREFIX><BuildId> in this repo | ||
| # - A PR targeting dev-copy (main pipeline) or v4-lts-copy (v4-lts pipeline) | ||
| # | ||
| # These are temporary artifacts that should not persist. This workflow closes any | ||
| # open PR whose head branch matches a test staging branch prefix and has been open | ||
| # longer than STALE_DAYS, then immediately deletes that branch. It also cleans up | ||
| # orphaned staging branches (no associated open PR) whose last commit falls outside | ||
| # the same window. | ||
| # | ||
| # PRs are matched by head branch name AND by PR author (must be the release automation | ||
| # account) AND by head repository (must equal this repo, ruling out forks). This ensures | ||
| # manually opened PRs are never affected regardless of their branch name. | ||
| # | ||
| # Naming conventions | ||
| # ------------------ | ||
| # Both the main release pipeline and the v4-lts release pipeline produce staging | ||
| # branches with the same prefix (test-release-staging-) because the BuildId suffix | ||
| # makes each branch unique across runs. One prefix covers both pipelines. | ||
| # Update the TEST_BRANCH_PREFIXES repository variable if naming conventions change, | ||
| # or add additional space-separated prefixes for new release tracks — no code change needed. | ||
| # | ||
| # Permissions required | ||
| # -------------------- | ||
| # contents: write — delete branches | ||
| # pull-requests: write — close PRs | ||
|
|
||
| name: Cleanup Test Release Branches | ||
|
|
||
| on: | ||
| schedule: | ||
| - cron: "0 6 * * *" # Daily at 6am UTC | ||
| workflow_dispatch: | ||
| inputs: | ||
| dry_run: | ||
| description: "Dry run — log actions without modifying anything" | ||
| type: boolean | ||
| default: false | ||
| stale_days: | ||
| description: "Days before a test PR/branch is considered stale (default: 7)" | ||
| type: number | ||
| default: 7 | ||
|
|
||
| permissions: | ||
| contents: write | ||
| pull-requests: write | ||
|
|
||
| env: | ||
| # Repository variables (Settings → Secrets and variables → Actions → Variables). | ||
| # Set these in the GitHub UI to change behaviour without pushing a code change. | ||
| # | ||
| # TEST_BRANCH_PREFIXES — space-separated list of head branch prefixes that identify | ||
| # test staging branches. Both the main and v4-lts release pipelines share the same | ||
| # prefix, so the default covers both. Add new prefixes if additional release tracks | ||
| # are introduced. | ||
| # | ||
| # TEST_PR_AUTHOR — GitHub login of the automation account that opens test release PRs. | ||
| # PRs are only closed when the head branch matches a configured prefix, the PR was | ||
| # opened by this account, AND the head repository matches this repo (preventing | ||
| # accidental closure of fork PRs or manually opened PRs). Update if the automation | ||
| # account changes. | ||
| TEST_BRANCH_PREFIXES: ${{ vars.TEST_BRANCH_PREFIXES || 'test-release-staging-' }} | ||
| TEST_PR_AUTHOR: ${{ vars.TEST_PR_AUTHOR || 'msal-js-release-automation' }} | ||
|
|
||
| STALE_DAYS: ${{ inputs.stale_days || 7 }} | ||
| DRY_RUN: ${{ inputs.dry_run || 'false' }} | ||
| GH_TOKEN: ${{ github.token }} | ||
|
|
||
| jobs: | ||
| cleanup: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Close stale test release PRs and delete their branches | ||
| run: | | ||
| set -euo pipefail | ||
| CUTOFF=$(date -u -d "${STALE_DAYS} days ago" +"%Y-%m-%dT%H:%M:%SZ") | ||
| REPO="${{ github.repository }}" | ||
| echo "Stale threshold: ${STALE_DAYS} days (before ${CUTOFF})" | ||
| echo "Dry run: ${DRY_RUN}" | ||
|
|
||
| for PREFIX in $TEST_BRANCH_PREFIXES; do | ||
| echo "" | ||
| echo "=== Checking PRs with head branch prefix: ${PREFIX} ===" | ||
|
|
||
| gh pr list \ | ||
| --repo "$REPO" \ | ||
| --state open \ | ||
| --limit 500 \ | ||
| --json number,title,headRefName,createdAt,author,headRepository \ | ||
| --jq '.[] | select(.headRefName | startswith("'"$PREFIX"'")) | select(.createdAt < "'"$CUTOFF"'") | select(.author.login == "'"$TEST_PR_AUTHOR"'") | select(.headRepository.nameWithOwner == "'"$REPO"'") | [.number, .headRefName, .title] | @tsv' \ | ||
| | while IFS=$'\t' read -r number branch title; do | ||
| echo "Stale PR #${number}: \"${title}\" (branch: ${branch})" | ||
| if [[ "$DRY_RUN" == "true" ]]; then | ||
| echo " [DRY RUN] Would close PR #${number} and delete branch: ${branch}" | ||
| else | ||
| gh pr close "$number" --repo "$REPO" --delete-branch | ||
| echo " Closed PR #${number} and deleted branch: ${branch}" | ||
| fi | ||
| done | ||
| done | ||
|
|
||
| - name: Delete orphaned test staging branches | ||
| run: | | ||
|
hectormmg marked this conversation as resolved.
|
||
| set -euo pipefail | ||
| CUTOFF=$(date -u -d "${STALE_DAYS} days ago" +"%Y-%m-%dT%H:%M:%SZ") | ||
| REPO="${{ github.repository }}" | ||
|
|
||
| # Collect branches that still have an open PR so we don't double-process them | ||
| OPEN_PR_BRANCHES=$( | ||
| for PREFIX in $TEST_BRANCH_PREFIXES; do | ||
| gh pr list \ | ||
| --repo "$REPO" \ | ||
| --state open \ | ||
| --limit 500 \ | ||
| --json headRefName \ | ||
| --jq '[.[].headRefName | select(startswith("'"$PREFIX"'"))] | .[]' | ||
| done | ||
| ) | ||
|
|
||
| for PREFIX in $TEST_BRANCH_PREFIXES; do | ||
| echo "" | ||
| echo "=== Checking orphaned branches with prefix: ${PREFIX} ===" | ||
|
|
||
| gh api "repos/$REPO/branches?per_page=100" --paginate \ | ||
| --jq '.[].name | select(startswith("'"$PREFIX"'"))' \ | ||
| | while read -r branch; do | ||
| # Skip if this branch still has an open PR (handled in the previous step) | ||
| if echo "$OPEN_PR_BRANCHES" | grep -qx "$branch"; then | ||
| continue | ||
| fi | ||
|
|
||
| # URL-encode the branch name for safe interpolation into API paths | ||
| ENCODED_BRANCH="${branch//\//%2F}" | ||
|
|
||
| # Use latest commit committer date as a proxy for branch age | ||
| BRANCH_DATE=$(gh api "repos/$REPO/branches/$ENCODED_BRANCH" \ | ||
| --jq '.commit.commit.committer.date') | ||
|
|
||
| if [[ "$BRANCH_DATE" < "$CUTOFF" ]]; then | ||
| echo "Orphaned stale branch: ${branch} (last commit: ${BRANCH_DATE})" | ||
| if [[ "$DRY_RUN" == "true" ]]; then | ||
| echo " [DRY RUN] Would delete branch: ${branch}" | ||
| else | ||
| gh api --method DELETE "repos/$REPO/git/refs/heads/$ENCODED_BRANCH" | ||
| echo " Deleted branch: ${branch}" | ||
| fi | ||
| fi | ||
| done | ||
| done | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.