-
-
Notifications
You must be signed in to change notification settings - Fork 217
174 lines (136 loc) · 5.81 KB
/
update-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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
name: "Update Changelog"
on:
release:
types:
- published
permissions:
id-token: write
contents: write
pull-requests: write
jobs:
update-changelog:
runs-on: ["ubuntu-latest"]
steps:
- name: Current Release
id: current-release
uses: actions/github-script@v6
with:
script: |
const event = ${{ toJSON(github.event) }};
const tag = event.release.tag_name;
const body = event.release.body;
console.log(`Current release tag: ${tag}`);
console.log(`Current release body: ${body}`);
core.setOutput('tag', tag);
core.setOutput('body', Buffer.from(body).toString('base64'));
- name: Previous Release
id: previous-release
uses: actions/github-script@v6
with:
script: |
const releases = await github.rest.repos.listReleases({
...context.repo
});
const currentReleaseIndex = releases.data.findIndex(release => "${{ steps.current-release.outputs.tag }}" === context.payload.release.tag_name);
const previousRelease = releases.data[currentReleaseIndex + 1];
const tag = previousRelease.tag_name;
console.log(`Previous release tag: ${tag}`);
core.setOutput('tag', tag);
- name: Checkout Current Release
uses: actions/checkout@v3
with:
ref: "${{ steps.current-release.outputs.tag }}"
path: current
- name: Checkout Previous Release
uses: actions/checkout@v3
with:
ref: "${{ steps.previous-release.outputs.tag }}"
path: previous
- name: Find Updated CHANGELOG.md files
id: updated
uses: actions/github-script@v6
with:
script: |
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
function findChangelogs(dir, fileList = []) {
const files = fs.readdirSync(dir);
files.forEach(file => {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
fileList = findChangelogs(path.join(dir, file), fileList);
} else if (file === 'CHANGELOG.md') {
fileList.push(path.join(dir, file));
}
});
return fileList;
}
function calculateHash(file) {
const hash = crypto.createHash('md5');
const fileContent = fs.readFileSync(file);
hash.update(fileContent);
return hash.digest('hex');
}
function trimPath(relativePath) {
return relativePath
.replace('modules/', '')
.replace('/CHANGELOG.md', '');
}
const currentChangeLogFiles = findChangelogs('./current/modules');
const components = [];
for (let i = 0; i < currentChangeLogFiles.length; i++) {
const currentReleaseFile = currentChangeLogFiles[i];
const relativePath = currentReleaseFile.replace(/^current\//, '');
const previousReleaseFile = `previous/${relativePath}`
if (!fs.existsSync(previousReleaseFile)) {
console.log(`New CHANGELOG.md found: ${relativePath}`);
components.push(trimPath(relativePath));
continue;
}
if (calculateHash(currentReleaseFile) !== calculateHash(previousReleaseFile)) {
console.log(`CHANGELOG.md changed: ${relativePath}`);
components.push(trimPath(relativePath));
} else {
console.log(`${relativePath} didn't change. Skipping ...`);
}
}
core.setOutput('components', JSON.stringify(components));
- name: Checkout
uses: actions/checkout@v3
- name: Generate Changelog
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const tag = "${{ steps.current-release.outputs.tag }}";
let body = Buffer.from("${{ steps.current-release.outputs.body }}", 'base64').toString('utf-8');
const updatedComponents = JSON.parse(`${{ steps.updated.outputs.components }}`);
let affectedComponents = "";
if (updatedComponents.length > 0) {
affectedComponents += "\n\n";
affectedComponents += "## Affected Components\n";
for (let i = 0; i < updatedComponents.length; i++) {
const relativePath = updatedComponents[i];
affectedComponents += `- [${relativePath}](https://docs.cloudposse.com/components/library/aws/${relativePath}#changelog)\n`
}
affectedComponents += "\n\n";
}
const content = `\n## ${tag}\n\n${affectedComponents}\n\n${body}\n`
console.log(content);
const filePath = 'CHANGELOG.md'
const fileContent = fs.readFileSync(filePath, 'utf-8');
const lines = fileContent.split('\n');
lines.splice(1, 0, content);
const updatedContent = lines.join('\n');
fs.writeFileSync(filePath, updatedContent, 'utf-8');
- name: Create Pull Request
uses: peter-evans/create-pull-request@v6
with:
title: 'Update Changelog for `${{ steps.current-release.outputs.tag }}`'
body: 'Update Changelog for [`${{ steps.current-release.outputs.tag }}`](${{ github.event.release.html_url }})'
base: main
branch: "changelog/${{ steps.current-release.outputs.tag }}"
delete-branch: "true"
commit-message: "Update Changelog for ${{ steps.current-release.outputs.tag }}"
labels: |
no-release