From 484cf104714fbe499d83bd2818038e8c14e8ce98 Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Mon, 14 Dec 2020 22:27:31 +0100 Subject: [PATCH 01/14] fix: Fixes for pre changelog generation --- .github/workflows/test.yml | 20 ++++++----- README.md | 11 +++--- src/helpers/bumpVersion.js | 21 +++++++++++- src/helpers/requireScript.js | 23 +++++++++++++ src/index.js | 58 +++++++++++++++++++------------- test/pre-changelog-generation.js | 29 ++++++++++------ 6 files changed, 112 insertions(+), 50 deletions(-) create mode 100644 src/helpers/requireScript.js diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1d843b4..fc240c7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,8 +1,5 @@ name: 'Action to test the action locally with act' -on: - push: - branches: - - branch-that-does-not-exist +on: [pull_request] jobs: test-json: @@ -85,8 +82,8 @@ jobs: skip-on-empty: 'false' - run: test -f pre-commit.test.json || (echo should be here && exit 1) - - run: cat pre-commit.test.json - + - run: cat pre-commit.test.json && echo "" + - run: cat ./package.json test-pre-changelog-generation: runs-on: ubuntu-latest @@ -105,9 +102,14 @@ jobs: github-token: ${{ secrets.github_token }} pre-changelog-generation: test/pre-changelog-generation.js version-file: './test-file.toml' + version-path: 'package.version' + + - run: test -f pre-changelog-generation.version.test.json || (echo should be here && exit 1) + - run: test -f pre-changelog-generation.tag.test.json || (echo should be here && exit 1) + - run: cat pre-changelog-generation.version.test.json && echo "" + - run: cat pre-changelog-generation.tag.test.json && echo "" + - run: cat ./test-file.toml - - 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: @@ -132,7 +134,7 @@ jobs: uses: actions/checkout@v2 with: path: "./" - + - run: git tag | xargs git tag -d - name: Generate changelog diff --git a/README.md b/README.md index bf4ddce..25163fd 100644 --- a/README.md +++ b/README.md @@ -58,17 +58,16 @@ A bunch of useful environment variables are available to the script with `proces > 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` +Same restrictions as for the pre-commit hook, but exported functions names should be `preVersionGeneration` for modifications to the version and `preTagGeneration` for modifications to the git tag. Following props will be passed to the function as a single parameter and same output is expected: ```typescript -interface Props { - tag: string; // Next tag e.g. v1.12.3 - version: string; // Next version e.g. 1.12.3 -} +// Next version e.g. 1.12.3 +export function preVersionGeneration(version: string): string {} -export function preChangelogGeneration(props: Props): Props {} +// Next tag e.g. v1.12.3 +export function preTagGeneration(tag: string): string {} ``` ### Config-File-Path diff --git a/src/helpers/bumpVersion.js b/src/helpers/bumpVersion.js index b7a5882..e2f478f 100644 --- a/src/helpers/bumpVersion.js +++ b/src/helpers/bumpVersion.js @@ -1,6 +1,8 @@ const core = require('@actions/core') const semverValid = require('semver').valid +const requireScript = require('./requireScript') + /** * Bumps the given version with the given release type * @@ -44,5 +46,22 @@ module.exports = (releaseType, version) => { core.info(`The version could not be detected, using fallback version '${major}.${minor}.${patch}'.`) } - return `${major}.${minor}.${patch}` + const preChangelogGenerationFile = core.getInput('pre-changelog-generation') + + let newVersion = `${major}.${minor}.${patch}` + + if (preChangelogGenerationFile) { + const preChangelogGenerationScript = requireScript(preChangelogGenerationFile) + + // Double check if we want to update / do something with the version + if (preChangelogGenerationScript && preChangelogGenerationScript.preVersionGeneration) { + const modifiedVersion = preChangelogGenerationScript.preVersionGeneration(newVersion) + + if (modifiedVersion) { + newVersion = modifiedVersion + } + } + } + + return newVersion } diff --git a/src/helpers/requireScript.js b/src/helpers/requireScript.js new file mode 100644 index 0000000..8127b6d --- /dev/null +++ b/src/helpers/requireScript.js @@ -0,0 +1,23 @@ +const core = require('@actions/core') +const path = require('path') +const fs = require('fs') + +/** + * Requires an script + * + * @param file + */ +module.exports = (file) => { + const fileLocation = path.resolve(process.cwd(), file) + + // Double check the script exists before loading it + if (fs.existsSync(fileLocation)) { + core.info(`Loading "${fileLocation}" script`) + + return require(fileLocation) + } + + core.error(`Tried to load "${fileLocation}" script but it does not exists!`) + + return undefined +} diff --git a/src/index.js b/src/index.js index 429084a..95a8118 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,7 @@ const path = require('path') const getVersioning = require('./version') const git = require('./helpers/git') const changelog = require('./helpers/generateChangelog') +const requireScript = require('./helpers/requireScript') async function handleVersioningByExtension(ext, file, versionPath, releaseType) { const versioning = getVersioning(ext) @@ -29,7 +30,7 @@ async function run() { const gitUserEmail = core.getInput('git-user-email') const tagPrefix = core.getInput('tag-prefix') const preset = !core.getInput('config-file-path') ? core.getInput('preset') : '' - const preCommit = core.getInput('pre-commit') + const preCommitFile = core.getInput('pre-commit') const outputFile = core.getInput('output-file') const releaseCount = core.getInput('release-count') const versionFile = core.getInput('version-file') @@ -38,7 +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') + const preChangelogGenerationFile = core.getInput('pre-changelog-generation') core.info(`Using "${preset}" preset`) core.info(`Using "${gitCommitMessage}" as commit message`) @@ -51,12 +52,12 @@ async function run() { core.info(`Using "${outputFile}" as output file`) core.info(`Using "${conventionalConfigFile}" as config file`) - if (preCommit) { - core.info(`Using "${preCommit}" as pre-commit script`) + if (preCommitFile) { + core.info(`Using "${preCommitFile}" as pre-commit script`) } - if (preChangelogGeneration) { - core.info(`Using "${preChangelogGeneration}" as pre-changelog-generation script`) + if (preChangelogGenerationFile) { + core.info(`Using "${preChangelogGenerationFile}" as pre-changelog-generation script`) } core.info(`Skipping empty releases is "${skipEmptyRelease ? 'enabled' : 'disabled'}"`) @@ -65,9 +66,9 @@ async function run() { core.info('Pull to make sure we have the full git history') await git.pull() - const config = conventionalConfigFile && require(path.resolve(process.cwd(), conventionalConfigFile)) + const config = conventionalConfigFile && requireScript(conventionalConfigFile) - conventionalRecommendedBump({ preset, tagPrefix, config }, async (error, recommendation) => { + conventionalRecommendedBump({ preset, tagPrefix, config }, async(error, recommendation) => { if (error) { core.setFailed(error.message) return @@ -91,9 +92,11 @@ async function run() { 'git', versionFile, versionPath, - recommendation.releaseType + recommendation.releaseType, ) + newVersion = versioning.newVersion + } else { const files = versionFile.split(',').map((f) => f.trim()) core.info(`Files to bump: ${files.join(', ')}`) @@ -102,8 +105,9 @@ async function run() { 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 @@ -111,15 +115,16 @@ async function run() { let gitTag = `${tagPrefix}${newVersion}` - if (preChangelogGeneration) { - const newVersionAndTag = await require(path.resolve(process.cwd(), preChangelogGeneration)).preChangelogGeneration({ - tag: gitTag, - version: newVersion, - }) + if (preChangelogGenerationFile) { + const preChangelogGenerationScript = requireScript(preChangelogGenerationFile) - if (newVersionAndTag) { - if (newVersionAndTag.tag) gitTag = newVersionAndTag.tag - if (newVersionAndTag.version) newVersion = newVersionAndTag.version + // Double check if we want to update / do something with the tag + if (preChangelogGenerationScript && preChangelogGenerationScript.preTagGeneration) { + const modifiedTag = preChangelogGenerationScript.preTagGeneration(gitTag) + + if (modifiedTag) { + gitTag = modifiedTag + } } } @@ -147,12 +152,18 @@ async function run() { if (!skipCommit) { // Add changed files to git - if (preCommit) { - await require(path.resolve(process.cwd(), preCommit)).preCommit({ - tag: gitTag, - version: newVersion, - }) + if (preCommitFile) { + const preCommitScript = await requireScript(preCommitFile) + + // Double check if the file exists and the export exists + if (preCommitScript && preCommitScript.preCommit) { + preCommitScript.preCommit({ + tag: gitTag, + version: newVersion, + }) + } } + await git.add('.') await git.commit(gitCommitMessage.replace('{version}', gitTag)) } @@ -163,6 +174,7 @@ async function run() { core.info('Push all changes') try { await git.push() + } catch (error) { core.setFailed(error.message) } diff --git a/test/pre-changelog-generation.js b/test/pre-changelog-generation.js index 762518e..b5e75ea 100644 --- a/test/pre-changelog-generation.js +++ b/test/pre-changelog-generation.js @@ -1,22 +1,29 @@ const fs = require('fs') const t = require('assert') -exports.preChangelogGeneration = (props) => { +exports.preVersionGeneration = (version) => { 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') + t.ok(version, 'version should not be empty') const newVersion = '1.0.100' - const newTag = 'v1.0.100' - const body = { - version: newVersion, - tag: newTag, - } + fs.writeFileSync('pre-changelog-generation.version.test.json', newVersion) - fs.writeFileSync('pre-changelog-generation.test.json', JSON.stringify(body, null, 2)) - - return body + return newVersion +} + +exports.preTagGeneration = (tag) => { + const { GITHUB_WORKSPACE } = process.env + + t.ok(GITHUB_WORKSPACE, 'GITHUB_WORKSPACE should not be empty') + t.ok(tag, 'tag should not be empty') + t.strictEqual(tag, 'v1.0.100') + + const newTag = 'v1.0.100-alpha' + + fs.writeFileSync('pre-changelog-generation.tag.test.json', newTag) + + return newTag } From f666183a60ece6a924f19370b30095b1a9fc2f3f Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Mon, 14 Dec 2020 22:30:29 +0100 Subject: [PATCH 02/14] ci: Updated test file --- .github/workflows/test.yml | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fc240c7..2cf8e79 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,6 +10,8 @@ jobs: with: path: "./" + - run: npm ci --prod + - name: Generate changelog id: changelog uses: ./ @@ -26,6 +28,8 @@ jobs: with: path: "./" + - run: npm ci --prod + - name: Generate changelog id: changelog uses: ./ @@ -48,6 +52,8 @@ jobs: with: path: "./" + - run: npm ci --prod + - run: touch ./test-file-empty.json - name: Generate changelog @@ -69,6 +75,8 @@ jobs: with: path: "./" + - run: npm ci --prod + - run: test -f pre-commit.test.json && (echo should not be here yet && exit 1) || exit 0 - name: Generate changelog @@ -92,7 +100,11 @@ jobs: uses: actions/checkout@v2 with: path: "./" + + - run: npm ci --prod + - run: test -f pre-changelog-generation.test.json && (echo should not be here yet && exit 1) || exit 0 + - name: Generate changelog id: changelog uses: ./ @@ -118,6 +130,8 @@ jobs: with: path: "./" + - run: npm ci --prod + - name: Generate changelog id: changelog uses: ./ @@ -135,6 +149,8 @@ jobs: with: path: "./" + - run: npm ci --prod + - run: git tag | xargs git tag -d - name: Generate changelog @@ -154,6 +170,8 @@ jobs: with: path: "./" + - run: npm ci --prod + - name: Generate changelog id: changelog uses: ./ @@ -172,6 +190,8 @@ jobs: with: path: "./" + - run: npm ci --prod + - run: touch ./test-file-empty.yaml - name: Generate changelog @@ -194,6 +214,8 @@ jobs: with: path: "./" + - run: npm ci --prod + - name: Generate changelog id: changelog uses: ./ @@ -214,6 +236,8 @@ jobs: with: path: "./" + - run: npm ci --prod + - name: Generate changelog id: changelog uses: ./ @@ -232,6 +256,8 @@ jobs: with: path: "./" + - run: npm ci --prod + - name: Generate changelog id: changelog uses: ./ @@ -252,6 +278,8 @@ jobs: with: path: "./" + - run: npm ci --prod + - run: touch ./test-file-empty.toml - name: Generate changelog @@ -274,6 +302,8 @@ jobs: with: path: "./" + - run: npm ci --prod + - name: Generate changelog id: changelog uses: ./ @@ -291,6 +321,8 @@ jobs: with: path: "./" + - run: npm ci --prod + - name: Install Packages run: yarn From fa3313b72c9f439b9564d2727feb4071d86e805c Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Mon, 14 Dec 2020 22:31:03 +0100 Subject: [PATCH 03/14] ci: Updated test file --- .github/workflows/test.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2cf8e79..5e5c17e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -323,9 +323,6 @@ jobs: - run: npm ci --prod - - name: Install Packages - run: yarn - - name: Generate Changelog id: changelog uses: ./ From 8b503dcc9f26f7735fedded80387d97800c3ad91 Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Mon, 14 Dec 2020 22:38:57 +0100 Subject: [PATCH 04/14] ci: Updated test file --- .github/workflows/test.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5e5c17e..aa0f00c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,4 +1,4 @@ -name: 'Action to test the action locally with act' +name: 'Test the action' on: [pull_request] jobs: @@ -12,6 +12,9 @@ jobs: - run: npm ci --prod + - run: touch ./fake-file.log + - run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'" + - name: Generate changelog id: changelog uses: ./ From d55112e04c729997788712912a4ee1cb01205227 Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Mon, 14 Dec 2020 22:40:36 +0100 Subject: [PATCH 05/14] ci: Updated test file --- .github/workflows/test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index aa0f00c..b09108f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -13,6 +13,8 @@ jobs: - run: npm ci --prod - run: touch ./fake-file.log + - run: "git config --global user.email 'changelog@github.com'" + - run: "git config --global user.name 'Awesome Github action'" - run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'" - name: Generate changelog From 7c35f3c2863828a71323a49e16c5542c789591f9 Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Mon, 14 Dec 2020 22:57:38 +0100 Subject: [PATCH 06/14] fix: Fixed silent git errors #74 --- .github/workflows/test.yml | 57 +++++++++++++++++++++++++++++++++++++- action.yml | 3 +- src/helpers/git.js | 34 +++++++---------------- src/index.js | 9 ++---- 4 files changed, 70 insertions(+), 33 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b09108f..edef98a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,7 +11,6 @@ jobs: path: "./" - run: npm ci --prod - - run: touch ./fake-file.log - run: "git config --global user.email 'changelog@github.com'" - run: "git config --global user.name 'Awesome Github action'" @@ -34,6 +33,10 @@ jobs: path: "./" - run: npm ci --prod + - run: touch ./fake-file.log + - run: "git config --global user.email 'changelog@github.com'" + - run: "git config --global user.name 'Awesome Github action'" + - run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'" - name: Generate changelog id: changelog @@ -58,6 +61,10 @@ jobs: path: "./" - run: npm ci --prod + - run: touch ./fake-file.log + - run: "git config --global user.email 'changelog@github.com'" + - run: "git config --global user.name 'Awesome Github action'" + - run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'" - run: touch ./test-file-empty.json @@ -81,6 +88,10 @@ jobs: path: "./" - run: npm ci --prod + - run: touch ./fake-file.log + - run: "git config --global user.email 'changelog@github.com'" + - run: "git config --global user.name 'Awesome Github action'" + - run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'" - run: test -f pre-commit.test.json && (echo should not be here yet && exit 1) || exit 0 @@ -107,6 +118,10 @@ jobs: path: "./" - run: npm ci --prod + - run: touch ./fake-file.log + - run: "git config --global user.email 'changelog@github.com'" + - run: "git config --global user.name 'Awesome Github action'" + - run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'" - run: test -f pre-changelog-generation.test.json && (echo should not be here yet && exit 1) || exit 0 @@ -136,6 +151,10 @@ jobs: path: "./" - run: npm ci --prod + - run: touch ./fake-file.log + - run: "git config --global user.email 'changelog@github.com'" + - run: "git config --global user.name 'Awesome Github action'" + - run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'" - name: Generate changelog id: changelog @@ -155,6 +174,10 @@ jobs: path: "./" - run: npm ci --prod + - run: touch ./fake-file.log + - run: "git config --global user.email 'changelog@github.com'" + - run: "git config --global user.name 'Awesome Github action'" + - run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'" - run: git tag | xargs git tag -d @@ -176,6 +199,10 @@ jobs: path: "./" - run: npm ci --prod + - run: touch ./fake-file.log + - run: "git config --global user.email 'changelog@github.com'" + - run: "git config --global user.name 'Awesome Github action'" + - run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'" - name: Generate changelog id: changelog @@ -196,6 +223,10 @@ jobs: path: "./" - run: npm ci --prod + - run: touch ./fake-file.log + - run: "git config --global user.email 'changelog@github.com'" + - run: "git config --global user.name 'Awesome Github action'" + - run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'" - run: touch ./test-file-empty.yaml @@ -220,6 +251,10 @@ jobs: path: "./" - run: npm ci --prod + - run: touch ./fake-file.log + - run: "git config --global user.email 'changelog@github.com'" + - run: "git config --global user.name 'Awesome Github action'" + - run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'" - name: Generate changelog id: changelog @@ -242,6 +277,10 @@ jobs: path: "./" - run: npm ci --prod + - run: touch ./fake-file.log + - run: "git config --global user.email 'changelog@github.com'" + - run: "git config --global user.name 'Awesome Github action'" + - run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'" - name: Generate changelog id: changelog @@ -262,6 +301,10 @@ jobs: path: "./" - run: npm ci --prod + - run: touch ./fake-file.log + - run: "git config --global user.email 'changelog@github.com'" + - run: "git config --global user.name 'Awesome Github action'" + - run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'" - name: Generate changelog id: changelog @@ -284,6 +327,10 @@ jobs: path: "./" - run: npm ci --prod + - run: touch ./fake-file.log + - run: "git config --global user.email 'changelog@github.com'" + - run: "git config --global user.name 'Awesome Github action'" + - run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'" - run: touch ./test-file-empty.toml @@ -308,6 +355,10 @@ jobs: path: "./" - run: npm ci --prod + - run: touch ./fake-file.log + - run: "git config --global user.email 'changelog@github.com'" + - run: "git config --global user.name 'Awesome Github action'" + - run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'" - name: Generate changelog id: changelog @@ -327,6 +378,10 @@ jobs: path: "./" - run: npm ci --prod + - run: touch ./fake-file.log + - run: "git config --global user.email 'changelog@github.com'" + - run: "git config --global user.name 'Awesome Github action'" + - run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'" - name: Generate Changelog id: changelog diff --git a/action.yml b/action.yml index 58aaca7..96671c9 100644 --- a/action.yml +++ b/action.yml @@ -86,7 +86,8 @@ inputs: fallback-version: description: 'The fallback version, if no older one can be detected, or if it is the first one' default: '0.1.0' - + required: false + config-file-path: description: 'Path to the conventional changelog config file. If set, the preset setting will be ignored' required: false diff --git a/src/helpers/git.js b/src/helpers/git.js index a2c06af..95d03bf 100644 --- a/src/helpers/git.js +++ b/src/helpers/git.js @@ -37,28 +37,14 @@ module.exports = new (class Git { * @param command * @return {Promise<>} */ - exec = command => new Promise(async(resolve, reject) => { - let myOutput = '' - let myError = '' + exec = (command) => new Promise(async(resolve, reject) => { + const exitCode = await exec.exec(`git ${command}`) - const options = { - listeners: { - stdout: (data) => { - myOutput += data.toString() - }, - stderr: (data) => { - myError += data.toString() - }, - }, - } + if (exitCode === 0) { + resolve() - try { - await exec.exec(`git ${command}`, null, options) - - resolve(myOutput) - - } catch (e) { - reject(e) + } else { + reject(`Command "git ${command}" exited with code ${exitCode}.`) } }) @@ -77,7 +63,7 @@ module.exports = new (class Git { * @param file * @returns {*} */ - add = file => this.exec(`add ${file}`) + add = (file) => this.exec(`add ${file}`) /** * Commit all changes @@ -124,7 +110,7 @@ module.exports = new (class Git { * * @return {Promise<>} */ - isShallow = async () => { + isShallow = async() => { const isShallow = await this.exec('rev-parse --is-shallow-repository') // isShallow does not return anything on local machine @@ -141,7 +127,7 @@ module.exports = new (class Git { * @param repo * @return {Promise<>} */ - updateOrigin = repo => this.exec(`remote set-url origin ${repo}`) + updateOrigin = (repo) => this.exec(`remote set-url origin ${repo}`) /** * Creates git tag @@ -149,6 +135,6 @@ module.exports = new (class Git { * @param tag * @return {Promise<>} */ - createTag = tag => this.exec(`tag -a ${tag} -m "${tag}"`) + createTag = (tag) => this.exec(`tag -a ${tag} -m "${tag}"`) })() diff --git a/src/index.js b/src/index.js index 95a8118..212ef85 100644 --- a/src/index.js +++ b/src/index.js @@ -172,12 +172,7 @@ async function run() { await git.createTag(gitTag) core.info('Push all changes') - try { - await git.push() - - } catch (error) { - core.setFailed(error.message) - } + await git.push() // Set outputs so other actions (for example actions/create-release) can use it core.setOutput('changelog', stringChangelog) @@ -187,7 +182,7 @@ async function run() { core.setOutput('skipped', 'false') }) } catch (error) { - core.setFailed(error.message) + core.setFailed(error) } } From 9925916d8e514d7265aabaf22dcabcbc89a121b0 Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Mon, 14 Dec 2020 23:06:43 +0100 Subject: [PATCH 07/14] test: Improved test cases to output the files for validation --- .github/workflows/test.yml | 38 ++++++++++++++++++++++++++++++++------ test-file.json | 4 ++++ 2 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 test-file.json diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index edef98a..005d1ed 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -23,6 +23,11 @@ jobs: ENV: 'dont-use-git' with: github-token: ${{ secrets.github_token }} + version-file: 'test-file.json' + + - name: Show file + run: | + echo "$( Date: Mon, 14 Dec 2020 23:11:38 +0100 Subject: [PATCH 08/14] test: Improved test cases --- .github/workflows/test.yml | 6 +++--- test-file.toml | 2 +- test-file.yaml | 2 +- ...generation.js => test-pre-changelog-generation.js | 12 ++++++------ test/pre-commit.js => test-pre-commit.js | 8 ++++---- 5 files changed, 15 insertions(+), 15 deletions(-) rename test/pre-changelog-generation.js => test-pre-changelog-generation.js (59%) rename test/pre-commit.js => test-pre-commit.js (59%) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 005d1ed..a40338c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -106,7 +106,7 @@ jobs: ENV: 'dont-use-git' with: github-token: ${{ secrets.github_token }} - pre-commit: test/pre-commit.js + pre-commit: './test-pre-commit.js' skip-on-empty: 'false' - run: test -f pre-commit.test.json || (echo should be here && exit 1) @@ -136,7 +136,7 @@ jobs: ENV: 'dont-use-git' with: github-token: ${{ secrets.github_token }} - pre-changelog-generation: test/pre-changelog-generation.js + pre-changelog-generation: './test-pre-changelog-generation.js' version-file: './test-file.toml' version-path: 'package.version' @@ -359,7 +359,7 @@ jobs: ENV: 'dont-use-git' with: github-token: ${{ secrets.github_token }} - version-file: './test/test-file-empty.toml' + version-file: './test-file-empty.toml' version-path: 'package.version' - name: Show file diff --git a/test-file.toml b/test-file.toml index 5d4eb44..468a5b2 100644 --- a/test-file.toml +++ b/test-file.toml @@ -3,4 +3,4 @@ title = "test" # Comment [package] name = "test file" -version = "0.1.0" +version = "0.9.3" diff --git a/test-file.yaml b/test-file.yaml index 2b255eb..aba43fc 100644 --- a/test-file.yaml +++ b/test-file.yaml @@ -1,5 +1,5 @@ package: - version: '0.1.0' + version: '9.4.5' # Comment different: diff --git a/test/pre-changelog-generation.js b/test-pre-changelog-generation.js similarity index 59% rename from test/pre-changelog-generation.js rename to test-pre-changelog-generation.js index b5e75ea..0536992 100644 --- a/test/pre-changelog-generation.js +++ b/test-pre-changelog-generation.js @@ -1,11 +1,11 @@ const fs = require('fs') -const t = require('assert') +const assert = require('assert') exports.preVersionGeneration = (version) => { const { GITHUB_WORKSPACE } = process.env - t.ok(GITHUB_WORKSPACE, 'GITHUB_WORKSPACE should not be empty') - t.ok(version, 'version should not be empty') + assert.ok(GITHUB_WORKSPACE, 'GITHUB_WORKSPACE should not be empty') + assert.ok(version, 'version should not be empty') const newVersion = '1.0.100' @@ -17,9 +17,9 @@ exports.preVersionGeneration = (version) => { exports.preTagGeneration = (tag) => { const { GITHUB_WORKSPACE } = process.env - t.ok(GITHUB_WORKSPACE, 'GITHUB_WORKSPACE should not be empty') - t.ok(tag, 'tag should not be empty') - t.strictEqual(tag, 'v1.0.100') + assert.ok(GITHUB_WORKSPACE, 'GITHUB_WORKSPACE should not be empty') + assert.ok(tag, 'tag should not be empty') + assert.strictEqual(tag, 'v1.0.100') const newTag = 'v1.0.100-alpha' diff --git a/test/pre-commit.js b/test-pre-commit.js similarity index 59% rename from test/pre-commit.js rename to test-pre-commit.js index 15b1e7e..a828856 100644 --- a/test/pre-commit.js +++ b/test-pre-commit.js @@ -1,12 +1,12 @@ const fs = require('fs') -const t = require('assert') +const assert = require('assert') exports.preCommit = (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') + assert.ok(GITHUB_WORKSPACE, 'GITHUB_WORKSPACE should not be empty') + assert.ok(props.tag, 'tag should not be empty') + assert.ok(props.version, 'version should not be empty') const body = { workspace: GITHUB_WORKSPACE, From 35206c51048844fb3e645398b5c9a0f692f8bb56 Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Tue, 15 Dec 2020 16:05:01 +0100 Subject: [PATCH 09/14] fix: Fixed awaits missing --- src/helpers/bumpVersion.js | 4 ++-- src/index.js | 6 +++--- src/version/git.js | 4 ++-- src/version/json.js | 4 ++-- src/version/toml.js | 4 ++-- src/version/yaml.js | 4 ++-- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/helpers/bumpVersion.js b/src/helpers/bumpVersion.js index e2f478f..d67c6b7 100644 --- a/src/helpers/bumpVersion.js +++ b/src/helpers/bumpVersion.js @@ -10,7 +10,7 @@ const requireScript = require('./requireScript') * @param version * @returns {string} */ -module.exports = (releaseType, version) => { +module.exports = async (releaseType, version) => { let major, minor, patch if (version) { @@ -55,7 +55,7 @@ module.exports = (releaseType, version) => { // Double check if we want to update / do something with the version if (preChangelogGenerationScript && preChangelogGenerationScript.preVersionGeneration) { - const modifiedVersion = preChangelogGenerationScript.preVersionGeneration(newVersion) + const modifiedVersion = await preChangelogGenerationScript.preVersionGeneration(newVersion) if (modifiedVersion) { newVersion = modifiedVersion diff --git a/src/index.js b/src/index.js index 212ef85..ce228dc 100644 --- a/src/index.js +++ b/src/index.js @@ -120,7 +120,7 @@ async function run() { // Double check if we want to update / do something with the tag if (preChangelogGenerationScript && preChangelogGenerationScript.preTagGeneration) { - const modifiedTag = preChangelogGenerationScript.preTagGeneration(gitTag) + const modifiedTag = await preChangelogGenerationScript.preTagGeneration(gitTag) if (modifiedTag) { gitTag = modifiedTag @@ -153,11 +153,11 @@ async function run() { if (!skipCommit) { // Add changed files to git if (preCommitFile) { - const preCommitScript = await requireScript(preCommitFile) + const preCommitScript = requireScript(preCommitFile) // Double check if the file exists and the export exists if (preCommitScript && preCommitScript.preCommit) { - preCommitScript.preCommit({ + await preCommitScript.preCommit({ tag: gitTag, version: newVersion, }) diff --git a/src/version/git.js b/src/version/git.js index 4077c93..0171882 100644 --- a/src/version/git.js +++ b/src/version/git.js @@ -12,11 +12,11 @@ module.exports = new (class Git extends BaseVersioning { gitSemverTags({ tagPrefix, - }, (err, tags) => { + }, async (err, tags) => { const currentVersion = tags.length > 0 ? tags.shift().replace(tagPrefix, '') : null // Get the new version - this.newVersion = bumpVersion( + this.newVersion = await bumpVersion( releaseType, currentVersion, ) diff --git a/src/version/json.js b/src/version/json.js index 7a75c52..33ce3ee 100644 --- a/src/version/json.js +++ b/src/version/json.js @@ -12,7 +12,7 @@ module.exports = new (class Json extends BaseVersioning { * @param {!string} releaseType - The type of release * @return {*} */ - bump = (releaseType) => { + bump = async (releaseType) => { // Read the file const fileContent = this.read() @@ -33,7 +33,7 @@ module.exports = new (class Json extends BaseVersioning { const oldVersion = objectPath.get(jsonContent, this.versionPath, null) // Get the new version - this.newVersion = bumpVersion( + this.newVersion = await bumpVersion( releaseType, oldVersion, ) diff --git a/src/version/toml.js b/src/version/toml.js index ff889ee..50aec9a 100644 --- a/src/version/toml.js +++ b/src/version/toml.js @@ -12,14 +12,14 @@ module.exports = new (class Toml extends BaseVersioning{ * @param {!string} releaseType - The type of release * @return {*} */ - bump = (releaseType) => { + bump = async(releaseType) => { // Read the file const fileContent = this.read() const tomlContent = toml.parse(fileContent) const oldVersion = objectPath.get(tomlContent, this.versionPath, null) // Get the new version - this.newVersion = bumpVersion( + this.newVersion = await bumpVersion( releaseType, oldVersion, ) diff --git a/src/version/yaml.js b/src/version/yaml.js index c29b9b2..1f674cf 100644 --- a/src/version/yaml.js +++ b/src/version/yaml.js @@ -12,14 +12,14 @@ module.exports = new (class Yaml extends BaseVersioning{ * @param {!string} releaseType - The type of release * @return {*} */ - bump = (releaseType) => { + bump = async (releaseType) => { // Read the file const fileContent = this.read() const yamlContent = yaml.parse(fileContent) || {} const oldVersion = objectPath.get(yamlContent, this.versionPath, null) // Get the new version - this.newVersion = bumpVersion( + this.newVersion =await bumpVersion( releaseType, oldVersion, ) From 289e972b9b3c818d85a6adabed59145417ed5d08 Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Tue, 15 Dec 2020 17:23:47 +0100 Subject: [PATCH 10/14] test: Added tests for flows --- .github/workflows/test.yml | 538 ++++++++++++++++++++++--------------- src/helpers/git.js | 46 +++- src/index.js | 10 + src/version/git.js | 2 +- src/version/json.js | 2 +- src/version/toml.js | 2 +- src/version/yaml.js | 6 +- test-output.js | 57 ++++ 8 files changed, 431 insertions(+), 232 deletions(-) create mode 100644 test-output.js diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a40338c..4cda755 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,5 +1,5 @@ name: 'Test the action' -on: [pull_request] +on: [ pull_request ] jobs: test-json: @@ -21,6 +21,7 @@ jobs: uses: ./ env: ENV: 'dont-use-git' + EXPECTED_TAG: 'v1.5.0' with: github-token: ${{ secrets.github_token }} version-file: 'test-file.json' @@ -29,6 +30,12 @@ jobs: run: | echo "$( { - console.log(`Skipping "git ${command}" because of test env`) + const fullCommand = `git ${command}` + + console.log(`Skipping "${fullCommand}" because of test env`) + + this.commandsRun.push(fullCommand) } } @@ -69,12 +76,11 @@ module.exports = new (class Git { * Commit all changes * * @param message - * @param args * * @return {Promise<>} */ - commit = (message, args = []) => ( - this.exec(`commit -m "${message}" ${args.join(' ')}`) + commit = (message) => ( + this.exec(`commit -m "${message}"`) ) /** @@ -137,4 +143,34 @@ module.exports = new (class Git { */ createTag = (tag) => this.exec(`tag -a ${tag} -m "${tag}"`) + /** + * Validates the commands run + */ + testHistory = () => { + if (ENV === 'dont-use-git') { + const { EXPECTED_TAG, SKIPPED_COMMIT } = process.env + + const expectedCommands = [ + 'git config user.name "Conventional Changelog Action"', + 'git config user.email "conventional.changelog.action@github.com"', + 'git remote set-url origin https://x-access-token:fake-token@github.com/TriPSs/conventional-changelog-action.git', + 'git rev-parse --is-shallow-repository', + 'git pull --tags --ff-only', + ] + + if (!SKIPPED_COMMIT) { + expectedCommands.push('git add .') + expectedCommands.push(`git commit -m "chore(release): ${EXPECTED_TAG}"`) + } + + expectedCommands.push(`git tag -a ${EXPECTED_TAG} -m "${EXPECTED_TAG}"`) + expectedCommands.push(`git push origin ${branch} --follow-tags`) + + assert.deepStrictEqual( + this.commandsRun, + expectedCommands, + ) + } + } + })() diff --git a/src/index.js b/src/index.js index ce228dc..f3ceb30 100644 --- a/src/index.js +++ b/src/index.js @@ -180,6 +180,16 @@ async function run() { core.setOutput('version', newVersion) core.setOutput('tag', gitTag) core.setOutput('skipped', 'false') + + try { + // If we are running in test mode we use this to validate everything still runs + git.testHistory() + + } catch (error) { + console.error(error) + + core.setFailed(error) + } }) } catch (error) { core.setFailed(error) diff --git a/src/version/git.js b/src/version/git.js index 0171882..38de4cb 100644 --- a/src/version/git.js +++ b/src/version/git.js @@ -12,7 +12,7 @@ module.exports = new (class Git extends BaseVersioning { gitSemverTags({ tagPrefix, - }, async (err, tags) => { + }, async(err, tags) => { const currentVersion = tags.length > 0 ? tags.shift().replace(tagPrefix, '') : null // Get the new version diff --git a/src/version/json.js b/src/version/json.js index 33ce3ee..4f75ba0 100644 --- a/src/version/json.js +++ b/src/version/json.js @@ -12,7 +12,7 @@ module.exports = new (class Json extends BaseVersioning { * @param {!string} releaseType - The type of release * @return {*} */ - bump = async (releaseType) => { + bump = async(releaseType) => { // Read the file const fileContent = this.read() diff --git a/src/version/toml.js b/src/version/toml.js index 50aec9a..33ce04d 100644 --- a/src/version/toml.js +++ b/src/version/toml.js @@ -4,7 +4,7 @@ const toml = require('@iarna/toml') const BaseVersioning = require('./base') const bumpVersion = require('../helpers/bumpVersion') -module.exports = new (class Toml extends BaseVersioning{ +module.exports = new (class Toml extends BaseVersioning { /** * Bumps the version in the package.json diff --git a/src/version/yaml.js b/src/version/yaml.js index 1f674cf..80975e2 100644 --- a/src/version/yaml.js +++ b/src/version/yaml.js @@ -4,7 +4,7 @@ const yaml = require('yaml') const BaseVersioning = require('./base') const bumpVersion = require('../helpers/bumpVersion') -module.exports = new (class Yaml extends BaseVersioning{ +module.exports = new (class Yaml extends BaseVersioning { /** * Bumps the version in the package.json @@ -12,14 +12,14 @@ module.exports = new (class Yaml extends BaseVersioning{ * @param {!string} releaseType - The type of release * @return {*} */ - bump = async (releaseType) => { + bump = async(releaseType) => { // Read the file const fileContent = this.read() const yamlContent = yaml.parse(fileContent) || {} const oldVersion = objectPath.get(yamlContent, this.versionPath, null) // Get the new version - this.newVersion =await bumpVersion( + this.newVersion = await bumpVersion( releaseType, oldVersion, ) diff --git a/test-output.js b/test-output.js new file mode 100644 index 0000000..8ae7821 --- /dev/null +++ b/test-output.js @@ -0,0 +1,57 @@ +const fs = require('fs') +const assert = require('assert') +const objectPath = require('object-path') +const yaml = require('yaml') +const toml = require('@iarna/toml') + +const actionConfig = yaml.parse(fs.readFileSync('./action.yml', 'utf8')) + +const { + FILES = actionConfig.inputs['version-file'].default, + EXPECTED_VERSION_PATH = actionConfig.inputs['version-path'].default, + EXPECTED_VERSION = actionConfig.inputs['fallback-version'].default, +} = process.env + +assert.ok(FILES, 'Files not defined!') + +/** + * Test if all the files are updated + */ +FILES.split(',').map((file, index) => { + const fileContent = fs.readFileSync(file.trim(), 'utf8') + const fileExtension = file.split('.').pop() + + assert.ok(fileExtension, 'No file extension found!') + + let parsedContent = null + + switch (fileExtension.toLowerCase()) { + case 'json': + parsedContent = JSON.parse(fileContent) + break + + case 'yaml': + case 'yml': + parsedContent = yaml.parse(fileContent) + break + + case 'toml': + parsedContent = toml.parse(fileContent) + break + + default: + assert.fail('File extension not supported!') + } + + assert.ok(parsedContent, 'Content could not be parsed!') + + const newVersionInFile = objectPath.get(parsedContent, EXPECTED_VERSION_PATH, null) + + const expectedVersions = EXPECTED_VERSION.split(',') + const expectedVersion = expectedVersions.length > 0 + ? expectedVersions[index] + : expectedVersions + + assert.strictEqual(newVersionInFile, expectedVersion.trim(), 'Version does not match what is expected') +}) + From 360a184f94a95de5c2f787eaa16b6c0977530b75 Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Tue, 15 Dec 2020 17:25:26 +0100 Subject: [PATCH 11/14] ci: Updated workflow --- .github/workflows/test.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4cda755..2c68572 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -505,12 +505,11 @@ jobs: config-file-path: './test-changelog.config.js' - name: Test output - with: - GENERATED_CHANGELOG_OUTPUT: ${{ steps.changelog.outputs.clean_changelog }} run: | if grep -q "### New Features" "./CHANGELOG.md"; then echo "Generated changelog file has config applied" else echo "Changelog config not applied" && exit 1 fi - + with: + GENERATED_CHANGELOG_OUTPUT: ${{ steps.changelog.outputs.clean_changelog }} From b4e03b0477818240561e67ffc2f4ba49d6ba2316 Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Tue, 15 Dec 2020 17:26:18 +0100 Subject: [PATCH 12/14] ci: Updated workflow --- .github/workflows/test.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2c68572..eb3e495 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -511,5 +511,3 @@ jobs: else echo "Changelog config not applied" && exit 1 fi - with: - GENERATED_CHANGELOG_OUTPUT: ${{ steps.changelog.outputs.clean_changelog }} From 953d0875210556795a5a149ba03bd8dc77731bed Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Tue, 15 Dec 2020 17:29:33 +0100 Subject: [PATCH 13/14] test: Updated tests --- src/helpers/git.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/helpers/git.js b/src/helpers/git.js index 11a9e14..ff80ccb 100644 --- a/src/helpers/git.js +++ b/src/helpers/git.js @@ -26,7 +26,9 @@ module.exports = new (class Git { console.log(`Skipping "${fullCommand}" because of test env`) - this.commandsRun.push(fullCommand) + if (!fullCommand.includes('git remote set-url origin')) { + this.commandsRun.push(fullCommand) + } } } @@ -153,7 +155,6 @@ module.exports = new (class Git { const expectedCommands = [ 'git config user.name "Conventional Changelog Action"', 'git config user.email "conventional.changelog.action@github.com"', - 'git remote set-url origin https://x-access-token:fake-token@github.com/TriPSs/conventional-changelog-action.git', 'git rev-parse --is-shallow-repository', 'git pull --tags --ff-only', ] From 483b99b3f8f98df26b5466c425b6eaf3fe374cb9 Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Tue, 15 Dec 2020 17:45:17 +0100 Subject: [PATCH 14/14] test: Updated tests --- .github/workflows/test.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index eb3e495..7511ffd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -113,9 +113,12 @@ jobs: path: "./" - run: npm ci --prod - - run: touch ./fake-file.log - run: "git config --global user.email 'changelog@github.com'" - run: "git config --global user.name 'Awesome Github action'" + + - run: git tag | xargs git tag -d + - name: Create fake tag + run: "git tag -a 'v0.55.8' -m 'v0.55.8'" - run: "git add . && git commit -m 'feat: Added fake file so version will be bumped'" - name: Generate changelog @@ -123,7 +126,7 @@ jobs: uses: ./ env: ENV: 'dont-use-git' - EXPECTED_TAG: 'v0.1.0' + EXPECTED_TAG: 'v0.56.0' SKIPPED_COMMIT: true with: github-token: ${{ secrets.github_token }} @@ -398,10 +401,12 @@ jobs: uses: ./ env: ENV: 'dont-use-git' + EXPECTED_TAG: 'v1.5.0' with: github-token: ${{ secrets.github_token }} pre-commit: './test-pre-commit.js' skip-on-empty: 'false' + version-file: './test-file.json' - run: test -f pre-commit.test.json || (echo should be here && exit 1) - run: cat pre-commit.test.json && echo ""