diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e1c4d98..70b6fe1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,6 +21,29 @@ jobs: with: github-token: ${{ secrets.github_token }} + test-pre-commit: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + path: "./" + + - run: test -f pre-commit.test.json && (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-commit: test/pre-commit.js + skip-on-empty: 'false' + + - run: test -f pre-commit.test.json || (echo should be here && exit 1) + - run: cat pre-commit.test.json + test-git: runs-on: ubuntu-latest steps: diff --git a/README.md b/README.md index bcaffe9..c02b018 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,36 @@ This action will bump version, tag commit and generate a changelog with conventi - **Optional** `skip-on-empty`: Boolean to specify if you want to skip empty release (no-changelog generated). This case occured when you push `chore` commit with `angular` for example. Default `'false'`. - **Optional** `skip-version-file`: Do not update the version file. Default `'false'`. - **Optional** `skip-commit`: Do create a release commit. Default `'false'`. +- **Optional** `pre-commit`: Path to the pre-commit script file. No hook by default. + +### Pre-Commit hook + +> Function in a specified file will be run right before the git-add-git-commit phase, when the next +> version is already known and a new changelog has been generated. You can run any chores across your +> repository that should be added and commited with the release commit. + +Specified path could be relative or absolute. If it is relative, then it will be based on the `GITHUB_WORKSPACE` path. + +Script should: +- be a CommonJS module +- have a single export: `exports.preCommit = (props) => { /* ... */ }` +- not have any return value +- be bundled (contain all dependencies in itself, just like the bundled webapp) + +`preCommit` function can be `async`. + +Following props will be passed to the function as a single parameter: + +```typescript +interface Props { + tag: string; // Next tag e.g. v1.12.3 + version: string; // Next version e.g. 1.12.3 +} + +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. ## Outputs @@ -89,6 +119,16 @@ Use a custom file for versioning version-file: 'my-custom-file.yaml' ``` +Use a pre-commit hook + +```yaml +- name: Conventional Changelog Action + uses: TriPSs/conventional-changelog-action@v3 + with: + github-token: ${{ secrets.github_token }} + pre-commit: some/path/pre-commit.js +``` + Github releases ```yaml @@ -129,6 +169,9 @@ $ act -j test-yaml -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s gith # To run / toml git versioning $ act -j test-toml -P ubuntu-latest=nektos/act-environments-ubuntu:18.04 -s github_token=fake-token + +# To run pre-commit test +$ act -j test-pre-commit -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 365971a..e212cb9 100644 --- a/action.yml +++ b/action.yml @@ -79,6 +79,10 @@ inputs: default: 'false' required: false + pre-commit: + description: 'Path to the pre-commit script file' + required: false + outputs: changelog: diff --git a/src/index.js b/src/index.js index 0c0f049..713819c 100644 --- a/src/index.js +++ b/src/index.js @@ -13,6 +13,7 @@ async function run() { const gitUserEmail = core.getInput('git-user-email') const tagPrefix = core.getInput('tag-prefix') const preset = core.getInput('preset') + const preCommit = core.getInput('pre-commit') const outputFile = core.getInput('output-file') const releaseCount = core.getInput('release-count') const versionFile = core.getInput('version-file') @@ -31,6 +32,10 @@ async function run() { core.info(`Using "${tagPrefix}" as tag prefix`) core.info(`Using "${outputFile}" as output file`) + if (preCommit) { + core.info(`Using "${preCommit}" as pre-commit script`) + } + core.info(`Skipping empty releases is "${skipEmptyRelease ? 'enabled' : 'disabled'}"`) core.info(`Skipping the update of the version file is "${skipVersionFile ? 'enabled' : 'disabled'}"`) @@ -97,6 +102,12 @@ async function run() { if (!skipCommit) { // Add changed files to git + if (preCommit) { + await require(path.resolve(preCommit)).preCommit({ + tag: gitTag, + version: versioning.newVersion, + }) + } await git.add('.') await git.commit(gitCommitMessage.replace('{version}', gitTag)) } diff --git a/test/pre-commit.js b/test/pre-commit.js new file mode 100644 index 0000000..15b1e7e --- /dev/null +++ b/test/pre-commit.js @@ -0,0 +1,19 @@ +const fs = require('fs') +const t = 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') + + const body = { + workspace: GITHUB_WORKSPACE, + tag: props.tag, + version: props.version, + random: Math.random(), + } + + fs.writeFileSync('pre-commit.test.json', JSON.stringify(body, null, 2)) +}