From f0fabf6d88a3b7cef366530cc9cad6160a00d128 Mon Sep 17 00:00:00 2001 From: Janno Rothfos Date: Thu, 13 Aug 2020 08:50:58 +0200 Subject: [PATCH] feat: Allow to specify a config file --- .github/workflows/test.yml | 21 +++++++++++++++++++++ README.md | 21 +++++++++++++++++++++ action.yml | 3 +++ src/helpers/generateChangelog.js | 22 +++++++++++++--------- src/index.js | 19 ++++++++++++------- test-changelog.config.js | 9 +++++++++ 6 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 test-changelog.config.js diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8a088c4..f130a34 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -259,3 +259,24 @@ jobs: with: github-token: ${{ secrets.github_token }} version-file: 'package.json, package-lock.json' + + test-config-file-path: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + path: "./" + + - name: Install Packages + run: yarn + + - name: Generate Changelog + id: changelog + uses: ./ + env: + ENV: 'dont-use-git' + with: + github-token: ${{ secrets.github_token }} + skip-version-file: 'true' + config-file-path: './test-changelog.config.js' diff --git a/README.md b/README.md index 2dfd4dc..3e12299 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ This action will bump version, tag commit and generate a changelog with conventi - **Optional** `skip-commit`: Do create a release commit. Default `'false'`. - **Optional** `pre-commit`: Path to the pre-commit script file. No hook by default. - **Optional** `fallback-version`: The fallback version, if no older one can be detected, or if it is the first one. Default `'0.1.0'` +- **Optional** `config-file-path`: Path to the conventional changelog config file. If set, the preset setting will be ignored ### Pre-Commit hook @@ -51,6 +52,23 @@ 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. +### 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. + +example: +```javascript +'use strict' +const config = require('conventional-changelog-conventionalcommits'); + +module.exports = config({ + "issuePrefixes": ["TN-"], + "issueUrlFormat": "https://jira.example.com/browse/{{prefix}}{{id}}" +}) +``` +The specified path can be relative or absolute. If it is relative, then it will be based on the `GITHUB_WORKSPACE` path. + +Make sure to install all required packages in the workflow before executing this action. + ## Outputs - `changelog`: The generated changelog for the new version. @@ -185,6 +203,9 @@ $ act -j test-pre-commit -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 - # To run / multiple files test $ act -j multiple-files -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token + +# 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 ``` ## [License](./LICENSE) diff --git a/action.yml b/action.yml index fc44ad1..e2161e0 100644 --- a/action.yml +++ b/action.yml @@ -86,6 +86,9 @@ 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' + + 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/generateChangelog.js b/src/helpers/generateChangelog.js index ca2f250..a2e147e 100644 --- a/src/helpers/generateChangelog.js +++ b/src/helpers/generateChangelog.js @@ -10,15 +10,19 @@ const conventionalChangelog = require('conventional-changelog') * @param releaseCount * @returns {*} */ -const getChangelogStream = (tagPrefix, preset, version, releaseCount) => conventionalChangelog({ - preset, - releaseCount: parseInt(releaseCount, 10), - tagPrefix, - }, +const getChangelogStream = (tagPrefix, preset, version, releaseCount, config) => conventionalChangelog({ + preset, + releaseCount: parseInt(releaseCount, 10), + tagPrefix, + config +}, { version, currentTag: `${tagPrefix}${version}`, }, + {}, + config && config.parserOpts, + config && config.writerOpts ) module.exports = getChangelogStream @@ -32,8 +36,8 @@ module.exports = getChangelogStream * @param releaseCount * @returns {Promise} */ -module.exports.generateStringChangelog = (tagPrefix, preset, version, releaseCount) => new Promise((resolve, reject) => { - const changelogStream = getChangelogStream(tagPrefix, preset, version, releaseCount) +module.exports.generateStringChangelog = (tagPrefix, preset, version, releaseCount, config) => new Promise((resolve, reject) => { + const changelogStream = getChangelogStream(tagPrefix, preset, version, releaseCount, config) let changelog = '' @@ -54,8 +58,8 @@ module.exports.generateStringChangelog = (tagPrefix, preset, version, releaseCou * @param releaseCount * @returns {Promise<>} */ -module.exports.generateFileChangelog = (tagPrefix, preset, version, fileName, releaseCount) => new Promise((resolve) => { - const changelogStream = getChangelogStream(tagPrefix, preset, version, releaseCount) +module.exports.generateFileChangelog = (tagPrefix, preset, version, fileName, releaseCount, config) => new Promise((resolve) => { + const changelogStream = getChangelogStream(tagPrefix, preset, version, releaseCount, config) changelogStream .pipe(fs.createWriteStream(fileName)) diff --git a/src/index.js b/src/index.js index 3a5f406..0f52798 100644 --- a/src/index.js +++ b/src/index.js @@ -5,13 +5,14 @@ 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) // File type not supported if (versioning === null) { - throw new Error(`File extension "${ext}" from file "${x}" is not supported`) + throw new Error(`File extension "${ext}" from file "${file}" is not supported`) } versioning.init(path.resolve(file), versionPath) @@ -28,7 +29,7 @@ async function run() { const gitUserName = core.getInput('git-user-name') const gitUserEmail = core.getInput('git-user-email') const tagPrefix = core.getInput('tag-prefix') - const preset = core.getInput('preset') + const preset = !core.getInput('config-file-path') ? core.getInput('preset') : '' const preCommit = core.getInput('pre-commit') const outputFile = core.getInput('output-file') const releaseCount = core.getInput('release-count') @@ -37,6 +38,7 @@ async function run() { const skipVersionFile = core.getInput('skip-version-file').toLowerCase() === 'true' const skipCommit = core.getInput('skip-commit').toLowerCase() === 'true' const skipEmptyRelease = core.getInput('skip-on-empty').toLowerCase() === 'true' + const conventionalConfigFile = core.getInput('config-file-path') core.info(`Using "${preset}" preset`) core.info(`Using "${gitCommitMessage}" as commit message`) @@ -47,6 +49,7 @@ async function run() { core.info(`Using "${versionPath}" as version path`) core.info(`Using "${tagPrefix}" as tag prefix`) core.info(`Using "${outputFile}" as output file`) + core.info(`Using "${conventionalConfigFile}" as config file`) if (preCommit) { core.info(`Using "${preCommit}" as pre-commit script`) @@ -58,7 +61,9 @@ async function run() { core.info('Pull to make sure we have the full git history') await git.pull() - conventionalRecommendedBump({ preset, tagPrefix }, async(error, recommendation) => { + const config = conventionalConfigFile && require(resolve(process.cwd(), conventionalConfigFile)); + + conventionalRecommendedBump({ preset, tagPrefix, config }, async (error, recommendation) => { if (error) { core.setFailed(error.message) return @@ -96,7 +101,7 @@ async function run() { // Generate the string changelog - const stringChangelog = await changelog.generateStringChangelog(tagPrefix, preset, newVersion, 1) + const stringChangelog = await changelog.generateStringChangelog(tagPrefix, preset, newVersion, 1, config) core.info('Changelog generated') core.info(stringChangelog) @@ -114,7 +119,7 @@ async function run() { // If output file === 'false' we don't write it to file if (outputFile !== 'false') { // Generate the changelog - await changelog.generateFileChangelog(tagPrefix, preset, newVersion, outputFile, releaseCount) + await changelog.generateFileChangelog(tagPrefix, preset, newVersion, outputFile, releaseCount, config) } const gitTag = `${tagPrefix}${newVersion}` @@ -135,9 +140,9 @@ async function run() { await git.createTag(gitTag) core.info('Push all changes') - try{ + try { await git.push() - }catch (error) { + } catch (error) { core.setFailed(error.message) } diff --git a/test-changelog.config.js b/test-changelog.config.js new file mode 100644 index 0000000..9741d3d --- /dev/null +++ b/test-changelog.config.js @@ -0,0 +1,9 @@ +'use strict' +const config = require('conventional-changelog-conventionalcommits'); + +module.exports = config({ + "types": [ + { type: 'feat', section: 'New Features' }, + { type: 'fix', section: 'Bugs' } + ] +}) \ No newline at end of file