Skip to content

Commit

Permalink
added branch protection check, ref: #17
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamkrishnar committed Jan 14, 2024
1 parent 64e8399 commit d1915ce
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion library.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> | Promise<Object>} - 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']});
Expand Down

0 comments on commit d1915ce

Please sign in to comment.