diff --git a/src/github.ts b/src/github.ts index 9906c654..6d34f321 100644 --- a/src/github.ts +++ b/src/github.ts @@ -4,7 +4,7 @@ import * as github from '@actions/github'; import * as io from '@actions/io'; import { GetResponseDataTypeFromEndpointMethod } from '@octokit/types'; import { GitHub } from '@actions/github/lib/utils.js'; -import { extractBumpDigest, fsExists } from './common.js'; +import { extractBumpDigest, fsExists, shaDigest } from './common.js'; // These are types which are not exposed directly by Github libs // which we need to define @@ -24,7 +24,7 @@ export class Repo { readonly headSha?: string; private _docDigest: string; - constructor(docDigest: string) { + constructor(doc: string, hub?: string, branch?: string) { // Fetch GitHub Action context // from GITHUB_REPOSITORY & GITHUB_EVENT_PATH const { owner, repo } = github.context.repo; @@ -38,7 +38,7 @@ export class Repo { this.headSha = pull_request.head.sha; } this.octokit = this.getOctokit(); - this._docDigest = docDigest; + this._docDigest = shaDigest([doc, hub, branch]); } public get docDigest() { diff --git a/src/main.ts b/src/main.ts index a0ab3247..19809db9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,7 +5,6 @@ import * as bump from 'bump-cli'; import * as diff from './diff.js'; import { Repo } from './github.js'; -import { shaDigest } from './common.js'; export async function run(): Promise { try { @@ -68,8 +67,7 @@ export async function run(): Promise { await bump.Deploy.run(cliParams.concat(deployParams), oclifConfig); break; case 'diff': - const docDigest = shaDigest([doc, hub]); - const repo = new Repo(docDigest); + const repo = new Repo(doc, hub, branch); let file1 = await repo.getBaseFile(file); let file2: string | undefined; diff --git a/tests/common.test.ts b/tests/common.test.ts index 1f4f14e5..d7c85dc5 100644 --- a/tests/common.test.ts +++ b/tests/common.test.ts @@ -7,6 +7,8 @@ test('shaDigest function', async () => { expect(common.shaDigest(texts)).toBe('6adfb183a4a2c94a2f92dab5ade762a47889a5a1'); texts.pop(); expect(common.shaDigest(texts)).toBe('aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'); + texts.push(''); + expect(common.shaDigest(texts)).toBe('aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'); }); test('fsExists function', async () => { diff --git a/tests/diff.test.ts b/tests/diff.test.ts index f6fb56b6..60809cc8 100644 --- a/tests/diff.test.ts +++ b/tests/diff.test.ts @@ -1,6 +1,7 @@ import * as github from './fixtures/github.js'; import * as bump from 'bump-cli'; import { jest } from '@jest/globals'; +import { shaDigest } from '../src/common.js'; // Mocks should be declared before the module being tested is imported. jest.unstable_mockModule('@actions/github', () => github); @@ -43,6 +44,7 @@ describe('diff.ts', () => { const digest = '4b81e612cafa6580f8ad3bfe9e970b2d961f58c2'; const repo = new Repo('hello'); + const docDigest = shaDigest(['hello']); await diff.run(result, repo); @@ -57,7 +59,7 @@ describe('diff.ts', () => { [Preview documentation](https://bump.sh/doc/my-doc/changes/654) > _Powered by [Bump.sh](https://bump.sh)_ -`, +`, digest, ); }); @@ -71,6 +73,7 @@ describe('diff.ts', () => { const digest = '3999a0fe6ad27841bd6342128f7028ab2cea1c57'; const repo = new Repo('hello'); + const docDigest = shaDigest(['hello']); await diff.run(result, repo); expect(repo.createOrUpdateComment).toHaveBeenCalledWith( @@ -81,7 +84,7 @@ No structural change, nothing to display. [Preview documentation](https://bump.sh/doc/my-doc/changes/654) > _Powered by [Bump.sh](https://bump.sh)_ -`, +`, digest, ); }); @@ -98,6 +101,7 @@ No structural change, nothing to display. }; const digest = '4b81e612cafa6580f8ad3bfe9e970b2d961f58c2'; const repo = new Repo('hello'); + const docDigest = shaDigest(['hello']); await diff.run(result, repo); expect(repo.createOrUpdateComment).toHaveBeenCalledWith( @@ -111,7 +115,7 @@ No structural change, nothing to display. [Preview documentation](https://bump.sh/doc/my-doc/changes/654) > _Powered by [Bump.sh](https://bump.sh)_ -`, +`, digest, ); }); @@ -128,6 +132,7 @@ No structural change, nothing to display. const digest = 'c1f04e5c83235377b88745d13dc9b1ebd3a125a8'; const repo = new Repo('hello'); + const docDigest = shaDigest(['hello']); await diff.run(result, repo); expect(repo.createOrUpdateComment).toHaveBeenCalledWith( @@ -139,7 +144,7 @@ No structural change, nothing to display. > _Powered by [Bump.sh](https://bump.sh)_ -`, +`, digest, ); }); diff --git a/tests/github.test.ts b/tests/github.test.ts index 4dc33ac2..2982e4c9 100644 --- a/tests/github.test.ts +++ b/tests/github.test.ts @@ -5,6 +5,7 @@ import * as common from './fixtures/common.js'; import { jest } from '@jest/globals'; import fixtureGithubContext from './fixtures/github-context.json'; import nock from 'nock'; +import { shaDigest } from '../src/common.js'; nock.disableNetConnect(); stdout.start(); @@ -43,13 +44,14 @@ describe('github.ts', () => { common.fsExists.mockResolvedValue(true); const repo = new Repo('hello'); + const docDigest = shaDigest(['hello']); const headFile = 'openapi.yml'; const baseFile = await repo.getBaseFile('openapi.yml'); const baseSha = fixtureGithubContext.payload.pull_request.base.sha; const headSha = fixtureGithubContext.payload.pull_request.head.sha; const baseBranch = ''; - expect(repo.docDigest).toEqual('hello'); + expect(repo.docDigest).toEqual(docDigest); // Expect git executions expect(exec.exec.mock.calls).toEqual([ ['git', ['fetch', 'origin', baseSha, headSha]], @@ -106,8 +108,9 @@ describe('github.ts', () => { test('Calls octokit to update the issue comment', async () => { const digest = 'existing-comment'; const doc = 'hello'; - const body = `coucou\n`; - const newBody = `New coucou\n`; + const docDigest = shaDigest(['hello']); + const body = `coucou\n`; + const newBody = `New coucou\n`; mockGithubComments([{ id: 1, body }]); mockGithubCommentUpdate(1, newBody); @@ -133,4 +136,14 @@ describe('github.ts', () => { expect(buildRepo).toThrow('No GITHUB_TOKEN env variable available'); }); }); + + describe('constructor', () => { + test('instanciate a github API client and a doc digest', async () => { + const repo = new Repo('hello', 'my-hub', 'my-branch'); + const docDigest = shaDigest(['hello', 'my-hub', 'my-branch']); + + expect(repo.docDigest).toEqual(docDigest); + expect(repo.octokit).toBeDefined(); + }); + }); }); diff --git a/tests/main.test.ts b/tests/main.test.ts index 979ba00b..015bea33 100644 --- a/tests/main.test.ts +++ b/tests/main.test.ts @@ -257,15 +257,14 @@ describe('main.ts', () => { describe('diff command', () => { it('test action run diff correctly', async () => { const file = 'my-file-to-diff.yml'; - const emptyDocDigest = 'da39a3ee5e6b4b0d3255bfef95601890afd80709'; const commentDigest = '1ab9a6fb70e07d910650e1895e9fc53570f99011'; - mockInputs({ file, command: 'diff' }); + mockInputs({ file, command: 'diff', doc: 'my-doc' }); // Mock return value from bump-cli core diff lib bump.mockRunDiff.mockResolvedValue(diffExample); await run(); - expect(repo.Repo).toHaveBeenCalledWith(emptyDocDigest); + expect(repo.Repo).toHaveBeenCalledWith('my-doc', '', ''); expect(repo.mockGetBaseFile).toHaveBeenCalledWith(file); expect(repo.mockCreateOrUpdateComment).toHaveBeenCalledWith( `🚨 Breaking API change detected: @@ -282,7 +281,7 @@ one expect(bump.mockRunDiff).toHaveBeenCalledWith( 'my-file-to-diff.yml', undefined, - '', + 'my-doc', '', '', '', @@ -294,7 +293,6 @@ one it('test action run diff on existing documentation correctly', async () => { const file = 'my-file-to-diff.yml'; - const docDigest = '398b995591d7e5f6676e44f06be071abe850b38e'; const commentDigest = '1ab9a6fb70e07d910650e1895e9fc53570f99011'; mockInputs({ file, doc: 'my-doc', command: 'diff' }); // Mock return value from bump-cli core diff lib @@ -302,7 +300,7 @@ one await run(); - expect(repo.Repo).toHaveBeenCalledWith(docDigest); + expect(repo.Repo).toHaveBeenCalledWith('my-doc', '', ''); expect(repo.mockGetBaseFile).toHaveBeenCalledWith(file); expect(repo.mockCreateOrUpdateComment).toHaveBeenCalledWith( `🚨 Breaking API change detected: @@ -331,13 +329,12 @@ one it('test action run diff with Branch correctly', async () => { const file = 'my-file-to-diff.yml'; - const docDigest = 'da39a3ee5e6b4b0d3255bfef95601890afd80709'; const commentDigest = '1ab9a6fb70e07d910650e1895e9fc53570f99011'; mockInputs({ file, branch: 'latest', command: 'diff' }); await run(); - expect(repo.Repo).toHaveBeenCalledWith(docDigest); + expect(repo.Repo).toHaveBeenCalledWith('', '', 'latest'); expect(repo.mockGetBaseFile).toHaveBeenCalledWith(file); expect(repo.mockCreateOrUpdateComment).toHaveBeenCalledWith( `🚨 Breaking API change detected: @@ -366,7 +363,6 @@ one it('test action run diff on PR correctly', async () => { const file = 'my-file-to-diff.yml'; - const docDigest = 'da39a3ee5e6b4b0d3255bfef95601890afd80709'; const commentDigest = '1ab9a6fb70e07d910650e1895e9fc53570f99011'; mockInputs({ file, command: 'diff' }); // Mock base file from PR @@ -374,7 +370,7 @@ one await run(); - expect(repo.Repo).toHaveBeenCalledWith(docDigest); + expect(repo.Repo).toHaveBeenCalledWith('', '', ''); expect(repo.mockCreateOrUpdateComment).toHaveBeenCalledWith( `🚨 Breaking API change detected: