Skip to content

Commit 4a9ed04

Browse files
committed
check for lock branches before checking for lock files - also add coloring
1 parent 304e4d2 commit 4a9ed04

File tree

5 files changed

+90
-22
lines changed

5 files changed

+90
-22
lines changed

__tests__/functions/unlock-on-merge.test.js

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import * as core from '@actions/core'
22
import * as unlock from '../../src/functions/unlock'
33
import * as checkLockFile from '../../src/functions/check-lock-file'
4+
import * as checkBranch from '../../src/functions/lock'
45
import {unlockOnMerge} from '../../src/functions/unlock-on-merge'
6+
import {COLORS} from '../../src/functions/colors'
57

68
const setOutputMock = jest.spyOn(core, 'setOutput')
79
const infoMock = jest.spyOn(core, 'info')
@@ -27,6 +29,9 @@ beforeEach(() => {
2729
link: 'https://github.com/corp/test/pull/123#issuecomment-123456789'
2830
}
2931
})
32+
jest.spyOn(checkBranch, 'checkBranch').mockImplementation(() => {
33+
return true
34+
})
3035

3136
context = {
3237
eventName: 'pull_request',
@@ -54,13 +59,13 @@ test('successfully unlocks all environments on a pull request merge', async () =
5459
await unlockOnMerge(octokit, context, environment_targets)
5560
).toStrictEqual(true)
5661
expect(infoMock).toHaveBeenCalledWith(
57-
'🔓 removed lock - environment: staging'
62+
`🔓 removed lock - environment: ${COLORS.highlight}staging${COLORS.reset}`
5863
)
5964
expect(infoMock).toHaveBeenCalledWith(
60-
'🔓 removed lock - environment: development'
65+
`🔓 removed lock - environment: ${COLORS.highlight}development${COLORS.reset}`
6166
)
6267
expect(infoMock).toHaveBeenCalledWith(
63-
'🔓 removed lock - environment: production'
68+
`🔓 removed lock - environment: ${COLORS.highlight}production${COLORS.reset}`
6469
)
6570
expect(setOutputMock).toHaveBeenCalledWith(
6671
'unlocked_environments',
@@ -95,14 +100,41 @@ test('only unlocks one environment because the other has no lock and the other i
95100
expect(
96101
await unlockOnMerge(octokit, context, environment_targets)
97102
).toStrictEqual(true)
98-
expect(debugMock).toHaveBeenCalledWith(
99-
'⏩ lock for PR 111 (env: production) is not associated with PR 123 - skipping...'
103+
expect(infoMock).toHaveBeenCalledWith(
104+
`⏩ lock for PR ${COLORS.info}111${COLORS.reset} (env: ${COLORS.highlight}production${COLORS.reset}) is not associated with PR ${COLORS.info}123${COLORS.reset} - skipping...`
100105
)
101-
expect(debugMock).toHaveBeenCalledWith(
102-
'⏩ no lock found for environment development - skipping...'
106+
expect(infoMock).toHaveBeenCalledWith(
107+
`⏩ no lock file found for environment ${COLORS.highlight}development${COLORS.reset} - skipping...`
108+
)
109+
expect(infoMock).toHaveBeenCalledWith(
110+
`🔓 removed lock - environment: ${COLORS.highlight}staging${COLORS.reset}`
111+
)
112+
})
113+
114+
test('only unlocks one environment because the other is not associated with the pull request and the other has no lock branch', async () => {
115+
checkLockFile.checkLockFile.mockImplementationOnce(() => {
116+
return {
117+
link: 'https://github.com/corp/test/pull/111#issuecomment-123456789'
118+
}
119+
})
120+
checkBranch.checkBranch.mockImplementationOnce(() => {
121+
return true
122+
})
123+
checkBranch.checkBranch.mockImplementationOnce(() => {
124+
return false
125+
})
126+
127+
expect(
128+
await unlockOnMerge(octokit, context, environment_targets)
129+
).toStrictEqual(true)
130+
expect(infoMock).toHaveBeenCalledWith(
131+
`⏩ lock for PR ${COLORS.info}111${COLORS.reset} (env: ${COLORS.highlight}production${COLORS.reset}) is not associated with PR ${COLORS.info}123${COLORS.reset} - skipping...`
132+
)
133+
expect(infoMock).toHaveBeenCalledWith(
134+
`⏩ no lock branch found for environment ${COLORS.highlight}development${COLORS.reset} - skipping...`
103135
)
104136
expect(infoMock).toHaveBeenCalledWith(
105-
'🔓 removed lock - environment: staging'
137+
`🔓 removed lock - environment: ${COLORS.highlight}staging${COLORS.reset}`
106138
)
107139
})
108140

dist/index.js

Lines changed: 24 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/functions/lock.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ async function findReason(context, sticky) {
245245
// :param context: The GitHub Actions event context
246246
// :param branchName: The name of the branch to check
247247
// :return: true if the branch exists, false if not
248-
async function checkBranch(octokit, context, branchName) {
248+
export async function checkBranch(octokit, context, branchName) {
249249
core.debug(`checking if branch ${branchName} exists...`)
250250
// Check if the lock branch already exists
251251
try {

src/functions/unlock-on-merge.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import * as core from '@actions/core'
22
import {unlock} from './unlock'
33
import {LOCK_METADATA} from './lock-metadata'
44
import {checkLockFile} from './check-lock-file'
5+
import {checkBranch} from './lock'
56
import {constructValidBranchName} from './valid-branch-name'
7+
import {COLORS} from './colors'
68

79
// Helper function to automatically find, and release a deployment lock when a pull request is merged
810
// :param octokit: the authenticated octokit instance
@@ -31,14 +33,27 @@ export async function unlockOnMerge(octokit, context, environment_targets) {
3133
// construct the lock branch name for this environment
3234
var lockBranch = `${constructValidBranchName(environment)}-${LOCK_METADATA.lockBranchSuffix}`
3335

36+
// Check if the lock branch exists
37+
const branchExists = await checkBranch(octokit, context, lockBranch)
38+
39+
// if the lock branch does not exist at all, then there is no lock to release
40+
if (!branchExists) {
41+
core.info(
42+
`⏩ no lock branch found for environment ${COLORS.highlight}${environment}${COLORS.reset} - skipping...`
43+
)
44+
continue
45+
}
46+
3447
// attempt to fetch the lockFile for this branch
3548
var lockFile = await checkLockFile(octokit, context, lockBranch)
3649

3750
// check to see if the lockFile exists and if it does, check to see if it has a link property
3851
if (lockFile && lockFile?.link) {
3952
// if the lockFile has a link property, find the PR number from the link
4053
var prNumber = lockFile.link.split('/pull/')[1].split('#issuecomment')[0]
41-
core.info(`🔍 checking lock for PR ${prNumber} (env: ${environment})`)
54+
core.info(
55+
`🔍 checking lock for PR ${COLORS.info}${prNumber}${COLORS.reset} (env: ${COLORS.highlight}${environment}${COLORS.reset})`
56+
)
4257

4358
// if the PR number matches the PR number of the merged pull request, then this lock is associated with the merged pull request
4459
if (prNumber === context.payload.pull_request.number.toString()) {
@@ -60,16 +75,19 @@ export async function unlockOnMerge(octokit, context, environment_targets) {
6075

6176
// log the result and format the output as it will always be a string ending with '- silent'
6277
var resultFmt = result.replace('- silent', '')
63-
core.info(`🔓 ${resultFmt.trim()} - environment: ${environment}`)
78+
core.info(
79+
`🔓 ${resultFmt.trim()} - environment: ${COLORS.highlight}${environment}${COLORS.reset}`
80+
)
6481
} else {
65-
core.debug(
66-
`⏩ lock for PR ${prNumber} (env: ${environment}) is not associated with PR ${context.payload.pull_request.number} - skipping...`
82+
core.info(
83+
`⏩ lock for PR ${COLORS.info}${prNumber}${COLORS.reset} (env: ${COLORS.highlight}${environment}${COLORS.reset}) is not associated with PR ${COLORS.info}${context.payload.pull_request.number}${COLORS.reset} - skipping...`
6784
)
6885
}
6986
} else {
70-
core.debug(
71-
`⏩ no lock found for environment ${environment} - skipping...`
87+
core.info(
88+
`⏩ no lock file found for environment ${COLORS.highlight}${environment}${COLORS.reset} - skipping...`
7289
)
90+
continue
7391
}
7492
}
7593

0 commit comments

Comments
 (0)