-
Notifications
You must be signed in to change notification settings - Fork 932
[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
Open
deiga
wants to merge
14
commits into
integrations:main
Choose a base branch
from
F-Secure-web:add-github_repository_vulnerability_alerts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0e5b4c3
Add resource for repository vulnerability alerts
deiga 25f08a0
Mark `vulnerability_alerts` in `github_repository` as deprecated
deiga 19d51d2
Update formatting
deiga 804a963
Update import after rebase
deiga 0019c57
Correct file names
deiga 5351829
Address review comments
deiga cf518f4
Simplify `SetId`
deiga 0ca677e
Comment out `owner` until we can support it better
deiga dfe9732
Refactor to use `ConfigStateChecks`
deiga a0da2e7
Update import ID to only repository name
deiga f0e94ed
Improves tests
deiga 569224b
Updates docs
deiga 447bb03
Add clearer error for user when trying to add to an archived repository
deiga 4f3c392
Re-order `SetId`
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
181 changes: 181 additions & 0 deletions
181
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,181 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/google/go-github/v83/github" | ||
| "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 { | ||
| 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 { | ||
| resp, err := client.Repositories.EnableVulnerabilityAlerts(ctx, owner, repoName) | ||
| if err != nil { | ||
| return diag.FromErr(handleVulnerabilityAlertsErrorOnArchivedRepository(err, resp, repoName)) | ||
| } | ||
| } else { | ||
| resp, err := client.Repositories.DisableVulnerabilityAlerts(ctx, owner, repoName) | ||
| if err != nil { | ||
| return diag.FromErr(handleVulnerabilityAlertsErrorOnArchivedRepository(err, resp, repoName)) | ||
| } | ||
| } | ||
| repo, _, err := client.Repositories.Get(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 { | ||
| meta := m.(*Owner) | ||
| client := meta.v3client | ||
|
|
||
| owner := meta.name // TODO: Add owner support // d.Get("owner").(string) | ||
| repoName := d.Get("repository").(string) | ||
| vulnerabilityAlertsEnabled, _, err := client.Repositories.GetVulnerabilityAlerts(ctx, owner, repoName) | ||
| if err != nil { | ||
| return diag.Errorf("error reading repository vulnerability alerts: %s", err.Error()) | ||
| } | ||
| 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 { | ||
| 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 { | ||
| 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 | ||
| } | ||
|
|
||
| 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 | ||
| } | ||
|
|
||
| func handleVulnerabilityAlertsErrorOnArchivedRepository(err error, resp *github.Response, repoName string) error { | ||
| var ghErr *github.ErrorResponse | ||
| if errors.As(err, &ghErr) { | ||
| // Error response when trying to enable vulnerability alerts on an archived repository. "422 Failed to change dependabot alerts status" | ||
| if resp.StatusCode == http.StatusUnprocessableEntity && strings.Contains(strings.ToLower(ghErr.Message), "failed to change dependabot alerts status") { | ||
| return fmt.Errorf("failed to change vulnerability alerts for repository %s. The repository is most likely archived: %s", repoName, ghErr.Message) | ||
| } | ||
| } | ||
| return err | ||
| } | ||
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.