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