diff --git a/README.md b/README.md index 23d32fc..90882e2 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ jobs: path-to-opencover-xml: ./test/opencover.xml path-to-badges: ./ minimum-coverage: 75 + commit-badges: false repo-token: ${{ secrets.CI_TOKEN }} ``` @@ -51,6 +52,14 @@ Path to the open cover xml file Threshold percentage at which a red badge would appear. +### `commit-badges` (Optional) + +**Optional:** When set will commit the changed badges to the repo. Default `true`. + +### `commit-branch-name` (Optional) + +**Optional:** When set will checkout the given branch name before committing the changed badges. Default is the current main branch. + ### `repo-token` (Required) GitHub repo token so that the changed file can be committed, like `${{ secrets.CI_TOKEN }}` diff --git a/action.yml b/action.yml index 29a8b4a..79444ec 100644 --- a/action.yml +++ b/action.yml @@ -11,6 +11,13 @@ inputs: minimum-coverage: description: 'Threshold percentage at which a red badge would appear' required: true + commit-badges: + description: 'Commit changed badges to repository' + required: false + default: true + commit-branch-name: + description: 'Specify alternative branch to commit changed badges into' + required: false repo-token: description: 'Github repo token so that the changed file can be committed, like secrets.GITHUB_TOKEN' required: true diff --git a/src/git-utils.js b/src/git-utils.js index 944a596..3c474cd 100644 --- a/src/git-utils.js +++ b/src/git-utils.js @@ -3,11 +3,17 @@ const core = require('@actions/core'); const commitAndPush = async function(files) { const repoTokenInput = core.getInput('repo-token', { required: true }); + const branchName = core.getInput('commit-branch-name', { required: false }); core.info('Committing new badge'); await exec.exec('git', ['config', 'user.name', `"${process.env['GITHUB_ACTOR']}"`]); await exec.exec('git', ['config', 'user.email', `"${process.env['GITHUB_ACTOR']}@users.noreply.github.com"`]); await exec.exec('git', ['remote', 'set-url', 'origin', `https://x-access-token:${repoTokenInput}@github.com/${process.env['GITHUB_REPOSITORY']}.git`]); + + if (branchName.length > 0) { + await exec.exec('git', ['checkout', branchName]); + } + for (const file of files) { await exec.exec('git', ['add', file]); } diff --git a/src/index.js b/src/index.js index 0370c08..83e104c 100644 --- a/src/index.js +++ b/src/index.js @@ -7,7 +7,7 @@ const generateBadge = require('./generate-badge'); async function run() { try { const openCoverFilePathInput = core.getInput('path-to-opencover-xml', { required: true }); - let badgesFilePathInput = core.getInput('path-to-badges', { required: false, }); + let badgesFilePathInput = core.getInput('path-to-badges', { required: false }); if (!badgesFilePathInput) { badgesFilePathInput = './/'; } @@ -32,10 +32,14 @@ async function run() { ); if (wasNewLineBadgeCreated || wasNewBranchBadgeCreated) { - await commitAndPush([ - lineBadgePath, - branchBadgePath - ]); + // Default for no value is 'true' + const commitBadges = core.getInput('commit-badges', { required: false }); + if (commitBadges.length === 0 || commitBadges === 'true') { + await commitAndPush([ + lineBadgePath, + branchBadgePath + ]); + } } else { core.info('No new badges were created, skipping git commit') }