From 8f6aa1969f7dd949c0b8c6456c792fa55dd21ce5 Mon Sep 17 00:00:00 2001 From: Egor Kurnev Date: Mon, 14 Dec 2020 18:09:04 +0300 Subject: [PATCH] feat: Allow to specify custom tags which override auto generated ones --- .github/workflows/test.yml | 22 +++++++++++++++++ README.md | 21 ++++++++++++++++ action.yml | 4 +++ src/index.js | 42 ++++++++++++++++++++++---------- test/pre-changelog-generation.js | 16 ++++++++++++ 5 files changed, 92 insertions(+), 13 deletions(-) create mode 100644 test/pre-changelog-generation.js diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f130a34..ca4957f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -87,6 +87,28 @@ jobs: - run: test -f pre-commit.test.json || (echo should be here && exit 1) - run: cat pre-commit.test.json + + test-pre-changelog-generation: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + path: "./" + - run: test -f test-version && (echo should not be here yet && exit 1) || exit 0 + - name: Generate changelog + id: changelog + uses: ./ + env: + ENV: 'dont-use-git' + with: + github-token: ${{ secrets.github_token }} + pre-changelog-generation: test/pre-changelog-generation.js + version-file: './test-file.toml' + skip-on-empty: 'false' + + - run: test -f test-version || (echo should be here && exit 1) + - run: cat test-version test-git: runs-on: ubuntu-latest steps: diff --git a/README.md b/README.md index 3e12299..f8f213f 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,24 @@ export function preCommit(props: Props): void {} A bunch of useful environment variables are available to the script with `process.env`. See [docs.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables](https://docs.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables) to learn more. +### Pre-Changelog-Generation hook + +> Function in a specified file will be run right before the changelog generation phase, when the next +> version is already known, but it was not used anywhere yet. It can be useful if you want to manually update version or tag. + +Same restrictions as for the pre-commit hook, but exported function name should be `preChangelogGeneration` + +Following props will be passed to the function as a single parameter and string output with version is expected: + +```typescript +interface Props { + tag: string; // Next tag e.g. v1.12.3 + version: string; // Next version e.g. 1.12.3 +} + +export function preChangelogGeneration(props: Props): string {} +``` + ### Config-File-Path A config file to define the conventional commit settings. Use it if you need to override values like `issuePrefix` or `issueUrlFormat`. If you set a `config-file-path`, the `preset` setting will be ignored. Therefore use an existing config and override the values you want to adjust. @@ -206,6 +224,9 @@ $ act -j multiple-files -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s # To run / config file path test $ act -j test-config-file-path -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token + +# To run pre-changelog-generation test +$ act -j test-pre-changelog-generation -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token ``` ## [License](./LICENSE) diff --git a/action.yml b/action.yml index e2161e0..58aaca7 100644 --- a/action.yml +++ b/action.yml @@ -91,6 +91,10 @@ inputs: description: 'Path to the conventional changelog config file. If set, the preset setting will be ignored' required: false + pre-changelog-generation: + description: 'Path to the pre-changelog-generation script file' + required: false + outputs: changelog: diff --git a/src/index.js b/src/index.js index 0f52798..13b7211 100644 --- a/src/index.js +++ b/src/index.js @@ -39,6 +39,7 @@ async function run() { const skipCommit = core.getInput('skip-commit').toLowerCase() === 'true' const skipEmptyRelease = core.getInput('skip-on-empty').toLowerCase() === 'true' const conventionalConfigFile = core.getInput('config-file-path') + const preChangelogGeneration = core.getInput('pre-changelog-generation') core.info(`Using "${preset}" preset`) core.info(`Using "${gitCommitMessage}" as commit message`) @@ -55,13 +56,17 @@ async function run() { core.info(`Using "${preCommit}" as pre-commit script`) } + if (preChangelogGeneration) { + core.info(`Using "${preChangelogGeneration}" as pre-changelog-generation script`) + } + core.info(`Skipping empty releases is "${skipEmptyRelease ? 'enabled' : 'disabled'}"`) core.info(`Skipping the update of the version file is "${skipVersionFile ? 'enabled' : 'disabled'}"`) core.info('Pull to make sure we have the full git history') await git.pull() - const config = conventionalConfigFile && require(resolve(process.cwd(), conventionalConfigFile)); + const config = conventionalConfigFile && require(resolve(process.cwd(), conventionalConfigFile)) conventionalRecommendedBump({ preset, tagPrefix, config }, async (error, recommendation) => { if (error) { @@ -82,23 +87,37 @@ async function run() { // skipVersionFile can mean there is no version file and skipCommit can mean that the user // is only interested in tags if (skipVersionFile || skipCommit) { - core.info('Using GIT to determine the new version'); - const versioning = await handleVersioningByExtension('git', versionFile, versionPath, recommendation.releaseType) + core.info('Using GIT to determine the new version') + const versioning = await handleVersioningByExtension( + 'git', + versionFile, + versionPath, + recommendation.releaseType + ) newVersion = versioning.newVersion - } else { - const files = versionFile.split(',').map(f => f.trim()) + const files = versionFile.split(',').map((f) => f.trim()) core.info(`Files to bump: ${files.join(', ')}`) - const versioning = await Promise.all(files.map((file) => { - const fileExtension = file.split('.').pop() - core.info(`Bumping version to file "${file}" with extension "${fileExtension}"`) - return handleVersioningByExtension(fileExtension, file, versionPath, recommendation.releaseType) - })); + const versioning = await Promise.all( + files.map((file) => { + const fileExtension = file.split('.').pop() + core.info(`Bumping version to file "${file}" with extension "${fileExtension}"`) + return handleVersioningByExtension(fileExtension, file, versionPath, recommendation.releaseType) + }) + ) newVersion = versioning[0].newVersion } + const gitTag = `${tagPrefix}${newVersion}` + + if (preChangelogGeneration) { + newVersion = await require(path.resolve(preChangelogGeneration)).preChangelogGeneration({ + tag: gitTag, + version: newVersion, + }) + } // Generate the string changelog const stringChangelog = await changelog.generateStringChangelog(tagPrefix, preset, newVersion, 1, config) @@ -122,8 +141,6 @@ async function run() { await changelog.generateFileChangelog(tagPrefix, preset, newVersion, outputFile, releaseCount, config) } - const gitTag = `${tagPrefix}${newVersion}` - if (!skipCommit) { // Add changed files to git if (preCommit) { @@ -153,7 +170,6 @@ async function run() { core.setOutput('tag', gitTag) core.setOutput('skipped', 'false') }) - } catch (error) { core.setFailed(error.message) } diff --git a/test/pre-changelog-generation.js b/test/pre-changelog-generation.js new file mode 100644 index 0000000..8f1561a --- /dev/null +++ b/test/pre-changelog-generation.js @@ -0,0 +1,16 @@ +const fs = require('fs') +const t = require('assert') + +exports.preChangelogGeneration = (props) => { + const { GITHUB_WORKSPACE } = process.env + + t.ok(GITHUB_WORKSPACE, 'GITHUB_WORKSPACE should not be empty') + t.ok(props.tag, 'tag should not be empty') + t.ok(props.version, 'version should not be empty') + + const newVersion = '1.0.100' + + fs.writeFileSync('test-version', newVersion) + + return newVersion +}