Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock Branch Validation #55

Merged
merged 2 commits into from
Sep 3, 2024
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
53 changes: 53 additions & 0 deletions __tests__/functions/valid-branch-name.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {constructValidBranchName} from '../../src/functions/valid-branch-name'
import * as core from '@actions/core'

const debugMock = jest.spyOn(core, 'debug')

const branchName = 'production'

beforeEach(() => {
jest.clearAllMocks()
jest.spyOn(core, 'debug').mockImplementation(() => {})
})

test('does not make any modifications to a valid branch name', async () => {
expect(constructValidBranchName(branchName)).toBe(branchName)
expect(debugMock).toHaveBeenCalledWith(
`constructing valid branch name: ${branchName}`
)
expect(debugMock).toHaveBeenCalledWith(
`constructed valid branch name: ${branchName}`
)
})

test('replaces spaces with hyphens', async () => {
expect(constructValidBranchName(`super ${branchName}`)).toBe(
`super-${branchName}`
)
expect(debugMock).toHaveBeenCalledWith(
`constructing valid branch name: super ${branchName}`
)
expect(debugMock).toHaveBeenCalledWith(
`constructed valid branch name: super-${branchName}`
)
})

test('replaces multiple spaces with hyphens', async () => {
expect(constructValidBranchName(`super duper ${branchName}`)).toBe(
`super-duper-${branchName}`
)
expect(debugMock).toHaveBeenCalledWith(
`constructing valid branch name: super duper ${branchName}`
)
expect(debugMock).toHaveBeenCalledWith(
`constructed valid branch name: super-duper-${branchName}`
)
})

test('returns null if the branch is null', async () => {
expect(constructValidBranchName(null)).toBe(null)
})

test('returns undefined if the branch is undefined', async () => {
expect(constructValidBranchName(undefined)).toBe(undefined)
})
45 changes: 37 additions & 8 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.

3 changes: 2 additions & 1 deletion src/functions/check.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as core from '@actions/core'
import {LOCK_METADATA} from './lock-metadata'
import {checkLockFile} from './checkLockFile'
import {constructValidBranchName} from './valid-branch-name'

// Helper function for checking if a deployment lock exists
// :param octokit: The octokit client
Expand Down Expand Up @@ -34,7 +35,7 @@ export async function check(octokit, context, environment) {
}

// if a global lock does not exist, check if a lock exists for the environment
const lockBranch = `${environment}-${LOCK_METADATA.lockBranchSuffix}`
const lockBranch = `${constructValidBranchName(environment)}-${LOCK_METADATA.lockBranchSuffix}`
const environmentLockExists = await checkLockFile(
octokit,
context,
Expand Down
3 changes: 3 additions & 0 deletions src/functions/checkLockFile.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import * as core from '@actions/core'
import {LOCK_METADATA} from './lock-metadata'
import {constructValidBranchName} from './valid-branch-name'

// Helper function to check if a lock branch + lock file exists
// :param octokit: The authenticated octokit instance
// :param context: The github context
// :param branch: The branch to check for a lock file
// :returns: True if the lock file exists, false otherwise
export async function checkLockFile(octokit, context, branch) {
branch = constructValidBranchName(branch)

try {
// try to get the lock branch
await octokit.rest.repos.getBranch({
Expand Down
7 changes: 4 additions & 3 deletions src/functions/lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import dedent from 'dedent-js'
import {actionStatus} from './action-status'
import {timeDiff} from './time-diff'
import {LOCK_METADATA} from './lock-metadata'
import {constructValidBranchName} from './valid-branch-name'

// Constants for the lock file
const LOCK_BRANCH_SUFFIX = LOCK_METADATA.lockBranchSuffix
Expand Down Expand Up @@ -128,7 +129,7 @@ async function constructBranchName(environment, global) {
}

// If the lock is not global, return the environment-specific lock branch name
return `${environment}-${LOCK_BRANCH_SUFFIX}`
return `${constructValidBranchName(environment)}-${LOCK_BRANCH_SUFFIX}`
}

// Helper function to construct the unlock command
Expand Down Expand Up @@ -447,8 +448,8 @@ export async function lock(
// find the global flag for returning
const globalFlag = core.getInput('global_lock_flag').trim()

// construct the lock branch name
const branchName = `${environment}-${LOCK_BRANCH_SUFFIX}`
// construct the branch name for the lock
const branchName = await constructBranchName(environment, global)

// lock debug info
core.debug(`detected lock env: ${environment}`)
Expand Down
7 changes: 4 additions & 3 deletions src/functions/unlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as core from '@actions/core'
import {actionStatus} from './action-status'
import {LOCK_METADATA} from './lock-metadata'
import dedent from 'dedent-js'
import {constructValidBranchName} from './valid-branch-name'

// Constants for the lock file
const LOCK_BRANCH = LOCK_METADATA.lockBranchSuffix
Expand Down Expand Up @@ -35,7 +36,7 @@ export async function unlock(
// Delete the lock branch
const result = await octokit.rest.git.deleteRef({
...context.repo,
ref: `heads/${environment}-${LOCK_BRANCH}`
ref: `heads/${constructValidBranchName(environment)}-${LOCK_BRANCH}`
})

// If the lock was successfully released, return true
Expand All @@ -48,7 +49,7 @@ export async function unlock(
}

// construct the branch name and success message text
const branchName = `${environment}-${LOCK_BRANCH}`
const branchName = `${constructValidBranchName(environment)}-${LOCK_BRANCH}`
var successText = ''
if (global === true) {
successText = '`global`'
Expand Down Expand Up @@ -76,7 +77,7 @@ export async function unlock(
return true
} else {
// If the lock was not successfully released, return false and log the HTTP code
const comment = `failed to delete lock branch: ${environment}-${LOCK_BRANCH} - HTTP: ${result.status}`
const comment = `failed to delete lock branch: ${constructValidBranchName(environment)}-${LOCK_BRANCH} - HTTP: ${result.status}`
core.info(comment)

// If headless, exit here
Expand Down
20 changes: 20 additions & 0 deletions src/functions/valid-branch-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as core from '@actions/core'

// Helper function to create a valid branch name that will pass GitHub's API ref validation
// :param branch: The branch name
// :returns: A string of the branch name with proper formatting
export function constructValidBranchName(branch) {
core.debug(`constructing valid branch name: ${branch}`)

if (branch === null) {
return null
} else if (branch === undefined) {
return undefined
}

// If environment contains any spaces, replace all of them with a hyphen
branch = branch.replace(/\s/g, '-')

core.debug(`constructed valid branch name: ${branch}`)
return branch
}
3 changes: 2 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {environmentTargets} from './functions/environment-targets'
import * as github from '@actions/github'
import {context} from '@actions/github'
import dedent from 'dedent-js'
import {constructValidBranchName} from './functions/valid-branch-name'

// :returns: 'success', 'success - noop', 'failure', 'safe-exit', or raises an error
export async function run() {
Expand Down Expand Up @@ -157,7 +158,7 @@ export async function run() {
// special comment for global deploy locks
let globalMsg = ''
let environmentMsg = `- __Environment__: \`${lockData.environment}\``
let lockBranchName = `${lockData.environment}-${LOCK_METADATA.lockBranchSuffix}`
let lockBranchName = `${constructValidBranchName(lockData.environment)}-${LOCK_METADATA.lockBranchSuffix}`
if (lockData.global === true) {
globalMsg = dedent(`

Expand Down