Skip to content

Commit 502544c

Browse files
committed
Diff: Use doc_name when returned by the CLI
When diff is requested to the CLI, doc.id may has been used as doc parameter. In this case, GitHub comment is not easy to read, api.id has no sense on this comment. We add a test in the docName logic (moved in its own function as tiny refacto), and favor DiffResponse.doc_name when provided. use Diff.DiffResult - import Diff instead of explicit type DiffResponse - use Diff.DiffResult Require version v2.10.1 of bump-cli (cf previous commit) cf related PR on CLI bump-sh/cli#828
1 parent 48a8e84 commit 502544c

4 files changed

Lines changed: 67 additions & 20 deletions

File tree

src/diff.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Repo } from './github.js';
2-
import type { DiffResponse } from 'bump-cli';
2+
import { Diff } from 'bump-cli';
33
import { bumpDiffComment, shaDigest } from './common.js';
44

5-
export async function run(diff: DiffResponse, repo: Repo): Promise<void> {
5+
export async function run(diff: Diff.DiffResult, repo: Repo): Promise<void> {
66
const digestContent = [diff.markdown];
77
if (diff.public_url) {
88
digestContent.push(diff.public_url);
@@ -13,7 +13,7 @@ export async function run(diff: DiffResponse, repo: Repo): Promise<void> {
1313
return repo.createOrUpdateComment(body, digest);
1414
}
1515

16-
function buildCommentBody(repo: Repo, diff: DiffResponse, digest: string) {
16+
function buildCommentBody(repo: Repo, diff: Diff.DiffResult, digest: string) {
1717
const emptySpace = '';
1818
const poweredByBump = '###### _Powered by [Bump.sh](https://bump.sh)_';
1919
let text = 'No structural change, nothing to display.';
@@ -25,20 +25,14 @@ ${diff.markdown}
2525
</details>`;
2626
}
2727

28-
return [title(diff, repo.doc, repo.hub, repo.branch)]
28+
return [title(diff, docName(diff, repo.doc, repo.hub, repo.branch))]
2929
.concat([viewDiffLink(diff)])
3030
.concat([text, emptySpace])
3131
.concat([poweredByBump, bumpDiffComment(repo.docDigest, digest)])
3232
.join('\n');
3333
}
3434

35-
function title(diff: DiffResponse, doc: string, hub?: string, branch?: string): string {
36-
let docName = [hub, doc].filter((e) => e).join('/');
37-
// Capitalize doc name
38-
docName = docName.charAt(0).toUpperCase() + docName.slice(1);
39-
if (branch) {
40-
docName = `${docName} (branch: ${branch})`;
41-
}
35+
function title(diff: Diff.DiffResult, docName: string): string {
4236
const structureTitle = `### 🤖 ${docName} API structural change detected`;
4337
const contentTitle = `### ℹ️ ${docName} API content change detected`;
4438
const breakingTitle = `### 🚨 Breaking ${docName} API change detected`;
@@ -52,7 +46,7 @@ function title(diff: DiffResponse, doc: string, hub?: string, branch?: string):
5246
}
5347
}
5448

55-
function viewDiffLink(diff: DiffResponse): string {
49+
function viewDiffLink(diff: Diff.DiffResult): string {
5650
if (diff.public_url) {
5751
return `
5852
[Preview documentation](${diff.public_url!})
@@ -61,3 +55,23 @@ function viewDiffLink(diff: DiffResponse): string {
6155
return '';
6256
}
6357
}
58+
59+
function docName(
60+
diff: Diff.DiffResult,
61+
doc: string,
62+
hub?: string,
63+
branch?: string,
64+
): string {
65+
const docNameFromDiff = diff.doc_name;
66+
let name: string;
67+
if (docNameFromDiff) {
68+
name = docNameFromDiff;
69+
} else {
70+
name = [hub, doc].filter((e) => e).join('/');
71+
name = name.charAt(0).toUpperCase() + name.slice(1);
72+
}
73+
if (branch) {
74+
name = `${name} (branch: ${branch})`;
75+
}
76+
return name;
77+
}

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export async function run(): Promise<void> {
100100
overlays1,
101101
overlays2,
102102
)
103-
.then((result: bump.DiffResponse | undefined) => {
103+
.then((result: bump.Diff.DiffResult | undefined) => {
104104
if (result) {
105105
diff.run(result, repo).catch(handleErrors);
106106
} else {

tests/diff.test.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('diff.ts', () => {
3232
});
3333

3434
test('test github diff run process', async () => {
35-
const result: bump.DiffResponse = {
35+
const result: bump.Diff.DiffResult = {
3636
id: '123abc',
3737
markdown: `* one
3838
* two
@@ -60,6 +60,38 @@ describe('diff.ts', () => {
6060
* three
6161
6262
63+
</details>
64+
65+
###### _Powered by [Bump.sh](https://bump.sh)_
66+
<!-- Bump.sh digest=${digest} doc=${docDigest} -->`,
67+
digest,
68+
);
69+
});
70+
71+
test('test github diff run process (with doc_name)', async () => {
72+
const result: bump.Diff.DiffResult = {
73+
id: '123abc',
74+
markdown: `**Doc has a name**`,
75+
public_url: 'https://bump.sh/doc/my-doc/changes/654',
76+
breaking: false,
77+
doc_name: 'My API Documentation',
78+
};
79+
const digest = 'ccc5f7d8e472d46cd38e108bf43fbc46243807a2';
80+
81+
const repo = new Repo('id-42', '', 'v2');
82+
const docDigest = shaDigest(['id-42', '', 'v2']);
83+
84+
await diff.run(result, repo);
85+
86+
expect(repo.createOrUpdateComment).toHaveBeenCalledWith(
87+
`### 🤖 My API Documentation (branch: v2) API structural change detected
88+
89+
[Preview documentation](https://bump.sh/doc/my-doc/changes/654)
90+
91+
<details open><summary>Structural change details</summary>
92+
93+
**Doc has a name**
94+
6395
</details>
6496
6597
###### _Powered by [Bump.sh](https://bump.sh)_
@@ -69,10 +101,11 @@ describe('diff.ts', () => {
69101
});
70102

71103
test('test github diff with no structural change', async () => {
72-
const result: bump.DiffResponse = {
104+
const result: bump.Diff.DiffResult = {
73105
id: '123abc',
74106
public_url: 'https://bump.sh/doc/my-doc/changes/654',
75107
breaking: false,
108+
doc_name: 'My API Documentation',
76109
};
77110
const digest = '3999a0fe6ad27841bd6342128f7028ab2cea1c57';
78111

@@ -81,7 +114,7 @@ describe('diff.ts', () => {
81114
await diff.run(result, repo);
82115

83116
expect(repo.createOrUpdateComment).toHaveBeenCalledWith(
84-
`### ℹ️ My-hub/hello (branch: v1) API content change detected
117+
`### ℹ️ My API Documentation (branch: v1) API content change detected
85118
86119
[Preview documentation](https://bump.sh/doc/my-doc/changes/654)
87120
@@ -94,7 +127,7 @@ No structural change, nothing to display.
94127
});
95128

96129
test('test github diff with breaking changes', async () => {
97-
const result: bump.DiffResponse = {
130+
const result: bump.Diff.DiffResult = {
98131
id: '123abc',
99132
markdown: `* one
100133
* two
@@ -129,7 +162,7 @@ No structural change, nothing to display.
129162
});
130163

131164
test('test github diff without public url', async () => {
132-
const result: bump.DiffResponse = {
165+
const result: bump.Diff.DiffResult = {
133166
id: '123abc',
134167
markdown: `* one
135168
* two

tests/main.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ nock.disableNetConnect();
1111
// Mock stdout/stderr
1212
stdout.start();
1313

14-
import type { DiffResponse } from 'bump-cli';
14+
import { Diff } from 'bump-cli';
1515

1616
// Mock the Bump CLI commands
1717
jest.unstable_mockModule('bump-cli', () => bump);
@@ -20,7 +20,7 @@ jest.unstable_mockModule('@actions/github', () => github);
2020
jest.unstable_mockModule('../src/github.js', () => repo);
2121

2222
const originalGhToken = process.env['GITHUB_TOKEN'];
23-
const diffExample: DiffResponse = {
23+
const diffExample: Diff.DiffResult = {
2424
id: 'hello-123',
2525
markdown: 'one',
2626
public_url: 'https://bump.sh/doc/my-doc/changes/654',

0 commit comments

Comments
 (0)