-
Notifications
You must be signed in to change notification settings - Fork 1k
[BUG] Ensure autolink reference doesn't error when repo is renamed #3271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c812520
e40cad1
54e875a
7ae7789
8a6360b
dc39df0
02afd33
d0543de
d0b4b47
aaa15da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,65 +4,49 @@ import ( | |||||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||||||
| "errors" | ||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||
| "log" | ||||||||||||||||||||||||||||||||||||
| "net/http" | ||||||||||||||||||||||||||||||||||||
| "regexp" | ||||||||||||||||||||||||||||||||||||
| "strconv" | ||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| "github.com/google/go-github/v89/github" | ||||||||||||||||||||||||||||||||||||
| "github.com/hashicorp/terraform-plugin-log/tflog" | ||||||||||||||||||||||||||||||||||||
| "github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||||||||||||||||||||||||||||||||||||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||||||||||||||||||||||||||||||||||||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| func resourceGithubRepositoryAutolinkReference() *schema.Resource { | ||||||||||||||||||||||||||||||||||||
| return &schema.Resource{ | ||||||||||||||||||||||||||||||||||||
| Create: resourceGithubRepositoryAutolinkReferenceCreate, | ||||||||||||||||||||||||||||||||||||
| Read: resourceGithubRepositoryAutolinkReferenceRead, | ||||||||||||||||||||||||||||||||||||
| Delete: resourceGithubRepositoryAutolinkReferenceDelete, | ||||||||||||||||||||||||||||||||||||
| CreateContext: resourceGithubRepositoryAutolinkReferenceCreate, | ||||||||||||||||||||||||||||||||||||
| UpdateContext: resourceGithubRepositoryAutolinkReferenceUpdate, | ||||||||||||||||||||||||||||||||||||
| ReadContext: resourceGithubRepositoryAutolinkReferenceRead, | ||||||||||||||||||||||||||||||||||||
| DeleteContext: resourceGithubRepositoryAutolinkReferenceDelete, | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| Importer: &schema.ResourceImporter{ | ||||||||||||||||||||||||||||||||||||
|
deiga marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||
| StateContext: func(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) { | ||||||||||||||||||||||||||||||||||||
| parts := strings.Split(d.Id(), "/") | ||||||||||||||||||||||||||||||||||||
| if len(parts) != 2 { | ||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("invalid ID specified: supplied ID must be written as <repository>/<autolink_reference_id>") | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| repository := parts[0] | ||||||||||||||||||||||||||||||||||||
| id := parts[1] | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // If the second part of the provided ID isn't an integer, assume that the | ||||||||||||||||||||||||||||||||||||
| // caller provided the key prefix for the autolink reference, and look up | ||||||||||||||||||||||||||||||||||||
| // the autolink by the key prefix. | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| _, err := strconv.Atoi(id) | ||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||
| client := meta.(*Owner).v3client | ||||||||||||||||||||||||||||||||||||
| owner := meta.(*Owner).name | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| autolink, err := getAutolinkByKeyPrefix(ctx, client, owner, repository, id) | ||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| id = strconv.FormatInt(*autolink.ID, 10) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if err = d.Set("repository", repository); err != nil { | ||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| d.SetId(id) | ||||||||||||||||||||||||||||||||||||
| return []*schema.ResourceData{d}, nil | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| StateContext: resourceGithubRepositoryAutolinkReferenceImport, | ||||||||||||||||||||||||||||||||||||
|
deiga marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| SchemaVersion: 1, | ||||||||||||||||||||||||||||||||||||
| CustomizeDiff: diffRepository, | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| SchemaVersion: 2, | ||||||||||||||||||||||||||||||||||||
|
stevehipwell marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||
| StateUpgraders: []schema.StateUpgrader{ | ||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||
| Type: resourceGithubRepositoryAutolinkReferenceV1().CoreConfigSchema().ImpliedType(), | ||||||||||||||||||||||||||||||||||||
| Upgrade: resourceGithubRepositoryAutolinkReferenceStateUpgradeV1, | ||||||||||||||||||||||||||||||||||||
| Version: 1, | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| Schema: map[string]*schema.Schema{ | ||||||||||||||||||||||||||||||||||||
| "repository": { | ||||||||||||||||||||||||||||||||||||
| Type: schema.TypeString, | ||||||||||||||||||||||||||||||||||||
| Required: true, | ||||||||||||||||||||||||||||||||||||
| ForceNew: true, | ||||||||||||||||||||||||||||||||||||
| Description: "The repository name", | ||||||||||||||||||||||||||||||||||||
| Description: "The repository name. If the repository is renamed, the autolink reference will be updated in-place rather than recreated.", | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| "repository_id": { | ||||||||||||||||||||||||||||||||||||
| Type: schema.TypeInt, | ||||||||||||||||||||||||||||||||||||
| Computed: true, | ||||||||||||||||||||||||||||||||||||
| Description: "The ID of the GitHub repository.", | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| "key_prefix": { | ||||||||||||||||||||||||||||||||||||
| Type: schema.TypeString, | ||||||||||||||||||||||||||||||||||||
|
|
@@ -96,15 +80,15 @@ func resourceGithubRepositoryAutolinkReference() *schema.Resource { | |||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| func resourceGithubRepositoryAutolinkReferenceCreate(d *schema.ResourceData, meta any) error { | ||||||||||||||||||||||||||||||||||||
| client := meta.(*Owner).v3client | ||||||||||||||||||||||||||||||||||||
| func resourceGithubRepositoryAutolinkReferenceCreate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { | ||||||||||||||||||||||||||||||||||||
| meta, _ := m.(*Owner) | ||||||||||||||||||||||||||||||||||||
| client := meta.v3client | ||||||||||||||||||||||||||||||||||||
| owner := meta.name | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| owner := meta.(*Owner).name | ||||||||||||||||||||||||||||||||||||
| repoName := d.Get("repository").(string) | ||||||||||||||||||||||||||||||||||||
| keyPrefix := d.Get("key_prefix").(string) | ||||||||||||||||||||||||||||||||||||
| targetURLTemplate := d.Get("target_url_template").(string) | ||||||||||||||||||||||||||||||||||||
| isAlphanumeric := d.Get("is_alphanumeric").(bool) | ||||||||||||||||||||||||||||||||||||
| ctx := context.Background() | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| opts := &github.AutolinkOptions{ | ||||||||||||||||||||||||||||||||||||
| KeyPrefix: &keyPrefix, | ||||||||||||||||||||||||||||||||||||
|
|
@@ -114,23 +98,35 @@ func resourceGithubRepositoryAutolinkReferenceCreate(d *schema.ResourceData, met | |||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| autolinkRef, _, err := client.Repositories.AddAutolink(ctx, owner, repoName, opts) | ||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||
| return diag.FromErr(err) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| d.SetId(strconv.FormatInt(autolinkRef.GetID(), 10)) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| return resourceGithubRepositoryAutolinkReferenceRead(d, meta) | ||||||||||||||||||||||||||||||||||||
| repo, _, err := client.Repositories.Get(ctx, owner, repoName) | ||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||
| return diag.FromErr(err) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if err := d.Set("repository_id", int(repo.GetID())); err != nil { | ||||||||||||||||||||||||||||||||||||
| return diag.FromErr(err) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| return resourceGithubRepositoryAutolinkReferenceRead(ctx, d, m) | ||||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be able to remove this call and just set the etag. |
||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| func resourceGithubRepositoryAutolinkReferenceRead(d *schema.ResourceData, meta any) error { | ||||||||||||||||||||||||||||||||||||
| client := meta.(*Owner).v3client | ||||||||||||||||||||||||||||||||||||
| func resourceGithubRepositoryAutolinkReferenceRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { | ||||||||||||||||||||||||||||||||||||
| ctx = tflog.SetField(ctx, "id", d.Id()) | ||||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep with the current pattern where we only set attributes when we need them. And as a rule of thumb |
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| meta, _ := m.(*Owner) | ||||||||||||||||||||||||||||||||||||
| client := meta.v3client | ||||||||||||||||||||||||||||||||||||
| owner := meta.name | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| owner := meta.(*Owner).name | ||||||||||||||||||||||||||||||||||||
| repoName := d.Get("repository").(string) | ||||||||||||||||||||||||||||||||||||
| autolinkRefID, err := strconv.ParseInt(d.Id(), 10, 64) | ||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||
| return unconvertibleIdErr(d.Id(), err) | ||||||||||||||||||||||||||||||||||||
| return diag.FromErr(unconvertibleIdErr(d.Id(), err)) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
125
to
128
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not add a new |
||||||||||||||||||||||||||||||||||||
| ctx := context.WithValue(context.Background(), ctxId, d.Id()) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if !d.IsNewResource() { | ||||||||||||||||||||||||||||||||||||
| ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string)) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
130
to
132
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be removed as create shouldn't be calling through. |
||||||||||||||||||||||||||||||||||||
|
|
@@ -140,44 +136,113 @@ func resourceGithubRepositoryAutolinkReferenceRead(d *schema.ResourceData, meta | |||||||||||||||||||||||||||||||||||
| var ghErr *github.ErrorResponse | ||||||||||||||||||||||||||||||||||||
| if errors.As(err, &ghErr) { | ||||||||||||||||||||||||||||||||||||
| if ghErr.Response.StatusCode == http.StatusNotFound { | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
136
to
138
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please could we use the documented |
||||||||||||||||||||||||||||||||||||
| log.Printf("[INFO] Removing autolink reference for repository %s/%s from state because it no longer exists in GitHub", | ||||||||||||||||||||||||||||||||||||
| owner, repoName) | ||||||||||||||||||||||||||||||||||||
| tflog.Info(ctx, "Autolink reference not found, removing from state.", map[string]any{ | ||||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make this a single line. |
||||||||||||||||||||||||||||||||||||
| "owner": owner, | ||||||||||||||||||||||||||||||||||||
| "repository": repoName, | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
| d.SetId("") | ||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||
| return diag.FromErr(err) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Set resource fields | ||||||||||||||||||||||||||||||||||||
| d.SetId(strconv.FormatInt(autolinkRef.GetID(), 10)) | ||||||||||||||||||||||||||||||||||||
| if err = d.Set("repository", repoName); err != nil { | ||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||
| return diag.FromErr(err) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| repo, _, err := client.Repositories.Get(ctx, owner, repoName) | ||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||
| return diag.FromErr(err) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| if err = d.Set("repository_id", int(repo.GetID())); err != nil { | ||||||||||||||||||||||||||||||||||||
| return diag.FromErr(err) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+155
to
161
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't needed here. |
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if err = d.Set("key_prefix", autolinkRef.KeyPrefix); err != nil { | ||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||
| return diag.FromErr(err) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| if err = d.Set("target_url_template", autolinkRef.URLTemplate); err != nil { | ||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||
| return diag.FromErr(err) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| if err = d.Set("is_alphanumeric", autolinkRef.IsAlphanumeric); err != nil { | ||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||
| return diag.FromErr(err) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| func resourceGithubRepositoryAutolinkReferenceDelete(d *schema.ResourceData, meta any) error { | ||||||||||||||||||||||||||||||||||||
| client := meta.(*Owner).v3client | ||||||||||||||||||||||||||||||||||||
| func resourceGithubRepositoryAutolinkReferenceUpdate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { | ||||||||||||||||||||||||||||||||||||
| tflog.Warn(ctx, "Update function of autolink reference. This should not be called. But it's necessary when 'repository' doesn't have `ForceNew`", map[string]any{ | ||||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we make this a single line? |
||||||||||||||||||||||||||||||||||||
| "repository": d.Get("repository"), | ||||||||||||||||||||||||||||||||||||
| "repository_id": d.Get("repository_id"), | ||||||||||||||||||||||||||||||||||||
| "id": d.Id(), | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+177
to
+181
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make this a single line. |
||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| func resourceGithubRepositoryAutolinkReferenceDelete(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { | ||||||||||||||||||||||||||||||||||||
| ctx = tflog.SetField(ctx, "id", d.Id()) | ||||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| meta, _ := m.(*Owner) | ||||||||||||||||||||||||||||||||||||
| client := meta.v3client | ||||||||||||||||||||||||||||||||||||
| owner := meta.name | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| owner := meta.(*Owner).name | ||||||||||||||||||||||||||||||||||||
| repoName := d.Get("repository").(string) | ||||||||||||||||||||||||||||||||||||
| autolinkRefID, err := strconv.ParseInt(d.Id(), 10, 64) | ||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||
| return unconvertibleIdErr(d.Id(), err) | ||||||||||||||||||||||||||||||||||||
| return diag.FromErr(unconvertibleIdErr(d.Id(), err)) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| ctx := context.WithValue(context.Background(), ctxId, d.Id()) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| _, err = client.Repositories.DeleteAutolink(ctx, owner, repoName, autolinkRefID) | ||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||
| return diag.FromErr(err) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| func resourceGithubRepositoryAutolinkReferenceImport(ctx context.Context, d *schema.ResourceData, m any) ([]*schema.ResourceData, error) { | ||||||||||||||||||||||||||||||||||||
| parts := strings.Split(d.Id(), "/") | ||||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use the |
||||||||||||||||||||||||||||||||||||
| if len(parts) != 2 { | ||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("invalid ID specified: supplied ID must be written as <repository>/<autolink_reference_id>") | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+207
to
+210
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the ID should be migrated as part of the schema migration so it can use the ID functions. |
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| repository := parts[0] | ||||||||||||||||||||||||||||||||||||
| id := parts[1] | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| meta, _ := m.(*Owner) | ||||||||||||||||||||||||||||||||||||
| client := meta.v3client | ||||||||||||||||||||||||||||||||||||
| owner := meta.name | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // If the second part of the provided ID isn't an integer, assume that the | ||||||||||||||||||||||||||||||||||||
| // caller provided the key prefix for the autolink reference, and look up | ||||||||||||||||||||||||||||||||||||
| // the autolink by the key prefix. | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| _, err := strconv.Atoi(id) | ||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||
| autolink, err := getAutolinkByKeyPrefix(ctx, client, owner, repository, id) | ||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| id = strconv.FormatInt(*autolink.ID, 10) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+223
to
+231
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| d.SetId(id) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| repo, _, err := client.Repositories.Get(ctx, owner, repository) | ||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("failed to retrieve repository %s: %w", repository, err) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if err = d.Set("repository", repository); err != nil { | ||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| if err = d.Set("repository_id", int(repo.GetID())); err != nil { | ||||||||||||||||||||||||||||||||||||
| return nil, err | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| return []*schema.ResourceData{d}, nil | ||||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to set all of the fields as they're force new. |
||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-log/tflog" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| ) | ||
|
|
||
| func resourceGithubRepositoryAutolinkReferenceV1() *schema.Resource { | ||
| return &schema.Resource{ | ||
| Schema: map[string]*schema.Schema{ | ||
| "repository": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
| }, | ||
| "key_prefix": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
| }, | ||
| "target_url_template": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
| }, | ||
| "is_alphanumeric": { | ||
| Type: schema.TypeBool, | ||
| Optional: true, | ||
| ForceNew: true, | ||
| Default: true, | ||
| }, | ||
| "etag": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func resourceGithubRepositoryAutolinkReferenceStateUpgradeV1(ctx context.Context, rawState map[string]any, m any) (map[string]any, error) { | ||
| tflog.Debug(ctx, "GitHub Repository Autolink Reference state before v1 migration", rawState) | ||
|
|
||
| meta, _ := m.(*Owner) | ||
| client := meta.v3client | ||
| owner := meta.name | ||
|
|
||
| repoName := "" | ||
| if v, ok := rawState["repository"]; ok { | ||
| if s, ok := v.(string); ok && s != "" { | ||
| repoName = s | ||
| } | ||
| } | ||
| if repoName == "" { | ||
| return nil, fmt.Errorf("state upgrade v1: repository is not a string or not set") | ||
| } | ||
|
|
||
| repo, _, err := client.Repositories.Get(ctx, owner, repoName) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use the |
||
| if err != nil { | ||
| return nil, fmt.Errorf("state upgrade v1: failed to retrieve repository '%s/%s': %w", owner, repoName, err) | ||
| } | ||
|
|
||
| rawState["repository_id"] = int(repo.GetID()) | ||
|
|
||
| tflog.Debug(ctx, "GitHub Repository Autolink Reference state after v1 migration", rawState) | ||
|
|
||
| return rawState, nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.