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
39 changes: 11 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@actions/github": "^5.1.1",
"@actions/io": "^1.1.3",
"@octokit/types": "^14.1.0",
"bump-cli": "^2.9.9"
"bump-cli": "^2.9.10"
},
"devDependencies": {
"@github/local-action": "^5.1.0",
Expand Down
22 changes: 22 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ export class Repo {
}
}

/* WARNING: you need to first call .getBaseFile to have base files
* available in the tmpDir */
async getBaseOverlays(overlays: string[] | undefined): Promise<string[]> {
const tmpDir = 'tmp/';

const tmpOverlays: (string | undefined)[] = await Promise.all(
(overlays || []).map(async (overlayFile) => {
if (!overlayFile) return undefined;

// All files of the “previous” version are in the tmp/ dir
// so we take all the previous versions' overlay files (if
// they exist)
const tmpOverlay = `${tmpDir}${overlayFile}`;
if (await fsExists(tmpOverlay)) {
return tmpOverlay;
}
}),
);

return tmpOverlays.filter((e) => e) as string[];
}

async createOrUpdateComment(body: string, digest: string): Promise<void> {
if (!this.prNumber) {
core.info('Not a pull request, nothing more to do.');
Expand Down
17 changes: 16 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,32 @@ export async function run(): Promise<void> {
case 'diff':
const repo = new Repo(doc, hub, branch);
let file1 = await repo.getBaseFile(file);
const overlays1: string[] | undefined =
(await repo.getBaseOverlays(overlays)) || [];
let file2: string | undefined;
let overlays2: string[] | undefined;

if (file1) {
overlays2 = overlays;
file2 = file;
} else {
file1 = file;
}

const config = await Config.load(oclifConfig);
await new bump.Diff.Diff(config)
.run(file1, file2, doc, hub, branch, token, 'markdown', expires, overlays)
.run(
file1,
file2,
doc,
hub,
branch,
token,
'markdown',
expires,
overlays1,
overlays2,
)
.then((result: bump.DiffResponse | undefined) => {
if (result) {
diff.run(result, repo).catch(handleErrors);
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import type * as repo from '../../src/github.js';
import { jest } from '@jest/globals';

export const mockGetBaseFile = jest.fn<repo.Repo['getBaseFile']>();
export const mockGetBaseOverlays = jest.fn<repo.Repo['getBaseOverlays']>();
export const mockCreateOrUpdateComment = jest.fn<repo.Repo['createOrUpdateComment']>();
export const mockDeleteExistingComment = jest.fn<repo.Repo['deleteExistingComment']>();
export const Repo = jest.fn().mockImplementation(() => {
return {
doc: 'my-doc',
getBaseFile: mockGetBaseFile,
getBaseOverlays: mockGetBaseOverlays,
createOrUpdateComment: mockCreateOrUpdateComment,
deleteExistingComment: mockDeleteExistingComment,
};
Expand Down
11 changes: 11 additions & 0 deletions tests/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ describe('github.ts', () => {
});
});

describe('getBaseOverlays function', () => {
test('Returns the given list of filenames prepended with the tmpDir', async () => {
common.fsExists.mockResolvedValue(true);

const repo = new Repo('hello');
const baseOverlays = await repo.getBaseOverlays(['overlay.yml']);

expect(baseOverlays).toStrictEqual([`tmp/overlay.yml`]);
});
});

describe('createOrUpdateComment function', () => {
// Mock GitHub API
const nockScope = nock('https://api.github.com');
Expand Down
9 changes: 9 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('main.ts', () => {
stdout.stop();
stdout.start();
repo.mockGetBaseFile.mockReset();
repo.mockGetBaseOverlays.mockReset();
});

afterEach(() => {
Expand Down Expand Up @@ -292,6 +293,7 @@ one
'markdown',
'',
[],
undefined,
);
});

Expand Down Expand Up @@ -332,6 +334,7 @@ one
'markdown',
'',
[],
undefined,
);
});

Expand Down Expand Up @@ -370,6 +373,7 @@ one
'markdown',
'',
[],
undefined,
);
});

Expand Down Expand Up @@ -409,6 +413,7 @@ one
'markdown',
'',
[],
[],
);
});

Expand Down Expand Up @@ -436,6 +441,7 @@ one
'markdown',
expect.anything(),
[],
[],
);
});

Expand All @@ -449,6 +455,7 @@ one
});
// Mock base file from PR
repo.mockGetBaseFile.mockResolvedValue('my-base-file-to-diff.yml');
repo.mockGetBaseOverlays.mockResolvedValue(['my-base-overlays.yml']);

await run();

Expand All @@ -461,6 +468,7 @@ one
expect.anything(),
'markdown',
expect.anything(),
['my-base-overlays.yml'],
['overlay1.yml', 'overlay2.yml'],
);
});
Expand Down Expand Up @@ -502,6 +510,7 @@ one
'markdown',
expect.anything(),
[],
undefined,
);
});
});
Expand Down