Repository navigation
[Release] Version packages (beta) #1675
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
| name: Changeset Auto Release Checkbox | ||
|
Check warning on line 1 in .github/workflows/changeset-auto-release.yml
|
||
| on: | ||
| pull_request: | ||
| types: [opened, reopened, synchronize, edited] | ||
| pull_request_target: | ||
| types: [opened, reopened, synchronize, edited] | ||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
| jobs: | ||
| sync-auto-release-checkbox: | ||
| name: Sync auto-release checkbox | ||
| runs-on: ubuntu-latest | ||
| if: >- | ||
| github.repository == 'udecode/plate' && ( | ||
| (github.event_name == 'pull_request' && | ||
| github.event.pull_request.head.repo.full_name == github.repository) || | ||
| (github.event_name == 'pull_request_target' && | ||
| github.event.pull_request.head.repo.full_name != github.repository) | ||
| ) | ||
| steps: | ||
| - name: 📥 Checkout workflow helper | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| ref: >- | ||
| ${{ | ||
| github.event_name == 'pull_request' | ||
| && github.event.pull_request.head.sha | ||
| || github.event.repository.default_branch | ||
| }} | ||
| persist-credentials: false | ||
| - name: 🔁 Sync auto-release checkbox | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const { readFile } = await import('node:fs/promises'); | ||
| const { resolve } = await import('node:path'); | ||
| const { pathToFileURL } = await import('node:url'); | ||
| const helperUrl = pathToFileURL( | ||
| `${process.env.GITHUB_WORKSPACE}/tooling/scripts/auto-release-pr.mjs` | ||
| ).href; | ||
| const { | ||
| getChangesetReleaseType, | ||
| shouldManageAutoReleaseBlock, | ||
| upsertAutoReleaseBlock, | ||
| } = await import(helperUrl); | ||
| const pullRequest = context.payload.pull_request; | ||
| const files = await github.paginate(github.rest.pulls.listFiles, { | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| pull_number: pullRequest.number, | ||
| per_page: 100, | ||
| }); | ||
| const headRepository = pullRequest.head.repo; | ||
| const isHeadCheckedOut = headRepository?.full_name === context.payload.repository.full_name; | ||
| const filesWithContents = []; | ||
| for (const file of files) { | ||
| const isChangeset = | ||
| file.filename.startsWith('.changeset/') && | ||
| file.filename.endsWith('.md') && | ||
| file.filename !== '.changeset/README.md' && | ||
| file.status !== 'removed'; | ||
| if (!isChangeset) { | ||
| filesWithContents.push(file); | ||
| continue; | ||
| } | ||
| let contents; | ||
| if (isHeadCheckedOut) { | ||
| contents = await readFile( | ||
| resolve(process.env.GITHUB_WORKSPACE, file.filename), | ||
| 'utf8' | ||
| ); | ||
| } else { | ||
| const { data } = await github.rest.repos.getContent({ | ||
| owner: headRepository?.owner.login ?? context.repo.owner, | ||
| repo: headRepository?.name ?? context.repo.repo, | ||
| path: file.filename, | ||
| ref: pullRequest.head.sha, | ||
| }); | ||
| if (Array.isArray(data) || data.type !== 'file' || data.encoding !== 'base64') { | ||
| throw new Error(`Could not read ${file.filename} from the pull request head.`); | ||
| } | ||
| contents = Buffer.from(data.content, 'base64').toString('utf8'); | ||
| } | ||
| filesWithContents.push({ ...file, contents }); | ||
| } | ||
| const hasChangeset = shouldManageAutoReleaseBlock({ | ||
| files: filesWithContents, | ||
| title: pullRequest.title ?? '', | ||
| }); | ||
| const releaseType = hasChangeset | ||
| ? getChangesetReleaseType(filesWithContents) | ||
| : null; | ||
| const currentBody = pullRequest.body ?? ''; | ||
| const nextBody = upsertAutoReleaseBlock(currentBody, { | ||
| defaultChecked: releaseType === 'patch', | ||
| hasChangeset, | ||
| }); | ||
| if (nextBody === currentBody) { | ||
| core.info('PR body already matches auto-release policy.'); | ||
| return; | ||
| } | ||
| await github.rest.pulls.update({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| pull_number: pullRequest.number, | ||
| body: nextBody, | ||
| }); | ||