|
| 1 | +name: Snapshot |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: |
| 6 | + - created |
| 7 | + |
| 8 | +concurrency: ${{ github.workflow }}-${{ github.ref }} |
| 9 | + |
| 10 | +jobs: |
| 11 | + snapshot: |
| 12 | + name: Snapshot Release |
| 13 | + if: | |
| 14 | + github.event.issue.pull_request && |
| 15 | + (startsWith(github.event.comment.body, '/snapit') || startsWith(github.event.comment.body, '/snapshot-release')) |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - name: Enforce permission requirement |
| 19 | + uses: prince-chrismc/check-actor-permissions-action@v1 |
| 20 | + with: |
| 21 | + permission: write |
| 22 | + |
| 23 | + - name: Add initial reaction |
| 24 | + uses: peter-evans/create-or-update-comment@v2 |
| 25 | + with: |
| 26 | + comment-id: ${{ github.event.comment.id }} |
| 27 | + reactions: eyes |
| 28 | + |
| 29 | + - name: Validate pull request |
| 30 | + uses: actions/github-script@v6 |
| 31 | + id: pr_data |
| 32 | + env: |
| 33 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 34 | + with: |
| 35 | + script: | |
| 36 | + try { |
| 37 | + const pullRequest = await github.rest.pulls.get({ |
| 38 | + owner: context.repo.owner, |
| 39 | + repo: context.repo.repo, |
| 40 | + pull_number: context.issue.number, |
| 41 | + }) |
| 42 | +
|
| 43 | + // Pull request from fork |
| 44 | + if (context.payload.repository.full_name !== pullRequest.data.head.repo.full_name) { |
| 45 | + const errorMessage = '`/snapit` is not supported on pull requests from forked repositories.' |
| 46 | +
|
| 47 | + await github.rest.issues.createComment({ |
| 48 | + issue_number: context.issue.number, |
| 49 | + owner: context.repo.owner, |
| 50 | + repo: context.repo.repo, |
| 51 | + body: errorMessage, |
| 52 | + }) |
| 53 | +
|
| 54 | + core.setFailed(errorMessage) |
| 55 | + } |
| 56 | + } catch (err) { |
| 57 | + core.setFailed(`Request failed with error ${err}`) |
| 58 | + } |
| 59 | +
|
| 60 | + - name: Checkout default branch |
| 61 | + uses: actions/checkout@v3 |
| 62 | + |
| 63 | + # issue_comment requires us to checkout the branch |
| 64 | + # https://github.com/actions/checkout/issues/331#issuecomment-1120113003 |
| 65 | + - name: Checkout pull request branch |
| 66 | + run: hub pr checkout ${{ github.event.issue.number }} |
| 67 | + env: |
| 68 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 69 | + |
| 70 | + # Because changeset entries are consumed and removed on the |
| 71 | + # 'changeset-release/main' branch, we need to reset the files |
| 72 | + # so the following 'changeset version --snapshot' command will |
| 73 | + # regenerate the package version bumps with the snapshot releases |
| 74 | + - name: Reset changeset entries on changeset-release/main branch |
| 75 | + run: | |
| 76 | + if [[ $(git branch --show-current) == 'changeset-release/main' ]]; then |
| 77 | + git checkout origin/main -- .changeset |
| 78 | + fi |
| 79 | +
|
| 80 | + - name: Setup Node.js |
| 81 | + uses: actions/setup-node@v3 |
| 82 | + with: |
| 83 | + node-version: '18.17.1' |
| 84 | + |
| 85 | + - name: Install dependencies |
| 86 | + run: yarn --frozen-lockfile |
| 87 | + |
| 88 | + - name: Create an .npmrc |
| 89 | + env: |
| 90 | + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 91 | + run: | |
| 92 | + cat << EOF > "$HOME/.npmrc" |
| 93 | + //registry.npmjs.org/:_authToken=$NPM_TOKEN |
| 94 | + EOF |
| 95 | +
|
| 96 | + - name: Create and publish snapshot release |
| 97 | + uses: actions/github-script@v6 |
| 98 | + env: |
| 99 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 100 | + with: |
| 101 | + script: | |
| 102 | + const execa = require('execa') |
| 103 | +
|
| 104 | + const releaseProcess = execa.command('yarn release -- --no-git-tags --snapshot --tag snapshot-release') |
| 105 | + releaseProcess.stdout.pipe(process.stdout) |
| 106 | +
|
| 107 | + const {stdout} = await releaseProcess |
| 108 | +
|
| 109 | + const newTags = Array |
| 110 | + .from(stdout.matchAll(/New tag:\s+([^\s\n]+)/g)) |
| 111 | + .map(([_, tag]) => tag) |
| 112 | +
|
| 113 | + if (newTags.length) { |
| 114 | + const multiple = newTags.length > 1 |
| 115 | +
|
| 116 | + const body = ( |
| 117 | + `🫰✨ **Thanks @${context.actor}! ` + |
| 118 | + `Your snapshot${multiple ? 's have' : ' has'} been published to npm.**\n\n` + |
| 119 | + `Test the snapshot${multiple ? 's' : ''} by updating your \`package.json\` ` + |
| 120 | + `with the newly published version${multiple ? 's' : ''}:\n` + |
| 121 | + newTags.map(tag => ( |
| 122 | + '```sh\n' + |
| 123 | + `yarn add ${tag}\n` + |
| 124 | + '```' |
| 125 | + )).join('\n') |
| 126 | + ) |
| 127 | + await github.rest.issues.createComment({ |
| 128 | + issue_number: context.issue.number, |
| 129 | + owner: context.repo.owner, |
| 130 | + repo: context.repo.repo, |
| 131 | + body, |
| 132 | + }) |
| 133 | + } |
| 134 | +
|
| 135 | + - name: Add final reaction |
| 136 | + uses: peter-evans/create-or-update-comment@v2 |
| 137 | + with: |
| 138 | + comment-id: ${{ github.event.comment.id }} |
| 139 | + reactions: rocket |
0 commit comments