From 0ac5dfa2c725fef9c335b6e0dc3ac3a126f46085 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 13 Dec 2020 01:24:19 +0000 Subject: [PATCH 1/5] build(deps): bump ini from 1.3.5 to 1.3.8 Bumps [ini](https://github.com/isaacs/ini) from 1.3.5 to 1.3.8. - [Release notes](https://github.com/isaacs/ini/releases) - [Commits](https://github.com/isaacs/ini/compare/v1.3.5...v1.3.8) Signed-off-by: dependabot[bot] --- package-lock.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 666f4f8..7f34389 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1410,9 +1410,9 @@ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, "ini": { - "version": "1.3.5", - "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.5.tgz", - "integrity": "sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==" + "version": "1.3.8", + "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.8.tgz", + "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==" }, "interpret": { "version": "1.4.0", From 8f6aa1969f7dd949c0b8c6456c792fa55dd21ce5 Mon Sep 17 00:00:00 2001 From: Egor Kurnev Date: Mon, 14 Dec 2020 18:09:04 +0300 Subject: [PATCH 2/5] 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 +} From c0f41727e6b6df5561d358a6bb0aaded9c25da61 Mon Sep 17 00:00:00 2001 From: Egor Kurnev Date: Mon, 14 Dec 2020 18:56:21 +0300 Subject: [PATCH 3/5] fix: Tag name can also be changed in pre-changelog-generation --- .github/workflows/test.yml | 6 +++--- README.md | 4 ++-- src/index.js | 7 +++++-- test/pre-changelog-generation.js | 10 ++++++++-- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ca4957f..a3234b9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -95,7 +95,7 @@ jobs: uses: actions/checkout@v2 with: path: "./" - - run: test -f test-version && (echo should not be here yet && exit 1) || exit 0 + - run: test -f pre-changelog-generation.test.json && (echo should not be here yet && exit 1) || exit 0 - name: Generate changelog id: changelog uses: ./ @@ -107,8 +107,8 @@ jobs: version-file: './test-file.toml' skip-on-empty: 'false' - - run: test -f test-version || (echo should be here && exit 1) - - run: cat test-version + - run: test -f pre-changelog-generation.test.json || (echo should be here && exit 1) + - run: cat pre-changelog-generation.test.json test-git: runs-on: ubuntu-latest steps: diff --git a/README.md b/README.md index f8f213f..6ca34c8 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ A bunch of useful environment variables are available to the script with `proces 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: +Following props will be passed to the function as a single parameter and same output is expected: ```typescript interface Props { @@ -67,7 +67,7 @@ interface Props { version: string; // Next version e.g. 1.12.3 } -export function preChangelogGeneration(props: Props): string {} +export function preChangelogGeneration(props: Props): Props {} ``` ### Config-File-Path diff --git a/src/index.js b/src/index.js index 13b7211..ddeca49 100644 --- a/src/index.js +++ b/src/index.js @@ -110,13 +110,16 @@ async function run() { newVersion = versioning[0].newVersion } - const gitTag = `${tagPrefix}${newVersion}` + let gitTag = `${tagPrefix}${newVersion}` if (preChangelogGeneration) { - newVersion = await require(path.resolve(preChangelogGeneration)).preChangelogGeneration({ + const newVersionAndTag = await require(path.resolve(preChangelogGeneration)).preChangelogGeneration({ tag: gitTag, version: newVersion, }) + + gitTag = newVersionAndTag.tag + newVersion = newVersionAndTag.version } // Generate the string changelog diff --git a/test/pre-changelog-generation.js b/test/pre-changelog-generation.js index 8f1561a..762518e 100644 --- a/test/pre-changelog-generation.js +++ b/test/pre-changelog-generation.js @@ -9,8 +9,14 @@ exports.preChangelogGeneration = (props) => { t.ok(props.version, 'version should not be empty') const newVersion = '1.0.100' + const newTag = 'v1.0.100' - fs.writeFileSync('test-version', newVersion) + const body = { + version: newVersion, + tag: newTag, + } - return newVersion + fs.writeFileSync('pre-changelog-generation.test.json', JSON.stringify(body, null, 2)) + + return body } From c6043fb4f9cc46d59dea99e88ebae07976b3a87f Mon Sep 17 00:00:00 2001 From: Egor Kurnev Date: Mon, 14 Dec 2020 19:38:42 +0300 Subject: [PATCH 4/5] fix: More checks. Fix test for changelog generation --- .github/workflows/test.yml | 1 - src/helpers/git.js | 7 ++++++- src/index.js | 6 ++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a3234b9..1d843b4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -105,7 +105,6 @@ jobs: 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 pre-changelog-generation.test.json || (echo should be here && exit 1) - run: cat pre-changelog-generation.test.json diff --git a/src/helpers/git.js b/src/helpers/git.js index 0050143..a2c06af 100644 --- a/src/helpers/git.js +++ b/src/helpers/git.js @@ -127,7 +127,12 @@ module.exports = new (class Git { isShallow = async () => { const isShallow = await this.exec('rev-parse --is-shallow-repository') - return isShallow.trim().replace('\n', '') === 'true' + // isShallow does not return anything on local machine + if (isShallow) { + return isShallow.trim().replace('\n', '') === 'true' + } else { + return false + } } /** diff --git a/src/index.js b/src/index.js index ddeca49..54ffa95 100644 --- a/src/index.js +++ b/src/index.js @@ -118,8 +118,10 @@ async function run() { version: newVersion, }) - gitTag = newVersionAndTag.tag - newVersion = newVersionAndTag.version + if (newVersionAndTag) { + if (newVersionAndTag.tag) gitTag = newVersionAndTag.tag + if (newVersionAndTag.version) newVersion = newVersionAndTag.version + } } // Generate the string changelog From 040d8e90f4548fdebc6fe291763582d202c62e88 Mon Sep 17 00:00:00 2001 From: Egor Kurnev Date: Mon, 14 Dec 2020 20:21:30 +0300 Subject: [PATCH 5/5] fix: Remove unneded import. More precise path for requiring hooks --- src/index.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 54ffa95..429084a 100644 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,6 @@ const path = require('path') const getVersioning = require('./version') const git = require('./helpers/git') const changelog = require('./helpers/generateChangelog') -const resolve = require('path').resolve async function handleVersioningByExtension(ext, file, versionPath, releaseType) { const versioning = getVersioning(ext) @@ -66,7 +65,7 @@ async function run() { 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(path.resolve(process.cwd(), conventionalConfigFile)) conventionalRecommendedBump({ preset, tagPrefix, config }, async (error, recommendation) => { if (error) { @@ -113,7 +112,7 @@ async function run() { let gitTag = `${tagPrefix}${newVersion}` if (preChangelogGeneration) { - const newVersionAndTag = await require(path.resolve(preChangelogGeneration)).preChangelogGeneration({ + const newVersionAndTag = await require(path.resolve(process.cwd(), preChangelogGeneration)).preChangelogGeneration({ tag: gitTag, version: newVersion, }) @@ -149,7 +148,7 @@ async function run() { if (!skipCommit) { // Add changed files to git if (preCommit) { - await require(path.resolve(preCommit)).preCommit({ + await require(path.resolve(process.cwd(), preCommit)).preCommit({ tag: gitTag, version: newVersion, })