|
| 1 | +name: Changesets |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +jobs: |
| 10 | + changesets: |
| 11 | + name: Create or Update Release PR |
| 12 | + runs-on: ubuntu-latest |
| 13 | + permissions: |
| 14 | + contents: write |
| 15 | + pull-requests: write |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout main branch |
| 19 | + uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + fetch-depth: 0 |
| 22 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 23 | + |
| 24 | + - name: Set up Python |
| 25 | + uses: actions/setup-python@v5 |
| 26 | + with: |
| 27 | + python-version: "3.13" |
| 28 | + |
| 29 | + - name: Install uv |
| 30 | + uses: astral-sh/setup-uv@v2 |
| 31 | + |
| 32 | + - name: Check for changesets |
| 33 | + id: check_changesets |
| 34 | + run: | |
| 35 | + if ls .changeset/*.md 2>/dev/null | grep -v README.md > /dev/null; then |
| 36 | + echo "has_changesets=true" >> $GITHUB_OUTPUT |
| 37 | + else |
| 38 | + echo "has_changesets=false" >> $GITHUB_OUTPUT |
| 39 | + fi |
| 40 | +
|
| 41 | + - name: Get PR metadata |
| 42 | + if: steps.check_changesets.outputs.has_changesets == 'true' |
| 43 | + id: pr_metadata |
| 44 | + run: | |
| 45 | + # Get the merge commit info |
| 46 | + COMMIT_SHA="${{ github.sha }}" |
| 47 | + echo "COMMIT_SHA=$COMMIT_SHA" >> $GITHUB_ENV |
| 48 | +
|
| 49 | + # Try to extract PR info from commit message |
| 50 | + PR_NUMBER=$(git log -1 --pretty=%B | grep -oP '(?<=#)\d+' | head -1 || echo "") |
| 51 | + if [ -n "$PR_NUMBER" ]; then |
| 52 | + echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV |
| 53 | +
|
| 54 | + # Get PR author using GitHub API |
| 55 | + PR_AUTHOR=$(gh api repos/${{ github.repository }}/pulls/$PR_NUMBER --jq '.user.login' || echo "") |
| 56 | + echo "PR_AUTHOR=$PR_AUTHOR" >> $GITHUB_ENV |
| 57 | + fi |
| 58 | + env: |
| 59 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 60 | + |
| 61 | + - name: Generate changelogs and PR description |
| 62 | + if: steps.check_changesets.outputs.has_changesets == 'true' |
| 63 | + run: | |
| 64 | + # Generate changelogs and PR description |
| 65 | + uvx changeset changelog --output-pr-description pr-description.md |
| 66 | +
|
| 67 | + # Save PR description for later use |
| 68 | + echo "PR_DESCRIPTION<<EOF" >> $GITHUB_ENV |
| 69 | + cat pr-description.md >> $GITHUB_ENV |
| 70 | + echo "EOF" >> $GITHUB_ENV |
| 71 | + rm pr-description.md |
| 72 | +
|
| 73 | + - name: Bump versions |
| 74 | + if: steps.check_changesets.outputs.has_changesets == 'true' |
| 75 | + run: | |
| 76 | + uvx changeset version --skip-changelog |
| 77 | +
|
| 78 | + - name: Commit changes |
| 79 | + if: steps.check_changesets.outputs.has_changesets == 'true' |
| 80 | + id: commit |
| 81 | + run: | |
| 82 | + git config user.name "github-actions[bot]" |
| 83 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 84 | +
|
| 85 | + # Add all changes |
| 86 | + git add . |
| 87 | +
|
| 88 | + # Commit if there are changes |
| 89 | + if ! git diff --cached --quiet; then |
| 90 | + git commit -m "Version packages and update changelogs" |
| 91 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 92 | + else |
| 93 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 94 | + fi |
| 95 | +
|
| 96 | + - name: Force push to changeset branch |
| 97 | + if: steps.check_changesets.outputs.has_changesets == 'true' && steps.commit.outputs.has_changes == 'true' |
| 98 | + run: | |
| 99 | + # Force push to the changeset-release branch |
| 100 | + git push origin HEAD:changeset-release --force |
| 101 | +
|
| 102 | + - name: Create or update PR |
| 103 | + if: steps.check_changesets.outputs.has_changesets == 'true' && steps.commit.outputs.has_changes == 'true' |
| 104 | + uses: actions/github-script@v7 |
| 105 | + with: |
| 106 | + script: | |
| 107 | + const { data: prs } = await github.rest.pulls.list({ |
| 108 | + owner: context.repo.owner, |
| 109 | + repo: context.repo.repo, |
| 110 | + head: `${context.repo.owner}:changeset-release`, |
| 111 | + base: 'main', |
| 112 | + state: 'open' |
| 113 | + }); |
| 114 | +
|
| 115 | + const prBody = process.env.PR_DESCRIPTION; |
| 116 | + const prTitle = '🚀 Release packages'; |
| 117 | +
|
| 118 | + if (prs.length > 0) { |
| 119 | + // Update existing PR |
| 120 | + const pr = prs[0]; |
| 121 | + await github.rest.pulls.update({ |
| 122 | + owner: context.repo.owner, |
| 123 | + repo: context.repo.repo, |
| 124 | + pull_number: pr.number, |
| 125 | + title: prTitle, |
| 126 | + body: prBody |
| 127 | + }); |
| 128 | + console.log(`Updated PR #${pr.number}`); |
| 129 | + } else { |
| 130 | + // Create new PR |
| 131 | + const { data: pr } = await github.rest.pulls.create({ |
| 132 | + owner: context.repo.owner, |
| 133 | + repo: context.repo.repo, |
| 134 | + title: prTitle, |
| 135 | + body: prBody, |
| 136 | + head: 'changeset-release', |
| 137 | + base: 'main' |
| 138 | + }); |
| 139 | + console.log(`Created PR #${pr.number}`); |
| 140 | + } |
0 commit comments