Skip to content

Commit cb0ee23

Browse files
committed
Fix behaviour on archived repository
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
1 parent 6ada67e commit cb0ee23

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

github/resource_github_repository_vulnerability_alerts.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package github
22

33
import (
44
"context"
5+
"net/http"
56
"strconv"
67

78
"github.com/hashicorp/terraform-plugin-log/tflog"
@@ -51,6 +52,7 @@ func resourceGithubRepositoryVulnerabilityAlerts() *schema.Resource {
5152
}
5253

5354
func resourceGithubRepositoryVulnerabilityAlertsCreate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
55+
tflog.Info(ctx, "Creating repository vulnerability alerts", map[string]any{"id": d.Id()})
5456
meta := m.(*Owner)
5557
client := meta.v3client
5658

@@ -87,15 +89,31 @@ func resourceGithubRepositoryVulnerabilityAlertsCreate(ctx context.Context, d *s
8789
}
8890

8991
func resourceGithubRepositoryVulnerabilityAlertsRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
92+
tflog.Info(ctx, "Reading repository vulnerability alerts", map[string]any{"id": d.Id()})
9093
meta := m.(*Owner)
9194
client := meta.v3client
9295

9396
owner := meta.name // TODO: Add owner support // d.Get("owner").(string)
9497
repoName := d.Get("repository").(string)
95-
vulnerabilityAlertsEnabled, _, err := client.Repositories.GetVulnerabilityAlerts(ctx, owner, repoName)
98+
vulnerabilityAlertsEnabled, resp, err := client.Repositories.GetVulnerabilityAlerts(ctx, owner, repoName)
9699
if err != nil {
100+
if resp.StatusCode == http.StatusNotFound {
101+
d.SetId("")
102+
return diag.Errorf("vulnerability alerts don't exist for repository %s/%s, removing resource from state", owner, repoName)
103+
}
97104
return diag.Errorf("error reading repository vulnerability alerts: %s", err.Error())
98105
}
106+
// If no error, but the response status code is 404, we need to check if the repository is accessible.
107+
if resp.StatusCode == http.StatusNotFound {
108+
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
109+
if err != nil {
110+
return diag.Errorf("repository doesn't exist anymore, please remove the resource from your configuration: %s", err.Error())
111+
}
112+
if repo.GetArchived() {
113+
return diag.Errorf("repository %s/%s is archived, please remove the resource from your configuration", owner, repoName)
114+
}
115+
}
116+
tflog.Debug(ctx, "Setting vulnerability alerts enabled state", map[string]any{"owner": owner, "repo_name": repoName, "vulnerability_alerts_enabled": vulnerabilityAlertsEnabled, "response_status": resp.StatusCode})
99117
if err = d.Set("enabled", vulnerabilityAlertsEnabled); err != nil {
100118
return diag.FromErr(err)
101119
}
@@ -104,6 +122,7 @@ func resourceGithubRepositoryVulnerabilityAlertsRead(ctx context.Context, d *sch
104122
}
105123

106124
func resourceGithubRepositoryVulnerabilityAlertsUpdate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
125+
tflog.Info(ctx, "Updating repository vulnerability alerts", map[string]any{"id": d.Id()})
107126
meta := m.(*Owner)
108127
client := meta.v3client
109128

@@ -127,6 +146,7 @@ func resourceGithubRepositoryVulnerabilityAlertsUpdate(ctx context.Context, d *s
127146
}
128147

129148
func resourceGithubRepositoryVulnerabilityAlertsDelete(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
149+
tflog.Info(ctx, "Deleting repository vulnerability alerts", map[string]any{"id": d.Id()})
130150
meta := m.(*Owner)
131151
client := meta.v3client
132152

github/resource_github_repository_vulnerability_alerts_test.go

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
1010
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1111
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
12-
"github.com/hashicorp/terraform-plugin-testing/plancheck"
1312
"github.com/hashicorp/terraform-plugin-testing/statecheck"
1413
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
1514
)
@@ -191,7 +190,7 @@ func TestAccGithubRepositoryVulnerabilityAlerts(t *testing.T) {
191190
})
192191
})
193192

194-
t.Run("destroys_from_archived_repository_without_error", func(t *testing.T) {
193+
t.Run("errors_when_reading_archived_repository", func(t *testing.T) {
195194
randomID := acctest.RandString(5)
196195
repoName := fmt.Sprintf("%svuln-alerts-%s", testResourcePrefix, randomID)
197196

@@ -209,15 +208,6 @@ func TestAccGithubRepositoryVulnerabilityAlerts(t *testing.T) {
209208
}
210209
`
211210

212-
archivedOnlyConfig := fmt.Sprintf(`
213-
resource "github_repository" "test" {
214-
name = "%s"
215-
visibility = "private"
216-
auto_init = true
217-
archived = true
218-
}
219-
`, repoName)
220-
221211
resource.Test(t, resource.TestCase{
222212
PreCheck: func() { skipUnauthenticated(t) },
223213
ProviderFactories: providerFactories,
@@ -231,17 +221,7 @@ func TestAccGithubRepositoryVulnerabilityAlerts(t *testing.T) {
231221
{
232222
Config: fmt.Sprintf(withAlertsConfig, repoName, true),
233223
ExpectNonEmptyPlan: true,
234-
ConfigStateChecks: []statecheck.StateCheck{
235-
statecheck.ExpectKnownValue("github_repository.test", tfjsonpath.New("archived"), knownvalue.Bool(true)),
236-
},
237-
},
238-
{
239-
Config: archivedOnlyConfig,
240-
ConfigPlanChecks: resource.ConfigPlanChecks{
241-
PreApply: []plancheck.PlanCheck{
242-
plancheck.ExpectResourceAction("github_repository_vulnerability_alerts.test", plancheck.ResourceActionDestroy),
243-
},
244-
},
224+
ExpectError: regexp.MustCompile(`is archived, please remove the resource`),
245225
},
246226
},
247227
})

github/util.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ func deleteResourceOn404AndSwallow304OtherwiseReturnError(err error, d *schema.R
242242
var ghErr *github.ErrorResponse
243243
if errors.As(err, &ghErr) {
244244
if ghErr.Response.StatusCode == http.StatusNotModified {
245+
log.Printf("[INFO] Resource %s not modified, skipping", resourceDescription)
245246
return nil
246247
}
247248
if ghErr.Response.StatusCode == http.StatusNotFound {

0 commit comments

Comments
 (0)