Skip to content

Commit 9c162ad

Browse files
committed
Merge branch 'main' into refactor/migrate-resource-github-issue-label-to-tflog-and-context
2 parents 5a652b1 + cd39775 commit 9c162ad

8 files changed

Lines changed: 524 additions & 93 deletions

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ linters:
1212
- errcheck
1313
- errname
1414
- errorlint
15-
# - forcetypeassert TODO: Re-enable when we can fix the issues
15+
# - forcetypeassert # TODO: Re-enable when we can fix the issues
1616
- godot
1717
- govet
1818
- ineffassign

docs/resources/repository_collaborators.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ Teams will be added to the repository on apply, and removed if removed from the
2525

2626
## Personal Repositories
2727

28-
For personal repositories, collaborators can only be granted [write](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/repository-access-and-collaboration/permission-levels-for-a-personal-account-repository#collaborator-access-for-a-repository-owned-by-a-personal-account) permission.
29-
30-
!> If the repository owner is not added as a collaborator with admin access, the provider will churn this resource on every plan/apply. To prevent this, ensure that the repository owner is included in the set of user collaborators.
28+
For personal repositories, non-owner collaborators can only be granted [write](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/repository-access-and-collaboration/permission-levels-for-a-personal-account-repository#collaborator-access-for-a-repository-owned-by-a-personal-account) permission. Owners will be ignored unless they are explicitly added, in which case they must be granted `admin` permission.
3129

3230
## Users
3331

@@ -86,6 +84,7 @@ resource "github_repository_collaborators" "some_repo_collaborators" {
8684

8785
- `id` (String) The ID of this resource.
8886
- `invitation_ids` (Map of String) Map of usernames to invitation ID for users that haven't yet accepted their invitation to become a collaborator. This is only set on read, and is used internally to track pending invitations for users that aren't yet collaborators.
87+
- `owner_configured` (Boolean) Indicates whether the owner of a personal repository is configured as a collaborator.
8988
- `repository_id` (Number) ID of the repository.
9089

9190
<a id="nestedblock--ignore_team"></a>

github/resource_github_issue_label.go

Lines changed: 45 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313

1414
func resourceGithubIssueLabel() *schema.Resource {
1515
return &schema.Resource{
16-
CreateContext: resourceGithubIssueLabelCreateOrUpdate,
16+
CreateContext: resourceGithubIssueLabelCreate,
1717
ReadContext: resourceGithubIssueLabelRead,
18-
UpdateContext: resourceGithubIssueLabelCreateOrUpdate,
18+
UpdateContext: resourceGithubIssueLabelUpdate,
1919
DeleteContext: resourceGithubIssueLabelDelete,
2020
Importer: &schema.ResourceImporter{
2121
StateContext: schema.ImportStatePassthroughContext,
@@ -61,17 +61,15 @@ func resourceGithubIssueLabel() *schema.Resource {
6161
}
6262
}
6363

64-
// resourceGithubIssueLabelCreateOrUpdate idempotently creates or updates an
65-
// issue label. Issue labels are keyed off of their "name", so pre-existing
66-
// issue labels result in a 422 HTTP error if they exist outside of Terraform.
67-
// Normally this would not be an issue, except new repositories are created with
68-
// a "default" set of labels, and those labels easily conflict with custom ones.
64+
// resourceGithubIssueLabelCreate idempotently creates an issue label.
65+
// Issue labels are keyed off of their "name", so pre-existing issue labels
66+
// result in a 422 HTTP error if they exist outside of Terraform. Normally this
67+
// would not be an issue, except new repositories are created with a "default"
68+
// set of labels, and those labels can conflict with custom ones.
6969
//
70-
// This function will first check if the label exists, and then issue an update,
71-
// otherwise it will create. This is also advantageous in that we get to use the
72-
// same function for two schema funcs.
73-
74-
func resourceGithubIssueLabelCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
70+
// This function first checks if the label exists and updates it if present;
71+
// otherwise it creates a new label.
72+
func resourceGithubIssueLabelCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
7573
client := meta.(*Owner).v3client
7674
orgName := meta.(*Owner).name
7775
repoName := d.Get("repository").(string)
@@ -82,69 +80,59 @@ func resourceGithubIssueLabelCreateOrUpdate(ctx context.Context, d *schema.Resou
8280
Name: new(name),
8381
Color: new(color),
8482
}
85-
if !d.IsNewResource() {
86-
ctx = context.WithValue(ctx, ctxId, d.Id())
87-
}
88-
89-
// Pull out the original name. If we already have a resource, this is the
90-
// parsed ID. If not, it's the value given to the resource.
91-
var originalName string
92-
if d.Id() == "" {
93-
originalName = name
94-
} else {
95-
var err error
96-
_, originalName, err = parseID2(d.Id())
97-
if err != nil {
98-
return diag.FromErr(err)
99-
}
100-
}
101-
102-
existing, resp, err := client.Issues.GetLabel(ctx,
103-
orgName, repoName, originalName)
83+
existing, resp, err := client.Issues.GetLabel(ctx, orgName, repoName, name)
10484
if err != nil {
10585
if resp == nil || resp.StatusCode != http.StatusNotFound {
10686
return diag.FromErr(err)
10787
}
10888
}
109-
11089
if existing != nil {
11190
label.Description = new(d.Get("description").(string))
11291

113-
// Pull out the original name. If we already have a resource, this is the
114-
// parsed ID. If not, it's the value given to the resource.
115-
var originalName string
116-
if d.Id() == "" {
117-
originalName = name
118-
} else {
119-
var err error
120-
_, originalName, err = parseID2(d.Id())
121-
if err != nil {
122-
return diag.FromErr(err)
123-
}
124-
}
125-
126-
_, _, err := client.Issues.EditLabel(ctx,
127-
orgName, repoName, originalName, label)
128-
if err != nil {
129-
return diag.FromErr(err)
130-
}
92+
_, _, err = client.Issues.EditLabel(ctx, orgName, repoName, name, label)
13193
} else {
13294
if v, ok := d.GetOk("description"); ok {
13395
label.Description = new(v.(string))
13496
}
13597

136-
_, _, err := client.Issues.CreateLabel(ctx,
137-
orgName, repoName, label)
138-
if err != nil {
139-
return diag.FromErr(err)
140-
}
98+
_, _, err = client.Issues.CreateLabel(ctx, orgName, repoName, label)
99+
}
100+
if err != nil {
101+
return diag.FromErr(err)
141102
}
142-
143103
d.SetId(buildTwoPartID(repoName, name))
104+
return nil
144105

145-
return resourceGithubIssueLabelRead(ctx, d, meta)
146106
}
107+
func resourceGithubIssueLabelUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
108+
client := meta.(*Owner).v3client
109+
orgName := meta.(*Owner).name
110+
repoName := d.Get("repository").(string)
111+
name := d.Get("name").(string)
112+
color := d.Get("color").(string)
113+
114+
ctx = context.WithValue(ctx, ctxId, d.Id())
115+
116+
_, originalName, err := parseID2(d.Id())
117+
if err != nil {
118+
return diag.FromErr(err)
119+
}
120+
121+
label := &github.Label{
122+
Name: new(name),
123+
Color: new(color),
124+
Description: new(d.Get("description").(string)),
125+
}
126+
127+
_, _, err = client.Issues.EditLabel(ctx, orgName, repoName, originalName, label)
128+
if err != nil {
129+
return diag.FromErr(err)
130+
}
147131

132+
d.SetId(buildTwoPartID(repoName, name))
133+
return nil
134+
135+
}
148136
func resourceGithubIssueLabelRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
149137
client := meta.(*Owner).v3client
150138
repoName, name, err := parseID2(d.Id())
@@ -200,7 +188,6 @@ func resourceGithubIssueLabelRead(ctx context.Context, d *schema.ResourceData, m
200188

201189
return nil
202190
}
203-
204191
func resourceGithubIssueLabelDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
205192
client := meta.(*Owner).v3client
206193

0 commit comments

Comments
 (0)