Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ inputs:
required: false
default: "true"
mode:
description: 'The mode to use "lock" or "unlock". If not provided, the default mode assumes the workflow is not headless and triggered by a comment on a pull request - Example: .lock / .unlock'
description: 'The mode to use "lock", "unlock", or "check". If not provided, the default mode assumes the workflow is not headless and triggered by a comment on a pull request - Example: .lock / .unlock'
required: false
reason:
description: Reason for claiming the deployment lock for this repository
Expand All @@ -42,6 +42,8 @@ outputs:
description: The comment id which triggered this deployment (if it was not headless)
headless:
description: 'The string "true" if the run was headless, otherwise the string "false" - Headless in this context would be if the "mode" was set and the Action was not invoked by a comment on a pull request'
locked:
description: 'If the mode is set to "check", this output will be "true" if a lock exists, otherwise "false"'
runs:
using: "node16"
main: "dist/index.js"
66 changes: 66 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions src/functions/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as core from '@actions/core'

// Constants for the lock file
const LOCK_BRANCH = 'branch-deploy-lock'
const LOCK_FILE = 'lock.json'

// Helper function for checking if a deployment lock exists
// :param octokit: The octokit client
// :param context: The GitHub Actions event context
// :returns: true if the lock exists, false if it does not
export async function check(octokit, context) {
// Check if the lock branch already exists
try {
await octokit.rest.repos.getBranch({
...context.repo,
branch: LOCK_BRANCH
})
} catch (error) {
// If the lock file doesn't exist, return
if (error.status === 404) {
core.saveState('locked', 'false')
return false
}
}

// If the lock branch exists, check if a lock file exists
try {
// Get the lock file contents
const response = await octokit.rest.repos.getContent({
...context.repo,
path: LOCK_FILE,
ref: LOCK_BRANCH
})

// Decode the file contents to json
const lockData = JSON.parse(
Buffer.from(response.data.content, 'base64').toString()
)

// If the lock file exists, and can be decoded, return true
if (lockData !== null && lockData !== undefined) {
// Set locked to true if the lock file exists
core.saveState('locked', 'true')
return true
}

// If we get here, the lock file may exist but it cannot be decoded
core.saveState('locked', 'false')
return false
} catch (error) {
// If the lock file doesn't exist, return false
if (error.status === 404) {
core.saveState('locked', 'false')
return false
}

// If some other error occurred, throw it
throw new Error(error)
}
}
4 changes: 4 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {actionStatus} from './functions/action-status'
import {validPermissions} from './functions/valid-permissions'
import {lock} from './functions/lock'
import {unlock} from './functions/unlock'
import {check} from './functions/check'
import {timeDiff} from './functions/time-diff'
import * as github from '@actions/github'
import {context} from '@actions/github'
Expand Down Expand Up @@ -43,6 +44,9 @@ export async function run() {
} else if (lock_mode === 'unlock') {
await unlock(octokit, context, null, true)
return 'success - headless'
} else if (lock_mode === 'check') {
await check(octokit, context)
return 'success - headless'
}

// Get the body of the IssueOps command
Expand Down