From 0aa82ce2ad5a23a200c8ce1eeba32eaefc846d9a Mon Sep 17 00:00:00 2001 From: Viktor Kuroljov Date: Mon, 13 Jul 2020 03:39:41 +0300 Subject: [PATCH 1/5] feat: Add pre-commit script file (hook) The idea is to have a way for users to be able to manipulate their repositories in different ways right before the git-add-git-commit phase. Might be useful in a lot of different ways: updating arbitrary files that are not directly related to the package version, but need to have an information about the next version and being commited before the said version released; or running some chores right before the commit: including updating docs, dependencies, running scripts, etc. --- .github/workflows/test.yml | 23 +++++++++++++++++++++++ action.yml | 4 ++++ src/index.js | 12 ++++++++++++ test/pre-commit.js | 15 +++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 test/pre-commit.js diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e1c4d98..bcde197 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: ${{ github.workspace }}/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/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..8d16464 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,13 @@ async function run() { if (!skipCommit) { // Add changed files to git + if (preCommit) { + await require(preCommit).preCommit({ + workspace: process.env.GITHUB_WORKSPACE, + 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..3775e61 --- /dev/null +++ b/test/pre-commit.js @@ -0,0 +1,15 @@ +const fs = require('fs') +const path = require('path') + +exports.preCommit = (props) => { + const body = { + workspace: props.workspace, + tag: props.tag, + version: props.version, + random: Math.random(), + } + + const dest = path.resolve(props.workspace, 'pre-commit.test.json') + + fs.writeFileSync(dest, JSON.stringify(body, null, 2)) +} From 1fe4a384140c41e6229ac5886ed1926af37627f0 Mon Sep 17 00:00:00 2001 From: Viktor Kuroljov Date: Mon, 13 Jul 2020 04:29:25 +0300 Subject: [PATCH 2/5] docs(readme): Add information about pre-commit hook --- README.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/README.md b/README.md index bcaffe9..2adb4ec 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. + +An absolute path to the `.js` script file must be provided when specifying the `pre-commit` hook. File +should contain all its dependencies bundled in itself just like for the webapp. + +Script should: +- be a CommonJS module +- have a single export: `exports.preCommit = (props) => { /* ... */ }` +- not have any return value +- be bundled + +`preCommit` function can be `async`. + +Following props will be passed to the function as a single parameter: + +```typescript +interface Props { + workspace: string; // same as process.env.GITHUB_WORKSPACE + tag: string; // Next tag e.g. v1.12.3 + version: string; // Next version e.g. 1.12.3 +} + +export function preCommit(props: Props): void {} +``` ## 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: ${{ github.workspace }}/some/path/pre-commit.js # requires absolute path +``` + 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) From 5675cecaa44e5c0d4bad1499626efb8639b7c266 Mon Sep 17 00:00:00 2001 From: Viktor Kuroljov Date: Mon, 13 Jul 2020 13:53:45 +0300 Subject: [PATCH 3/5] test(pre-commit): Test that pre-commit script gets env variables --- src/index.js | 1 - test/pre-commit.js | 14 +++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 8d16464..4e9455d 100644 --- a/src/index.js +++ b/src/index.js @@ -104,7 +104,6 @@ async function run() { // Add changed files to git if (preCommit) { await require(preCommit).preCommit({ - workspace: process.env.GITHUB_WORKSPACE, tag: gitTag, version: versioning.newVersion, }) diff --git a/test/pre-commit.js b/test/pre-commit.js index 3775e61..15b1e7e 100644 --- a/test/pre-commit.js +++ b/test/pre-commit.js @@ -1,15 +1,19 @@ const fs = require('fs') -const path = require('path') +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: props.workspace, + workspace: GITHUB_WORKSPACE, tag: props.tag, version: props.version, random: Math.random(), } - const dest = path.resolve(props.workspace, 'pre-commit.test.json') - - fs.writeFileSync(dest, JSON.stringify(body, null, 2)) + fs.writeFileSync('pre-commit.test.json', JSON.stringify(body, null, 2)) } From afea49fa57e678f9c8117d73415a488600b3cd28 Mon Sep 17 00:00:00 2001 From: Viktor Kuroljov Date: Mon, 13 Jul 2020 13:58:49 +0300 Subject: [PATCH 4/5] feat(pre-commit): Allow relative path for the pre-commit input --- .github/workflows/test.yml | 2 +- src/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bcde197..70b6fe1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -38,7 +38,7 @@ jobs: ENV: 'dont-use-git' with: github-token: ${{ secrets.github_token }} - pre-commit: ${{ github.workspace }}/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) diff --git a/src/index.js b/src/index.js index 4e9455d..713819c 100644 --- a/src/index.js +++ b/src/index.js @@ -103,7 +103,7 @@ async function run() { if (!skipCommit) { // Add changed files to git if (preCommit) { - await require(preCommit).preCommit({ + await require(path.resolve(preCommit)).preCommit({ tag: gitTag, version: versioning.newVersion, }) From 677ff927a40382a2c1ab9f596d27de32f1e36d54 Mon Sep 17 00:00:00 2001 From: Viktor Kuroljov Date: Mon, 13 Jul 2020 14:09:17 +0300 Subject: [PATCH 5/5] docs(readme): Path can be relative or absolute for pre-commit input --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 2adb4ec..c02b018 100644 --- a/README.md +++ b/README.md @@ -26,14 +26,13 @@ This action will bump version, tag commit and generate a changelog with conventi > 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. -An absolute path to the `.js` script file must be provided when specifying the `pre-commit` hook. File -should contain all its dependencies bundled in itself just like for the webapp. +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 +- be bundled (contain all dependencies in itself, just like the bundled webapp) `preCommit` function can be `async`. @@ -41,7 +40,6 @@ Following props will be passed to the function as a single parameter: ```typescript interface Props { - workspace: string; // same as process.env.GITHUB_WORKSPACE tag: string; // Next tag e.g. v1.12.3 version: string; // Next version e.g. 1.12.3 } @@ -49,6 +47,8 @@ interface Props { 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 - `changelog`: The generated changelog for the new version. @@ -126,7 +126,7 @@ Use a pre-commit hook uses: TriPSs/conventional-changelog-action@v3 with: github-token: ${{ secrets.github_token }} - pre-commit: ${{ github.workspace }}/some/path/pre-commit.js # requires absolute path + pre-commit: some/path/pre-commit.js ``` Github releases