|
| 1 | +name: changelog |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + create: |
| 7 | + description: Add a log to the changelog |
| 8 | + type: boolean |
| 9 | + required: false |
| 10 | + default: false |
| 11 | + update: |
| 12 | + description: Update the existing changelog |
| 13 | + type: boolean |
| 14 | + required: false |
| 15 | + default: false |
| 16 | + |
| 17 | +jobs: |
| 18 | + changelog: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + env: |
| 21 | + file: CHANGELOG.md |
| 22 | + steps: |
| 23 | + - uses: actions/checkout@v3 |
| 24 | + with: |
| 25 | + fetch-depth: 0 |
| 26 | + - name: Check ${{ env.file }} |
| 27 | + run: | |
| 28 | + if [[ $(git diff --name-only origin/master HEAD -- ${{ env.file }} | grep '^${{ env.file }}$' -c) -eq 0 ]]; then |
| 29 | + echo "Expected '${{ env.file }}' to be modified" |
| 30 | + exit 1 |
| 31 | + fi |
| 32 | + update: |
| 33 | + runs-on: ubuntu-latest |
| 34 | + needs: changelog |
| 35 | + if: (inputs.create && failure()) || (inputs.update && success()) |
| 36 | + continue-on-error: true |
| 37 | + env: |
| 38 | + file: CHANGELOG.md |
| 39 | + next_version: next |
| 40 | + link: '[#${{ github.event.number }}](https://github.com/fabricjs/fabric.js/pull/${{ github.event.number }})' |
| 41 | + steps: |
| 42 | + - uses: actions/checkout@v3 |
| 43 | + with: |
| 44 | + ref: ${{ github.event.pull_request.head.ref }} |
| 45 | + - name: Update ${{ env.file }} from PR title |
| 46 | + id: update |
| 47 | + uses: actions/github-script@v6 |
| 48 | + env: |
| 49 | + log: '- ${{ github.event.pull_request.title }} ${{ env.link }}\n' |
| 50 | + prev_log: '- ${{ github.event.changes.title.from }} ${{ env.link }}\n' |
| 51 | + with: |
| 52 | + result-encoding: string |
| 53 | + script: | |
| 54 | + const fs = require('fs'); |
| 55 | + const file = './${{ env.file }}'; |
| 56 | + let content = fs.readFileSync(file).toString(); |
| 57 | + const title = '[${{ env.next_version }}]'; |
| 58 | + const log = '${{ env.log }}'; |
| 59 | + let exists = ${{ needs.changelog.result == 'success' }}; |
| 60 | +
|
| 61 | + if (!content.includes(title)) { |
| 62 | + const insertAt = content.indexOf('\n') + 1; |
| 63 | + content = |
| 64 | + content.slice(0, insertAt) + |
| 65 | + `\n## ${title}\n\n\n` + |
| 66 | + content.slice(insertAt); |
| 67 | + } |
| 68 | +
|
| 69 | + const insertAt = content.indexOf('\n', content.indexOf(title) + title.length + 1) + 1; |
| 70 | + if (exists && ${{ github.event.action == 'edited' }}) { |
| 71 | + const prevLog = '${{ env.prev_log }}'; |
| 72 | + const index = content.indexOf(prevLog, insertAt); |
| 73 | + if (index > -1) { |
| 74 | + content = content.slice(0, index) + content.slice(index + prevLog.length); |
| 75 | + exists = false; |
| 76 | + } |
| 77 | + } |
| 78 | +
|
| 79 | + if (!exists) { |
| 80 | + content = content.slice(0, insertAt) + log + content.slice(insertAt); |
| 81 | + fs.writeFileSync(file, content); |
| 82 | + return true; |
| 83 | + } |
| 84 | +
|
| 85 | + return false; |
| 86 | + - name: Setup node |
| 87 | + if: fromJson(steps.update.outputs.result) |
| 88 | + uses: actions/setup-node@v3 |
| 89 | + with: |
| 90 | + node-version: 18.x |
| 91 | + - name: Commit & Push |
| 92 | + if: fromJson(steps.update.outputs.result) |
| 93 | + run: | |
| 94 | + npm ci |
| 95 | + npx prettier --write ${{ env.file }} |
| 96 | + git config user.name github-actions[bot] |
| 97 | + git config user.email github-actions[bot]@users.noreply.github.com |
| 98 | + git add ${{ env.file }} |
| 99 | + git commit -m "update ${{ env.file }}" |
| 100 | + git push |
0 commit comments