Skip to content

Commit

Permalink
[#38] Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
zero88 committed Aug 23, 2022
1 parent 4babaad commit 2b1c758
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions src/gitOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ export class GitOps {
this.config = config;
}

static getCommitMsg = async (sha: string) => GitOps.execSilent(['log', '--format=%B', '-n', '1', sha]);
static getCommitMsg = async (sha: string) => GitOps.exec(['log', '--format=%B', '-n', '1', sha]);

static removeRemoteBranch = async (branch: string) => GitOps.execSilent(['push', 'origin', `:${branch}`]);
static removeRemoteBranch = async (branch: string) => GitOps.exec(['push', 'origin', `:${branch}`]);

// git tag -l --sort=-creatordate 'v*' | head -n 1
// git describe --tags --abbrev=0 --match 'v*'
static getLatestTag = async (pattern?: string) =>
GitOps.execSilent(['fetch', '--tag'])
.then(() => GitOps.execSilent(['tag', '-l', '--sort=-creatordate', `${pattern}*`]))
GitOps.exec(['fetch', '--tag'])
.then(() => GitOps.exec(['tag', '-l', '--sort=-creatordate', `${pattern}*`]))
.then(out => out.split('\n')[0]);

async commit(msg: string, branch?: string): Promise<CommitStatus> {
Expand Down Expand Up @@ -175,8 +175,8 @@ export class GitOps {
}

private async configGitUser(): Promise<string[]> {
const userName = await GitOps.execSilent(['config', 'user.name'], this.config.userName);
const userEmail = await GitOps.execSilent(['config', 'user.email'], this.config.userEmail);
const userName = await GitOps.exec(['config', 'user.name'], this.config.userName);
const userEmail = await GitOps.exec(['config', 'user.email'], this.config.userEmail);
return Promise.resolve(['-c', `user.name=${userName}`, '-c', `user.email=${userEmail}`]);
};

Expand All @@ -188,7 +188,7 @@ export class GitOps {
await strictExec('git', ['checkout', branch!], 'Cannot checkout');
};

private static async execSilent(args: string[], fallback: string = ''): Promise<string> {
private static async exec(args: string[], fallback: string = ''): Promise<string> {
const r = await exec('git', args);
if (!r.success) {
core.warning(`Cannot exec GIT ${args[0]}: ${r.stderr}`);
Expand Down
18 changes: 10 additions & 8 deletions src/projectOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,16 @@ export class ProjectOps {
}

private async generateChangelog(version: string, dryRun: boolean): Promise<ChangelogResult> {
const tagPrefix = this.projectConfig.gitParserConfig.tagPrefix;
const latestTag = await GitOps.getLatestTag(tagPrefix);
const result = await this.changelogOps.generate(latestTag, tagPrefix + version, dryRun);
if (result.generated) {
const { isPushed, ...status } = await this.gitOps.commit(result.commitMsg);
return { ...result, ...status };
}
return { ...result, isCommitted: false };
return core.group(`[CHANGELOG] Generating CHANGELOG ${version}`, async () => {
const tagPrefix = this.projectConfig.gitParserConfig.tagPrefix;
const latestTag = await GitOps.getLatestTag(tagPrefix);
const result = await this.changelogOps.generate(latestTag, tagPrefix + version, dryRun);
if (result.generated) {
const { isPushed, ...status } = await this.gitOps.commit(result.commitMsg);
return { ...result, ...status };
}
return { ...result, isCommitted: false };
});
}
}

0 comments on commit 2b1c758

Please sign in to comment.