Skip to content

Commit 6ada67e

Browse files
committed
Simplify archived repo handling
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
1 parent 4e039f2 commit 6ada67e

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

github/resource_github_repository_vulnerability_alerts.go

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@ package github
22

33
import (
44
"context"
5-
"errors"
6-
"fmt"
7-
"net/http"
85
"strconv"
9-
"strings"
106

11-
"github.com/google/go-github/v83/github"
127
"github.com/hashicorp/terraform-plugin-log/tflog"
138
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
149
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -63,21 +58,24 @@ func resourceGithubRepositoryVulnerabilityAlertsCreate(ctx context.Context, d *s
6358
repoName := d.Get("repository").(string)
6459

6560
vulnerabilityAlertsEnabled := d.Get("enabled").(bool)
61+
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
62+
if err != nil {
63+
return diag.FromErr(err)
64+
}
65+
if repo.GetArchived() {
66+
return diag.Errorf("cannot enable vulnerability alerts on archived repository %s/%s", owner, repoName)
67+
}
6668
if vulnerabilityAlertsEnabled {
67-
resp, err := client.Repositories.EnableVulnerabilityAlerts(ctx, owner, repoName)
69+
_, err := client.Repositories.EnableVulnerabilityAlerts(ctx, owner, repoName)
6870
if err != nil {
69-
return diag.FromErr(handleVulnerabilityAlertsErrorOnArchivedRepository(err, resp, repoName))
71+
return diag.FromErr(err)
7072
}
7173
} else {
72-
resp, err := client.Repositories.DisableVulnerabilityAlerts(ctx, owner, repoName)
74+
_, err := client.Repositories.DisableVulnerabilityAlerts(ctx, owner, repoName)
7375
if err != nil {
74-
return diag.FromErr(handleVulnerabilityAlertsErrorOnArchivedRepository(err, resp, repoName))
76+
return diag.FromErr(err)
7577
}
7678
}
77-
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
78-
if err != nil {
79-
return diag.FromErr(err)
80-
}
8179

8280
d.SetId(strconv.Itoa(int(repo.GetID())))
8381

@@ -168,14 +166,3 @@ func resourceGithubRepositoryVulnerabilityAlertsImport(ctx context.Context, d *s
168166
}
169167
return []*schema.ResourceData{d}, nil
170168
}
171-
172-
func handleVulnerabilityAlertsErrorOnArchivedRepository(err error, resp *github.Response, repoName string) error {
173-
var ghErr *github.ErrorResponse
174-
if errors.As(err, &ghErr) {
175-
// Error response when trying to enable vulnerability alerts on an archived repository. "422 Failed to change dependabot alerts status"
176-
if resp.StatusCode == http.StatusUnprocessableEntity && strings.Contains(strings.ToLower(ghErr.Message), "failed to change dependabot alerts status") {
177-
return fmt.Errorf("failed to change vulnerability alerts for repository %s. The repository is most likely archived: %s", repoName, ghErr.Message)
178-
}
179-
}
180-
return err
181-
}

github/resource_github_repository_vulnerability_alerts_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ func TestAccGithubRepositoryVulnerabilityAlerts(t *testing.T) {
280280
},
281281
{
282282
Config: fmt.Sprintf(repoConfig, repoName, true, alertsBlock),
283-
ExpectError: regexp.MustCompile(`repository is most likely archived`),
283+
ExpectError: regexp.MustCompile(`cannot enable vulnerability alerts on archived repository`),
284284
},
285285
},
286286
})

0 commit comments

Comments
 (0)