Skip to content

Commit

Permalink
Merge pull request #206 from technote-space/release/v0.8.1
Browse files Browse the repository at this point in the history
Release/v0.8.1
  • Loading branch information
technote-space authored Jan 23, 2020
2 parents a0e187a + 4217027 commit f071998
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 59 deletions.
91 changes: 52 additions & 39 deletions __tests__/git-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() => {
Expand Down Expand Up @@ -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 || :',
]);
});

Expand All @@ -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',
]);
});
Expand All @@ -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',
]);
});
Expand All @@ -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',
]);
});
Expand All @@ -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',
]);
});
Expand Down Expand Up @@ -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 || :',
]);
});
});
Expand All @@ -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 || :',
]);
});
Expand All @@ -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 || :',
]);
});
Expand All @@ -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\' || :',
]);
});
Expand All @@ -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 || :',
]);
});
});
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
]);
});

Expand All @@ -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',
]);
});
});
Expand All @@ -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 || :',
]);
});
Expand All @@ -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 || :',
]);
});
Expand All @@ -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 || :',
]);
Expand All @@ -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',
]);
});
});
Expand Down Expand Up @@ -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',
]);
});

Expand All @@ -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',
]);
});
});
Expand All @@ -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',
]);
});
});
Expand Down Expand Up @@ -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 || :',
]);
});
});
Expand All @@ -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() => {
Expand All @@ -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 || :',
]);
});
});
Expand All @@ -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:[email protected]/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();

Expand All @@ -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 || :',
]);
});
});
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-helper",
"version": "0.8.0",
"version": "0.8.1",
"description": "Helper to filter GitHub Action.",
"author": "Technote <[email protected]> (https://technote.space)",
"license": "MIT",
Expand Down
23 changes: 11 additions & 12 deletions src/context-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
};
12 changes: 8 additions & 4 deletions src/git-helper.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit f071998

Please sign in to comment.