|
| 1 | +import * as core from '@actions/core'; |
1 | 2 | import { Context } from '@actions/github/lib/context';
|
2 | 3 | import { exec, strictExec } from './exec';
|
3 | 4 | import { CIContext, GitContextOutput } from './output';
|
@@ -198,72 +199,71 @@ export class GitInteractorInput {
|
198 | 199 | */
|
199 | 200 | export class GitInteractor {
|
200 | 201 |
|
201 |
| - readonly interactorInput: GitInteractorInput; |
202 |
| - readonly contextInput: GitContextInput; |
203 |
| - |
204 |
| - constructor(interactorInput: GitInteractorInput, gitContextInput: GitContextInput) { |
205 |
| - this.interactorInput = interactorInput; |
206 |
| - this.contextInput = gitContextInput; |
207 |
| - } |
208 |
| - |
209 |
| - commitPushIfNeed = async (branch: string, version: string, mustFixVersion: boolean, |
210 |
| - dryRun: boolean): Promise<CIContext> => { |
211 |
| - const committable = this.interactorInput.allowCommit && mustFixVersion && !dryRun; |
| 202 | + commitPushIfNeed = async (iInput: GitInteractorInput, branch: string, version: string, |
| 203 | + mustFixVersion: boolean, dryRun: boolean): Promise<CIContext> => { |
| 204 | + const committable = iInput.allowCommit && mustFixVersion && !dryRun; |
212 | 205 | let commitMsg = '';
|
213 | 206 | let commitId = '';
|
214 | 207 | if (committable) {
|
215 |
| - commitMsg = `${this.interactorInput.prefixCiMsg} ${this.interactorInput.correctVerMsg} ${version}`; |
| 208 | + commitMsg = `${iInput.prefixCiMsg} ${iInput.correctVerMsg} ${version}`; |
216 | 209 | await strictExec('git', ['fetch', '--depth=1'], 'Cannot fetch');
|
217 | 210 | await strictExec('git', ['checkout', branch], 'Cannot checkout');
|
218 |
| - await this.globalConfig() |
219 |
| - .then(gc => strictExec('git', [...gc, ...this.commitArgs(commitMsg)], `Cannot commit`)); |
| 211 | + await this.globalConfig(iInput.userName, iInput.userEmail) |
| 212 | + .then(gc => strictExec('git', [...gc, ...this.commitArgs(iInput, commitMsg)], `Cannot commit`)); |
220 | 213 | await strictExec('git', ['show', '--shortstat', '--show-signature'], `Cannot show recently commit`, false);
|
221 | 214 | await strictExec('git', ['push'], `Cannot push`, false);
|
222 | 215 | commitId = (await strictExec('git', ['rev-parse', 'HEAD'], 'Cannot show last commit')).stdout;
|
223 | 216 | }
|
224 | 217 | return Promise.resolve({ mustFixVersion, isPushed: committable, commitMsg, commitId });
|
225 | 218 | };
|
226 | 219 |
|
227 |
| - tagThenPush = async (version: string, needTag: boolean, dryRun: boolean): Promise<CIContext> => { |
228 |
| - const taggable = this.interactorInput.allowTag && needTag && !dryRun; |
229 |
| - const v = `${this.contextInput.tagPrefix}${version}`; |
| 220 | + tagThenPush = async (iInput: GitInteractorInput, gInput: GitContextInput, version: string, needTag: boolean, |
| 221 | + dryRun: boolean): Promise<CIContext> => { |
| 222 | + const taggable = iInput.allowTag && needTag && !dryRun; |
| 223 | + const v = `${gInput.tagPrefix}${version}`; |
230 | 224 | let commitMsg = '';
|
231 | 225 | let commitId = '';
|
232 | 226 | if (taggable) {
|
233 |
| - commitMsg = `${this.interactorInput.releaseVerMsg} ${v}`; |
| 227 | + commitMsg = `${iInput.releaseVerMsg} ${v}`; |
234 | 228 | commitId = (await strictExec('git', ['rev-parse', '--short', 'HEAD'], 'Cannot show last commit')).stdout;
|
235 | 229 | await strictExec('git', ['fetch', '--depth=1'], 'Cannot fetch');
|
236 |
| - await this.globalConfig() |
237 |
| - .then(gc => strictExec('git', [...gc, ...this.tagArgs(commitMsg, v, commitId)], `Cannot tag`)); |
| 230 | + await this.globalConfig(iInput.userName, iInput.userEmail) |
| 231 | + .then(gc => strictExec('git', [...gc, ...this.tagArgs(iInput, commitMsg, v, commitId)], `Cannot tag`)); |
238 | 232 | await strictExec('git', ['show', '--shortstat', '--show-signature', v], `Cannot show tag`, false);
|
239 | 233 | await strictExec('git', ['push', '-uf', 'origin', v], `Cannot push`, false);
|
240 | 234 | }
|
241 | 235 | return Promise.resolve({ needTag, isPushed: taggable, commitMsg, commitId });
|
242 | 236 | };
|
243 | 237 |
|
244 |
| - getCommitMsg = async (commitId: string): Promise<string> => { |
245 |
| - return await exec('git', ['log', '--format=%B', '-n', '1', commitId]).then(r => r.success ? r.stdout : ''); |
246 |
| - }; |
| 238 | + getCommitMsg = async (commitId: string): Promise<string> => this.execSilent(['log', '--format=%B', '-n', '1', |
| 239 | + commitId]); |
| 240 | + |
| 241 | + removeRemoteBranch = async (branch: string): Promise<string> => this.execSilent(['push', 'origin', `:${branch}`]); |
247 | 242 |
|
248 |
| - private commitArgs(commitMsg: string) { |
249 |
| - return ['commit', ...this.gpgSign(true), '-a', '-m', commitMsg]; |
| 243 | + private commitArgs(iInput: GitInteractorInput, commitMsg: string) { |
| 244 | + return ['commit', ...this.gpgSign(iInput, true), '-a', '-m', commitMsg]; |
250 | 245 | }
|
251 | 246 |
|
252 |
| - private tagArgs(commitMsg: string, version: string, commitId: string) { |
253 |
| - return ['tag', ...this.gpgSign(false), '-a', '-m', `${commitMsg}`, version, commitId]; |
| 247 | + private tagArgs(iInput: GitInteractorInput, commitMsg: string, version: string, commitId: string) { |
| 248 | + return ['tag', ...this.gpgSign(iInput, false), '-a', '-m', `${commitMsg}`, version, commitId]; |
254 | 249 | }
|
255 | 250 |
|
256 |
| - private gpgSign(isCommit: boolean): string[] { |
257 |
| - return this.interactorInput.mustSign ? (isCommit ? [`-S`] : [`-s`]) : []; |
| 251 | + private gpgSign(iInput: GitInteractorInput, isCommit: boolean): string[] { |
| 252 | + return iInput.mustSign ? (isCommit ? [`-S`] : [`-s`]) : []; |
258 | 253 | }
|
259 | 254 |
|
260 |
| - private async globalConfig(): Promise<string[]> { |
261 |
| - const userName = (await this.getConfig('user.name', this.interactorInput.userName)); |
262 |
| - const userEmail = (await this.getConfig('user.email', this.interactorInput.userEmail)); |
| 255 | + private async globalConfig(uName: string, uEmail: string): Promise<string[]> { |
| 256 | + const userName = (await this.execSilent(['config', 'user.name'], uName)); |
| 257 | + const userEmail = (await this.execSilent(['config', 'user.email'], uEmail)); |
263 | 258 | return Promise.resolve(['-c', `user.name=${userName}`, '-c', `user.email=${userEmail}`]);
|
264 | 259 | }
|
265 | 260 |
|
266 |
| - private async getConfig(key: string, fallback: string): Promise<string> { |
267 |
| - return await exec('git', ['config', key]).then(r => r.success ? r.stdout : fallback); |
| 261 | + private async execSilent(args: string[], fallback: string = ''): Promise<string> { |
| 262 | + return await exec('git', args).then(r => { |
| 263 | + if (!r.success) { |
| 264 | + core.warning(`Cannot exec GIT ${args[0]}: ${r.stderr}`); |
| 265 | + } |
| 266 | + return r.success ? r.stdout : fallback; |
| 267 | + }); |
268 | 268 | }
|
269 | 269 | }
|
0 commit comments