@@ -2,13 +2,8 @@ package github
22
33import (
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- }
0 commit comments