-
Notifications
You must be signed in to change notification settings - Fork 933
[FEAT]: Add Support for GitHub Enterprise Rulesets #3110
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
Ravio1i
wants to merge
33
commits into
integrations:main
Choose a base branch
from
siemens:feat/enterprise-rulesets
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
33 commits
Select commit
Hold shift + click to select a range
dc77805
feat: add functionality and tests for GitHub Enterprise rulesets
Ravio1i e251233
docs: GitHub enterprise ruleset data source and resource
Ravio1i 9d8b390
refactor: simplify logging messages for enterprise ruleset operations…
Ravio1i a30f1af
feat: add description to GitHub enterprise ruleset resource and updat…
Ravio1i 44cbb56
chore: improve error handling for setting attributes in GitHub enterp…
Ravio1i 2d03157
refactor: streamline test check composition in GitHub enterprise rule…
Ravio1i 09bc68d
fix: update conflicts handling for repository_name in GitHub enterpri…
Ravio1i 8d8bfea
feat: add repository target rules and update handling in GitHub enter…
Ravio1i ee91775
feat: add organization_id condition support to GitHub enterprise ruleset
Ravio1i e2a870f
feat: add merge queue and required deployments support to GitHub ente…
Ravio1i f0cdf67
feat: remove merge queue and required deployments from GitHub enterpr…
Ravio1i 6ea80ea
docs: add examples for GitHub Enterprise rulesets including branch, t…
Ravio1i 7aacee3
feat: add customization validation for enterprise ruleset configuration
Ravio1i a3787e9
docs: enhance enterprise ruleset examples with repository target supp…
Ravio1i fda8d21
feat: add import functionality for GitHub enterprise ruleset and upda…
Ravio1i 686da11
chore: upgrade go-github dependency to v82 and update context usage i…
Ravio1i 07a8a49
test: update test cases to use ProviderFactories instead of Providers…
Ravio1i d5160d7
test: update flattenConditions tests to use context for organization_id
Ravio1i 19f69db
feat: enhance enterprise ruleset schema with repository property cond…
Ravio1i 59d8167
feat: add repository_property support in conditions validation and ex…
Ravio1i 87f0adf
feat: add example branch ruleset with repository property conditions
Ravio1i 08cc9aa
fix(enterprise_ruleset): add description to GitHub enterprise ruleset…
Ravio1i cb73bdb
refactor(enterprise_ruleset): streamline ruleset target types and imp…
Ravio1i 3e7d102
refactor(ruleset): simplify validation logic and enhance conditions r…
Ravio1i 3ac4706
test(enterprise_ruleset): add test for conflicting repository conditi…
Ravio1i e31eb8f
refactor(enterprise_ruleset): update validation functions to use Vali…
Ravio1i dcdea85
refactor(enterprise_ruleset): simplify resource creation by using poi…
Ravio1i 21d8823
docs(enterprise_ruleset): clarify comment for repository target rules…
Ravio1i ac5cbe3
test(enterprise_ruleset): Replace deprecated Check/ComposeTestCheckFu…
Ravio1i 12f7413
refactor(enterprise_ruleset): update ruleset ID handling in resource …
Ravio1i 4fa9e60
refactor(enterprise_ruleset): improve ruleset ID handling and enforce…
Ravio1i 2a59f50
refactor(ruleset): standardize ruleset handling across repository, or…
Ravio1i b613648
test(enterprise_ruleset): restructure acceptance tests into subtests …
Ravio1i 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # GitHub Enterprise Ruleset Examples | ||
|
|
||
| This directory demonstrates how to configure GitHub Enterprise rulesets using the Terraform GitHub provider. | ||
|
|
||
| ## Overview | ||
|
|
||
| Enterprise rulesets allow you to enforce policies across all organizations in your GitHub Enterprise. The examples showcase all four target types: | ||
|
|
||
| - **Branch Target** (`branch_target.tf`) - Branch protection rules with PR requirements, status checks, and commit patterns | ||
| - **Tag Target** (`tag_target.tf`) - Tag protection rules with naming patterns and immutability controls | ||
| - **Push Target** (`push_target.tf`) - File restrictions, size limits, and content policies (beta feature) | ||
| - **Repository Target** (`rulesets.tf`) - Repository management rules for creation, deletion, and naming conventions | ||
|
|
||
| ## Requirements | ||
|
|
||
| - GitHub Enterprise Cloud account | ||
| - Personal access token with enterprise admin permissions | ||
| - Terraform >= 0.14 | ||
|
|
||
| ## Usage | ||
|
|
||
| 1. Set your environment variables: | ||
|
|
||
| ```bash | ||
| export TF_VAR_github_token="your_github_token" | ||
| export TF_VAR_enterprise_slug="your-enterprise-slug" | ||
| ``` | ||
|
|
||
| 2. Customize the examples by replacing `"your-enterprise"` with your actual enterprise slug | ||
|
|
||
| 3. Apply the configuration: | ||
|
|
||
| ```bash | ||
| terraform init | ||
| terraform plan | ||
| terraform apply | ||
| ``` | ||
|
|
||
| ## Target Types | ||
|
|
||
| Each target type supports different rules: | ||
|
|
||
| - **Branch/Tag**: creation, deletion, update, signatures, linear history, PR requirements, status checks | ||
| - **Push**: file restrictions, size limits, file extensions, commit patterns | ||
| - **Repository**: creation, deletion, transfer, naming patterns, visibility controls | ||
|
|
||
| See the individual `.tf` files for detailed examples and available rules. | ||
|
|
||
| ## Important Notes | ||
|
|
||
| - All enterprise rulesets require organization and repository targeting via `conditions` | ||
| - The `push` target is currently in beta and subject to change | ||
| - Branch and tag targets require `ref_name` conditions | ||
| - Repository and push targets do not use `ref_name` conditions |
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,174 @@ | ||
| # Example: Branch target ruleset with comprehensive branch protection rules | ||
| # This ruleset applies to branches across the enterprise | ||
|
|
||
| resource "github_enterprise_ruleset" "branch_protection" { | ||
| enterprise_slug = "your-enterprise" | ||
| name = "branch-protection-ruleset" | ||
| target = "branch" | ||
| enforcement = "active" | ||
|
|
||
| # Optional: Allow certain users/teams to bypass the ruleset | ||
| bypass_actors { | ||
| actor_id = 1 | ||
| actor_type = "OrganizationAdmin" | ||
| bypass_mode = "always" | ||
| } | ||
|
|
||
| bypass_actors { | ||
| actor_type = "DeployKey" | ||
| bypass_mode = "always" | ||
| } | ||
|
|
||
| # Conditions define which organizations, repositories, and refs this ruleset applies to | ||
| conditions { | ||
| # Target all organizations in the enterprise | ||
| organization_name { | ||
| include = ["~ALL"] | ||
| exclude = [] | ||
| } | ||
|
|
||
| # Target all repositories | ||
| repository_name { | ||
| include = ["~ALL"] | ||
| exclude = ["test-*"] # Exclude test repositories | ||
| } | ||
|
|
||
| # Target all branches (required for branch target) | ||
| ref_name { | ||
| include = ["~DEFAULT_BRANCH", "main", "master", "release/*"] | ||
| exclude = ["experimental/*"] | ||
| } | ||
| } | ||
|
|
||
| # Rules that apply to matching branches | ||
| rules { | ||
| # Prevent branch creation without bypass permission | ||
| creation = true | ||
|
|
||
| # Prevent branch updates without bypass permission | ||
| update = false | ||
|
|
||
| # Prevent branch deletion without bypass permission | ||
| deletion = true | ||
|
|
||
| # Require linear history (no merge commits) | ||
| required_linear_history = true | ||
|
|
||
| # Require signed commits | ||
| required_signatures = true | ||
|
|
||
| # Prevent force pushes | ||
| non_fast_forward = true | ||
|
|
||
| # Pull request requirements | ||
| pull_request { | ||
| dismiss_stale_reviews_on_push = true | ||
| require_code_owner_review = true | ||
| require_last_push_approval = true | ||
| required_approving_review_count = 2 | ||
| required_review_thread_resolution = true | ||
| allowed_merge_methods = ["squash", "merge"] | ||
| } | ||
|
|
||
| # Status check requirements | ||
| required_status_checks { | ||
| strict_required_status_checks_policy = true | ||
| do_not_enforce_on_create = false | ||
|
|
||
| required_check { | ||
| context = "ci/build" | ||
| integration_id = 0 | ||
| } | ||
|
|
||
| required_check { | ||
| context = "ci/test" | ||
| integration_id = 0 | ||
| } | ||
| } | ||
|
|
||
| # Commit message pattern requirements | ||
| commit_message_pattern { | ||
| name = "Conventional Commits" | ||
| operator = "regex" | ||
| pattern = "^(feat|fix|docs|style|refactor|test|chore)(\\(.+\\))?: .{1,50}" | ||
| negate = false | ||
| } | ||
|
|
||
| # Commit author email pattern | ||
| commit_author_email_pattern { | ||
| name = "Corporate Email Only" | ||
| operator = "regex" | ||
| pattern = "@your-company\\.com$" | ||
| negate = false | ||
| } | ||
|
|
||
| # Committer email pattern | ||
| committer_email_pattern { | ||
| name = "Corporate Email Only" | ||
| operator = "regex" | ||
| pattern = "@your-company\\.com$" | ||
| negate = false | ||
| } | ||
|
|
||
| # Branch name pattern (only for branch target) | ||
| branch_name_pattern { | ||
| name = "Valid Branch Names" | ||
| operator = "regex" | ||
| pattern = "^(main|master|develop|feature/|bugfix/|hotfix/|release/)" | ||
| negate = false | ||
| } | ||
|
|
||
| # Code scanning requirements | ||
| required_code_scanning { | ||
| required_code_scanning_tool { | ||
| tool = "CodeQL" | ||
| alerts_threshold = "errors" | ||
| security_alerts_threshold = "high_or_higher" | ||
| } | ||
| } | ||
|
|
||
| # Copilot code review (if enabled) | ||
| copilot_code_review { | ||
| review_on_push = true | ||
| review_draft_pull_requests = false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| resource "github_enterprise_ruleset" "branch_by_property" { | ||
| enterprise_slug = "your-enterprise" | ||
| name = "production-repos-branch-protection" | ||
| target = "branch" | ||
| enforcement = "active" | ||
|
|
||
| conditions { | ||
| organization_name { | ||
| include = ["~ALL"] | ||
| exclude = [] | ||
| } | ||
|
|
||
| # Target repositories based on custom properties | ||
| repository_property { | ||
| include { | ||
| name = "environment" | ||
| property_values = ["production", "staging"] | ||
| source = "custom" | ||
| } | ||
|
|
||
| exclude { | ||
| name = "lifecycle" | ||
| property_values = ["deprecated", "archived"] | ||
| } | ||
| } | ||
|
|
||
| ref_name { | ||
| include = ["~DEFAULT_BRANCH", "refs/heads/release/*"] | ||
| exclude = [] | ||
| } | ||
| } | ||
|
|
||
| rules { | ||
| deletion = true | ||
| non_fast_forward = 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| terraform { | ||
| required_providers { | ||
| github = { | ||
| source = "integrations/github" | ||
| version = "~> 6.0" | ||
| } | ||
| } | ||
| } |
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,154 @@ | ||
| # Example: Push target ruleset for file and content restrictions | ||
| # This ruleset applies to all pushes across the enterprise | ||
|
|
||
| resource "github_enterprise_ruleset" "push_restrictions" { | ||
| enterprise_slug = "your-enterprise" | ||
| name = "push-restrictions-ruleset" | ||
| target = "push" | ||
| enforcement = "active" | ||
|
|
||
| # Allow deploy keys and organization admins to bypass | ||
| bypass_actors { | ||
| actor_type = "DeployKey" | ||
| bypass_mode = "always" | ||
| } | ||
|
|
||
| bypass_actors { | ||
| actor_id = 1 | ||
| actor_type = "OrganizationAdmin" | ||
| bypass_mode = "always" | ||
| } | ||
|
|
||
| # Conditions define which organizations and repositories this ruleset applies to | ||
| # Note: ref_name is NOT used for push target | ||
| conditions { | ||
| # Target all organizations | ||
| organization_name { | ||
| include = ["~ALL"] | ||
| exclude = [] | ||
| } | ||
|
|
||
| # Target all repositories | ||
| repository_name { | ||
| include = ["~ALL"] | ||
| exclude = ["sandbox-*"] | ||
| } | ||
| } | ||
|
|
||
| # Rules that apply to all pushes | ||
| rules { | ||
| # Restrict specific file paths from being pushed | ||
| file_path_restriction { | ||
| restricted_file_paths = [ | ||
| "secrets.txt", | ||
| "*.key", | ||
| "*.pem", | ||
| ".env", | ||
| "credentials/*" | ||
| ] | ||
| } | ||
|
|
||
| # Limit maximum file size to prevent large files | ||
| max_file_size { | ||
| max_file_size = 100 # Max 100 MB | ||
| } | ||
|
|
||
| # Limit maximum file path length | ||
| max_file_path_length { | ||
| max_file_path_length = 255 | ||
| } | ||
|
|
||
| # Restrict specific file extensions | ||
| file_extension_restriction { | ||
| restricted_file_extensions = [ | ||
| "*.exe", | ||
| "*.dll", | ||
| "*.so", | ||
| "*.dylib", | ||
| "*.zip", | ||
| "*.tar.gz" | ||
| ] | ||
| } | ||
|
|
||
| # Commit message pattern | ||
| commit_message_pattern { | ||
| name = "Valid Commit Message" | ||
| operator = "regex" | ||
| pattern = "^(feat|fix|docs|style|refactor|test|chore)(\\(.+\\))?: .+" | ||
| negate = false | ||
| } | ||
|
|
||
| # Commit author email pattern | ||
| commit_author_email_pattern { | ||
| name = "Corporate Email" | ||
| operator = "ends_with" | ||
| pattern = "@your-company.com" | ||
| negate = false | ||
| } | ||
|
|
||
| # Committer email pattern | ||
| committer_email_pattern { | ||
| name = "Corporate Email" | ||
| operator = "ends_with" | ||
| pattern = "@your-company.com" | ||
| negate = false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Example: Security-focused push ruleset | ||
| resource "github_enterprise_ruleset" "security_push_restrictions" { | ||
| enterprise_slug = "your-enterprise" | ||
| name = "security-push-restrictions" | ||
| target = "push" | ||
| enforcement = "active" | ||
|
|
||
| conditions { | ||
| organization_name { | ||
| include = ["~ALL"] | ||
| exclude = [] | ||
| } | ||
|
|
||
| repository_name { | ||
| include = ["*-prod", "*-production"] | ||
| exclude = [] | ||
| } | ||
| } | ||
|
|
||
| rules { | ||
| # Block common secret file patterns | ||
| file_path_restriction { | ||
| restricted_file_paths = [ | ||
| "*.pem", | ||
| "*.key", | ||
| "*.cert", | ||
| "*.p12", | ||
| "*.pfx", | ||
| ".env", | ||
| ".env.*", | ||
| "secrets.yml", | ||
| "credentials.json" | ||
| ] | ||
| } | ||
|
|
||
| # Strict file size limits for production | ||
| max_file_size { | ||
| max_file_size = 50 # Max 50 MB | ||
| } | ||
|
|
||
| # Block executable and archive files | ||
| file_extension_restriction { | ||
| restricted_file_extensions = [ | ||
| "*.exe", | ||
| "*.dll", | ||
| "*.so", | ||
| "*.dylib", | ||
| "*.bin", | ||
| "*.dmg" | ||
| ] | ||
| } | ||
|
|
||
| # Require signed commits | ||
| required_signatures = true | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.