Skip to content

Commit a48ea4f

Browse files
committed
Add clearer error for user when trying to add to an archived repository
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
1 parent 42beb1b commit a48ea4f

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

github/resource_github_repository_vulnerability_alerts.go

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

33
import (
44
"context"
5+
"errors"
6+
"fmt"
7+
"net/http"
58
"strconv"
9+
"strings"
610

11+
"github.com/google/go-github/v83/github"
712
"github.com/hashicorp/terraform-plugin-log/tflog"
813
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
914
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -59,14 +64,14 @@ func resourceGithubRepositoryVulnerabilityAlertsCreate(ctx context.Context, d *s
5964

6065
vulnerabilityAlertsEnabled := d.Get("enabled").(bool)
6166
if vulnerabilityAlertsEnabled {
62-
_, err := client.Repositories.EnableVulnerabilityAlerts(ctx, owner, repoName)
67+
resp, err := client.Repositories.EnableVulnerabilityAlerts(ctx, owner, repoName)
6368
if err != nil {
64-
return diag.FromErr(err)
69+
return diag.FromErr(handleVulnerabilityAlertsErrorOnArchivedRepository(err, resp, repoName))
6570
}
6671
} else {
67-
_, err := client.Repositories.DisableVulnerabilityAlerts(ctx, owner, repoName)
72+
resp, err := client.Repositories.DisableVulnerabilityAlerts(ctx, owner, repoName)
6873
if err != nil {
69-
return diag.FromErr(err)
74+
return diag.FromErr(handleVulnerabilityAlertsErrorOnArchivedRepository(err, resp, repoName))
7075
}
7176
}
7277
repo, _, err := client.Repositories.Get(ctx, owner, repoName)
@@ -162,3 +167,14 @@ func resourceGithubRepositoryVulnerabilityAlertsImport(ctx context.Context, d *s
162167

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

github/resource_github_repository_vulnerability_alerts_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package github
22

33
import (
44
"fmt"
5+
"regexp"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-testing/compare"
@@ -246,6 +247,45 @@ func TestAccGithubRepositoryVulnerabilityAlerts(t *testing.T) {
246247
})
247248
})
248249

250+
t.Run("creates_on_archived_repository_with_error)", func(t *testing.T) {
251+
randomID := acctest.RandString(5)
252+
repoName := fmt.Sprintf("%svuln-alerts-%s", testResourcePrefix, randomID)
253+
254+
repoConfig := `
255+
resource "github_repository" "test" {
256+
name = "%s"
257+
visibility = "private"
258+
auto_init = true
259+
archived = %t
260+
}
261+
%s
262+
`
263+
264+
alertsBlock := `
265+
resource "github_repository_vulnerability_alerts" "test" {
266+
repository = github_repository.test.name
267+
enabled = true
268+
}
269+
`
270+
271+
resource.Test(t, resource.TestCase{
272+
PreCheck: func() { skipUnauthenticated(t) },
273+
ProviderFactories: providerFactories,
274+
Steps: []resource.TestStep{
275+
{
276+
Config: fmt.Sprintf(repoConfig, repoName, false, ""),
277+
},
278+
{
279+
Config: fmt.Sprintf(repoConfig, repoName, true, ""),
280+
},
281+
{
282+
Config: fmt.Sprintf(repoConfig, repoName, true, alertsBlock),
283+
ExpectError: regexp.MustCompile(`repository is most likely archived`),
284+
},
285+
},
286+
})
287+
})
288+
249289
t.Run("creates_on_public_repo_without_error", func(t *testing.T) {
250290
randomID := acctest.RandString(5)
251291
repoName := fmt.Sprintf("%svuln-alerts-%s", testResourcePrefix, randomID)

0 commit comments

Comments
 (0)