|
1 | 1 | # Terraform Provider KW GitHub
|
2 | 2 |
|
3 |
| -[](https://pkg.go.dev/github.com/knowledge-work/terraform-provider-kw-github) |
4 |
| -[](https://opensource.org/licenses/Apache-2.0) |
5 |
| - |
6 | 3 | A Terraform provider for managing GitHub repository rulesets with enhanced support for merge method configurations.
|
7 | 4 |
|
8 |
| -## Features |
9 |
| - |
10 |
| -- **Merge Methods Management**: Configure allowed merge methods for GitHub repository rulesets |
11 |
| -- **Auto-Recovery**: Automatically detects and restores merge methods when they are reset by GitHub |
12 |
| -- **Dependency Handling**: Properly handles dependencies with other ruleset resources |
13 |
| -- **Force Update**: Manual trigger for updates when dependent resources change |
14 |
| - |
15 |
| -## Requirements |
16 |
| - |
17 |
| -- [Terraform](https://www.terraform.io/downloads.html) >= 1.0 |
18 |
| -- [Go](https://golang.org/doc/install) >= 1.23.0 (for development) |
19 |
| -- GitHub Personal Access Token with appropriate permissions |
20 |
| - |
21 | 5 | ## Installation
|
22 | 6 |
|
23 |
| -### Using the Provider |
24 |
| - |
25 |
| -To use this provider in your Terraform configuration: |
26 |
| - |
27 | 7 | ```hcl
|
28 | 8 | terraform {
|
29 | 9 | required_providers {
|
30 | 10 | kwgithub = {
|
31 |
| - source = "knowledge-work/kwgithub" |
32 |
| - version = "~> 0.0.2" |
| 11 | + source = "knowledge-work/kw-github" |
| 12 | + version = "~> 0.0.4" |
33 | 13 | }
|
34 | 14 | }
|
35 | 15 | }
|
36 | 16 | ```
|
37 | 17 |
|
38 |
| -### Development Setup |
| 18 | +## Authentication |
39 | 19 |
|
40 |
| -1. Clone the repository: |
41 |
| - |
42 |
| -```bash |
43 |
| -git clone https://github.com/knowledge-work/terraform-provider-kw-github.git |
44 |
| -cd terraform-provider-kw-github |
45 |
| -``` |
46 |
| - |
47 |
| -2. Build the provider: |
48 |
| - |
49 |
| -```bash |
50 |
| -go build -o terraform-provider-kw-github ./cmd/terraform-provider-kwgithub |
51 |
| -``` |
52 |
| - |
53 |
| -3. Install the provider locally: |
54 |
| - |
55 |
| -```bash |
56 |
| -make install |
57 |
| -``` |
58 |
| - |
59 |
| -Or use the development override in your `~/.terraformrc`: |
| 20 | +### Personal Access Token |
60 | 21 |
|
61 | 22 | ```hcl
|
62 |
| -provider_installation { |
63 |
| - dev_overrides { |
64 |
| - "knowledge-work/kwgithub" = "/path/to/your/terraform-provider-kw-github" |
65 |
| - } |
66 |
| - direct {} |
| 23 | +provider "kwgithub" { |
| 24 | + token = var.github_token |
| 25 | + owner = "knowledge-work" |
67 | 26 | }
|
68 | 27 | ```
|
69 | 28 |
|
70 |
| -## Usage |
71 |
| - |
72 |
| -### Provider Configuration |
| 29 | +### GitHub App Installation |
73 | 30 |
|
74 | 31 | ```hcl
|
75 |
| -terraform { |
76 |
| - required_providers { |
77 |
| - kwgithub = { |
78 |
| - source = "knowledge-work/kwgithub" |
79 |
| - version = "~> 0.0.2" |
80 |
| - } |
81 |
| - } |
82 |
| -} |
83 |
| -
|
84 | 32 | provider "kwgithub" {
|
85 |
| - # GitHub token (optional, can also use GITHUB_TOKEN environment variable) |
86 |
| - token = var.github_token |
87 |
| -
|
88 |
| - # GitHub base URL (optional, defaults to https://api.github.com) |
89 |
| - github_base_url = "https://api.github.com" |
| 33 | + owner = "knowledge-work" |
| 34 | + app_auth {} |
90 | 35 | }
|
91 | 36 | ```
|
92 | 37 |
|
93 |
| -### Authentication |
94 |
| - |
95 |
| -The provider supports two authentication methods: |
96 |
| - |
97 |
| -1. **Provider Configuration** (Recommended): |
98 |
| - |
99 |
| - ```hcl |
100 |
| - provider "kwgithub" { |
101 |
| - token = var.github_token |
102 |
| - } |
103 |
| - ``` |
104 |
| - |
105 |
| -2. **Environment Variable**: |
| 38 | +Set environment variables: |
| 39 | +- `GITHUB_APP_ID` |
| 40 | +- `GITHUB_APP_INSTALLATION_ID` |
| 41 | +- `GITHUB_APP_PEM_FILE` |
106 | 42 |
|
107 |
| - ```bash |
108 |
| - export GITHUB_TOKEN="your_github_token" |
109 |
| - ``` |
110 |
| - |
111 |
| -The provider will use the `token` attribute if provided, otherwise it will fall back to the `GITHUB_TOKEN` environment variable. |
112 |
| - |
113 |
| -## Resources |
114 |
| - |
115 |
| -### `kwgithub_ruleset_allowed_merge_methods` |
116 |
| - |
117 |
| -Manages allowed merge methods for a GitHub repository ruleset. |
118 |
| - |
119 |
| -#### Example Usage |
| 43 | +## Usage |
120 | 44 |
|
121 | 45 | ```hcl
|
122 | 46 | resource "kwgithub_ruleset_allowed_merge_methods" "example" {
|
123 | 47 | repository = "owner/repo"
|
124 | 48 | ruleset_id = "123456"
|
125 | 49 | allowed_merge_methods = ["merge", "squash"]
|
126 |
| -
|
127 |
| - # Optional: Force update when dependent resources change |
128 |
| - force_update = timestamp() |
129 |
| -
|
130 |
| - # Ensure this runs after other ruleset changes |
131 |
| - depends_on = [ |
132 |
| - kwgithub_other_ruleset_resource.example |
133 |
| - ] |
134 | 50 | }
|
135 | 51 | ```
|
136 | 52 |
|
137 |
| -#### Arguments |
138 |
| - |
139 |
| -- `repository` (Required) - Repository in the format "owner/repo" |
140 |
| -- `ruleset_id` (Required) - ID of the GitHub ruleset |
141 |
| -- `allowed_merge_methods` (Required) - Set of allowed merge methods. Valid values: `"merge"`, `"squash"`, `"rebase"` |
142 |
| -- `force_update` (Optional) - Timestamp or value to force update when dependent resources change |
143 |
| - |
144 |
| -#### Attributes |
145 |
| - |
146 |
| -- `id` - Resource identifier in the format "owner/repo:ruleset_id" |
147 |
| - |
148 |
| -#### Import |
149 |
| - |
150 |
| -```bash |
151 |
| -terraform import kwgithub_ruleset_allowed_merge_methods.example owner/repo:123456 |
152 |
| -``` |
153 |
| - |
154 | 53 | ## Why This Provider?
|
155 | 54 |
|
156 |
| -While the official GitHub Terraform provider (`integrations/github`) supports repository rulesets via `github_repository_ruleset`, it has a limitation with merge method configurations: |
157 |
| - |
158 |
| -**The Problem**: When you update other rules in a GitHub ruleset (like pull request rules, status checks, etc.), GitHub's API resets the `allowed_merge_methods` configuration to its default values. This causes Terraform state drift and unexpected behavior. |
159 |
| - |
160 |
| -**Our Solution**: This specialized provider: |
161 |
| - |
162 |
| -- **Detects** when merge methods have been reset by GitHub |
163 |
| -- **Automatically restores** the expected configuration during the next Terraform run |
164 |
| -- **Provides force update mechanism** to handle dependency changes |
165 |
| -- **Works alongside** the official GitHub provider |
166 |
| - |
167 |
| -## Use Cases |
168 |
| - |
169 |
| -1. **Complement Official Provider**: Use `github_repository_ruleset` for main ruleset configuration and `kwgithub_ruleset_allowed_merge_methods` for reliable merge method management |
170 |
| -2. **Existing Rulesets**: Manage merge methods for rulesets created outside of Terraform |
171 |
| -3. **Complex Dependencies**: Handle scenarios where ruleset updates from other sources affect merge methods |
172 |
| - |
173 |
| -## Examples |
174 |
| - |
175 |
| -```hcl |
176 |
| -terraform { |
177 |
| - required_providers { |
178 |
| - github = { |
179 |
| - source = "integrations/github" |
180 |
| - version = "~> 6.0" |
181 |
| - } |
182 |
| - kwgithub = { |
183 |
| - source = "knowledge-work/kwgithub" |
184 |
| - version = "~> 0.0.2" |
185 |
| - } |
186 |
| - } |
187 |
| -} |
188 |
| -
|
189 |
| -provider "github" { |
190 |
| - token = var.github_token |
191 |
| -} |
192 |
| -
|
193 |
| -provider "kwgithub" { |
194 |
| - token = var.github_token |
195 |
| -} |
196 |
| -
|
197 |
| -# Create ruleset with GitHub official provider |
198 |
| -resource "github_repository_ruleset" "main" { |
199 |
| - repository = "myorg/myrepo" |
200 |
| - name = "Main Branch Protection" |
201 |
| - target = "branch" |
202 |
| - enforcement = "active" |
203 |
| -
|
204 |
| - conditions { |
205 |
| - ref_name { |
206 |
| - include = ["refs/heads/main"] |
207 |
| - exclude = [] |
208 |
| - } |
209 |
| - } |
210 |
| -
|
211 |
| - rules { |
212 |
| - pull_request { |
213 |
| - required_approving_review_count = 2 |
214 |
| - dismiss_stale_reviews_on_push = true |
215 |
| - require_code_owner_review = true |
216 |
| - } |
217 |
| - required_status_checks { |
218 |
| - required_check { |
219 |
| - context = "ci/tests" |
220 |
| - } |
221 |
| - } |
222 |
| - } |
223 |
| -} |
224 |
| -
|
225 |
| -# Manage merge methods with our specialized provider |
226 |
| -resource "kwgithub_ruleset_allowed_merge_methods" "main" { |
227 |
| - repository = "myorg/myrepo" |
228 |
| - ruleset_id = github_repository_ruleset.main.id |
229 |
| - allowed_merge_methods = ["merge", "squash"] |
230 |
| -
|
231 |
| - # Option 1: Use etag for specific dependency tracking |
232 |
| - force_update = github_repository_ruleset.main.etag |
233 |
| -
|
234 |
| - # Option 2: Use timestamp for general force updates |
235 |
| - # force_update = timestamp() |
236 |
| -
|
237 |
| - depends_on = [ |
238 |
| - github_repository_ruleset.main |
239 |
| - ] |
240 |
| -} |
241 |
| -``` |
242 |
| - |
243 |
| -#### Force Update Options |
244 |
| - |
245 |
| -- **`github_repository_ruleset.main.etag`**: Recommended when you want updates triggered only when the specific ruleset changes |
246 |
| -- **`timestamp()`**: Use when you want to force updates regardless of specific dependencies |
247 |
| - |
248 |
| -## Development |
249 |
| - |
250 |
| -### Building from Source |
251 |
| - |
252 |
| -```bash |
253 |
| -make build |
254 |
| -# Or manually: |
255 |
| -go build -o terraform-provider-kw-github ./cmd/terraform-provider-kwgithub |
256 |
| -``` |
257 |
| - |
258 |
| -### Running Tests |
259 |
| - |
260 |
| -```bash |
261 |
| -# Unit tests |
262 |
| -make test |
263 |
| - |
264 |
| -# Acceptance tests (requires GITHUB_TOKEN) |
265 |
| -make test-acc |
266 |
| -``` |
267 |
| - |
268 |
| -### Code Quality |
269 |
| - |
270 |
| -```bash |
271 |
| -# Format and lint code |
272 |
| -make lint |
273 |
| - |
274 |
| -# Or run individual commands: |
275 |
| -make fmt # Format code |
276 |
| -make vet # Run go vet |
277 |
| -make tidy # Tidy dependencies |
278 |
| -``` |
279 |
| - |
280 |
| -## Contributing |
281 |
| - |
282 |
| -Contributions are welcome! Please feel free to submit a Pull Request. |
283 |
| - |
284 |
| -1. Fork the repository |
285 |
| -2. Create your feature branch (`git checkout -b feature/amazing-feature`) |
286 |
| -3. Commit your changes (`git commit -m 'Add some amazing feature'`) |
287 |
| -4. Push to the branch (`git push origin feature/amazing-feature`) |
288 |
| -5. Open a Pull Request |
| 55 | +The official GitHub provider resets `allowed_merge_methods` when updating other ruleset rules. This provider automatically detects and restores the expected configuration. |
289 | 56 |
|
290 | 57 | ## License
|
291 | 58 |
|
292 |
| -This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details. |
293 |
| - |
294 |
| -## Acknowledgments |
295 |
| - |
296 |
| -- [Terraform Plugin Framework](https://github.com/hashicorp/terraform-plugin-framework) for the provider development framework |
297 |
| -- [go-github](https://github.com/google/go-github) for GitHub API client |
298 |
| -- The Terraform community for inspiration and best practices |
| 59 | +Apache License 2.0 |
0 commit comments