generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 51
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
Branch Ruleset Checks #342
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
884bd22
init branch protection checks
GrantBirki 812029c
the `getBranchProtection()` method requires admin rights so it is not…
GrantBirki 4cadbff
extract data
GrantBirki f9a0528
add new `use_security_warnings` input for ruleset checks
GrantBirki cae8c7c
bundle
GrantBirki 26f8a49
update text
GrantBirki bc64338
info log for dev
GrantBirki 35c83a6
check against the suggested rulesets
GrantBirki cf65ef7
ensure that `required_approving_review_count` is set to a non-zero in…
GrantBirki 6bf8ba2
comments
GrantBirki 0542aad
move back to debug
GrantBirki d8ab95b
pass through use_security_warnings
GrantBirki 6a65a3b
rename to `branchRulesetChecks()`
GrantBirki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,221 @@ | ||
import {branchRulesetChecks} from '../../src/functions/branch-ruleset-checks' | ||
import * as core from '@actions/core' | ||
import {COLORS} from '../../src/functions/colors' | ||
import {SUGGESTED_RULESETS} from '../../src/functions/suggested-rulesets' | ||
|
||
var context | ||
var octokit | ||
var data | ||
var rulesets | ||
|
||
// const debugMock = jest.spyOn(core, 'debug').mockImplementation(() => {}) | ||
const warningMock = jest.spyOn(core, 'warning').mockImplementation(() => {}) | ||
const infoMock = jest.spyOn(core, 'info').mockImplementation(() => {}) | ||
|
||
beforeEach(() => { | ||
jest.spyOn(core, 'info').mockImplementation(() => {}) | ||
jest.spyOn(core, 'debug').mockImplementation(() => {}) | ||
jest.spyOn(core, 'warning').mockImplementation(() => {}) | ||
jest.clearAllMocks() | ||
|
||
data = { | ||
branch: 'main' | ||
} | ||
|
||
rulesets = [ | ||
{ | ||
type: 'deletion' | ||
}, | ||
{ | ||
type: 'non_fast_forward' | ||
}, | ||
{ | ||
type: 'pull_request', | ||
parameters: { | ||
required_approving_review_count: 1, | ||
dismiss_stale_reviews_on_push: true, | ||
required_reviewers: [], | ||
require_code_owner_review: true, | ||
require_last_push_approval: false, | ||
required_review_thread_resolution: false, | ||
automatic_copilot_code_review_enabled: false, | ||
allowed_merge_methods: ['merge', 'squash', 'rebase'] | ||
} | ||
}, | ||
{ | ||
type: 'required_status_checks', | ||
parameters: { | ||
strict_required_status_checks_policy: true, | ||
do_not_enforce_on_create: false, | ||
required_status_checks: [] | ||
} | ||
}, | ||
{ | ||
type: 'required_deployments', | ||
parameters: { | ||
required_deployment_environments: [] | ||
} | ||
} | ||
] | ||
|
||
context = { | ||
repo: { | ||
owner: 'corp', | ||
repo: 'test' | ||
}, | ||
issue: { | ||
number: 1 | ||
} | ||
} | ||
|
||
octokit = { | ||
rest: { | ||
repos: { | ||
getBranchRules: jest.fn().mockReturnValueOnce({data: rulesets}) | ||
} | ||
} | ||
} | ||
}) | ||
|
||
test('finds that no branch protections or rulesets are defined', async () => { | ||
octokit = { | ||
rest: { | ||
repos: { | ||
getBranchRules: jest.fn().mockReturnValueOnce({data: []}) | ||
} | ||
} | ||
} | ||
expect(await branchRulesetChecks(context, octokit, data)).toStrictEqual({ | ||
success: false, | ||
failed_checks: ['missing_branch_rulesets'] | ||
}) | ||
expect(warningMock).toHaveBeenCalledWith( | ||
`🔐 branch ${COLORS.highlight}rulesets${COLORS.reset} are not defined for branch ${COLORS.highlight}${data.branch}${COLORS.reset}` | ||
) | ||
}) | ||
|
||
test('exits early if the user has disabled security warnings', async () => { | ||
data.use_security_warnings = false | ||
expect(await branchRulesetChecks(context, octokit, data)).toStrictEqual({ | ||
success: true | ||
}) | ||
expect(warningMock).not.toHaveBeenCalled() | ||
expect(infoMock).not.toHaveBeenCalledWith( | ||
`🔐 branch ruleset checks ${COLORS.success}passed${COLORS.reset}` | ||
) | ||
}) | ||
|
||
test('finds that the branch ruleset is missing the deletion rule', async () => { | ||
rulesets = rulesets.filter(rule => rule.type !== 'deletion') | ||
|
||
octokit = { | ||
rest: { | ||
repos: { | ||
getBranchRules: jest.fn().mockReturnValueOnce({data: rulesets}) | ||
} | ||
} | ||
} | ||
|
||
expect(await branchRulesetChecks(context, octokit, data)).toStrictEqual({ | ||
success: false, | ||
failed_checks: ['missing_deletion'] | ||
}) | ||
expect(warningMock).toHaveBeenCalledWith( | ||
`🔐 branch ${COLORS.highlight}rulesets${COLORS.reset} for branch ${COLORS.highlight}${data.branch}${COLORS.reset} is missing a rule of type ${COLORS.highlight}deletion${COLORS.reset}` | ||
) | ||
}) | ||
|
||
test('finds that the branch ruleset is missing the dismiss_stale_reviews_on_push parameter on the pull_request rule', async () => { | ||
rulesets = rulesets.map(rule => { | ||
if (rule.type === 'pull_request') { | ||
return { | ||
type: 'pull_request', | ||
parameters: { | ||
...rule.parameters, | ||
dismiss_stale_reviews_on_push: false | ||
} | ||
} | ||
} | ||
return rule | ||
}) | ||
|
||
octokit = { | ||
rest: { | ||
repos: { | ||
getBranchRules: jest.fn().mockReturnValueOnce({data: rulesets}) | ||
} | ||
} | ||
} | ||
|
||
expect(await branchRulesetChecks(context, octokit, data)).toStrictEqual({ | ||
success: false, | ||
failed_checks: ['mismatch_pull_request_dismiss_stale_reviews_on_push'] | ||
}) | ||
expect(warningMock).toHaveBeenCalledWith( | ||
`🔐 branch ${COLORS.highlight}rulesets${COLORS.reset} for branch ${COLORS.highlight}${data.branch}${COLORS.reset} contains a rule of type ${COLORS.highlight}pull_request${COLORS.reset} with a parameter ${COLORS.highlight}dismiss_stale_reviews_on_push${COLORS.reset} which does not match the suggested parameter` | ||
) | ||
}) | ||
|
||
test('finds that all suggested branch rulesets are defined', async () => { | ||
rulesets = SUGGESTED_RULESETS.map(suggested_rule => { | ||
return { | ||
type: suggested_rule.type, | ||
parameters: suggested_rule.parameters | ||
} | ||
}) | ||
|
||
octokit = { | ||
rest: { | ||
repos: { | ||
getBranchRules: jest.fn().mockReturnValueOnce({data: rulesets}) | ||
} | ||
} | ||
} | ||
|
||
expect(await branchRulesetChecks(context, octokit, data)).toStrictEqual({ | ||
success: true, | ||
failed_checks: [] | ||
}) | ||
expect(warningMock).not.toHaveBeenCalled() | ||
expect(infoMock).toHaveBeenCalledWith( | ||
`🔐 branch ruleset checks ${COLORS.success}passed${COLORS.reset}` | ||
) | ||
}) | ||
|
||
test('finds that all suggested branch rulesets are defined but required reviews is set to 0', async () => { | ||
rulesets = SUGGESTED_RULESETS.map(suggested_rule => { | ||
return { | ||
type: suggested_rule.type, | ||
parameters: suggested_rule.parameters | ||
} | ||
}) | ||
|
||
rulesets = rulesets.map(rule => { | ||
if (rule.type === 'pull_request') { | ||
return { | ||
type: 'pull_request', | ||
parameters: { | ||
...rule.parameters, | ||
required_approving_review_count: 0 | ||
} | ||
} | ||
} | ||
return rule | ||
}) | ||
|
||
octokit = { | ||
rest: { | ||
repos: { | ||
getBranchRules: jest.fn().mockReturnValueOnce({data: rulesets}) | ||
} | ||
} | ||
} | ||
|
||
expect(await branchRulesetChecks(context, octokit, data)).toStrictEqual({ | ||
success: false, | ||
failed_checks: ['mismatch_pull_request_required_approving_review_count'] | ||
}) | ||
expect(warningMock).toHaveBeenCalledWith( | ||
`🔐 branch ${COLORS.highlight}rulesets${COLORS.reset} for branch ${COLORS.highlight}${data.branch}${COLORS.reset} contains the required_approving_review_count parameter but it is set to 0` | ||
) | ||
}) |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type for 'description' should be 'boolean' instead of 'string' to match the intended data type.
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.