Skip to content

Commit 1eaeaeb

Browse files
authored
[MAINT] Improve Import behaviour of github_default_branch (#3216)
* Refactor to use Context-aware functions Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Sort CRUD functions Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Updates naming and error handling Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Adds test cases for Update and Import functionality Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Refactor to use `tflog` Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Remove `Read` calls from `Update` and `Create` Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Update tests to use `ConfigStateChecks` Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Adds `repository_id` field Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Adds custom `Import` function Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Updates docs Signed-off-by: Timo Sand <timo.sand@f-secure.com> * `gofmt` Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Make `forcetypeassert` linter happy Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Rename branch only if it's different from current default branch Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Fix documentation error Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Add acc tests to verify Destroy behaviour Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Address review comments Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Use `errors.AsType` pattern Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Address review comments Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Add state migration and tests Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Update docs template to be mostly generated Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Ensure that ID gets updated if reponame changes Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Remove typeasserts from schema fields Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Address review comments Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Address rename Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Adopt `migrateRepositoryWithID` pattern Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Address review comments Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Add wait for branch rename Removing `etag` after rename made it visible that the GH API is eventually consistent when a branch is being renamed. Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Address review comments Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Simplify migration logic Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Address copilot comments Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Make waiting optional Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Add unit test to verify that waiting is skipped if not configured Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Update docs Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Fix linter Signed-off-by: Timo Sand <timo.sand@f-secure.com> --------- Signed-off-by: Timo Sand <timo.sand@f-secure.com>
1 parent 614a772 commit 1eaeaeb

12 files changed

Lines changed: 841 additions & 152 deletions

ARCHITECTURE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func resourceExampleRead(ctx context.Context, d *schema.ResourceData, m any) dia
9595
// Single API call to get all needed data
9696
resource, _, err := client.Resources.Get(ctx, owner, name)
9797
if err != nil {
98-
if ghErr, ok := errors.AsType[github.ErrorResponse](err); ok {
98+
if ghErr, ok := errors.AsType[*github.ErrorResponse](err); ok {
9999
if ghErr.Response.StatusCode == http.StatusNotFound {
100100
tflog.Info(ctx, "Removing resource from state because it no longer exists", map[string]any{"name": name})
101101
d.SetId("")
@@ -342,7 +342,7 @@ Handle 404s gracefully by removing from state:
342342
```go
343343
resource, _, err := client.Resources.Get(ctx, owner, name)
344344
if err != nil {
345-
if ghErr, ok := errors.AsType[github.ErrorResponse](err); ok {
345+
if ghErr, ok := errors.AsType[*github.ErrorResponse](err); ok {
346346
if ghErr.Response.StatusCode == http.StatusNotFound {
347347
tflog.Info(ctx, "Removing resource from state because it no longer exists", map[string]any{"name": name})
348348
d.SetId("")

docs/resources/branch_default.md

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
---
22
page_title: "github_branch_default (Resource) - GitHub"
33
description: |-
4-
Provides a GitHub branch default for a given repository.
4+
Configures the default branch for a GitHub repository.
55
---
66

77
# github_branch_default (Resource)
88

9-
Provides a GitHub branch default resource.
9+
Configures the default branch for a GitHub repository.
1010

11-
This resource allows you to set the default branch for a given repository.
12-
13-
Note that use of this resource is incompatible with the `default_branch` option of the `github_repository` resource. Using both will result in plans always showing a diff.
11+
~> This resource is incompatible with the `default_branch` option of the [`github_repository`](repository) resource. Using both will result in plans always showing a diff.
1412

1513
## Example Usage
1614

17-
Basic usage:
18-
1915
```terraform
16+
# Basic usage
17+
2018
resource "github_repository" "example" {
2119
name = "example"
2220
description = "My awesome codebase"
@@ -34,9 +32,9 @@ resource "github_branch_default" "default" {
3432
}
3533
```
3634

37-
Renaming to a branch that doesn't exist:
38-
3935
```terraform
36+
# Renaming to a branch that doesn't exist
37+
4038
resource "github_repository" "example" {
4139
name = "example"
4240
description = "My awesome codebase"
@@ -50,17 +48,39 @@ resource "github_branch_default" "default" {
5048
}
5149
```
5250

53-
## Argument Reference
51+
<!-- schema generated by tfplugindocs -->
52+
## Schema
53+
54+
### Required
55+
56+
- `branch` (String) The name of the branch to set as the default (e.g. 'main').
57+
- `repository` (String) The name of the GitHub repository.
58+
59+
### Optional
5460

55-
The following arguments are supported:
61+
- `etag` (String) The ETag header for the repository API response.
62+
- `rename` (Boolean) If `true` rename the existing branch when the `branch` input is changed. Defaults to 'false'.
63+
- `wait_for_rename` (Boolean) If `true`, poll until GitHub propagates the renamed default branch before proceeding. Only has effect when `rename` is also `true`. Defaults to 'false'.
5664

57-
- `repository` - (Required) The GitHub repository
58-
- `branch` - (Required) The branch (e.g. `main`)
59-
- `rename` - (Optional) Indicate if it should rename the branch rather than use an existing branch. Defaults to `false`.
65+
### Read-Only
66+
67+
- `id` (String) The ID of this resource.
68+
- `repository_id` (Number) The ID of the GitHub repository.
6069

6170
## Import
6271

63-
GitHub Branch Defaults can be imported using an ID made up of `repository`, e.g.
72+
Import is supported using the following syntax:
73+
74+
In Terraform v1.5.0 and later, the [`import` block](https://developer.hashicorp.com/terraform/language/import) can be used with the `id` attribute, for example:
75+
76+
```terraform
77+
import {
78+
to = github_branch_default.default
79+
id = "example"
80+
}
81+
```
82+
83+
The [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import) can be used, for example:
6484

6585
```shell
6686
terraform import github_branch_default.branch_default my-repo
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {
2+
to = github_branch_default.default
3+
id = "example"
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
terraform import github_branch_default.branch_default my-repo

examples/resources/branch_default/example_1.tf renamed to examples/resources/github_branch_default/resource_basic.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Basic usage
2+
13
resource "github_repository" "example" {
24
name = "example"
35
description = "My awesome codebase"

examples/resources/branch_default/example_2.tf renamed to examples/resources/github_branch_default/resource_rename_nonexistent_branch.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Renaming to a branch that doesn't exist
2+
13
resource "github_repository" "example" {
24
name = "example"
35
description = "My awesome codebase"

0 commit comments

Comments
 (0)