-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
215 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import path from 'path'; | ||
import { replaceInFile } from 'replace-in-file'; | ||
import { DockerRunCLI } from './docker'; | ||
import { readEnv } from './exec'; | ||
import { CommitStatus } from './gitOps'; | ||
import { isEmpty, RegexUtils, removeEmptyProperties } from './utils'; | ||
|
||
export interface ChangelogConfig { | ||
/** | ||
* Check whether changelog generator is active or not | ||
* <p> | ||
* Default value is `false` | ||
* @type {boolean} | ||
*/ | ||
readonly active: boolean; | ||
/** | ||
* Returns changelog-generator docker image | ||
* {@link https://hub.docker.com/r/githubchangeloggenerator/github-changelog-generator/} | ||
*/ | ||
readonly image: string; | ||
/** | ||
* Returns changelog-generator config file | ||
* {@link https://github.com/github-changelog-generator/github-changelog-generator#params-file} | ||
*/ | ||
readonly configFile: string; | ||
/** | ||
* Returns changelog-generator token to query GitHub API: | ||
* {@link https://github.com/github-changelog-generator/github-changelog-generator#github-token} | ||
*/ | ||
readonly commitMsg: string; | ||
/** | ||
* Returns changelog-generator token to query GitHub API: | ||
* {@link https://github.com/github-changelog-generator/github-changelog-generator#github-token} | ||
*/ | ||
readonly token?: string; | ||
} | ||
|
||
export type ChangelogResult = { | ||
readonly generated: boolean; | ||
readonly latestTag: string; | ||
readonly releaseTag: string; | ||
} & Omit<CommitStatus, 'isPushed'> | ||
|
||
const getTag = (tag?: string) => isEmpty(tag) ? '1.16.2' : tag; | ||
const getImage = (tag?: string) => `githubchangeloggenerator/github-changelog-generator:${getTag(tag)}`; | ||
|
||
const defaultConfig: ChangelogConfig = { | ||
active: false, | ||
image: getImage(), | ||
configFile: '.github_changelog_generator', | ||
commitMsg: 'Generated CHANGELOG', | ||
}; | ||
|
||
export const createChangelogConfig = (active?: boolean, imageTag?: string, configFile?: string, token?: string, | ||
commitMsg?: string): ChangelogConfig => { | ||
return { | ||
...defaultConfig, ...removeEmptyProperties({ active, configFile, token, commitMsg, image: getImage(imageTag) }), | ||
}; | ||
}; | ||
|
||
export type GenerateResult = Required<Omit<ChangelogResult, 'commitId' | 'isCommitted'>>; | ||
|
||
export class ChangelogOps { | ||
private readonly config: ChangelogConfig; | ||
|
||
constructor(config: ChangelogConfig) { | ||
this.config = config; | ||
} | ||
|
||
async generate(latestTag: string, releaseTag: string, dryRun: boolean): Promise<GenerateResult> { | ||
const commitMsg = `${this.config.commitMsg} ${releaseTag}`; | ||
const isExisted = await this.verifyExists(releaseTag); | ||
if (isExisted) { | ||
return { latestTag, releaseTag, commitMsg, generated: false }; | ||
} | ||
const workspace = readEnv('GITHUB_WORKSPACE'); | ||
const owner = readEnv('GITHUB_REPOSITORY_OWNER'); | ||
const repo = readEnv('GITHUB_REPOSITORY'); | ||
const ghApi = readEnv('GITHUB_API_URL'); | ||
const ghSite = readEnv('GITHUB_SERVER_URL'); | ||
const project = repo.replace(owner + '/', ''); | ||
const cmd = [ | ||
`--user`, owner, `--project`, project, `--config-file`, this.config.configFile, | ||
`--since-tag`, latestTag, `--future-release`, releaseTag, | ||
`--github-api`, ghApi, `--github-site`, ghSite, | ||
]; | ||
const envs = { 'CHANGELOG_GITHUB_TOKEN': this.config.token }; | ||
const volumes = { [workspace]: DockerRunCLI.DEFAULT_WORKDIR }; | ||
const dockerRun = await DockerRunCLI.execute(this.config.image, cmd, envs, volumes); | ||
return { latestTag, releaseTag, commitMsg, generated: dockerRun.success }; | ||
} | ||
|
||
async verifyExists(releaseTag: string): Promise<boolean> { | ||
const re = /((base|output)\s?=\s?)(.+)/; | ||
const result: string[] = []; | ||
await replaceInFile({ | ||
files: this.config.configFile, from: new RegExp(re.source, 'gm'), dry: true, allowEmptyPaths: true, | ||
to: match => { | ||
result.push(RegexUtils.searchMatch(match, re, 2)); | ||
return match; | ||
}, | ||
}); | ||
const dir = path.dirname(this.config.configFile); | ||
const files = (isEmpty(result) ? ['CHANGELOG.md'] : result).map(f => path.resolve(dir, f)); | ||
return await replaceInFile( | ||
{ files, from: releaseTag, dry: true, countMatches: true, allowEmptyPaths: true, to: match => match }) | ||
.then(rr => rr.some(r => r?.numMatches! > 0)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import * as core from '@actions/core'; | ||
import { ExecResult, strictExec } from './exec'; | ||
import { arrayToObjectWithVal, isNull } from './utils'; | ||
|
||
export class DockerRunCLI { | ||
static DEFAULT_WORKDIR = '/github/workspace'; | ||
|
||
static envs = (envs?: Record<string, any>): string[] => { | ||
const def = ['CI', 'GITHUB_API_URL', 'GITHUB_SERVER_URL', 'GITHUB_WORKSPACE']; | ||
const environments = { ...arrayToObjectWithVal(def, () => null), ...(envs ?? {}) }; | ||
const toEnv = kv => isNull(kv[1]) ? kv[0] : `${kv[0]}=${kv[1]}`; | ||
return Object.entries(environments).map(toEnv).map(e => ['-e', e]).flat(); | ||
}; | ||
|
||
static volumes = (volumes?: Record<string, any>): string[] => { | ||
const toVolume = kv => isNull(kv[1]) ? kv[0] : `${kv[0]}:${kv[1]}`; | ||
return Object.entries(volumes ?? {}).map(toVolume).map(v => ['-v', v]).flat(); | ||
}; | ||
|
||
static execute = (image: string, containerCmd?: Array<string>, envs?: Record<string, any>, | ||
volumes?: Record<string, any>): Promise<ExecResult> => { | ||
const args = [ | ||
'run', '-t', '--rm', | ||
'--workdir', DockerRunCLI.DEFAULT_WORKDIR, | ||
...DockerRunCLI.envs(envs), | ||
...DockerRunCLI.volumes(volumes), | ||
image, | ||
...(containerCmd ?? []), | ||
]; | ||
core.debug(`Docker run args: ${args}`); | ||
return strictExec('docker', args, 'Unable to run docker', false); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters