-
Notifications
You must be signed in to change notification settings - Fork 10
Branch Protection Rules support for main branch #16
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
Open
richm
wants to merge
6
commits into
linux-system-roles:main
Choose a base branch
from
richm:branch-protection-rules
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cd373c2
Branch Protection Rules support for main branch
richm 23268ce
update README
richm 966e972
sort status checks
richm 0f89832
allow isAdminEnforced per-role
richm bbf071d
additional debugging
richm 88c0572
merge group permissions wip
richm 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -2,3 +2,5 @@ github_actions: | |
| weekly_ci: | ||
| schedule: | ||
| - cron: "0 12 * * 6" | ||
| # branch protection | ||
| isAdminEnforced: true | ||
This file contains hidden or 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
14 changes: 14 additions & 0 deletions
14
playbooks/graphql_files/create_branch_protection_rules.graphql
This file contains hidden or 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,14 @@ | ||
| mutation { | ||
| createBranchProtectionRule(input:{ | ||
| {% for key, value in bpr.items() %} | ||
| {{ key }}:{{ value | to_nice_json }}, | ||
| {% endfor %} | ||
| repositoryId:"{{ repo.id }}" | ||
| }) { | ||
| branchProtectionRule { | ||
| id | ||
| allowsForcePushes | ||
| } | ||
| clientMutationId | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
playbooks/graphql_files/delete_branch_protection_rules.graphql
This file contains hidden or 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,7 @@ | ||
| mutation { | ||
| deleteBranchProtectionRule(input:{ | ||
| branchProtectionRuleId:"{{ bpr_id }}" | ||
| }) { | ||
| clientMutationId | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
playbooks/graphql_files/get_branch_protection_rules.graphql
This file contains hidden or 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,31 @@ | ||
| query { | ||
| repository(owner:"{{ github_org }}", name:"{{ inventory_hostname }}") { | ||
| id | ||
| branchProtectionRules(first:50) { | ||
| edges { | ||
| node { | ||
| id | ||
| {% for param in __bpr_fields %} | ||
| {{ param }} | ||
| {% endfor %} | ||
| {# | ||
| requiredStatusChecks { | ||
| context | ||
| app { | ||
| createdAt | ||
| databaseId | ||
| description | ||
| logoBackgroundColor | ||
| logoUrl | ||
| name | ||
| slug | ||
| updatedAt | ||
| url | ||
| } | ||
| } | ||
| #} | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,112 @@ | ||
| # use the graphql variable names | ||
| - name: Manage branch protection rules | ||
| hosts: active_roles | ||
| connection: local | ||
| gather_facts: false | ||
| tasks: | ||
| - name: Get gh config file | ||
| slurp: | ||
| path: "{{ lookup('env', 'HOME') ~ '/.config/gh/hosts.yml' }}" | ||
| register: gh_cfg_file | ||
|
|
||
| - name: Get token | ||
| set_fact: | ||
| token: "{{ cfg_dict['github.com']['oauth_token'] }}" | ||
| vars: | ||
| cfg_dict: "{{ gh_cfg_file.content | b64decode | from_yaml }}" | ||
|
|
||
| - name: Get Branch Protection Rules | ||
| uri: | ||
| url: https://api.github.com/graphql | ||
| method: POST | ||
| headers: | ||
| content-type: "application/json" | ||
| authorization: "Bearer {{ token }}" | ||
| X-Github-Next-Global-ID: "1" | ||
| body_format: json | ||
| body: | ||
| query: "{{ lookup('template', 'graphql_files/get_branch_protection_rules.graphql') }}" | ||
| # variables: "{{ lookup('template', './test_vars.yml') | from_yaml | to_nice_json }}" | ||
| vars: | ||
| __bpr_fields: "{{ (branchProtectionRules | first).keys() | list }}" | ||
| register: __query_result | ||
|
|
||
| - name: Result | ||
| debug: | ||
| msg: "{{ __query_result.json.data | to_nice_json }}" | ||
| verbosity: 3 | ||
|
|
||
| - name: Convert result to canonical format | ||
| set_fact: | ||
| repo: | | ||
| {% set __repo = {} %} | ||
| {% for key, value in __query_result.json.data.repository.items() %} | ||
| {% if key == "branchProtectionRules" %} | ||
| {% set _ = __repo.update({key: []})%} | ||
| {% for edge in value["edges"] %} | ||
| {% if "node" in edge %} | ||
| {% set _ = __repo[key].append(edge["node"]) %} | ||
| {% endif %} | ||
| {% endfor %} | ||
| {% else %} | ||
| {% set _ = __repo.update({key: value})%} | ||
| {% endif %} | ||
| {% endfor %} | ||
| {{ __repo }} | ||
|
|
||
| - name: Show converted result | ||
| debug: | ||
| msg: repo {{ repo | to_nice_json }} bpr {{ branchProtectionRules | to_nice_json }} | ||
| verbosity: 3 | ||
|
|
||
| - name: Update Branch Protection Rules if needed | ||
| vars: | ||
| __expected: "{{ branchProtectionRules | sort(attribute='pattern') | map('combine', {'id': ''}) }}" | ||
| __actual: "{{ repo.branchProtectionRules | sort(attribute='pattern') | map('combine', {'id': ''}) }}" | ||
| when: __expected != __actual | ||
| block: | ||
| - name: Remove existing rules | ||
| uri: | ||
| url: https://api.github.com/graphql | ||
| method: POST | ||
| headers: | ||
| content-type: "application/json" | ||
| authorization: "Bearer {{ token }}" | ||
| X-Github-Next-Global-ID: "1" | ||
| body_format: json | ||
| body: | ||
| query: "{{ lookup('template', 'graphql_files/delete_branch_protection_rules.graphql') }}" | ||
| # variables: "{{ lookup('template', './test_vars.yml') | from_yaml | to_nice_json }}" | ||
| register: __delete_result | ||
| vars: | ||
| bpr_id: "{{ item.id }}" | ||
| loop: "{{ repo.branchProtectionRules }}" | ||
|
|
||
| - name: Show delete result | ||
| debug: | ||
| msg: __delete_result {{ __delete_result.json.data | to_nice_json }} | ||
| verbosity: 3 | ||
| when: __delete_result.json is defined | ||
|
|
||
| - name: Add expected rules | ||
| uri: | ||
| url: https://api.github.com/graphql | ||
| method: POST | ||
| headers: | ||
| content-type: "application/json" | ||
| authorization: "Bearer {{ token }}" | ||
| X-Github-Next-Global-ID: "1" | ||
| body_format: json | ||
| body: | ||
| query: "{{ lookup('template', 'graphql_files/create_branch_protection_rules.graphql') }}" | ||
| # variables: "{{ lookup('template', './test_vars.yml') | from_yaml | to_nice_json }}" | ||
| register: __create_result | ||
| loop: "{{ branchProtectionRules }}" | ||
| loop_control: | ||
| loop_var: bpr | ||
|
|
||
| - name: Show create result | ||
| debug: | ||
| msg: "{{ __create_result.json.data | to_nice_json }}" | ||
| verbosity: 3 | ||
| when: __create_result.json is defined |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Currently, the UI also shows a source for each check (DCO, GitHub Actions, GitHub Code Scanning) depending on the status, the alternative seems to be "any source" which might be what this will be configuring, since it does not collect the source. It would be great to keep the sources as well.
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
ansible_lintetc. are the defaults applied to all roles - so the network.yml contains only those status checks unique (currently) to networkThere 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.
Why keep the sources?
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.
My assumption is that there is that there might be a security impact (maybe not that big since it seems to require write access to the repo to set a state) and to avoid that status checks conflict. For example, if someone experiments with a new tool that would set a markdownlint status, it could approve the PR without the official markdownlint being run. I guess, this also would allow users to circumvent the branch protection rules but they need to have already write access.
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.
Here is the API that allows you to create a status check context with the id: https://docs.github.com/en/graphql/reference/input-objects#requiredstatuscheckinput - the
appIdfield is used to specify a particular action that must already be in the database. Here is the output of searching the network role with the full requiredStatusCheck name + app information:Except for
DCOandCodeQL, every "app" is the generic github-actions "app". I guess I could change the input to reflect that. I'm assuming thedatabaseIdfield in the output is theappIdfield I should use in the input - the documentation doesn't really say. It's odd that "real" github actions like ansible_lint do not have their own appId - perhaps because we have custom code in there - for theansible-lintcheck we have some custom code to munge meta/main.yml role_name and namespace - foransible-testwe have code to convert to collection.Note that the key is the
context- this must exactly match the name of thejobe.g. https://github.com/linux-system-roles/.github/blob/main/playbooks/templates/.github/workflows/ansible-lint.yml#L12so as long as there is only 1 github action which defines
jobs.ansible_lint, we can be guaranteed that if we refer to a status check context name asansible_lintit will use the one defined in .github/workflows/ansible-lint.yml, which requires write access to the repo to change.Unless you can foresee some attack vector based on status check context naming, I would prefer to use status check context name as is being used currently in this PR.