-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add support for Dependabot alert endpoints #2554
New issue
Have a question about this project? Sign up for a f 8000 ree 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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ca3e639
Add dependabot alerts
francois2metz a29987e
Add First and Last parameters
francois2metz c80d5c4
fixup! Add dependabot alerts
francois2metz 70351f0
Add description to all public structs
francois2metz c81918f
Generate
francois2metz 83796ef
Rename field HTMLURL
francois2metz 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright 2022 The go-github AUTHORS. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
// Dependency reprensents the vulnerable dependency. | ||
type Dependency struct { | ||
Package *VulnerabilityPackage `json:"package,omitempty"` | ||
ManifestPath *string `json:"manifest_path,omitempty"` | ||
Scope *string `json:"scope,omitempty"` | ||
} | ||
|
||
// AdvisoryCVSs represents the advisory pertaining to the Common Vulnerability Scoring System. | ||
type AdvisoryCVSs struct { | ||
Score *float64 `json:"score,omitempty"` | ||
VectorString *string `json:"vector_string,omitempty"` | ||
} | ||
|
||
// AdvisoryCWEs reprensent the advisory pertaining to Common Weakness Enumeration. | ||
type AdvisoryCWEs struct { | ||
CWEID *string `json:"cwe_id,omitempty"` | ||
Name *string `json:"name,omitempty"` | ||
} | ||
|
||
// DependabotSecurityAdvisory represents the GitHub Security Advisory. | ||
type DependabotSecurityAdvisory struct { | ||
GHSAID *string `json:"ghsa_id,omitempty"` | ||
CVEID *string `json:"cve_id,omitempty"` | ||
Summary *string `json:"summary,omitempty"` | ||
Description *string `json:"description,omitempty"` | ||
Vulnerabilities []*AdvisoryVulnerability `json:"vulnerabilities,omitempty"` | ||
Severity *string `json:"severity,omitempty"` | ||
CVSs *AdvisoryCVSs `json:"cvss,omitempty"` | ||
CWEs []*AdvisoryCWEs `json:"cwes,omitempty"` | ||
Identifiers []*AdvisoryIdentifier `json:"identifiers,omitempty"` | ||
References []*AdvisoryReference `json:"references,omitempty"` | ||
PublishedAt *Timestamp `json:"published_at,omitempty"` | ||
UpdatedAt *Timestamp `json:"updated_at,omitempty"` | ||
WithdrawnAt *Timestamp `json:"withdrawn_at,omitempty"` | ||
} | ||
|
||
// DependabotAlert represents a Dependabot alert. | ||
type DependabotAlert struct { | ||
Number *int `json:"number,omitempty"` | ||
State *string `json:"state,omitempty"` | ||
Dependency *Dependency `json:"dependency,omitempty"` | ||
SecurityAdvisory *DependabotSecurityAdvisory `json:"security_advisory,omitempty"` | ||
SecurityVulnerability *AdvisoryVulnerability `json:"security_vulnerability,omitempty"` | ||
URL *string `json:"url,omitempty"` | ||
HTMLURL *string `json:"html_url,omitempty"` | ||
CreatedAt *Timestamp `json:"created_at,omitempty"` | ||
UpdatedAt *Timestamp `json:"updated_at,omitempty"` | ||
DismissedAt *Timestamp `json:"dismissed_at,omitempty"` | ||
DismissedBy *User `json:"dismissed_by,omitempty"` | ||
DismissedReason *string `json:"dismissed_reason,omitempty"` | ||
DismissedComment *string `json:"dismissed_comment,omitempty"` | ||
FixedAt *Timestamp `json:"fixed_at,omitempty"` | ||
} | ||
|
||
// ListAlertsOptions specifies the optional parameters to the DependabotService.ListRepoAlerts | ||
// and DependabotService.ListOrgAlerts methods. | ||
type ListAlertsOptions struct { | ||
State *string `url:"state,omitempty"` | ||
Severity *string `url:"severity,omitempty"` | ||
Ecosystem *string `url:"ecosystem,omitempty"` | ||
Package *string `url:"package,omitempty"` | ||
Scope *string `url:"scope,omitempty"` | ||
Sort *string `url:"sort,omitempty"` | ||
Direction *string `url:"direction,omitempty"` | ||
|
||
ListCursorOptions | ||
} | ||
|
||
func (s *DependabotService) listAlerts(ctx context.Context, url string, opts *ListAlertsOptions) ([]*DependabotAlert, *Response, error) { | ||
u, err := addOptions(url, opts) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
req, err := s.client.NewRequest("GET", u, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
var alerts []*DependabotAlert | ||
resp, err := s.client.Do(ctx, req, &alerts) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return alerts, resp, nil | ||
} | ||
|
||
// ListRepoAlerts lists all Dependabot alerts of a repository. | ||
// | ||
// GitHub API docs: https://docs.github.com/en/rest/dependabot/alerts#list-dependabot-alerts-for-a-repository | ||
func (s *DependabotService) ListRepoAlerts(ctx context.Context, owner, repo string, opts *ListAlertsOptions) ([]*DependabotAlert, *Response, error) { | ||
url := fmt.Sprintf("repos/%v/%v/dependabot/alerts", owner, repo) | ||
return s.listAlerts(ctx, url, opts) | ||
} | ||
|
||
// ListOrgAlerts lists all Dependabot alerts of an organization. | ||
// | ||
// GitHub API docs: https://docs.github.com/en/rest/dependabot/alerts#list-dependabot-alerts-for-an-organization | ||
func (s *DependabotService) ListOrgAlerts(ctx context.Context, org string, opts *ListAlertsOptions) ([]*DependabotAlert, *Response, error) { | ||
url := fmt.Sprintf("orgs/%v/dependabot/alerts", org) | ||
return s.listAlerts(ctx, url, opts) | ||
} | ||
|
||
// GetRepoAlert gets a single repository Dependabot alert. | ||
// | ||
// GitHub API docs: https://docs.github.com/en/rest/dependabot/alerts#get-a-dependabot-alert | ||
func (s *DependabotService) GetRepoAlert(ctx context.Context, owner, repo string, number int) (*DependabotAlert, *Response, error) { | ||
url := fmt.Sprintf("repos/%v/%v/dependabot/alerts/%v", owner, repo, number) | ||
req, err := s.client.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
alert := new(DependabotAlert) | ||
resp, err := s.client.Do(ctx, req, alert) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return alert, resp, nil | ||
} |
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,133 @@ | ||
// Copyright 2022 The go-github AUTHORS. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestDependabotService_ListRepoAlerts(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repos/o/r/dependabot/alerts", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
testFormValues(t, r, values{"state": "open"}) | ||
fmt.Fprint(w, `[{"number":1,"state":"open"},{"number":42,"state":"fixed"}]`) | ||
}) | ||
|
||
opts := &ListAlertsOptions{State: String("open")} | ||
ctx := context.Background() | ||
alerts, _, err := client.Dependabot.ListRepoAlerts(ctx, "o", "r", opts) | ||
if err != nil { | ||
t.Errorf("Dependabot.ListRepoAlerts returned error: %v", err) | ||
} | ||
|
||
want := []*DependabotAlert{ | ||
{Number: Int(1), State: String("open")}, | ||
{Number: Int(42), State: String("fixed")}, | ||
} | ||
if !cmp.Equal(alerts, want) { | ||
t.Errorf("Dependabot.ListRepoAlerts returned %+v, want %+v", alerts, want) | ||
} | ||
|
||
const methodName = "ListRepoAlerts" | ||
testBadOptions(t, methodName, func() (err error) { | ||
_, _, err = client.Dependabot.ListRepoAlerts(ctx, "\n", "\n", opts) | ||
return err | ||
}) | ||
|
||
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { | ||
got, resp, err := client.Dependabot.ListRepoAlerts(ctx, "o", "r", opts) | ||
if got != nil { | ||
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) | ||
} | ||
return resp, err | ||
}) | ||
} | ||
|
||
func TestDependabotService_GetRepoAlert(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/repos/o/r/dependabot/alerts/42", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
fmt.Fprint(w, `{"number":42,"state":"fixed"}`) | ||
}) | ||
|
||
ctx := context.Background() | ||
alert, _, err := client.Dependabot.GetRepoAlert(ctx, "o", "r", 42) | ||
if err != nil { | ||
t.Errorf("Dependabot.GetRepoAlert returned error: %v", err) | ||
} | ||
|
||
want := &DependabotAlert{ | ||
Number: Int(42), | ||
State: String("fixed"), | ||
} | ||
if !cmp.Equal(alert, want) { | ||
t.Errorf("Dependabot.GetRepoAlert returned %+v, want %+v", alert, want) | ||
} | ||
|
||
const methodName = "GetRepoAlert" | ||
testBadOptions(t, methodName, func() (err error) { | ||
_, _, err = client.Dependabot.GetRepoAlert(ctx, "\n", "\n", 0) | ||
return err | ||
}) | ||
|
||
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { | ||
got, resp, err := client.Dependabot.GetRepoAlert(ctx, "o", "r", 42) | ||
if got != nil { | ||
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) | ||
} | ||
return resp, err | ||
}) | ||
} | ||
|
||
func TestDependabotService_ListOrgAlerts(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/orgs/o/dependabot/alerts", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
testFormValues(t, r, values{"state": "open"}) | ||
fmt.Fprint(w, `[{"number":1,"state":"open"},{"number":42,"state":"fixed"}]`) | ||
}) | ||
|
||
opts := &ListAlertsOptions{State: String("open")} | ||
ctx := context.Background() | ||
alerts, _, err := client.Dependabot.ListOrgAlerts(ctx, "o", opts) | ||
if err != nil { | ||
t.Errorf("Dependabot.ListOrgAlerts returned error: %v", err) | ||
} | ||
|
||
want := []*DependabotAlert{ | ||
{Number: Int(1), State: String("open")}, | ||
{Number: Int(42), State: String("fixed")}, | ||
} | ||
if !cmp.Equal(alerts, want) { | ||
t.Errorf("Dependabot.ListOrgAlerts returned %+v, want %+v", alerts, want) | ||
} | ||
|
||
const methodName = "ListOrgAlerts" | ||
testBadOptions(t, methodName, func() (err error) { | ||
_, _, err = client.Dependabot.ListOrgAlerts(ctx, "\n", opts) | ||
return err | ||
}) | ||
|
||
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { | ||
got, resp, err := client.Dependabot.ListOrgAlerts(ctx, "o", opts) | ||
if got != nil { | ||
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) | ||
} | ||
return resp, 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.