From ea60b4fa2eb4748cb0e1b83b9877df06ec07e322 Mon Sep 17 00:00:00 2001 From: Technote Date: Fri, 24 Jan 2020 02:56:34 +0900 Subject: [PATCH 1/4] feat: update packages --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index 031bd3fd..a905d8ec 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1097,9 +1097,9 @@ cssom@~0.3.6: integrity sha512-b0tGHbfegbhPJpxpiBPU2sCkigAqtM9O121le6bbOlgyV+NyGyCmVfJ6QW9eRjz8CpNfWEOYBIMIGRYkLwsIYg== cssstyle@^2.0.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/cssstyle/-/cssstyle-2.1.0.tgz#99f50a3aa21d4af16e758ae3e454dcf5940b9122" - integrity sha512-1iwCdymVYhMdQWiZ+9mB7x+urdNLPGTWsIZt6euFk8Yi+dOERK2ccoAUA3Bl8I5vmK5qfz/eLkBRyLbs42ov4A== + version "2.2.0" + resolved "https://registry.yarnpkg.com/cssstyle/-/cssstyle-2.2.0.tgz#e4c44debccd6b7911ed617a4395e5754bba59992" + integrity sha512-sEb3XFPx3jNnCAMtqrXPDeSgQr+jojtCeNf8cvMNMh1cG970+lljssvQDzPq6lmmJu2Vhqood/gtEomBiHOGnA== dependencies: cssom "~0.3.6" From b6e9f38873de1275859cf597ab67881bbb9222e5 Mon Sep 17 00:00:00 2001 From: Technote Date: Fri, 24 Jan 2020 02:56:48 +0900 Subject: [PATCH 2/4] chore: tweaks --- src/context-helper.ts | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/context-helper.ts b/src/context-helper.ts index b8e46eb1..8e92327f 100644 --- a/src/context-helper.ts +++ b/src/context-helper.ts @@ -21,20 +21,19 @@ export const getSender = (context: Context): string | false => context.payload.s export const getRepository = (context: Context): string => `${context.repo.owner}/${context.repo.repo}`; -export const getGitUrl = (context: Context, accessTokenRequired = true): string => { - const token = getAccessToken(accessTokenRequired); - if (token) { - return `https://${getActor()}:${token}@github.com/${context.repo.owner}/${context.repo.repo}.git`; - } else { - return `https://github.com/${context.repo.owner}/${context.repo.repo}.git`; - } -}; +const getGitUrlAuthInfo = (token: string | undefined): string => token ? `${getActor()}:${token}@` : ''; + +export const getGitUrlWithToken = (context: Context, token?: string | undefined): string => `https://${getGitUrlAuthInfo(token)}github.com/${context.repo.owner}/${context.repo.repo}.git`; + +export const getGitUrl = (context: Context, accessTokenRequired = true): string => getGitUrlWithToken(context, getAccessToken(accessTokenRequired)); export const showActionInfo = (rootDir: string, logger: Logger, context: Context): void => { - const info = getBuildInfo(path.resolve(rootDir, 'build.json')); - const tagName = getTagName(context); + const info = getBuildInfo(path.resolve(rootDir, 'build.json')); + const tagName = getTagName(context); + const separator = '=================================================='; + logger.log(); - logger.log('=================================================='); + logger.log(separator); if (false !== info) { if ('owner' in info) { logger.log('Version: %s/%s@%s', info.owner, info.repo, info.tagName); @@ -65,6 +64,6 @@ export const showActionInfo = (rootDir: string, logger: Logger, context: Context logger.startProcess('Dump Payload'); console.log(context.payload); logger.endProcess(); - logger.log('=================================================='); + logger.log(separator); logger.log(); }; From a08a45019c16e56b054cf7e7326fc62720dad741 Mon Sep 17 00:00:00 2001 From: Technote Date: Fri, 24 Jan 2020 02:59:23 +0900 Subject: [PATCH 3/4] feat: add args to set token --- __tests__/git-helper.test.ts | 91 ++++++++++++++++++++---------------- src/git-helper.ts | 12 +++-- 2 files changed, 60 insertions(+), 43 deletions(-) diff --git a/__tests__/git-helper.test.ts b/__tests__/git-helper.test.ts index 7542c6ab..6c231812 100644 --- a/__tests__/git-helper.test.ts +++ b/__tests__/git-helper.test.ts @@ -25,11 +25,8 @@ const context = (override: object = {}): Context => getContext(Object.assign({ describe('GitHelper', () => { testEnv(); testChildProcess(); - beforeEach(() => { - process.env.INPUT_GITHUB_TOKEN = 'token'; - }); - const helper = new GitHelper(new Logger()); + const helper = new GitHelper(new Logger(), {token: 'token1'}); describe('getCurrentBranchName', () => { it('should return empty 1', async() => { @@ -69,7 +66,7 @@ describe('GitHelper', () => { })); execCalledWith(mockExec, [ - 'git clone \'--branch=test\' \'--depth=3\' \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :', + 'git clone \'--branch=test\' \'--depth=3\' \'https://octocat:token1@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :', ]); }); @@ -82,8 +79,8 @@ describe('GitHelper', () => { }))); execCalledWith(mockExec, [ - 'git clone \'--depth=3\' \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :', - 'git fetch \'https://octocat:token@github.com/hello/world.git\' \'+refs/pull/123/merge\' > /dev/null 2>&1', + 'git clone \'--depth=3\' \'https://octocat:token1@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :', + 'git fetch \'https://octocat:token1@github.com/hello/world.git\' \'+refs/pull/123/merge\' > /dev/null 2>&1', 'git checkout -qf FETCH_HEAD', ]); }); @@ -105,8 +102,8 @@ describe('GitHelper', () => { await helper.checkout(workDir, context()); execCalledWith(mockExec, [ - 'git clone \'--depth=3\' \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1', - 'git fetch \'https://octocat:token@github.com/hello/world.git\' refs/heads/test-ref > /dev/null 2>&1', + 'git clone \'--depth=3\' \'https://octocat:token1@github.com/hello/world.git\' \'.\' > /dev/null 2>&1', + 'git fetch \'https://octocat:token1@github.com/hello/world.git\' refs/heads/test-ref > /dev/null 2>&1', 'git checkout -qf test-sha', ]); }); @@ -117,7 +114,7 @@ describe('GitHelper', () => { await helper.checkout(workDir, context({sha: ''})); execCalledWith(mockExec, [ - 'git clone \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1', + 'git clone \'https://octocat:token1@github.com/hello/world.git\' \'.\' > /dev/null 2>&1', 'git checkout -qf test-ref', ]); }); @@ -128,7 +125,7 @@ describe('GitHelper', () => { await helper.checkout(workDir, context({sha: '', ref: 'refs/tags/test-tag'})); execCalledWith(mockExec, [ - 'git clone \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1', + 'git clone \'https://octocat:token1@github.com/hello/world.git\' \'.\' > /dev/null 2>&1', 'git checkout -qf refs/tags/test-tag', ]); }); @@ -169,7 +166,7 @@ describe('GitHelper', () => { execCalledWith(mockExec, [ `rm -rdf '${workDir}'`, 'git init \'.\'', - 'git remote add origin \'https://octocat:token@github.com/hello/world.git\' > /dev/null 2>&1 || :', + 'git remote add origin \'https://octocat:token1@github.com/hello/world.git\' > /dev/null 2>&1 || :', ]); }); }); @@ -183,7 +180,7 @@ describe('GitHelper', () => { execCalledWith(mockExec, [ `rm -rdf '${workDir}'`, 'git init \'.\'', - 'git remote add origin \'https://octocat:token@github.com/hello/world.git\' > /dev/null 2>&1 || :', + 'git remote add origin \'https://octocat:token1@github.com/hello/world.git\' > /dev/null 2>&1 || :', 'git fetch origin || :', ]); }); @@ -196,7 +193,7 @@ describe('GitHelper', () => { execCalledWith(mockExec, [ `rm -rdf '${workDir}'`, 'git init \'.\'', - 'git remote add origin \'https://octocat:token@github.com/hello/world.git\' > /dev/null 2>&1 || :', + 'git remote add origin \'https://octocat:token1@github.com/hello/world.git\' > /dev/null 2>&1 || :', 'git fetch --no-tags origin || :', ]); }); @@ -209,7 +206,7 @@ describe('GitHelper', () => { execCalledWith(mockExec, [ `rm -rdf '${workDir}'`, 'git init \'.\'', - 'git remote add origin \'https://octocat:token@github.com/hello/world.git\' > /dev/null 2>&1 || :', + 'git remote add origin \'https://octocat:token1@github.com/hello/world.git\' > /dev/null 2>&1 || :', 'git fetch origin \'+refs/pull/*/merge:refs/remotes/pull/*/merge\' \'+refs/heads/hoge:refs/remotes/origin/hoge\' || :', ]); }); @@ -222,7 +219,7 @@ describe('GitHelper', () => { await helper.fetchBranch(workDir, 'test-branch', context()); execCalledWith(mockExec, [ - 'git fetch --prune --no-recurse-submodules \'--depth=3\' \'https://octocat:token@github.com/hello/world.git\' \'+refs/heads/test-branch:refs/remotes/origin/test-branch\' > /dev/null 2>&1 || :', + 'git fetch --prune --no-recurse-submodules \'--depth=3\' \'https://octocat:token1@github.com/hello/world.git\' \'+refs/heads/test-branch:refs/remotes/origin/test-branch\' > /dev/null 2>&1 || :', ]); }); }); @@ -336,7 +333,7 @@ describe('GitHelper', () => { }; } - const helper = new GitHelper(new ThrowErrorLogger()); + const helper = new GitHelper(new ThrowErrorLogger(), {token: 'token'}); await expect(helper.runCommand(workDir, [ 'command1', 'command2', @@ -466,7 +463,7 @@ describe('GitHelper', () => { execCalledWith(mockExec, [ 'git tag', 'git tag -d \'v1.2.3\' \'v1.2.4\' \'v1.2.5\' \'v1.2.6\' || :', - 'git fetch \'https://octocat:token@github.com/hello/world.git\' --tags > /dev/null 2>&1', + 'git fetch \'https://octocat:token1@github.com/hello/world.git\' --tags > /dev/null 2>&1', ]); }); @@ -480,7 +477,7 @@ describe('GitHelper', () => { 'git tag', 'git tag -d \'v1.2.3\' \'v1.2.4\' \'v1.2.5\' || :', 'git tag -d \'v1.2.6\' || :', - 'git fetch \'https://octocat:token@github.com/hello/world.git\' --tags > /dev/null 2>&1', + 'git fetch \'https://octocat:token1@github.com/hello/world.git\' --tags > /dev/null 2>&1', ]); }); }); @@ -492,7 +489,7 @@ describe('GitHelper', () => { await helper.deleteTag(workDir, 'delete-tag', context()); execCalledWith(mockExec, [ - 'git push \'https://octocat:token@github.com/hello/world.git\' --delete tags/delete-tag > /dev/null 2>&1 || :', + 'git push \'https://octocat:token1@github.com/hello/world.git\' --delete tags/delete-tag > /dev/null 2>&1 || :', 'git tag -d delete-tag || :', ]); }); @@ -506,7 +503,7 @@ describe('GitHelper', () => { ], context()); execCalledWith(mockExec, [ - 'git push \'https://octocat:token@github.com/hello/world.git\' --delete tags/delete-tag1 tags/delete-tag2 > /dev/null 2>&1 || :', + 'git push \'https://octocat:token1@github.com/hello/world.git\' --delete tags/delete-tag1 tags/delete-tag2 > /dev/null 2>&1 || :', 'git tag -d delete-tag1 delete-tag2 || :', ]); }); @@ -523,8 +520,8 @@ describe('GitHelper', () => { ], context(), 3); execCalledWith(mockExec, [ - 'git push \'https://octocat:token@github.com/hello/world.git\' --delete tags/delete-tag1 tags/delete-tag2 tags/delete-tag3 > /dev/null 2>&1 || :', - 'git push \'https://octocat:token@github.com/hello/world.git\' --delete tags/delete-tag4 refs/tags/delete-tag5 > /dev/null 2>&1 || :', + 'git push \'https://octocat:token1@github.com/hello/world.git\' --delete tags/delete-tag1 tags/delete-tag2 tags/delete-tag3 > /dev/null 2>&1 || :', + 'git push \'https://octocat:token1@github.com/hello/world.git\' --delete tags/delete-tag4 refs/tags/delete-tag5 > /dev/null 2>&1 || :', 'git tag -d delete-tag1 delete-tag2 delete-tag3 || :', 'git tag -d delete-tag4 delete-tag5 || :', ]); @@ -538,10 +535,10 @@ describe('GitHelper', () => { await helper.copyTag(workDir, 'new-tag', 'from-tag', context()); execCalledWith(mockExec, [ - 'git push \'https://octocat:token@github.com/hello/world.git\' --delete tags/new-tag > /dev/null 2>&1 || :', + 'git push \'https://octocat:token1@github.com/hello/world.git\' --delete tags/new-tag > /dev/null 2>&1 || :', 'git tag -d new-tag || :', 'git tag new-tag from-tag', - 'git push \'https://octocat:token@github.com/hello/world.git\' refs/tags/new-tag > /dev/null 2>&1', + 'git push \'https://octocat:token1@github.com/hello/world.git\' refs/tags/new-tag > /dev/null 2>&1', ]); }); }); @@ -576,7 +573,7 @@ describe('GitHelper', () => { await helper.push(workDir, 'test-branch', true, context()); execCalledWith(mockExec, [ - 'git push --tags \'https://octocat:token@github.com/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1', + 'git push --tags \'https://octocat:token1@github.com/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1', ]); }); @@ -586,7 +583,7 @@ describe('GitHelper', () => { await helper.push(workDir, 'test-branch', false, context()); execCalledWith(mockExec, [ - 'git push \'https://octocat:token@github.com/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1', + 'git push \'https://octocat:token1@github.com/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1', ]); }); }); @@ -598,7 +595,7 @@ describe('GitHelper', () => { await helper.forcePush(workDir, 'test-branch', context()); execCalledWith(mockExec, [ - 'git push --force \'https://octocat:token@github.com/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1', + 'git push --force \'https://octocat:token1@github.com/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1', ]); }); }); @@ -705,11 +702,11 @@ describe('GitHelper', () => { })); execCalledWith(mockExec, [ - 'git clone \'--branch=test\' \'--depth=3\' \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :', + 'git clone \'--branch=test\' \'--depth=3\' \'https://octocat:token1@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :', 'git clone \'--branch=test\' \'--depth=3\' origin \'.\' > /dev/null 2>&1 || :', 'git clone \'--branch=test\' \'--depth=3\' test \'.\' > /dev/null 2>&1 || :', 'git clone \'--branch=test\' \'--depth=3\' origin \'.\' || :', - 'git clone \'--branch=test\' \'--depth=3\' \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :', + 'git clone \'--branch=test\' \'--depth=3\' \'https://octocat:token1@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :', ]); }); }); @@ -718,11 +715,8 @@ describe('GitHelper', () => { describe('GitHelper with params 1', () => { testEnv(); testChildProcess(); - beforeEach(() => { - process.env.INPUT_GITHUB_TOKEN = 'token'; - }); - const helper = new GitHelper(new Logger(), {depth: 1, filter: (line: string): boolean => line.endsWith('.md')}); + const helper = new GitHelper(new Logger(), {depth: 1, filter: (line: string): boolean => line.endsWith('.md'), token: 'token2'}); describe('clone', () => { it('should run git clone', async() => { @@ -734,7 +728,7 @@ describe('GitHelper with params 1', () => { })); execCalledWith(mockExec, [ - 'git clone \'--branch=test\' \'--depth=1\' \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :', + 'git clone \'--branch=test\' \'--depth=1\' \'https://octocat:token2@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :', ]); }); }); @@ -752,14 +746,33 @@ describe('GitHelper with params 1', () => { describe('GitHelper with params 2', () => { testEnv(); testChildProcess(); - beforeEach(() => { - process.env.INPUT_GITHUB_TOKEN = 'token'; + + describe('clone', () => { + it('should run git clone', async() => { + process.env.INPUT_GITHUB_TOKEN = 'token3'; + const helper = new GitHelper(new Logger(), {depth: -1}); + setExists(false); + const mockExec = spyOnExec(); + + await helper.clone(workDir, context({ + ref: 'refs/heads/test', + })); + + execCalledWith(mockExec, [ + 'git clone \'--branch=test\' \'https://octocat:token3@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :', + ]); + }); }); +}); - const helper = new GitHelper(new Logger(), {depth: -1}); +describe('GitHelper without params', () => { + testEnv(); + testChildProcess(); describe('clone', () => { it('should run git clone', async() => { + process.env.INPUT_GITHUB_TOKEN = 'token4'; + const helper = new GitHelper(new Logger()); setExists(false); const mockExec = spyOnExec(); @@ -768,7 +781,7 @@ describe('GitHelper with params 2', () => { })); execCalledWith(mockExec, [ - 'git clone \'--branch=test\' \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :', + 'git clone \'--branch=test\' \'--depth=3\' \'https://octocat:token4@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :', ]); }); }); diff --git a/src/git-helper.ts b/src/git-helper.ts index a4b6c165..67bf5165 100644 --- a/src/git-helper.ts +++ b/src/git-helper.ts @@ -1,8 +1,8 @@ import fs from 'fs'; import { Context } from '@actions/github/lib/context'; import { Command, Logger } from './index'; -import { getBranch, isBranch, isPrRef, isCloned, split, generateNewPatchVersion, arrayChunk, versionCompare } from './utils'; -import { getGitUrl } from './context-helper'; +import { getBranch, isBranch, isPrRef, isCloned, split, generateNewPatchVersion, arrayChunk, versionCompare, getAccessToken } from './utils'; +import { getGitUrlWithToken } from './context-helper'; type CommandType = string | { command: string; @@ -22,6 +22,7 @@ export default class GitHelper { private readonly command: Command; private readonly cloneDepth: string; private readonly filter: (string) => boolean; + private readonly token: string; private origin?: string = undefined; private quietIfNotOrigin = true; @@ -31,13 +32,16 @@ export default class GitHelper { * @param {number|undefined} options.depth depth * @param {function|undefined} options.filter filter */ - constructor(private readonly logger: Logger, options?: { depth?: number; filter?: (string: string) => boolean }) { + constructor(private readonly logger: Logger, options?: { depth?: number; filter?: (string: string) => boolean; token?: string }) { this.command = new Command(logger); + this.token = options?.token ?? getAccessToken(true); + if (options && options.depth) { this.cloneDepth = options.depth > 0 ? `--depth=${options.depth}` : ''; // eslint-disable-line no-magic-numbers } else { this.cloneDepth = '--depth=3'; } + if (options && options.filter) { this.filter = options.filter; } else { @@ -115,7 +119,7 @@ export default class GitHelper { * @param {Context} context context * @return {string} origin */ - private getOrigin = (context: Context): string => this.origin ?? getGitUrl(context); + private getOrigin = (context: Context): string => this.origin ?? getGitUrlWithToken(context, this.token); /** * @param {string} workDir work dir From 42170278c1b7fa825ddf8a73c703f4d97dca1513 Mon Sep 17 00:00:00 2001 From: Technote Date: Fri, 24 Jan 2020 02:59:48 +0900 Subject: [PATCH 4/4] feat: Update package version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d732e5b8..7c936a82 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@technote-space/github-action-helper", - "version": "0.8.0", + "version": "0.8.1", "description": "Helper to filter GitHub Action.", "author": "Technote (https://technote.space)", "license": "MIT",