diff --git a/src/diff.ts b/src/diff.ts index 0ad6bde7..72a2a869 100644 --- a/src/diff.ts +++ b/src/diff.ts @@ -8,26 +8,40 @@ export async function run(diff: DiffResponse, repo: Repo): Promise { 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 = `
Structural change details - return [title(diff)] - .concat([emptySpace, text]) - .concat([viewDiffLink(diff), poweredByBump, bumpDiffComment(docDigest, digest)]) +${diff.markdown} + +
`; + } + + 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; diff --git a/src/github.ts b/src/github.ts index 6d34f321..25ed2e76 100644 --- a/src/github.ts +++ b/src/github.ts @@ -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 @@ -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]); } diff --git a/tests/diff.test.ts b/tests/diff.test.ts index 60809cc8..6fd2f7b3 100644 --- a/tests/diff.test.ts +++ b/tests/diff.test.ts @@ -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) + +
Structural change details * one * two * three -[Preview documentation](https://bump.sh/doc/my-doc/changes/654) +
-> _Powered by [Bump.sh](https://bump.sh)_ +###### _Powered by [Bump.sh](https://bump.sh)_ `, digest, ); @@ -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)_ `, digest, ); @@ -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) + +
Structural change details * one * two * three -[Preview documentation](https://bump.sh/doc/my-doc/changes/654) +
-> _Powered by [Bump.sh](https://bump.sh)_ +###### _Powered by [Bump.sh](https://bump.sh)_ `, digest, ); @@ -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 + +
Structural change details * one * two * three -> _Powered by [Bump.sh](https://bump.sh)_ +
+ +###### _Powered by [Bump.sh](https://bump.sh)_ `, digest, ); diff --git a/tests/fixtures/repo.ts b/tests/fixtures/repo.ts index bb3b7d32..3742f380 100644 --- a/tests/fixtures/repo.ts +++ b/tests/fixtures/repo.ts @@ -6,6 +6,7 @@ export const mockCreateOrUpdateComment = jest.fn(); export const Repo = jest.fn().mockImplementation(() => { return { + doc: 'my-doc', getBaseFile: mockGetBaseFile, createOrUpdateComment: mockCreateOrUpdateComment, deleteExistingComment: mockDeleteExistingComment, diff --git a/tests/main.test.ts b/tests/main.test.ts index 015bea33..5c651381 100644 --- a/tests/main.test.ts +++ b/tests/main.test.ts @@ -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) + +
Structural change details one -[Preview documentation](https://bump.sh/doc/my-doc/changes/654) +
-> _Powered by [Bump.sh](https://bump.sh)_ +###### _Powered by [Bump.sh](https://bump.sh)_ `, commentDigest, ); @@ -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) + +
Structural change details one -[Preview documentation](https://bump.sh/doc/my-doc/changes/654) +
-> _Powered by [Bump.sh](https://bump.sh)_ +###### _Powered by [Bump.sh](https://bump.sh)_ `, commentDigest, ); @@ -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) + +
Structural change details one -[Preview documentation](https://bump.sh/doc/my-doc/changes/654) +
-> _Powered by [Bump.sh](https://bump.sh)_ +###### _Powered by [Bump.sh](https://bump.sh)_ `, commentDigest, ); @@ -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) + +
Structural change details one -[Preview documentation](https://bump.sh/doc/my-doc/changes/654) +
-> _Powered by [Bump.sh](https://bump.sh)_ +###### _Powered by [Bump.sh](https://bump.sh)_ `, commentDigest, );