-
Notifications
You must be signed in to change notification settings - Fork 951
[FEAT] Add new resource for repository vulnerability alerts #3166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
stevehipwell
merged 16 commits into
integrations:main
from
F-Secure-web:add-github_repository_vulnerability_alerts
Apr 16, 2026
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4e24b04
Add resource for repository vulnerability alerts
deiga 9356cd7
Mark `vulnerability_alerts` in `github_repository` as deprecated
deiga 2bb8368
Update formatting
deiga 48dbccb
Update import after rebase
deiga e760b7b
Correct file names
deiga 740179a
Address review comments
deiga 652e25f
Simplify `SetId`
deiga b82834c
Comment out `owner` until we can support it better
deiga 3e7bb3e
Refactor to use `ConfigStateChecks`
deiga 1aa29db
Update import ID to only repository name
deiga b2f9c82
Improves tests
deiga d95bca4
Updates docs
deiga 058b347
Add clearer error for user when trying to add to an archived repository
deiga 199f24b
Re-order `SetId`
deiga 2afe3e5
Simplify archived repo handling
deiga 3d0fd90
Fix behaviour on archived repository
deiga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
github/resource_github_repository_vulnerability_alerts.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "strconv" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-log/tflog" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| ) | ||
|
|
||
| func resourceGithubRepositoryVulnerabilityAlerts() *schema.Resource { | ||
| return &schema.Resource{ | ||
| CreateContext: resourceGithubRepositoryVulnerabilityAlertsCreate, | ||
| ReadContext: resourceGithubRepositoryVulnerabilityAlertsRead, | ||
| UpdateContext: resourceGithubRepositoryVulnerabilityAlertsUpdate, | ||
| DeleteContext: resourceGithubRepositoryVulnerabilityAlertsDelete, | ||
| Importer: &schema.ResourceImporter{ | ||
| StateContext: resourceGithubRepositoryVulnerabilityAlertsImport, | ||
| }, | ||
|
|
||
| Schema: map[string]*schema.Schema{ | ||
| "repository": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
| Description: "The repository name to configure vulnerability alerts for.", | ||
| }, | ||
| "repository_id": { | ||
| Type: schema.TypeInt, | ||
| Computed: true, | ||
| Description: "The ID of the repository to configure vulnerability alerts for.", | ||
| }, | ||
| // TODO: Uncomment this when we are ready to support owner fields properly. https://github.com/integrations/terraform-provider-github/pull/3166#discussion_r2816053082 | ||
| // "owner": { | ||
| // Type: schema.TypeString, | ||
| // Required: true, | ||
| // ForceNew: true, | ||
| // Description: "The owner of the repository to configure vulnerability alerts for.", | ||
| // }, | ||
| "enabled": { | ||
| Type: schema.TypeBool, | ||
| Optional: true, | ||
| Default: true, | ||
| Description: "Whether vulnerability alerts are enabled for the repository.", | ||
| }, | ||
| }, | ||
|
|
||
| CustomizeDiff: diffRepository, | ||
| } | ||
| } | ||
|
|
||
| func resourceGithubRepositoryVulnerabilityAlertsCreate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { | ||
| tflog.Info(ctx, "Creating repository vulnerability alerts", map[string]any{"id": d.Id()}) | ||
| meta := m.(*Owner) | ||
| client := meta.v3client | ||
|
|
||
| owner := meta.name // TODO: Add owner support // d.Get("owner").(string) | ||
| repoName := d.Get("repository").(string) | ||
|
|
||
| vulnerabilityAlertsEnabled := d.Get("enabled").(bool) | ||
| repo, _, err := client.Repositories.Get(ctx, owner, repoName) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if repo.GetArchived() { | ||
| return diag.Errorf("cannot enable vulnerability alerts on archived repository %s/%s", owner, repoName) | ||
| } | ||
| if vulnerabilityAlertsEnabled { | ||
| _, err := client.Repositories.EnableVulnerabilityAlerts(ctx, owner, repoName) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| } else { | ||
| _, err := client.Repositories.DisableVulnerabilityAlerts(ctx, owner, repoName) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| } | ||
|
|
||
| d.SetId(strconv.Itoa(int(repo.GetID()))) | ||
|
|
||
| if err = d.Set("repository_id", repo.GetID()); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func resourceGithubRepositoryVulnerabilityAlertsRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { | ||
| tflog.Info(ctx, "Reading repository vulnerability alerts", map[string]any{"id": d.Id()}) | ||
| meta := m.(*Owner) | ||
| client := meta.v3client | ||
|
|
||
| owner := meta.name // TODO: Add owner support // d.Get("owner").(string) | ||
| repoName := d.Get("repository").(string) | ||
| vulnerabilityAlertsEnabled, resp, err := client.Repositories.GetVulnerabilityAlerts(ctx, owner, repoName) | ||
| if err != nil { | ||
| if resp.StatusCode == http.StatusNotFound { | ||
| d.SetId("") | ||
| return diag.Errorf("vulnerability alerts don't exist for repository %s/%s, removing resource from state", owner, repoName) | ||
| } | ||
| return diag.Errorf("error reading repository vulnerability alerts: %s", err.Error()) | ||
| } | ||
| // If no error, but the response status code is 404, we need to check if the repository is accessible. | ||
| if resp.StatusCode == http.StatusNotFound { | ||
| repo, _, err := client.Repositories.Get(ctx, owner, repoName) | ||
| if err != nil { | ||
| return diag.Errorf("repository doesn't exist anymore, please remove the resource from your configuration: %s", err.Error()) | ||
| } | ||
| if repo.GetArchived() { | ||
| return diag.Errorf("repository %s/%s is archived, please remove the resource from your configuration", owner, repoName) | ||
| } | ||
| } | ||
| tflog.Debug(ctx, "Setting vulnerability alerts enabled state", map[string]any{"owner": owner, "repo_name": repoName, "vulnerability_alerts_enabled": vulnerabilityAlertsEnabled, "response_status": resp.StatusCode}) | ||
| if err = d.Set("enabled", vulnerabilityAlertsEnabled); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func resourceGithubRepositoryVulnerabilityAlertsUpdate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { | ||
| tflog.Info(ctx, "Updating repository vulnerability alerts", map[string]any{"id": d.Id()}) | ||
| meta := m.(*Owner) | ||
| client := meta.v3client | ||
|
|
||
| owner := meta.name // TODO: Add owner support // d.Get("owner").(string) | ||
| repoName := d.Get("repository").(string) | ||
|
|
||
| vulnerabilityAlertsEnabled := d.Get("enabled").(bool) | ||
| if vulnerabilityAlertsEnabled { | ||
| _, err := client.Repositories.EnableVulnerabilityAlerts(ctx, owner, repoName) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| } else { | ||
| _, err := client.Repositories.DisableVulnerabilityAlerts(ctx, owner, repoName) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func resourceGithubRepositoryVulnerabilityAlertsDelete(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { | ||
| tflog.Info(ctx, "Deleting repository vulnerability alerts", map[string]any{"id": d.Id()}) | ||
| meta := m.(*Owner) | ||
| client := meta.v3client | ||
|
|
||
| owner := meta.name // TODO: Add owner support // d.Get("owner").(string) | ||
| repoName := d.Get("repository").(string) | ||
| _, err := client.Repositories.DisableVulnerabilityAlerts(ctx, owner, repoName) | ||
| if err != nil { | ||
| return diag.FromErr(handleArchivedRepoDelete(err, "repository vulnerability alerts", d.Id(), owner, repoName)) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func resourceGithubRepositoryVulnerabilityAlertsImport(ctx context.Context, d *schema.ResourceData, m any) ([]*schema.ResourceData, error) { | ||
| tflog.Debug(ctx, "Importing repository vulnerability alerts", map[string]any{"id": d.Id()}) | ||
| repoName := d.Id() | ||
| // if err := d.Set("owner", repoOwner); err != nil { // TODO: Add owner support | ||
| // return nil, err | ||
| // } | ||
| if err := d.Set("repository", repoName); err != nil { | ||
| return nil, err | ||
| } | ||
|
deiga marked this conversation as resolved.
|
||
|
|
||
| meta := m.(*Owner) | ||
| owner := meta.name | ||
| client := meta.v3client | ||
|
|
||
| repo, _, err := client.Repositories.Get(ctx, owner, repoName) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| d.SetId(strconv.Itoa(int(repo.GetID()))) | ||
|
|
||
| if err = d.Set("repository_id", repo.GetID()); err != nil { | ||
| return nil, err | ||
| } | ||
| return []*schema.ResourceData{d}, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.