|
| 1 | +name: Prepare release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + tag: |
| 7 | + description: New version tag |
| 8 | + required: true |
| 9 | + |
| 10 | +jobs: |
| 11 | + prepare_release: |
| 12 | + name: Create release PR |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Get GitHub App token |
| 16 | + id: get_token |
| 17 | + uses: tibdex/github-app-token@v1 |
| 18 | + with: |
| 19 | + app_id: ${{ secrets.PIPELINE_GITHUB_APP_ID }} |
| 20 | + private_key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }} |
| 21 | + - name: Create PR |
| 22 | + uses: actions/github-script@v5 |
| 23 | + env: |
| 24 | + RELEASE_TAG: ${{ github.event.inputs.tag }} |
| 25 | + with: |
| 26 | + github-token: ${{ steps.get_token.outputs.token }} |
| 27 | + script: | |
| 28 | + const { data: notes } = await github.rest.repos.generateReleaseNotes({ |
| 29 | + owner: context.repo.owner, |
| 30 | + repo: context.repo.repo, |
| 31 | + tag_name: process.env.RELEASE_TAG, |
| 32 | + }); |
| 33 | +
|
| 34 | + const today = new Date().toJSON().slice(0, 10); |
| 35 | + const header = [`# Changelog\n\n## ${process.env.RELEASE_TAG} / ${today}\n`]; |
| 36 | + const changes = header.concat(notes.body.split("\n").slice(3)); |
| 37 | +
|
| 38 | + const { data: content } = await github.rest.repos.getContent({ |
| 39 | + owner: context.repo.owner, |
| 40 | + repo: context.repo.repo, |
| 41 | + path: "CHANGELOG.md", |
| 42 | + }); |
| 43 | +
|
| 44 | + const rawContent = Buffer.from(content.content, "base64") |
| 45 | + .toString("utf-8") |
| 46 | + .split("\n"); |
| 47 | + const newContent = changes.concat(rawContent.slice(1)).join("\n"); |
| 48 | +
|
| 49 | + const { data: master } = await github.rest.git.getRef({ |
| 50 | + owner: context.repo.owner, |
| 51 | + repo: context.repo.repo, |
| 52 | + ref: "heads/master", |
| 53 | + }); |
| 54 | + await github.rest.git.createRef({ |
| 55 | + owner: context.repo.owner, |
| 56 | + repo: context.repo.repo, |
| 57 | + ref: `refs/heads/release/${process.env.RELEASE_TAG}`, |
| 58 | + sha: master.object.sha, |
| 59 | + }); |
| 60 | +
|
| 61 | + const { data: commit } = await github.rest.repos.createOrUpdateFileContents({ |
| 62 | + owner: context.repo.owner, |
| 63 | + repo: context.repo.repo, |
| 64 | + message: "Update CHANGELOG", |
| 65 | + content: Buffer.from(newContent).toString("base64"), |
| 66 | + path: "CHANGELOG.md", |
| 67 | + branch: `release/${process.env.RELEASE_TAG}`, |
| 68 | + sha: content.sha, |
| 69 | + }); |
| 70 | +
|
| 71 | + const { data: pr } = await github.rest.pulls.create({ |
| 72 | + owner: context.repo.owner, |
| 73 | + repo: context.repo.repo, |
| 74 | + head: `release/${process.env.RELEASE_TAG}`, |
| 75 | + base: "master", |
| 76 | + title: `Release ${process.env.RELEASE_TAG}`, |
| 77 | + body: "Update CHANGELOG", |
| 78 | + }); |
| 79 | + await github.rest.issues.addLabels({ |
| 80 | + issue_number: pr.number, |
| 81 | + owner: context.repo.owner, |
| 82 | + repo: context.repo.repo, |
| 83 | + labels: ["changelog/no-changelog"], |
| 84 | + }); |
0 commit comments