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

feat: add base option #158

Merged
merged 2 commits into from
Jul 2, 2021
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
45 changes: 42 additions & 3 deletions __tests__/utils/command.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-magic-numbers */
import nock from 'nock';
import path, { resolve } from 'path';
import path, {resolve} from 'path';
import {
generateContext,
testEnv,
Expand All @@ -12,8 +12,8 @@ import {
disableNetConnect,
getApiFixture,
} from '@technote-space/github-action-test-helper';
import { Logger } from '@technote-space/github-action-log-helper';
import { getGitDiff, getFileDiff, getDiffFiles, sumResults } from '../../src/utils/command';
import {Logger} from '@technote-space/github-action-log-helper';
import {getGitDiff, getFileDiff, getDiffFiles, sumResults} from '../../src/utils/command';

const rootDir = path.resolve(__dirname, '../..');
const fixtureRootDir = resolve(__dirname, '..', 'fixtures');
Expand Down Expand Up @@ -415,6 +415,45 @@ describe('getGitDiff', () => {
]);
});

it('should get git diff (push, not found pr with base setting)', async() => {
process.env.GITHUB_WORKSPACE = '/home/runner/work/my-repo-name/my-repo-name';
process.env.INPUT_GITHUB_TOKEN = 'test token';
process.env.INPUT_BASE = 'main';

const mockExec = spyOnSpawn();
setChildProcessParams({
stdout: (command: string): string => {
if (command.startsWith('git diff')) {
return 'package.json\nabc/composer.json\nREADME.md\nsrc/main.ts';
}
return '';
},
});

nock('https://api.github.com')
.persist()
.get('/repos/hello/world/pulls?head=hello%3Atest')
.reply(200, () => []);

expect(await getGitDiff(logger, Object.assign({}, pushContext, {
ref: 'refs/heads/test',
}))).toEqual([
{file: 'package.json', ...emptyDiff},
{file: 'abc/composer.json', ...emptyDiff},
{file: 'README.md', ...emptyDiff},
{file: 'src/main.ts', ...emptyDiff},
]);
execCalledWith(mockExec, [
'git remote add get-diff-action \'https://octocat:test token@github.com/hello/world.git\' || :',
'git fetch --no-tags --no-recurse-submodules \'--depth=10000\' get-diff-action \'refs/heads/test:refs/remotes/get-diff-action/test\' || :',
'git diff \'main...after-sha\' \'--diff-filter=AMRC\' --name-only || :',
'git diff \'main...after-sha\' --shortstat -w -- \'package.json\'',
'git diff \'main...after-sha\' --shortstat -w -- \'abc/composer.json\'',
'git diff \'main...after-sha\' --shortstat -w -- \'README.md\'',
'git diff \'main...after-sha\' --shortstat -w -- \'src/main.ts\'',
]);
});

it('should get git diff (push tag)', async() => {
process.env.GITHUB_WORKSPACE = '/home/runner/work/my-repo-name/my-repo-name';
process.env.INPUT_GITHUB_TOKEN = 'test token';
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ inputs:
description: Dot.
default: '...'
required: true
BASE:
description: base
required: false
DIFF_FILTER:
description: Diff filter.
default: 'AMRC'
Expand Down
5 changes: 4 additions & 1 deletion src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const getDiffInfoForPR = (pull: PullRequestParams, context: Context): Dif

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

const getBase = (context: Context): string => getInput('BASE') || context.payload.before;

export const getDiffInfoForPush = async(octokit: Octokit, context: Context): Promise<DiffInfo> => {
if (Utils.isTagRef(context)) {
return {base: '', head: ''};
Expand All @@ -44,14 +46,15 @@ export const getDiffInfoForPush = async(octokit: Octokit, context: Context): Pro
}

if (/^0+$/.test(context.payload.before)) {
// new branch
return {
base: Utils.normalizeRef(await (new ApiHelper(octokit, context)).getDefaultBranch()),
head: context.payload.after,
};
}

return {
base: context.payload.before,
base: getBase(context),
head: context.payload.after,
};
};
Expand Down