diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 11232b6..2829248 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -189,6 +189,7 @@ describe('main', () => { '==================================================', '', '::group::Total:0 Succeeded:0 Failed:0 Skipped:0', + '::set-output name=result::skipped', '::endgroup::', ]); }); diff --git a/__tests__/utils/process1.test.ts b/__tests__/utils/process1.test.ts index 3bfe53b..86d00cd 100644 --- a/__tests__/utils/process1.test.ts +++ b/__tests__/utils/process1.test.ts @@ -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::', ]); }); @@ -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::', ]); }); @@ -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::', ]); }); @@ -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::', ]); }); @@ -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::', ]); }); @@ -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::', ]); }); @@ -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::', ]); }); diff --git a/__tests__/utils/process2.test.ts b/__tests__/utils/process2.test.ts index 8e8c285..b60c76b 100644 --- a/__tests__/utils/process2.test.ts +++ b/__tests__/utils/process2.test.ts @@ -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::', ]); }); @@ -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::', ]); }); @@ -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::', ]); }); @@ -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', @@ -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::', ]); }); @@ -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::', ]); }); @@ -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::', ]); }); @@ -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'; @@ -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') @@ -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', @@ -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::', ]); }); @@ -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::', ]); }); @@ -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::', ]); }); @@ -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::', ]); }); @@ -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::', ]); }); diff --git a/package.json b/package.json index 74c0ec7..076ee1f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/types.ts b/src/types.ts index 9c23253..665d3e7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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; } diff --git a/src/utils/process.ts b/src/utils/process.ts index 1b8b143..8e27ec2 100644 --- a/src/utils/process.ts +++ b/src/utils/process.ts @@ -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; @@ -46,12 +46,17 @@ const getResult = (result: 'succeeded' | 'failed' | 'skipped' | 'not changed', d }); const checkActionPr = async(logger: Logger, octokit: Octokit, context: ActionContext): Promise => { + 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; } @@ -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); @@ -292,6 +299,13 @@ 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; @@ -299,6 +313,7 @@ const outputResults = (results: ProcessResult[]): void => { 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, octokit: Octokit, context: ActionContext): Promise => {