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
1 change: 1 addition & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ describe('main', () => {
'==================================================',
'',
'::group::Total:0 Succeeded:0 Failed:0 Skipped:0',
'::set-output name=result::skipped',
'::endgroup::',
]);
});
Expand Down
7 changes: 7 additions & 0 deletions __tests__/utils/process1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ describe('execute', () => {
'::group::Total:2 Succeeded:1 Failed:1 Skipped:0',
'> \x1b[32;40m✔\x1b[0m\t[change/new-topic1] has been closed because there is no reference diff',
'> \x1b[31;40m×\x1b[0m\t[change/new-topic2] not found',
'::set-output name=result::failed',
'::endgroup::',
]);
});
Expand Down Expand Up @@ -358,6 +359,7 @@ describe('execute', () => {
'::group::Total:2 Succeeded:1 Failed:1 Skipped:0',
'> \x1b[32;40m✔\x1b[0m\t[change/new-topic1] has been closed because base PullRequest has been closed',
'> \x1b[31;40m×\x1b[0m\t[change/new-topic2] not found',
'::set-output name=result::failed',
'::endgroup::',
]);
});
Expand Down Expand Up @@ -448,6 +450,7 @@ describe('execute', () => {
'::group::Total:2 Succeeded:1 Failed:1 Skipped:0',
'> \x1b[32;40m✔\x1b[0m\t[change/new-topic1] has been closed because there is no reference diff',
'> \x1b[31;40m×\x1b[0m\t[change/new-topic2] not found',
'::set-output name=result::failed',
'::endgroup::',
]);
});
Expand Down Expand Up @@ -490,6 +493,7 @@ describe('execute', () => {
'::group::Total:2 Succeeded:1 Failed:1 Skipped:0',
'> \x1b[32;40m✔\x1b[0m\t[change/new-topic1] has been closed because base PullRequest does not exist',
'> \x1b[31;40m×\x1b[0m\t[change/new-topic2] not found',
'::set-output name=result::failed',
'::endgroup::',
]);
});
Expand Down Expand Up @@ -522,6 +526,7 @@ describe('execute', () => {
'::group::Total:2 Succeeded:0 Failed:2 Skipped:0',
'> \x1b[31;40m×\x1b[0m\t[change/new-topic1] not found',
'> \x1b[31;40m×\x1b[0m\t[change/new-topic2] not found',
'::set-output name=result::failed',
'::endgroup::',
]);
});
Expand Down Expand Up @@ -553,6 +558,7 @@ describe('execute', () => {
'> \x1b[33;40m→\x1b[0m\t[feature/new-topic3] This is not target branch',
'> \x1b[33;40m→\x1b[0m\t[feature/new-topic4] This is not target branch',
'> \x1b[31;40m×\x1b[0m\t[master] parameter [prBranchName] is required.',
'::set-output name=result::failed',
'::endgroup::',
]);
});
Expand All @@ -578,6 +584,7 @@ describe('execute', () => {
'> \x1b[33;40m→\x1b[0m\t[fork1:feature/new-topic3] PR from fork',
'> \x1b[33;40m→\x1b[0m\t[fork2:feature/new-topic4] PR from fork',
'> \x1b[31;40m×\x1b[0m\t[master] parameter [prBranchName] is required.',
'::set-output name=result::failed',
'::endgroup::',
]);
});
Expand Down
48 changes: 31 additions & 17 deletions __tests__/utils/process2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ describe('execute', () => {
'::group::Total:2 Succeeded:0 Failed:0 Skipped:2',
'> \x1b[33;40m✔\x1b[0m\t[change/new-topic1] This is close event',
'> \x1b[33;40m✔\x1b[0m\t[change/new-topic2] This is close event',
'::set-output name=result::not changed',
'::endgroup::',
]);
});
Expand Down Expand Up @@ -406,6 +407,7 @@ describe('execute', () => {
'::group::Total:2 Succeeded:0 Failed:0 Skipped:2',
'> \x1b[33;40m✔\x1b[0m\t[change/new-topic1] This is close event',
'> \x1b[33;40m→\x1b[0m\t[change/new-topic2] duplicated (test/test-branch)',
'::set-output name=result::not changed',
'::endgroup::',
]);
});
Expand Down Expand Up @@ -495,6 +497,7 @@ describe('execute', () => {
'::group::Total:2 Succeeded:0 Failed:1 Skipped:1',
'> \x1b[33;40m✔\x1b[0m\t[change/new-topic1] This is close event',
'> \x1b[31;40m×\x1b[0m\t[change/new-topic2] not found',
'::set-output name=result::failed',
'::endgroup::',
]);
});
Expand Down Expand Up @@ -629,8 +632,6 @@ describe('execute', () => {
'::endgroup::',
'::group::Pushing to octocat/Hello-World@feature/new-feature...',
'[command]git push origin feature/new-feature:refs/heads/feature/new-feature',
'::endgroup::',
'::group::Creating comment to PullRequest...',
'::set-output name=result::succeeded',
'::endgroup::',
'> \x1b[32;40m✔\x1b[0m\t[feature/new-feature] updated',
Expand Down Expand Up @@ -725,6 +726,7 @@ describe('execute', () => {
'::group::Total:2 Succeeded:1 Failed:1 Skipped:0',
'> \x1b[32;40m✔\x1b[0m\t[change/new-topic1] updated',
'> \x1b[31;40m×\x1b[0m\t[change/new-topic2] not found',
'::set-output name=result::failed',
'::endgroup::',
]);
});
Expand Down Expand Up @@ -817,6 +819,7 @@ describe('execute', () => {
'::group::Total:2 Succeeded:1 Failed:0 Skipped:1',
'> \x1b[32;40m✔\x1b[0m\t[feature/new-topic3] updated',
'> \x1b[33;40m→\x1b[0m\t[feature/new-topic4] duplicated (Hello-World/test-branch)',
'::set-output name=result::succeeded',
'::endgroup::',
]);
});
Expand Down Expand Up @@ -900,6 +903,7 @@ describe('execute', () => {
'::group::Total:2 Succeeded:0 Failed:1 Skipped:1',
'> \x1b[33;40m✔\x1b[0m\t[change/new-topic1] There is no diff',
'> \x1b[31;40m×\x1b[0m\t[change/new-topic2] not found',
'::set-output name=result::failed',
'::endgroup::',
]);
});
Expand Down Expand Up @@ -981,11 +985,12 @@ describe('execute', () => {
'::group::Total:2 Succeeded:0 Failed:1 Skipped:1',
'> \x1b[33;40m✔\x1b[0m\t[change/new-topic1] There is no diff',
'> \x1b[31;40m×\x1b[0m\t[change/new-topic2] not found',
'::set-output name=result::failed',
'::endgroup::',
]);
});

it('should process default branch', async() => {
it('should process default branch (not create pr)', async() => {
process.env.GITHUB_WORKSPACE = workDir;
process.env.GITHUB_REPOSITORY = 'octocat/Hello-World';
process.env.INPUT_GITHUB_TOKEN = 'test-token';
Expand All @@ -1011,9 +1016,9 @@ describe('execute', () => {
.get('/repos/octocat/Hello-World')
.reply(200, () => getApiFixture(rootDir, 'repos.get'))
.get('/repos/octocat/Hello-World/pulls?sort=created&direction=asc')
.reply(200, () => ([]))
.get('/repos/octocat/Hello-World/pulls?head=' + encodeURIComponent('octocat:Hello-World/test-0'))
.reply(200, () => getApiFixture(rootDir, 'pulls.list'))
.get('/repos/octocat/Hello-World/pulls?head=' + encodeURIComponent('octocat:master'))
.reply(200, () => getApiFixture(rootDir, 'pulls.list.state.open'))
.post('/repos/octocat/Hello-World/issues/1347/comments')
.reply(201)
.get('/repos/octocat/Hello-World/pulls/1347')
Expand All @@ -1027,25 +1032,31 @@ describe('execute', () => {
prBranchName: 'test-${PR_ID}',
prTitle: 'test: create pull request (${PR_NUMBER})',
prBody: 'pull request body',
checkOnlyDefaultBranch: true,
notCreatePr: true,
}));

stdoutCalledWith(mockStdout, [
'::group::Target PullRequest Ref [change/new-topic1]',
'::endgroup::',
'::group::Target PullRequest Ref [change/new-topic2]',
'::endgroup::',
'::group::Target PullRequest Ref [master]',
'> Fetching...',
'[command]git remote add origin',
'[command]git fetch --no-tags origin \'refs/heads/Hello-World/test-0:refs/remotes/origin/Hello-World/test-0\'',
'[command]git fetch --no-tags origin \'refs/heads/master:refs/remotes/origin/master\'',
'[command]git reset --hard',
'> Switching branch to [Hello-World/test-0]...',
'[command]git checkout -b Hello-World/test-0 origin/Hello-World/test-0',
'> Switching branch to [master]...',
'[command]git checkout -b master origin/master',
'[command]git rev-parse --abbrev-ref HEAD',
' >> test',
'> remote branch [Hello-World/test-0] not found.',
'> remote branch [master] not found.',
'> now branch: test',
'> Cloning [master] from the remote repo...',
'[command]git remote add origin',
'[command]git fetch --no-tags origin \'refs/heads/master:refs/remotes/origin/master\'',
'[command]git checkout -b master origin/master',
'[command]git checkout -b Hello-World/test-0',
'[command]git checkout -b master',
'[command]ls -la',
'> Running commands...',
'[command]yarn upgrade',
Expand All @@ -1057,15 +1068,14 @@ describe('execute', () => {
'> Committing...',
'[command]git commit -qm \'test: create pull request\'',
'[command]git show \'--stat-count=10\' HEAD',
'> Checking references diff...',
'[command]git fetch --prune --no-recurse-submodules origin +refs/heads/master:refs/remotes/origin/master',
'[command]git diff \'HEAD..origin/master\' --name-only',
'> Pushing to octocat/Hello-World@Hello-World/test-0...',
'[command]git push origin Hello-World/test-0:refs/heads/Hello-World/test-0',
'> Creating comment to PullRequest...',
'> Pushing to octocat/Hello-World@master...',
'[command]git push origin master:refs/heads/master',
'::endgroup::',
'::group::Total:1 Succeeded:1 Failed:0 Skipped:0',
'::group::Total:3 Succeeded:1 Failed:0 Skipped:2',
'> \x1b[33;40m→\x1b[0m\t[change/new-topic1] This is not target branch',
'> \x1b[33;40m→\x1b[0m\t[change/new-topic2] This is not target branch',
'> \x1b[32;40m✔\x1b[0m\t[master] updated',
'::set-output name=result::succeeded',
'::endgroup::',
]);
});
Expand Down Expand Up @@ -1137,6 +1147,7 @@ describe('execute', () => {
'> \x1b[31;40m×\x1b[0m\t[feature/new-topic3] command [git status] exited with code undefined. message: test error',
'> \x1b[33;40m→\x1b[0m\t[feature/new-topic4] duplicated (Hello-World/test-branch)',
'> \x1b[33;40m→\x1b[0m\t[master] duplicated (Hello-World/test-branch)',
'::set-output name=result::failed',
'::endgroup::',
]);
});
Expand Down Expand Up @@ -1240,6 +1251,7 @@ describe('execute', () => {
'> \x1b[31;40m×\x1b[0m\t[change/new-topic1] command [git status] exited with code undefined. message: test error',
'> \x1b[31;40m×\x1b[0m\t[change/new-topic2] not found',
'> \x1b[31;40m×\x1b[0m\t[master] command [git status] exited with code undefined. message: test error',
'::set-output name=result::failed',
'::endgroup::',
]);
});
Expand Down Expand Up @@ -1651,6 +1663,7 @@ describe('execute', () => {
'::group::Total:2 Succeeded:1 Failed:1 Skipped:0',
'> \x1b[32;40m✔\x1b[0m\t[change/new-topic1] has been auto merged',
'> \x1b[31;40m×\x1b[0m\t[change/new-topic2] not found',
'::set-output name=result::failed',
'::endgroup::',
]);
});
Expand Down Expand Up @@ -1749,6 +1762,7 @@ describe('execute', () => {
'::group::Total:2 Succeeded:0 Failed:1 Skipped:1',
'> \x1b[33;40m✔\x1b[0m\t[change/new-topic1] There is no diff',
'> \x1b[31;40m×\x1b[0m\t[change/new-topic2] not found',
'::set-output name=result::failed',
'::endgroup::',
]);
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@technote-space/github-action-pr-helper",
"version": "2.0.0",
"version": "2.0.1",
"description": "PullRequest Helper for GitHub Actions.",
"keywords": [
"github",
Expand Down
9 changes: 8 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@ export type ActionContext = {
cache: { [key: string]: any };
}

export const AllProcessResult = [
'skipped',
'not changed',
'succeeded',
'failed',
] as const;

export type ProcessResult = {
result: 'succeeded' | 'failed' | 'skipped' | 'not changed';
result: typeof AllProcessResult[number];
detail: string;
branch: string;
}
Expand Down
25 changes: 20 additions & 5 deletions src/utils/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
} from './misc';
import {getPrBranchName} from './variables';
import {INTERVAL_MS} from '../constant';
import {ActionContext, ProcessResult, PullsParams, CommandOutput} from '../types';
import {ActionContext, ProcessResult, AllProcessResult, PullsParams, CommandOutput} from '../types';

const {sleep, getBranch} = Utils;
const {isPr, isPush} = ContextHelper;
Expand All @@ -46,12 +46,17 @@ const getResult = (result: 'succeeded' | 'failed' | 'skipped' | 'not changed', d
});

const checkActionPr = async(logger: Logger, octokit: Octokit, context: ActionContext): Promise<ProcessResult | true> => {
const defaultBranch = await getDefaultBranch(octokit, context);
if (defaultBranch === getPrHeadRef(context)) {
return true;
}

const pr = await findPR(getPrHeadRef(context), octokit, context);
if (!pr) {
return getResult('failed', 'not found', context);
}

if (pr.base.ref === await getDefaultBranch(octokit, context)) {
if (pr.base.ref === defaultBranch) {
return true;
}

Expand Down Expand Up @@ -233,15 +238,17 @@ const createPr = async(makeGroup: boolean, isClose: boolean, helper: GitHelper,
commonLogger.startProcess('Target PullRequest Ref [%s]', getPrHeadRef(context));
}

if (!isActionPr(context) && !await isTargetBranch(getPrHeadRef(context), octokit, context)) {
return getResult('skipped', 'This is not target branch', context);
}

if (isActionPr(context) || isNotCreatePR(context)) {
const processResult = await checkActionPr(logger, octokit, context);
if (processResult !== true) {
return processResult;
}

return createCommit(true, isClose, logger, octokit, context);
} else if (!await isTargetBranch(getPrHeadRef(context), octokit, context)) {
return getResult('skipped', 'This is not target branch', context);
return createCommit(isActionPr(context), isClose, logger, octokit, context);
}

const {files, output} = await getChangedFiles(helper, logger, octokit, context);
Expand Down Expand Up @@ -292,13 +299,21 @@ const outputResult = (result: ProcessResult, endProcess = false): void => {
commonLogger.info(mark[result.result] + '\t[%s] %s', result.branch, result.detail);
};

const getOutputResult = (results: ProcessResult[]): typeof AllProcessResult[number] => {
const resultItems = results.map(result => result.result);

// eslint-disable-next-line no-magic-numbers
return (AllProcessResult.filter(item => resultItems.includes(item)).slice(-1)[0] as (typeof AllProcessResult[number]) | undefined) ?? AllProcessResult[0];
};

const outputResults = (results: ProcessResult[]): void => {
const total = results.length;
const succeeded = results.filter(item => item.result === 'succeeded').length;
const failed = results.filter(item => item.result === 'failed').length;

commonLogger.startProcess('Total:%d Succeeded:%d Failed:%d Skipped:%d', total, succeeded, failed, total - succeeded - failed);
results.forEach(result => outputResult(result));
setOutput('result', getOutputResult(results));
};

const runCreatePr = async(isClose: boolean, getPulls: (Octokit, ActionContext) => AsyncIterable<PullsParams>, octokit: Octokit, context: ActionContext): Promise<void> => {
Expand Down