Skip to content

Commit 2d09506

Browse files
committed
- Resolved #9
1 parent c4e70f7 commit 2d09506

File tree

5 files changed

+50
-42
lines changed

5 files changed

+50
-42
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/git.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as core from '@actions/core';
12
import { Context } from '@actions/github/lib/context';
23
import { exec, strictExec } from './exec';
34
import { CIContext, GitContextOutput } from './output';
@@ -198,72 +199,71 @@ export class GitInteractorInput {
198199
*/
199200
export class GitInteractor {
200201

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;
212205
let commitMsg = '';
213206
let commitId = '';
214207
if (committable) {
215-
commitMsg = `${this.interactorInput.prefixCiMsg} ${this.interactorInput.correctVerMsg} ${version}`;
208+
commitMsg = `${iInput.prefixCiMsg} ${iInput.correctVerMsg} ${version}`;
216209
await strictExec('git', ['fetch', '--depth=1'], 'Cannot fetch');
217210
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`));
220213
await strictExec('git', ['show', '--shortstat', '--show-signature'], `Cannot show recently commit`, false);
221214
await strictExec('git', ['push'], `Cannot push`, false);
222215
commitId = (await strictExec('git', ['rev-parse', 'HEAD'], 'Cannot show last commit')).stdout;
223216
}
224217
return Promise.resolve({ mustFixVersion, isPushed: committable, commitMsg, commitId });
225218
};
226219

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}`;
230224
let commitMsg = '';
231225
let commitId = '';
232226
if (taggable) {
233-
commitMsg = `${this.interactorInput.releaseVerMsg} ${v}`;
227+
commitMsg = `${iInput.releaseVerMsg} ${v}`;
234228
commitId = (await strictExec('git', ['rev-parse', '--short', 'HEAD'], 'Cannot show last commit')).stdout;
235229
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`));
238232
await strictExec('git', ['show', '--shortstat', '--show-signature', v], `Cannot show tag`, false);
239233
await strictExec('git', ['push', '-uf', 'origin', v], `Cannot push`, false);
240234
}
241235
return Promise.resolve({ needTag, isPushed: taggable, commitMsg, commitId });
242236
};
243237

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}`]);
247242

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];
250245
}
251246

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];
254249
}
255250

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`]) : [];
258253
}
259254

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));
263258
return Promise.resolve(['-c', `user.name=${userName}`, '-c', `user.email=${userEmail}`]);
264259
}
265260

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+
});
268268
}
269269
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ function process(context: Context, ghInput: GitContextInput, interactorInput: Gi
2626
.then(r => ops.checkCommitMsg(ghOutput).then(output => ops.ciStep(r, output, dryRun)))
2727
.then(output => ({ ...output, decision: ops.makeDecision(output) }))
2828
.then(output => ({ ...output, ver: ops.nextVersion(output) }))
29-
.then(output => ops.tweakVersion(output));
29+
.then(output => ops.tweakVersion(output))
30+
.then(output => ops.removeBranchIfNeed(output));
3031
}
3132

3233
function addActionOutputs(ghOutput: GitContextOutput) {

src/project.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ export class ProjectContextOps {
8787
if (ghOutput.commitMsg && ghOutput.commitMsg.trim().length > 0) {
8888
return Promise.resolve(ghOutput);
8989
}
90-
return new GitInteractor(this.iInput, this.gInput).getCommitMsg(ghOutput.commitId)
91-
.then(commitMsg => ({ ...ghOutput, commitMsg }));
90+
return new GitInteractor().getCommitMsg(ghOutput.commitId)
91+
.then(commitMsg => ({ ...ghOutput, commitMsg }));
9292
}
9393

9494
fixOrSearchVersion(ghOutput: GitContextOutput, dryRun: boolean = true): Promise<FileVersionResult> {
@@ -120,14 +120,14 @@ export class ProjectContextOps {
120120
if (ghOutput.isTag && mustFixVersion) {
121121
throw `Git tag version doesn't meet with current version in files. Invalid files: [${versionResult.files}]`;
122122
}
123-
const interactor = new GitInteractor(this.iInput, this.gInput);
123+
const interactor = new GitInteractor();
124124
if (mustFixVersion) {
125-
return interactor.commitPushIfNeed(ghOutput.branch, ghOutput.version, mustFixVersion, dryRun)
125+
return interactor.commitPushIfNeed(this.iInput, ghOutput.branch, ghOutput.version, mustFixVersion, dryRun)
126126
.then(ci => ({ ...ghOutput, ci, ver }));
127127
}
128128
if (ghOutput.isReleasePR && ghOutput.isMerged) {
129129
core.info(`Tag new version ${ghOutput.version}...`);
130-
return interactor.tagThenPush(ghOutput.version, ghOutput.isMerged, dryRun)
130+
return interactor.tagThenPush(this.iInput, this.gInput, ghOutput.version, ghOutput.isMerged, dryRun)
131131
.then(ci => ({ ...ghOutput, ci }));
132132
}
133133
return Promise.resolve({ ...ghOutput, ver });
@@ -153,6 +153,13 @@ export class ProjectContextOps {
153153
}
154154
return { ...output, ...{ version: output.ver?.current ?? output.version } };
155155
}
156+
157+
removeBranchIfNeed(output: GitContextOutput): Promise<GitContextOutput> {
158+
if (output.isPR && output.isMerged) {
159+
return new GitInteractor().removeRemoteBranch(output.branch).then(ignore => output);
160+
}
161+
return Promise.resolve(output);
162+
}
156163
}
157164

158165
export interface FileVersionResult {

0 commit comments

Comments
 (0)