Skip to content

Commit d2787e2

Browse files
authored
fix/auth (#7)
1 parent b1714f1 commit d2787e2

File tree

4 files changed

+44
-273
lines changed

4 files changed

+44
-273
lines changed

README.md

Lines changed: 17 additions & 256 deletions
Original file line numberDiff line numberDiff line change
@@ -1,298 +1,59 @@
11
# Terraform Provider KW GitHub
22

3-
[![Go Reference](https://pkg.go.dev/badge/github.com/knowledge-work/terraform-provider-kw-github.svg)](https://pkg.go.dev/github.com/knowledge-work/terraform-provider-kw-github)
4-
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
5-
63
A Terraform provider for managing GitHub repository rulesets with enhanced support for merge method configurations.
74

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-
215
## Installation
226

23-
### Using the Provider
24-
25-
To use this provider in your Terraform configuration:
26-
277
```hcl
288
terraform {
299
required_providers {
3010
kwgithub = {
31-
source = "knowledge-work/kwgithub"
32-
version = "~> 0.0.2"
11+
source = "knowledge-work/kw-github"
12+
version = "~> 0.0.4"
3313
}
3414
}
3515
}
3616
```
3717

38-
### Development Setup
18+
## Authentication
3919

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
6021

6122
```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"
6726
}
6827
```
6928

70-
## Usage
71-
72-
### Provider Configuration
29+
### GitHub App Installation
7330

7431
```hcl
75-
terraform {
76-
required_providers {
77-
kwgithub = {
78-
source = "knowledge-work/kwgithub"
79-
version = "~> 0.0.2"
80-
}
81-
}
82-
}
83-
8432
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 {}
9035
}
9136
```
9237

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`
10642

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
12044

12145
```hcl
12246
resource "kwgithub_ruleset_allowed_merge_methods" "example" {
12347
repository = "owner/repo"
12448
ruleset_id = "123456"
12549
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-
]
13450
}
13551
```
13652

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-
15453
## Why This Provider?
15554

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.
28956

29057
## License
29158

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

docs/index.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@ The provider supports two authentication methods:
1111
```terraform
1212
provider "kwgithub" {
1313
token = var.github_token
14+
owner = "knowledge-work"
1415
}
1516
```
1617

1718
### GitHub App Installation
1819

1920
```terraform
2021
provider "kwgithub" {
21-
app_auth {
22-
id = var.github_app_id
23-
installation_id = var.github_app_installation_id
24-
pem_file = var.github_app_pem_file
25-
}
22+
owner = "knowledge-work"
23+
app_auth {}
2624
}
2725
```
2826

@@ -31,18 +29,20 @@ provider "kwgithub" {
3129
### Arguments
3230

3331
* `token` - (Optional) GitHub personal access token. Can also be set via `GITHUB_TOKEN` environment variable.
32+
* `owner` - (Optional) GitHub owner name to manage. Can also be set via `GITHUB_OWNER` environment variable.
3433
* `github_base_url` - (Optional) GitHub base URL. Defaults to https://api.github.com. Can also be set via `GITHUB_BASE_URL` environment variable.
3534
* `app_auth` - (Optional) Configuration block to use GitHub App installation token. Conflicts with `token`.
36-
* `id` - (Required) GitHub App ID. Can also be set via `GITHUB_APP_ID` environment variable.
37-
* `installation_id` - (Required) GitHub App installation ID. Can also be set via `GITHUB_APP_INSTALLATION_ID` environment variable.
38-
* `pem_file` - (Required) GitHub App private key PEM file contents. Can also be set via `GITHUB_APP_PEM_FILE` environment variable.
35+
* `id` - (Optional) GitHub App ID. Can also be set via `GITHUB_APP_ID` environment variable.
36+
* `installation_id` - (Optional) GitHub App installation ID. Can also be set via `GITHUB_APP_INSTALLATION_ID` environment variable.
37+
* `pem_file` - (Optional) GitHub App private key PEM file contents. Can also be set via `GITHUB_APP_PEM_FILE` environment variable.
3938

4039
### Environment Variables
4140

4241
When using environment variables, an empty `app_auth` block is required to allow provider configurations from environment variables to be specified:
4342

4443
```terraform
4544
provider "kwgithub" {
45+
owner = "knowledge-work"
4646
app_auth {}
4747
}
4848
```

examples/provider/provider.tf

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
provider "kwgithub" {
22
token = var.github_token
3+
owner = "knowledge-work"
34
}
45

56
# GitHub App authentication example
67
provider "kwgithub" {
7-
app_auth {
8-
id = var.github_app_id
9-
installation_id = var.github_app_installation_id
10-
pem_file = var.github_app_pem_file
11-
}
8+
owner = "knowledge-work"
9+
app_auth {}
1210
}

0 commit comments

Comments
 (0)