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