Skip to content

Commit f071998

Browse files
Merge pull request #206 from technote-space/release/v0.8.1
Release/v0.8.1
2 parents a0e187a + 4217027 commit f071998

File tree

5 files changed

+75
-59
lines changed

5 files changed

+75
-59
lines changed

__tests__/git-helper.test.ts

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@ const context = (override: object = {}): Context => getContext(Object.assign({
2525
describe('GitHelper', () => {
2626
testEnv();
2727
testChildProcess();
28-
beforeEach(() => {
29-
process.env.INPUT_GITHUB_TOKEN = 'token';
30-
});
3128

32-
const helper = new GitHelper(new Logger());
29+
const helper = new GitHelper(new Logger(), {token: 'token1'});
3330

3431
describe('getCurrentBranchName', () => {
3532
it('should return empty 1', async() => {
@@ -69,7 +66,7 @@ describe('GitHelper', () => {
6966
}));
7067

7168
execCalledWith(mockExec, [
72-
'git clone \'--branch=test\' \'--depth=3\' \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :',
69+
'git clone \'--branch=test\' \'--depth=3\' \'https://octocat:token1@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :',
7370
]);
7471
});
7572

@@ -82,8 +79,8 @@ describe('GitHelper', () => {
8279
})));
8380

8481
execCalledWith(mockExec, [
85-
'git clone \'--depth=3\' \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :',
86-
'git fetch \'https://octocat:token@github.com/hello/world.git\' \'+refs/pull/123/merge\' > /dev/null 2>&1',
82+
'git clone \'--depth=3\' \'https://octocat:token1@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :',
83+
'git fetch \'https://octocat:token1@github.com/hello/world.git\' \'+refs/pull/123/merge\' > /dev/null 2>&1',
8784
'git checkout -qf FETCH_HEAD',
8885
]);
8986
});
@@ -105,8 +102,8 @@ describe('GitHelper', () => {
105102
await helper.checkout(workDir, context());
106103

107104
execCalledWith(mockExec, [
108-
'git clone \'--depth=3\' \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1',
109-
'git fetch \'https://octocat:token@github.com/hello/world.git\' refs/heads/test-ref > /dev/null 2>&1',
105+
'git clone \'--depth=3\' \'https://octocat:token1@github.com/hello/world.git\' \'.\' > /dev/null 2>&1',
106+
'git fetch \'https://octocat:token1@github.com/hello/world.git\' refs/heads/test-ref > /dev/null 2>&1',
110107
'git checkout -qf test-sha',
111108
]);
112109
});
@@ -117,7 +114,7 @@ describe('GitHelper', () => {
117114
await helper.checkout(workDir, context({sha: ''}));
118115

119116
execCalledWith(mockExec, [
120-
'git clone \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1',
117+
'git clone \'https://octocat:token1@github.com/hello/world.git\' \'.\' > /dev/null 2>&1',
121118
'git checkout -qf test-ref',
122119
]);
123120
});
@@ -128,7 +125,7 @@ describe('GitHelper', () => {
128125
await helper.checkout(workDir, context({sha: '', ref: 'refs/tags/test-tag'}));
129126

130127
execCalledWith(mockExec, [
131-
'git clone \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1',
128+
'git clone \'https://octocat:token1@github.com/hello/world.git\' \'.\' > /dev/null 2>&1',
132129
'git checkout -qf refs/tags/test-tag',
133130
]);
134131
});
@@ -169,7 +166,7 @@ describe('GitHelper', () => {
169166
execCalledWith(mockExec, [
170167
`rm -rdf '${workDir}'`,
171168
'git init \'.\'',
172-
'git remote add origin \'https://octocat:token@github.com/hello/world.git\' > /dev/null 2>&1 || :',
169+
'git remote add origin \'https://octocat:token1@github.com/hello/world.git\' > /dev/null 2>&1 || :',
173170
]);
174171
});
175172
});
@@ -183,7 +180,7 @@ describe('GitHelper', () => {
183180
execCalledWith(mockExec, [
184181
`rm -rdf '${workDir}'`,
185182
'git init \'.\'',
186-
'git remote add origin \'https://octocat:token@github.com/hello/world.git\' > /dev/null 2>&1 || :',
183+
'git remote add origin \'https://octocat:token1@github.com/hello/world.git\' > /dev/null 2>&1 || :',
187184
'git fetch origin || :',
188185
]);
189186
});
@@ -196,7 +193,7 @@ describe('GitHelper', () => {
196193
execCalledWith(mockExec, [
197194
`rm -rdf '${workDir}'`,
198195
'git init \'.\'',
199-
'git remote add origin \'https://octocat:token@github.com/hello/world.git\' > /dev/null 2>&1 || :',
196+
'git remote add origin \'https://octocat:token1@github.com/hello/world.git\' > /dev/null 2>&1 || :',
200197
'git fetch --no-tags origin || :',
201198
]);
202199
});
@@ -209,7 +206,7 @@ describe('GitHelper', () => {
209206
execCalledWith(mockExec, [
210207
`rm -rdf '${workDir}'`,
211208
'git init \'.\'',
212-
'git remote add origin \'https://octocat:token@github.com/hello/world.git\' > /dev/null 2>&1 || :',
209+
'git remote add origin \'https://octocat:token1@github.com/hello/world.git\' > /dev/null 2>&1 || :',
213210
'git fetch origin \'+refs/pull/*/merge:refs/remotes/pull/*/merge\' \'+refs/heads/hoge:refs/remotes/origin/hoge\' || :',
214211
]);
215212
});
@@ -222,7 +219,7 @@ describe('GitHelper', () => {
222219
await helper.fetchBranch(workDir, 'test-branch', context());
223220

224221
execCalledWith(mockExec, [
225-
'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 || :',
222+
'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 || :',
226223
]);
227224
});
228225
});
@@ -336,7 +333,7 @@ describe('GitHelper', () => {
336333
};
337334
}
338335

339-
const helper = new GitHelper(new ThrowErrorLogger());
336+
const helper = new GitHelper(new ThrowErrorLogger(), {token: 'token'});
340337
await expect(helper.runCommand(workDir, [
341338
'command1',
342339
'command2',
@@ -466,7 +463,7 @@ describe('GitHelper', () => {
466463
execCalledWith(mockExec, [
467464
'git tag',
468465
'git tag -d \'v1.2.3\' \'v1.2.4\' \'v1.2.5\' \'v1.2.6\' || :',
469-
'git fetch \'https://octocat:token@github.com/hello/world.git\' --tags > /dev/null 2>&1',
466+
'git fetch \'https://octocat:token1@github.com/hello/world.git\' --tags > /dev/null 2>&1',
470467
]);
471468
});
472469

@@ -480,7 +477,7 @@ describe('GitHelper', () => {
480477
'git tag',
481478
'git tag -d \'v1.2.3\' \'v1.2.4\' \'v1.2.5\' || :',
482479
'git tag -d \'v1.2.6\' || :',
483-
'git fetch \'https://octocat:token@github.com/hello/world.git\' --tags > /dev/null 2>&1',
480+
'git fetch \'https://octocat:token1@github.com/hello/world.git\' --tags > /dev/null 2>&1',
484481
]);
485482
});
486483
});
@@ -492,7 +489,7 @@ describe('GitHelper', () => {
492489
await helper.deleteTag(workDir, 'delete-tag', context());
493490

494491
execCalledWith(mockExec, [
495-
'git push \'https://octocat:token@github.com/hello/world.git\' --delete tags/delete-tag > /dev/null 2>&1 || :',
492+
'git push \'https://octocat:token1@github.com/hello/world.git\' --delete tags/delete-tag > /dev/null 2>&1 || :',
496493
'git tag -d delete-tag || :',
497494
]);
498495
});
@@ -506,7 +503,7 @@ describe('GitHelper', () => {
506503
], context());
507504

508505
execCalledWith(mockExec, [
509-
'git push \'https://octocat:token@github.com/hello/world.git\' --delete tags/delete-tag1 tags/delete-tag2 > /dev/null 2>&1 || :',
506+
'git push \'https://octocat:token1@github.com/hello/world.git\' --delete tags/delete-tag1 tags/delete-tag2 > /dev/null 2>&1 || :',
510507
'git tag -d delete-tag1 delete-tag2 || :',
511508
]);
512509
});
@@ -523,8 +520,8 @@ describe('GitHelper', () => {
523520
], context(), 3);
524521

525522
execCalledWith(mockExec, [
526-
'git push \'https://octocat:token@github.com/hello/world.git\' --delete tags/delete-tag1 tags/delete-tag2 tags/delete-tag3 > /dev/null 2>&1 || :',
527-
'git push \'https://octocat:token@github.com/hello/world.git\' --delete tags/delete-tag4 refs/tags/delete-tag5 > /dev/null 2>&1 || :',
523+
'git push \'https://octocat:token1@github.com/hello/world.git\' --delete tags/delete-tag1 tags/delete-tag2 tags/delete-tag3 > /dev/null 2>&1 || :',
524+
'git push \'https://octocat:token1@github.com/hello/world.git\' --delete tags/delete-tag4 refs/tags/delete-tag5 > /dev/null 2>&1 || :',
528525
'git tag -d delete-tag1 delete-tag2 delete-tag3 || :',
529526
'git tag -d delete-tag4 delete-tag5 || :',
530527
]);
@@ -538,10 +535,10 @@ describe('GitHelper', () => {
538535
await helper.copyTag(workDir, 'new-tag', 'from-tag', context());
539536

540537
execCalledWith(mockExec, [
541-
'git push \'https://octocat:token@github.com/hello/world.git\' --delete tags/new-tag > /dev/null 2>&1 || :',
538+
'git push \'https://octocat:token1@github.com/hello/world.git\' --delete tags/new-tag > /dev/null 2>&1 || :',
542539
'git tag -d new-tag || :',
543540
'git tag new-tag from-tag',
544-
'git push \'https://octocat:token@github.com/hello/world.git\' refs/tags/new-tag > /dev/null 2>&1',
541+
'git push \'https://octocat:token1@github.com/hello/world.git\' refs/tags/new-tag > /dev/null 2>&1',
545542
]);
546543
});
547544
});
@@ -576,7 +573,7 @@ describe('GitHelper', () => {
576573
await helper.push(workDir, 'test-branch', true, context());
577574

578575
execCalledWith(mockExec, [
579-
'git push --tags \'https://octocat:token@github.com/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1',
576+
'git push --tags \'https://octocat:token1@github.com/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1',
580577
]);
581578
});
582579

@@ -586,7 +583,7 @@ describe('GitHelper', () => {
586583
await helper.push(workDir, 'test-branch', false, context());
587584

588585
execCalledWith(mockExec, [
589-
'git push \'https://octocat:token@github.com/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1',
586+
'git push \'https://octocat:token1@github.com/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1',
590587
]);
591588
});
592589
});
@@ -598,7 +595,7 @@ describe('GitHelper', () => {
598595
await helper.forcePush(workDir, 'test-branch', context());
599596

600597
execCalledWith(mockExec, [
601-
'git push --force \'https://octocat:token@github.com/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1',
598+
'git push --force \'https://octocat:token1@github.com/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1',
602599
]);
603600
});
604601
});
@@ -705,11 +702,11 @@ describe('GitHelper', () => {
705702
}));
706703

707704
execCalledWith(mockExec, [
708-
'git clone \'--branch=test\' \'--depth=3\' \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :',
705+
'git clone \'--branch=test\' \'--depth=3\' \'https://octocat:token1@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :',
709706
'git clone \'--branch=test\' \'--depth=3\' origin \'.\' > /dev/null 2>&1 || :',
710707
'git clone \'--branch=test\' \'--depth=3\' test \'.\' > /dev/null 2>&1 || :',
711708
'git clone \'--branch=test\' \'--depth=3\' origin \'.\' || :',
712-
'git clone \'--branch=test\' \'--depth=3\' \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :',
709+
'git clone \'--branch=test\' \'--depth=3\' \'https://octocat:token1@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :',
713710
]);
714711
});
715712
});
@@ -718,11 +715,8 @@ describe('GitHelper', () => {
718715
describe('GitHelper with params 1', () => {
719716
testEnv();
720717
testChildProcess();
721-
beforeEach(() => {
722-
process.env.INPUT_GITHUB_TOKEN = 'token';
723-
});
724718

725-
const helper = new GitHelper(new Logger(), {depth: 1, filter: (line: string): boolean => line.endsWith('.md')});
719+
const helper = new GitHelper(new Logger(), {depth: 1, filter: (line: string): boolean => line.endsWith('.md'), token: 'token2'});
726720

727721
describe('clone', () => {
728722
it('should run git clone', async() => {
@@ -734,7 +728,7 @@ describe('GitHelper with params 1', () => {
734728
}));
735729

736730
execCalledWith(mockExec, [
737-
'git clone \'--branch=test\' \'--depth=1\' \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :',
731+
'git clone \'--branch=test\' \'--depth=1\' \'https://octocat:token2@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :',
738732
]);
739733
});
740734
});
@@ -752,14 +746,33 @@ describe('GitHelper with params 1', () => {
752746
describe('GitHelper with params 2', () => {
753747
testEnv();
754748
testChildProcess();
755-
beforeEach(() => {
756-
process.env.INPUT_GITHUB_TOKEN = 'token';
749+
750+
describe('clone', () => {
751+
it('should run git clone', async() => {
752+
process.env.INPUT_GITHUB_TOKEN = 'token3';
753+
const helper = new GitHelper(new Logger(), {depth: -1});
754+
setExists(false);
755+
const mockExec = spyOnExec();
756+
757+
await helper.clone(workDir, context({
758+
ref: 'refs/heads/test',
759+
}));
760+
761+
execCalledWith(mockExec, [
762+
'git clone \'--branch=test\' \'https://octocat:[email protected]/hello/world.git\' \'.\' > /dev/null 2>&1 || :',
763+
]);
764+
});
757765
});
766+
});
758767

759-
const helper = new GitHelper(new Logger(), {depth: -1});
768+
describe('GitHelper without params', () => {
769+
testEnv();
770+
testChildProcess();
760771

761772
describe('clone', () => {
762773
it('should run git clone', async() => {
774+
process.env.INPUT_GITHUB_TOKEN = 'token4';
775+
const helper = new GitHelper(new Logger());
763776
setExists(false);
764777
const mockExec = spyOnExec();
765778

@@ -768,7 +781,7 @@ describe('GitHelper with params 2', () => {
768781
}));
769782

770783
execCalledWith(mockExec, [
771-
'git clone \'--branch=test\' \'https://octocat:token@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :',
784+
'git clone \'--branch=test\' \'--depth=3\' \'https://octocat:token4@github.com/hello/world.git\' \'.\' > /dev/null 2>&1 || :',
772785
]);
773786
});
774787
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@technote-space/github-action-helper",
3-
"version": "0.8.0",
3+
"version": "0.8.1",
44
"description": "Helper to filter GitHub Action.",
55
"author": "Technote <[email protected]> (https://technote.space)",
66
"license": "MIT",

src/context-helper.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,19 @@ export const getSender = (context: Context): string | false => context.payload.s
2121

2222
export const getRepository = (context: Context): string => `${context.repo.owner}/${context.repo.repo}`;
2323

24-
export const getGitUrl = (context: Context, accessTokenRequired = true): string => {
25-
const token = getAccessToken(accessTokenRequired);
26-
if (token) {
27-
return `https://${getActor()}:${token}@github.com/${context.repo.owner}/${context.repo.repo}.git`;
28-
} else {
29-
return `https://github.com/${context.repo.owner}/${context.repo.repo}.git`;
30-
}
31-
};
24+
const getGitUrlAuthInfo = (token: string | undefined): string => token ? `${getActor()}:${token}@` : '';
25+
26+
export const getGitUrlWithToken = (context: Context, token?: string | undefined): string => `https://${getGitUrlAuthInfo(token)}github.com/${context.repo.owner}/${context.repo.repo}.git`;
27+
28+
export const getGitUrl = (context: Context, accessTokenRequired = true): string => getGitUrlWithToken(context, getAccessToken(accessTokenRequired));
3229

3330
export const showActionInfo = (rootDir: string, logger: Logger, context: Context): void => {
34-
const info = getBuildInfo(path.resolve(rootDir, 'build.json'));
35-
const tagName = getTagName(context);
31+
const info = getBuildInfo(path.resolve(rootDir, 'build.json'));
32+
const tagName = getTagName(context);
33+
const separator = '==================================================';
34+
3635
logger.log();
37-
logger.log('==================================================');
36+
logger.log(separator);
3837
if (false !== info) {
3938
if ('owner' in info) {
4039
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
6564
logger.startProcess('Dump Payload');
6665
console.log(context.payload);
6766
logger.endProcess();
68-
logger.log('==================================================');
67+
logger.log(separator);
6968
logger.log();
7069
};

src/git-helper.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import fs from 'fs';
22
import { Context } from '@actions/github/lib/context';
33
import { Command, Logger } from './index';
4-
import { getBranch, isBranch, isPrRef, isCloned, split, generateNewPatchVersion, arrayChunk, versionCompare } from './utils';
5-
import { getGitUrl } from './context-helper';
4+
import { getBranch, isBranch, isPrRef, isCloned, split, generateNewPatchVersion, arrayChunk, versionCompare, getAccessToken } from './utils';
5+
import { getGitUrlWithToken } from './context-helper';
66

77
type CommandType = string | {
88
command: string;
@@ -22,6 +22,7 @@ export default class GitHelper {
2222
private readonly command: Command;
2323
private readonly cloneDepth: string;
2424
private readonly filter: (string) => boolean;
25+
private readonly token: string;
2526
private origin?: string = undefined;
2627
private quietIfNotOrigin = true;
2728

@@ -31,13 +32,16 @@ export default class GitHelper {
3132
* @param {number|undefined} options.depth depth
3233
* @param {function|undefined} options.filter filter
3334
*/
34-
constructor(private readonly logger: Logger, options?: { depth?: number; filter?: (string: string) => boolean }) {
35+
constructor(private readonly logger: Logger, options?: { depth?: number; filter?: (string: string) => boolean; token?: string }) {
3536
this.command = new Command(logger);
37+
this.token = options?.token ?? getAccessToken(true);
38+
3639
if (options && options.depth) {
3740
this.cloneDepth = options.depth > 0 ? `--depth=${options.depth}` : ''; // eslint-disable-line no-magic-numbers
3841
} else {
3942
this.cloneDepth = '--depth=3';
4043
}
44+
4145
if (options && options.filter) {
4246
this.filter = options.filter;
4347
} else {
@@ -115,7 +119,7 @@ export default class GitHelper {
115119
* @param {Context} context context
116120
* @return {string} origin
117121
*/
118-
private getOrigin = (context: Context): string => this.origin ?? getGitUrl(context);
122+
private getOrigin = (context: Context): string => this.origin ?? getGitUrlWithToken(context, this.token);
119123

120124
/**
121125
* @param {string} workDir work dir

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,9 +1097,9 @@ cssom@~0.3.6:
10971097
integrity sha512-b0tGHbfegbhPJpxpiBPU2sCkigAqtM9O121le6bbOlgyV+NyGyCmVfJ6QW9eRjz8CpNfWEOYBIMIGRYkLwsIYg==
10981098

10991099
cssstyle@^2.0.0:
1100-
version "2.1.0"
1101-
resolved "https://registry.yarnpkg.com/cssstyle/-/cssstyle-2.1.0.tgz#99f50a3aa21d4af16e758ae3e454dcf5940b9122"
1102-
integrity sha512-1iwCdymVYhMdQWiZ+9mB7x+urdNLPGTWsIZt6euFk8Yi+dOERK2ccoAUA3Bl8I5vmK5qfz/eLkBRyLbs42ov4A==
1100+
version "2.2.0"
1101+
resolved "https://registry.yarnpkg.com/cssstyle/-/cssstyle-2.2.0.tgz#e4c44debccd6b7911ed617a4395e5754bba59992"
1102+
integrity sha512-sEb3XFPx3jNnCAMtqrXPDeSgQr+jojtCeNf8cvMNMh1cG970+lljssvQDzPq6lmmJu2Vhqood/gtEomBiHOGnA==
11031103
dependencies:
11041104
cssom "~0.3.6"
11051105

0 commit comments

Comments
 (0)