Skip to content

Commit 6ede14f

Browse files
feat: update git helper
1 parent 19546f6 commit 6ede14f

File tree

2 files changed

+99
-91
lines changed

2 files changed

+99
-91
lines changed

__tests__/git-helper.test.ts

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ describe('GitHelper', () => {
7070
]);
7171
});
7272

73-
it('should run git checkout', async() => {
73+
it('should run git clone PR', async() => {
7474
setExists(false);
7575
const mockExec = spyOnExec();
7676

77-
expect(await helper.clone(workDir, context({
77+
await helper.clone(workDir, context({
7878
ref: 'refs/pull/123/merge',
79-
})));
79+
}));
8080

8181
execCalledWith(mockExec, [
8282
'git clone \'--depth=3\' \'https://octocat:[email protected]/hello/world.git\' \'.\' > /dev/null 2>&1 || :',
@@ -85,48 +85,64 @@ describe('GitHelper', () => {
8585
]);
8686
});
8787

88-
it('should throw error', async() => {
88+
it('should run checkout', async() => {
8989
setExists(false);
90+
const mockExec = spyOnExec();
91+
92+
await helper.clone(workDir, context({
93+
ref: 'refs/tags/v1.2.3',
94+
sha: '1234567890',
95+
}));
9096

91-
await expect(helper.clone(workDir, context({
92-
ref: '',
93-
sha: '',
94-
}))).rejects.toThrow('Invalid context.');
97+
execCalledWith(mockExec, [
98+
'git init \'.\'',
99+
'git remote add origin \'https://octocat:[email protected]/hello/world.git\' > /dev/null 2>&1 || :',
100+
'git fetch --no-tags origin \'refs/tags/v1.2.3:refs/tags/v1.2.3\' || :',
101+
'git checkout -qf 1234567890',
102+
]);
95103
});
96104
});
97105

98106
describe('checkout', () => {
99-
it('should run checkout 1', async() => {
107+
it('should run checkout branch', async() => {
100108
const mockExec = spyOnExec();
101109

102110
await helper.checkout(workDir, context());
103111

104112
execCalledWith(mockExec, [
105-
'git clone \'--depth=3\' \'https://octocat:[email protected]/hello/world.git\' \'.\' > /dev/null 2>&1',
106-
'git fetch \'https://octocat:[email protected]/hello/world.git\' refs/heads/test-ref > /dev/null 2>&1',
113+
'rm -rdf \'.work\'',
114+
'git init \'.\'',
115+
'git remote add origin \'https://octocat:[email protected]/hello/world.git\' > /dev/null 2>&1 || :',
116+
'git fetch --no-tags origin \'refs/heads/test-ref:refs/remotes/origin/test-ref\' || :',
107117
'git checkout -qf test-sha',
108118
]);
109119
});
110120

111-
it('should run checkout 2', async() => {
121+
it('should run checkout merge ref', async() => {
112122
const mockExec = spyOnExec();
113123

114-
await helper.checkout(workDir, context({sha: ''}));
124+
await helper.checkout(workDir, context({ref: 'refs/pull/123/merge'}));
115125

116126
execCalledWith(mockExec, [
117-
'git clone \'https://octocat:[email protected]/hello/world.git\' \'.\' > /dev/null 2>&1',
118-
'git checkout -qf test-ref',
127+
'rm -rdf \'.work\'',
128+
'git init \'.\'',
129+
'git remote add origin \'https://octocat:[email protected]/hello/world.git\' > /dev/null 2>&1 || :',
130+
'git fetch --no-tags origin \'refs/pull/123/merge:refs/pull/123/merge\' || :',
131+
'git checkout -qf test-sha',
119132
]);
120133
});
121134

122-
it('should run checkout 3', async() => {
135+
it('should run checkout tag', async() => {
123136
const mockExec = spyOnExec();
124137

125-
await helper.checkout(workDir, context({sha: '', ref: 'refs/tags/test-tag'}));
138+
await helper.checkout(workDir, context({ref: 'refs/tags/v1.2.3'}));
126139

127140
execCalledWith(mockExec, [
128-
'git clone \'https://octocat:[email protected]/hello/world.git\' \'.\' > /dev/null 2>&1',
129-
'git checkout -qf refs/tags/test-tag',
141+
'rm -rdf \'.work\'',
142+
'git init \'.\'',
143+
'git remote add origin \'https://octocat:[email protected]/hello/world.git\' > /dev/null 2>&1 || :',
144+
'git fetch --no-tags origin \'refs/tags/v1.2.3:refs/tags/v1.2.3\' || :',
145+
'git checkout -qf test-sha',
130146
]);
131147
});
132148
});
@@ -451,6 +467,24 @@ describe('GitHelper', () => {
451467
'git show \'--stat-count=10\' HEAD',
452468
]);
453469
});
470+
471+
it('should run git commit with options', async() => {
472+
setChildProcessParams({stdout: 'M file1\n\nM file2\n'});
473+
const mockExec = spyOnExec();
474+
475+
expect(await helper.commit(workDir, 'test', {
476+
count: 20,
477+
allowEmpty: true,
478+
args: ['--dry-run'],
479+
})).toBeTruthy();
480+
481+
execCalledWith(mockExec, [
482+
'git add --all',
483+
'git status --short -uno',
484+
'git commit --allow-empty --dry-run -qm test',
485+
'git show \'--stat-count=20\' HEAD',
486+
]);
487+
});
454488
});
455489

456490
describe('fetchTags', () => {

src/git-helper.ts

Lines changed: 46 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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, getAccessToken } from './utils';
4+
import { getBranch, isBranch, isPrRef, getRefspec, isCloned, split, generateNewPatchVersion, arrayChunk, versionCompare, getAccessToken } from './utils';
55
import { getGitUrlWithToken } from './context-helper';
66

77
type CommandType = string | {
@@ -82,17 +82,34 @@ export default class GitHelper {
8282
}
8383
};
8484

85+
/**
86+
* @param {string} workDir work dir
87+
* @return {Promise<void>} void
88+
*/
89+
private initialize = async(workDir: string): Promise<void> => {
90+
if (fs.existsSync(workDir)) {
91+
await this.runCommand(workDir, {command: 'rm', args: ['-rdf', workDir]});
92+
}
93+
fs.mkdirSync(workDir, {recursive: true});
94+
await this.runCommand(workDir, {command: 'git init', args: ['.']});
95+
};
96+
97+
/**
98+
* @param {Context} context context
99+
* @return {string} origin
100+
*/
101+
private getOrigin = (context: Context): string => this.origin ?? getGitUrlWithToken(context, this.token);
102+
85103
/**
86104
* @param {string} workDir work dir
87105
* @param {Context} context context
88106
* @return {Promise<void>} void
89107
*/
90108
public addOrigin = async(workDir: string, context: Context): Promise<void> => {
91-
const url = this.getOrigin(context);
92109
await this.initialize(workDir);
93110
await this.runCommand(workDir, {
94111
command: 'git remote add',
95-
args: ['origin', url],
112+
args: ['origin', getGitUrlWithToken(context, this.token)],
96113
quiet: this.isQuiet(),
97114
altCommand: 'git remote add origin',
98115
suppressError: true,
@@ -115,12 +132,6 @@ export default class GitHelper {
115132
*/
116133
private isQuiet = (): boolean => !this.origin || this.quietIfNotOrigin;
117134

118-
/**
119-
* @param {Context} context context
120-
* @return {string} origin
121-
*/
122-
private getOrigin = (context: Context): string => this.origin ?? getGitUrlWithToken(context, this.token);
123-
124135
/**
125136
* @param {string} workDir work dir
126137
* @return {Promise<string>} branch name
@@ -199,64 +210,6 @@ export default class GitHelper {
199210
}
200211
};
201212

202-
/**
203-
* @param {string} workDir work dir
204-
* @param {Context} context context
205-
* @return {Promise<void>} void
206-
*/
207-
public checkout = async(workDir: string, context: Context): Promise<void> => {
208-
const url = this.getOrigin(context);
209-
if (this.cloneDepth && context.sha) {
210-
await this.runCommand(workDir, [
211-
{
212-
command: 'git clone',
213-
args: [this.cloneDepth, url, '.'],
214-
quiet: this.isQuiet(),
215-
altCommand: 'git clone',
216-
},
217-
{
218-
command: 'git fetch',
219-
args: [url, context.ref],
220-
quiet: this.isQuiet(),
221-
altCommand: `git fetch origin ${context.ref}`,
222-
},
223-
{
224-
command: 'git checkout',
225-
args: ['-qf', context.sha],
226-
},
227-
]);
228-
} else {
229-
const checkout = context.sha || getBranch(context) || context.ref;
230-
if (!checkout) {
231-
throw new Error('Invalid context.');
232-
}
233-
await this.runCommand(workDir, [
234-
{
235-
command: 'git clone',
236-
args: [url, '.'],
237-
quiet: this.isQuiet(),
238-
altCommand: 'git clone',
239-
},
240-
{
241-
command: 'git checkout',
242-
args: ['-qf', checkout],
243-
},
244-
]);
245-
}
246-
};
247-
248-
/**
249-
* @param {string} workDir work dir
250-
* @return {Promise<void>} void
251-
*/
252-
private initialize = async(workDir: string): Promise<void> => {
253-
if (fs.existsSync(workDir)) {
254-
await this.runCommand(workDir, {command: 'rm', args: ['-rdf', workDir]});
255-
}
256-
fs.mkdirSync(workDir, {recursive: true});
257-
await this.runCommand(workDir, {command: 'git init', args: ['.']});
258-
};
259-
260213
/**
261214
* @param {string} workDir work dir
262215
* @param {string} branch branch
@@ -288,6 +241,22 @@ export default class GitHelper {
288241
});
289242
};
290243

244+
/**
245+
* @param {string} workDir work dir
246+
* @param {Context} context context
247+
* @return {Promise<void>} void
248+
*/
249+
public checkout = async(workDir: string, context: Context): Promise<void> => {
250+
await this.fetchOrigin(workDir, context, ['--no-tags'], [getRefspec(context)]);
251+
await this.runCommand(workDir, [
252+
{
253+
command: 'git checkout',
254+
args: ['-qf', context.sha],
255+
stderrToStdout: true,
256+
},
257+
]);
258+
};
259+
291260
/**
292261
* @param {string} workDir work dir
293262
* @param {string} branch branch
@@ -387,29 +356,34 @@ export default class GitHelper {
387356
/**
388357
* @param {string} workDir work dir
389358
* @param {string} message message
359+
* @param {object} options options
390360
*/
391-
public commit = async(workDir: string, message: string): Promise<boolean> => {
361+
public commit = async(workDir: string, message: string, options?: { count?: number; allowEmpty?: boolean; args?: Array<string> }): Promise<boolean> => {
392362
await this.runCommand(workDir, {command: 'git add', args: ['--all']});
393363

394364
if (!await this.checkDiff(workDir)) {
395365
this.logger.info('There is no diff.');
396366
return false;
397367
}
398368

399-
await this.makeCommit(workDir, message);
369+
await this.makeCommit(workDir, message, options);
400370
return true;
401371
};
402372

403373
/**
404374
* @param {string} workDir work dir
405375
* @param {string} message message
406-
* @param {number} count stat count
376+
* @param {object} options options
407377
*/
408-
public makeCommit = async(workDir: string, message: string, count = 10): Promise<void> => { // eslint-disable-line no-magic-numbers
378+
public makeCommit = async(workDir: string, message: string, options?: { count?: number; allowEmpty?: boolean; args?: Array<string> }): Promise<void> => {
379+
const count = options?.count ?? 10; // eslint-disable-line no-magic-numbers
380+
const allowEmpty = options?.allowEmpty ?? false;
381+
const args = options?.args ?? [];
382+
409383
await this.runCommand(workDir, [
410384
{
411385
command: 'git commit',
412-
args: ['-qm', message],
386+
args: [allowEmpty ? '--allow-empty' : '', ...args, '-qm', message],
413387
},
414388
{
415389
command: 'git show',

0 commit comments

Comments
 (0)