Skip to content

Commit 2a192e4

Browse files
authored
Merge pull request #2727 from hey-api/chore/changeset-fix-4
chore: fix changelog
2 parents 596c3e8 + 210b96a commit 2a192e4

File tree

3 files changed

+105
-4
lines changed

3 files changed

+105
-4
lines changed

.changeset/changelog.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export default {
3333
/** @type string[] */
3434
const usersFromSummary = [];
3535

36+
// Remove PR, commit, author/user lines from summary
3637
const replacedChangelog = changeset.summary
3738
.replace(/^\s*(?:pr|pull|pull\s+request):\s*#?(\d+)/im, (_, pr) => {
3839
const num = Number(pr);
@@ -68,7 +69,12 @@ export default {
6869
}
6970
const commitToFetchFrom = commitFromSummary || changeset.commit;
7071
if (commitToFetchFrom) {
71-
const { links } = await getInfo({ commit: commitToFetchFrom, repo });
72+
let { links } = await getInfo({ commit: commitToFetchFrom, repo });
73+
const shortCommitId = commitToFetchFrom.slice(0, 7);
74+
links = {
75+
...links,
76+
commit: `[\`${shortCommitId}\`](https://github.com/${repo}/commit/${commitToFetchFrom})`,
77+
};
7278
return links;
7379
}
7480
return {
@@ -93,6 +99,31 @@ export default {
9399
users === null ? '' : ` by ${users}`,
94100
].join('');
95101

96-
return `\n- ${replacedChangelog}${metadata}`;
102+
// Split summary into first line and the rest
103+
const [firstLine, ...rest] = replacedChangelog.split('\n');
104+
const restSummary = rest.join('\n').trim();
105+
106+
// Post-process code blocks: replace triple backtick code blocks with indented code blocks
107+
function convertCodeBlocks(text) {
108+
// Replace ```lang\n...\n``` with indented code
109+
return text.replace(/```(\w*)\n([\s\S]*?)```/g, (match, lang, code) => {
110+
const langComment = lang ? `// ${lang}\n` : '';
111+
return (
112+
'\n' +
113+
langComment +
114+
code
115+
.split('\n')
116+
.map((line) => ' ' + line)
117+
.join('\n') +
118+
'\n'
119+
);
120+
});
121+
}
122+
123+
let releaseLine = `\n- ${firstLine}${metadata}`;
124+
if (restSummary) {
125+
releaseLine += '\n\n' + convertCodeBlocks(restSummary);
126+
}
127+
return releaseLine;
97128
},
98129
};

__tests__/changelog.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,76 @@ describe('changelog', () => {
134134
);
135135
});
136136

137+
it('places metadata on the first line and does not append it after code blocks', async () => {
138+
const summary = [
139+
'refactor(config): replace `off` with null to disable options',
140+
'',
141+
'### Updated `output` options',
142+
'',
143+
'We made the `output` configuration more consistent by using `null` to represent disabled options. [This change](https://heyapi.dev/openapi-ts/migrating#updated-output-options) does not affect boolean options.',
144+
'',
145+
'```js',
146+
'export default {',
147+
' input: "hey-api/backend", // sign up at app.heyapi.dev',
148+
' output: {',
149+
' format: null,',
150+
' lint: null,',
151+
' path: "src/client",',
152+
' tsConfigPath: null,',
153+
' },',
154+
'};',
155+
'```',
156+
].join('\n');
157+
const changeset = {
158+
commit: 'fcdd73b816d74babf47e6a1f46032f5b8ebb4b48',
159+
id: 'fake-id',
160+
releases: [],
161+
summary,
162+
};
163+
const line = await changelog.getReleaseLine(changeset, 'minor', {
164+
repo: 'hey-api/openapi-ts',
165+
});
166+
// Metadata should be on the first line
167+
expect(line).toMatch(
168+
/^\n- refactor\(config\): replace `off` with null to disable options \(\[#1613\]\(https:\/\/github.com\/hey-api\/openapi-ts\/pull\/1613\)\) \(\[`fcdd73b`\]\(https:\/\/github.com\/hey-api\/openapi-ts\/commit\/fcdd73b816d74babf47e6a1f46032f5b8ebb4b48\)\) by \[@someone\]\(https:\/\/github.com\/someone\)/,
169+
);
170+
// There should be no metadata at the end
171+
expect(line.trim().endsWith('```')).toBe(false);
172+
// Should not contain quadruple backticks
173+
expect(line).not.toContain('````');
174+
// Should contain indented code block
175+
expect(line).toContain(' export default {');
176+
});
177+
178+
it('converts multiple code blocks and preserves non-code content', async () => {
179+
const summary = [
180+
'feat: add foo',
181+
'',
182+
'```js',
183+
'console.log(1);',
184+
'```',
185+
'',
186+
'Some text.',
187+
'',
188+
'```ts',
189+
'console.log(2);',
190+
'```',
191+
].join('\n');
192+
const changeset = {
193+
commit: 'abc123',
194+
id: 'fake-id',
195+
releases: [],
196+
summary,
197+
};
198+
const line = await changelog.getReleaseLine(changeset, 'minor', {
199+
repo: 'hey-api/openapi-ts',
200+
});
201+
expect(line).toContain(' console.log(1);');
202+
expect(line).toContain(' console.log(2);');
203+
expect(line).toContain('Some text.');
204+
expect(line).not.toContain('```');
205+
});
206+
137207
describe.each(['author', 'user'])(
138208
'override author with %s keyword',
139209
(keyword) => {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
"lint:fix": "prettier --check --write . && eslint . --fix",
2828
"lint": "prettier --check . && eslint .",
2929
"prepare": "husky",
30-
"test:changelog": "vitest run .changeset",
31-
"test:changelog:watch": "vitest watch .changeset",
30+
"test:changelog": "vitest run __tests__/*.test.ts",
31+
"test:changelog:watch": "vitest watch __tests__/*.test.ts",
3232
"test:coverage": "turbo run test:coverage",
3333
"test:update": "turbo run test:update",
3434
"test:watch": "turbo run test:watch",

0 commit comments

Comments
 (0)