diff --git a/action.yml b/action.yml index c9c0b4d..6aa26f2 100644 --- a/action.yml +++ b/action.yml @@ -27,6 +27,10 @@ inputs: description: "Defines if the workflow pushes the changes automatically" default: "true" required: false + auto_write_check: + description: "Specifies whether the workflow will verify the repository's write access privilege for the token before executing" + default: "true" + required: false runs: using: node16 diff --git a/index.js b/index.js index 59d544a..2a90771 100644 --- a/index.js +++ b/index.js @@ -8,9 +8,10 @@ const committerEmail = core.getInput('committer_email'); const commitMessage = core.getInput('commit_message'); const autoPush = (core.getInput('auto_push') === 'true'); const timeElapsed = parseInt(core.getInput('time_elapsed')); +const autoWriteCheck = (core.getInput('auto_write_check') === 'true'); // Using the lib -KeepAliveWorkflow(githubToken, committerUsername, committerEmail, commitMessage, timeElapsed, autoPush) +KeepAliveWorkflow(githubToken, committerUsername, committerEmail, commitMessage, timeElapsed, autoPush, autoWriteCheck) .then((message) => { core.info(message); process.exit(0); diff --git a/library.js b/library.js index f4252ba..4fcfe8d 100644 --- a/library.js +++ b/library.js @@ -8,11 +8,19 @@ const {execute} = require('./util'); * @param {string} commitMessage - Commit message while doing dummy commit * @param {number} timeElapsed - Time elapsed from the last commit to trigger a new automated commit (in days). Default: 50 * @param {boolean} autoPush - Boolean flag to define if the library should automatically push the changes. Default: false + * @param {boolean} autoWriteCheck - Enables automatic checking of the token for branch protection rules * @return {Promise | Promise} - Promise with success message or failure object */ -const KeepAliveWorkflow = async (githubToken, committerUsername, committerEmail, commitMessage, timeElapsed = 50, autoPush = false) => { +const KeepAliveWorkflow = async (githubToken, committerUsername, committerEmail, commitMessage, timeElapsed = 50, autoPush = false, autoWriteCheck = false) => { return new Promise(async (resolve, reject) => { try { + // Write detection + if (autoWriteCheck) { + // Protected branches + if (process.env.GITHUB_REF_PROTECTED === 'true') { + reject(`Looks like the branch is write protected. You need to disable that for this to work: https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches`) + } + } // Calculating the last commit date const {outputData} = await execute('git', ['--no-pager', 'log', '-1', '--format=%ct'], {encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe']});