generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #299 from github/lock-branch-validation
Lock Branch Validation
- Loading branch information
Showing
9 changed files
with
118 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters