From 0aa82ce2ad5a23a200c8ce1eeba32eaefc846d9a Mon Sep 17 00:00:00 2001 From: Viktor Kuroljov Date: Mon, 13 Jul 2020 03:39:41 +0300 Subject: [PATCH] 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)) +}