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
6 changes: 3 additions & 3 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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() {
Expand Down
4 changes: 1 addition & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
try {
Expand Down Expand Up @@ -68,8 +67,7 @@ export async function run(): Promise<void> {
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;

Expand Down
2 changes: 2 additions & 0 deletions tests/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
13 changes: 9 additions & 4 deletions tests/diff.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -43,6 +44,7 @@ describe('diff.ts', () => {
const digest = '4b81e612cafa6580f8ad3bfe9e970b2d961f58c2';

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

await diff.run(result, repo);

Expand All @@ -57,7 +59,7 @@ describe('diff.ts', () => {
[Preview documentation](https://bump.sh/doc/my-doc/changes/654)

> _Powered by [Bump.sh](https://bump.sh)_
<!-- Bump.sh digest=${digest} doc=hello -->`,
<!-- Bump.sh digest=${digest} doc=${docDigest} -->`,
digest,
);
});
Expand All @@ -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(
Expand All @@ -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)_
<!-- Bump.sh digest=${digest} doc=hello -->`,
<!-- Bump.sh digest=${digest} doc=${docDigest} -->`,
digest,
);
});
Expand All @@ -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(
Expand All @@ -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)_
<!-- Bump.sh digest=${digest} doc=hello -->`,
<!-- Bump.sh digest=${digest} doc=${docDigest} -->`,
digest,
);
});
Expand All @@ -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(
Expand All @@ -139,7 +144,7 @@ No structural change, nothing to display.


> _Powered by [Bump.sh](https://bump.sh)_
<!-- Bump.sh digest=${digest} doc=hello -->`,
<!-- Bump.sh digest=${digest} doc=${docDigest} -->`,
digest,
);
});
Expand Down
19 changes: 16 additions & 3 deletions tests/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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]],
Expand Down Expand Up @@ -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<!-- Bump.sh digest=${digest} doc=${doc} -->`;
const newBody = `New coucou\n<!-- Bump.sh digest=new-coucou doc=${doc} -->`;
const docDigest = shaDigest(['hello']);
const body = `coucou\n<!-- Bump.sh digest=${digest} doc=${docDigest} -->`;
const newBody = `New coucou\n<!-- Bump.sh digest=new-coucou doc=${docDigest} -->`;

mockGithubComments([{ id: 1, body }]);
mockGithubCommentUpdate(1, newBody);
Expand All @@ -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();
});
});
});
16 changes: 6 additions & 10 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -282,7 +281,7 @@ one
expect(bump.mockRunDiff).toHaveBeenCalledWith(
'my-file-to-diff.yml',
undefined,
'',
'my-doc',
'',
'',
'',
Expand All @@ -294,15 +293,14 @@ 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
bump.mockRunDiff.mockResolvedValue(diffExample);

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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -366,15 +363,14 @@ 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
repo.mockGetBaseFile.mockResolvedValue('my-base-file-to-diff.yml');

await run();

expect(repo.Repo).toHaveBeenCalledWith(docDigest);
expect(repo.Repo).toHaveBeenCalledWith('', '', '');
expect(repo.mockCreateOrUpdateComment).toHaveBeenCalledWith(
`🚨 Breaking API change detected:

Expand Down