@@ -2,6 +2,7 @@ package github
22
33import (
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
5354func 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
8991func 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
106124func 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
129148func 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
0 commit comments