Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,40 @@ export async function run(diff: DiffResponse, repo: Repo): Promise<void> {
digestContent.push(diff.public_url);
}
const digest = shaDigest(digestContent);
const body = buildCommentBody(repo.docDigest, diff, digest);
const body = buildCommentBody(repo, diff, digest);

return repo.createOrUpdateComment(body, digest);
}

function buildCommentBody(docDigest: string, diff: DiffResponse, digest: string) {
function buildCommentBody(repo: Repo, diff: DiffResponse, digest: string) {
const emptySpace = '';
const poweredByBump = '> _Powered by [Bump.sh](https://bump.sh)_';
const text = diff.markdown || 'No structural change, nothing to display.';
const poweredByBump = '###### _Powered by [Bump.sh](https://bump.sh)_';
let text = 'No structural change, nothing to display.';
if (diff.markdown) {
text = `<details open><summary>Structural change details</summary>

return [title(diff)]
.concat([emptySpace, text])
.concat([viewDiffLink(diff), poweredByBump, bumpDiffComment(docDigest, digest)])
${diff.markdown}

</details>`;
}

return [title(diff, repo.doc, repo.hub, repo.branch)]
.concat([viewDiffLink(diff)])
.concat([text, emptySpace])
.concat([poweredByBump, bumpDiffComment(repo.docDigest, digest)])
.join('\n');
}

function title(diff: DiffResponse): string {
const structureTitle = '🤖 API structural change detected:';
const contentTitle = 'ℹ️ API content change detected:';
const breakingTitle = '🚨 Breaking API change detected:';
function title(diff: DiffResponse, doc: string, hub?: string, branch?: string): string {
let docName = [hub, doc].filter((e) => e).join('/');
// Capitalize doc name
docName = docName.charAt(0).toUpperCase() + docName.slice(1);
if (branch) {
docName = `${docName} (branch: ${branch})`;
}
const structureTitle = `### 🤖 ${docName} API structural change detected`;
const contentTitle = `### ℹ️ ${docName} API content change detected`;
const breakingTitle = `### 🚨 Breaking ${docName} API change detected`;

if (diff.breaking) {
return breakingTitle;
Expand Down
6 changes: 6 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export class Repo {
readonly baseSha?: string;
readonly headSha?: string;
private _docDigest: string;
readonly doc: string;
readonly hub: string | undefined;
readonly branch: string | undefined;

constructor(doc: string, hub?: string, branch?: string) {
// Fetch GitHub Action context
Expand All @@ -38,6 +41,9 @@ export class Repo {
this.headSha = pull_request.head.sha;
}
this.octokit = this.getOctokit();
this.doc = doc;
this.hub = hub;
this.branch = branch;
this._docDigest = shaDigest([doc, hub, branch]);
}

Expand Down
44 changes: 28 additions & 16 deletions tests/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,26 @@ describe('diff.ts', () => {
};
const digest = '4b81e612cafa6580f8ad3bfe9e970b2d961f58c2';

const repo = new Repo('hello');
const docDigest = shaDigest(['hello']);
const repo = new Repo('hello', '', 'v2');
const docDigest = shaDigest(['hello', '', 'v2']);

await diff.run(result, repo);

expect(repo.createOrUpdateComment).toHaveBeenCalledWith(
`🤖 API structural change detected:
`### 🤖 Hello (branch: v2) API structural change detected

[Preview documentation](https://bump.sh/doc/my-doc/changes/654)

<details open><summary>Structural change details</summary>

* one
* two
* three


[Preview documentation](https://bump.sh/doc/my-doc/changes/654)
</details>

> _Powered by [Bump.sh](https://bump.sh)_
###### _Powered by [Bump.sh](https://bump.sh)_
<!-- Bump.sh digest=${digest} doc=${docDigest} -->`,
digest,
);
Expand All @@ -72,18 +76,18 @@ describe('diff.ts', () => {
};
const digest = '3999a0fe6ad27841bd6342128f7028ab2cea1c57';

const repo = new Repo('hello');
const docDigest = shaDigest(['hello']);
const repo = new Repo('hello', 'my-hub', 'v1');
const docDigest = shaDigest(['hello', 'my-hub', 'v1']);
await diff.run(result, repo);

expect(repo.createOrUpdateComment).toHaveBeenCalledWith(
`ℹ️ API content change detected:

No structural change, nothing to display.
`### ℹ️ My-hub/hello (branch: v1) API content change detected

[Preview documentation](https://bump.sh/doc/my-doc/changes/654)

> _Powered by [Bump.sh](https://bump.sh)_
No structural change, nothing to display.

###### _Powered by [Bump.sh](https://bump.sh)_
<!-- Bump.sh digest=${digest} doc=${docDigest} -->`,
digest,
);
Expand All @@ -105,16 +109,20 @@ No structural change, nothing to display.
await diff.run(result, repo);

expect(repo.createOrUpdateComment).toHaveBeenCalledWith(
`🚨 Breaking API change detected:
`### 🚨 Breaking Hello API change detected

[Preview documentation](https://bump.sh/doc/my-doc/changes/654)

<details open><summary>Structural change details</summary>

* one
* two
* three


[Preview documentation](https://bump.sh/doc/my-doc/changes/654)
</details>

> _Powered by [Bump.sh](https://bump.sh)_
###### _Powered by [Bump.sh](https://bump.sh)_
<!-- Bump.sh digest=${digest} doc=${docDigest} -->`,
digest,
);
Expand All @@ -136,14 +144,18 @@ No structural change, nothing to display.
await diff.run(result, repo);

expect(repo.createOrUpdateComment).toHaveBeenCalledWith(
`🤖 API structural change detected:
`### 🤖 Hello API structural change detected

<details open><summary>Structural change details</summary>

* one
* two
* three


> _Powered by [Bump.sh](https://bump.sh)_
</details>

###### _Powered by [Bump.sh](https://bump.sh)_
<!-- Bump.sh digest=${digest} doc=${docDigest} -->`,
digest,
);
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const mockCreateOrUpdateComment = jest.fn<repo.Repo['createOrUpdateCommen
export const mockDeleteExistingComment = jest.fn<repo.Repo['deleteExistingComment']>();
export const Repo = jest.fn().mockImplementation(() => {
return {
doc: 'my-doc',
getBaseFile: mockGetBaseFile,
createOrUpdateComment: mockCreateOrUpdateComment,
deleteExistingComment: mockDeleteExistingComment,
Expand Down
40 changes: 28 additions & 12 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,17 @@ describe('main.ts', () => {
expect(repo.Repo).toHaveBeenCalledWith('my-doc', '', '');
expect(repo.mockGetBaseFile).toHaveBeenCalledWith(file);
expect(repo.mockCreateOrUpdateComment).toHaveBeenCalledWith(
`🚨 Breaking API change detected:
`### 🚨 Breaking My-doc API change detected

[Preview documentation](https://bump.sh/doc/my-doc/changes/654)

<details open><summary>Structural change details</summary>

one

[Preview documentation](https://bump.sh/doc/my-doc/changes/654)
</details>

> _Powered by [Bump.sh](https://bump.sh)_
###### _Powered by [Bump.sh](https://bump.sh)_
<!-- Bump.sh digest=${commentDigest} doc=undefined -->`,
commentDigest,
);
Expand Down Expand Up @@ -303,13 +307,17 @@ one
expect(repo.Repo).toHaveBeenCalledWith('my-doc', '', '');
expect(repo.mockGetBaseFile).toHaveBeenCalledWith(file);
expect(repo.mockCreateOrUpdateComment).toHaveBeenCalledWith(
`🚨 Breaking API change detected:
`### 🚨 Breaking My-doc API change detected

[Preview documentation](https://bump.sh/doc/my-doc/changes/654)

<details open><summary>Structural change details</summary>

one

[Preview documentation](https://bump.sh/doc/my-doc/changes/654)
</details>

> _Powered by [Bump.sh](https://bump.sh)_
###### _Powered by [Bump.sh](https://bump.sh)_
<!-- Bump.sh digest=${commentDigest} doc=undefined -->`,
commentDigest,
);
Expand Down Expand Up @@ -337,13 +345,17 @@ one
expect(repo.Repo).toHaveBeenCalledWith('', '', 'latest');
expect(repo.mockGetBaseFile).toHaveBeenCalledWith(file);
expect(repo.mockCreateOrUpdateComment).toHaveBeenCalledWith(
`🚨 Breaking API change detected:
`### 🚨 Breaking My-doc API change detected

[Preview documentation](https://bump.sh/doc/my-doc/changes/654)

<details open><summary>Structural change details</summary>

one

[Preview documentation](https://bump.sh/doc/my-doc/changes/654)
</details>

> _Powered by [Bump.sh](https://bump.sh)_
###### _Powered by [Bump.sh](https://bump.sh)_
<!-- Bump.sh digest=${commentDigest} doc=undefined -->`,
commentDigest,
);
Expand Down Expand Up @@ -372,13 +384,17 @@ one

expect(repo.Repo).toHaveBeenCalledWith('', '', '');
expect(repo.mockCreateOrUpdateComment).toHaveBeenCalledWith(
`🚨 Breaking API change detected:
`### 🚨 Breaking My-doc API change detected

[Preview documentation](https://bump.sh/doc/my-doc/changes/654)

<details open><summary>Structural change details</summary>

one

[Preview documentation](https://bump.sh/doc/my-doc/changes/654)
</details>

> _Powered by [Bump.sh](https://bump.sh)_
###### _Powered by [Bump.sh](https://bump.sh)_
<!-- Bump.sh digest=${commentDigest} doc=undefined -->`,
commentDigest,
);
Expand Down