Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Commit 6536b32

Browse files
Merge pull request #158 from technote-space/feature/#154
feat: add base option
2 parents 9e54295 + ca2d65c commit 6536b32

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

__tests__/utils/command.test.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-magic-numbers */
22
import nock from 'nock';
3-
import path, { resolve } from 'path';
3+
import path, {resolve} from 'path';
44
import {
55
generateContext,
66
testEnv,
@@ -12,8 +12,8 @@ import {
1212
disableNetConnect,
1313
getApiFixture,
1414
} from '@technote-space/github-action-test-helper';
15-
import { Logger } from '@technote-space/github-action-log-helper';
16-
import { getGitDiff, getFileDiff, getDiffFiles, sumResults } from '../../src/utils/command';
15+
import {Logger} from '@technote-space/github-action-log-helper';
16+
import {getGitDiff, getFileDiff, getDiffFiles, sumResults} from '../../src/utils/command';
1717

1818
const rootDir = path.resolve(__dirname, '../..');
1919
const fixtureRootDir = resolve(__dirname, '..', 'fixtures');
@@ -415,6 +415,45 @@ describe('getGitDiff', () => {
415415
]);
416416
});
417417

418+
it('should get git diff (push, not found pr with base setting)', async() => {
419+
process.env.GITHUB_WORKSPACE = '/home/runner/work/my-repo-name/my-repo-name';
420+
process.env.INPUT_GITHUB_TOKEN = 'test token';
421+
process.env.INPUT_BASE = 'main';
422+
423+
const mockExec = spyOnSpawn();
424+
setChildProcessParams({
425+
stdout: (command: string): string => {
426+
if (command.startsWith('git diff')) {
427+
return 'package.json\nabc/composer.json\nREADME.md\nsrc/main.ts';
428+
}
429+
return '';
430+
},
431+
});
432+
433+
nock('https://api.github.com')
434+
.persist()
435+
.get('/repos/hello/world/pulls?head=hello%3Atest')
436+
.reply(200, () => []);
437+
438+
expect(await getGitDiff(logger, Object.assign({}, pushContext, {
439+
ref: 'refs/heads/test',
440+
}))).toEqual([
441+
{file: 'package.json', ...emptyDiff},
442+
{file: 'abc/composer.json', ...emptyDiff},
443+
{file: 'README.md', ...emptyDiff},
444+
{file: 'src/main.ts', ...emptyDiff},
445+
]);
446+
execCalledWith(mockExec, [
447+
'git remote add get-diff-action \'https://octocat:test [email protected]/hello/world.git\' || :',
448+
'git fetch --no-tags --no-recurse-submodules \'--depth=10000\' get-diff-action \'refs/heads/test:refs/remotes/get-diff-action/test\' || :',
449+
'git diff \'main...after-sha\' \'--diff-filter=AMRC\' --name-only || :',
450+
'git diff \'main...after-sha\' --shortstat -w -- \'package.json\'',
451+
'git diff \'main...after-sha\' --shortstat -w -- \'abc/composer.json\'',
452+
'git diff \'main...after-sha\' --shortstat -w -- \'README.md\'',
453+
'git diff \'main...after-sha\' --shortstat -w -- \'src/main.ts\'',
454+
]);
455+
});
456+
418457
it('should get git diff (push tag)', async() => {
419458
process.env.GITHUB_WORKSPACE = '/home/runner/work/my-repo-name/my-repo-name';
420459
process.env.INPUT_GITHUB_TOKEN = 'test token';

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ inputs:
1414
description: Dot.
1515
default: '...'
1616
required: true
17+
BASE:
18+
description: base
19+
required: false
1720
DIFF_FILTER:
1821
description: Diff filter.
1922
default: 'AMRC'

src/utils/misc.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export const getDiffInfoForPR = (pull: PullRequestParams, context: Context): Dif
2121

2222
export const isDefaultBranch = async(octokit: Octokit, context: Context): Promise<boolean> => await (new ApiHelper(octokit, context)).getDefaultBranch() === Utils.getBranch(context);
2323

24+
const getBase = (context: Context): string => getInput('BASE') || context.payload.before;
25+
2426
export const getDiffInfoForPush = async(octokit: Octokit, context: Context): Promise<DiffInfo> => {
2527
if (Utils.isTagRef(context)) {
2628
return {base: '', head: ''};
@@ -44,14 +46,15 @@ export const getDiffInfoForPush = async(octokit: Octokit, context: Context): Pro
4446
}
4547

4648
if (/^0+$/.test(context.payload.before)) {
49+
// new branch
4750
return {
4851
base: Utils.normalizeRef(await (new ApiHelper(octokit, context)).getDefaultBranch()),
4952
head: context.payload.after,
5053
};
5154
}
5255

5356
return {
54-
base: context.payload.before,
57+
base: getBase(context),
5558
head: context.payload.after,
5659
};
5760
};

0 commit comments

Comments
 (0)