|
| 1 | +# Manages configuration for this repository. |
| 2 | + |
| 3 | +variable "github_owner" { |
| 4 | + default = "kmontag" |
| 5 | +} |
| 6 | + |
| 7 | +variable "github_repository_name" { |
| 8 | + default = "modeStep" |
| 9 | +} |
| 10 | + |
| 11 | +provider "github" { |
| 12 | + # Owner for e.g. repository resources. |
| 13 | + owner = var.github_owner |
| 14 | +} |
| 15 | + |
| 16 | +resource "github_repository" "default" { |
| 17 | + name = var.github_repository_name |
| 18 | + visibility = "public" |
| 19 | + |
| 20 | + description = "Ableton Live 12 control surface for the SoftStep 2" |
| 21 | + |
| 22 | + vulnerability_alerts = true |
| 23 | + |
| 24 | + # Suggest updating PR branches. |
| 25 | + allow_update_branch = true |
| 26 | + |
| 27 | + # Don't allow merge commits from PRs (they should be squashed or rebased instead). |
| 28 | + allow_merge_commit = false |
| 29 | + |
| 30 | + # Allow squash merges and use the PR body as the default commit content. |
| 31 | + allow_squash_merge = true |
| 32 | + squash_merge_commit_title = "PR_TITLE" |
| 33 | + squash_merge_commit_message = "PR_BODY" |
| 34 | + |
| 35 | + # Clean up branches after merge. |
| 36 | + delete_branch_on_merge = true |
| 37 | + |
| 38 | + has_downloads = true |
| 39 | + has_issues = true |
| 40 | + has_projects = false |
| 41 | + has_wiki = false |
| 42 | +} |
| 43 | + |
| 44 | +data "github_rest_api" "rulesets" { |
| 45 | + endpoint = "/repos/${var.github_owner}/${github_repository.default.name}/rulesets" |
| 46 | + |
| 47 | + lifecycle { |
| 48 | + postcondition { |
| 49 | + condition = self.code == 200 |
| 50 | + error_message = "Expected status code 200, but got ${self.code}" |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +locals { |
| 56 | + # Array containing entries like: |
| 57 | + # |
| 58 | + # {"id": 12345, "name": "some name", ...}. |
| 59 | + # |
| 60 | + rulesets = jsondecode(data.github_rest_api.rulesets.body) |
| 61 | + |
| 62 | + # Get the existing main ruleset ID. This will be used to import the ruleset resource. |
| 63 | + # |
| 64 | + # If the ruleset ever gets deleted for some reason, this will be `null`, and the associated import |
| 65 | + # block can simply be commented out temporarily. |
| 66 | + main_ruleset_name = "main" |
| 67 | + main_ruleset_id = one([for ruleset in local.rulesets : ruleset.id if ruleset.name == local.main_ruleset_name]) |
| 68 | +} |
| 69 | + |
| 70 | +resource "github_repository_ruleset" "main" { |
| 71 | + name = local.main_ruleset_name |
| 72 | + repository = github_repository.default.name |
| 73 | + target = "branch" |
| 74 | + enforcement = "active" |
| 75 | + |
| 76 | + conditions { |
| 77 | + ref_name { |
| 78 | + include = ["~DEFAULT_BRANCH"] |
| 79 | + exclude = [] |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + bypass_actors { |
| 84 | + actor_type = "RepositoryRole" |
| 85 | + |
| 86 | + # Allow repository admins to manually bypass checks in PRs. |
| 87 | + # |
| 88 | + # Actor IDs by role: maintain -> 2, write -> 4, admin -> 5. |
| 89 | + # |
| 90 | + # See |
| 91 | + # https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_ruleset#RepositoryRole-1. |
| 92 | + actor_id = 5 |
| 93 | + |
| 94 | + # Don't be too strict about required checks. Allow bypass actors to bypass them: |
| 95 | + # |
| 96 | + # - when merging pull requests (requires manual confirmation on the PR page) |
| 97 | + # |
| 98 | + # - when pushing directly to main (bypass happens automatically, though a warning will be |
| 99 | + # printed during `git push`) |
| 100 | + bypass_mode = "always" |
| 101 | + } |
| 102 | + |
| 103 | + rules { |
| 104 | + # Require bypass permission to create/delete the default branch. |
| 105 | + creation = true |
| 106 | + deletion = true |
| 107 | + |
| 108 | + # Don't allow merge commits. |
| 109 | + required_linear_history = true |
| 110 | + |
| 111 | + # Prevent force-pushes to the default branch. |
| 112 | + non_fast_forward = true |
| 113 | + |
| 114 | + # Require status checks to pass before merging PRs. |
| 115 | + required_status_checks { |
| 116 | + # Require checks to pass with the latest code. |
| 117 | + strict_required_status_checks_policy = true |
| 118 | + |
| 119 | + required_check { |
| 120 | + context = "lint" |
| 121 | + } |
| 122 | + |
| 123 | + required_check { |
| 124 | + context = "check-types" |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +# Import statements allowing the entire workspace to be imported. If re-creating |
| 131 | +# resources from scratch, some or all of these will need to be commented out. |
| 132 | +import { |
| 133 | + to = github_repository.default |
| 134 | + id = var.github_repository_name |
| 135 | +} |
| 136 | + |
| 137 | +import { |
| 138 | + to = github_repository_ruleset.main |
| 139 | + id = "${github_repository.default.name}:${local.main_ruleset_id}" |
| 140 | +} |
0 commit comments