Skip to content

Commit f36dfa5

Browse files
authored
Add support for Security Advisories Request CVE endpoint (#2857)
Fixes: #2855.
1 parent 899235e commit f36dfa5

File tree

4 files changed

+122
-30
lines changed

4 files changed

+122
-30
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ Derek Jobst <[email protected]>
125125
DeviousLab <[email protected]>
126126
Dhi Aurrahman <[email protected]>
127127
Diego Lapiduz <[email protected]>
128+
Diogo Vilela <[email protected]>
128129
Dmitri Shuralyov <[email protected]>
129130
130131
Don Petersen <[email protected]>

github/github.go

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -179,36 +179,37 @@ type Client struct {
179179
common service // Reuse a single struct instead of allocating one for each service on the heap.
180180

181181
// Services used for talking to different parts of the GitHub API.
182-
Actions *ActionsService
183-
Activity *ActivityService
184-
Admin *AdminService
185-
Apps *AppsService
186-
Authorizations *AuthorizationsService
187-
Billing *BillingService
188-
Checks *ChecksService
189-
CodeScanning *CodeScanningService
190-
Codespaces *CodespacesService
191-
Dependabot *DependabotService
192-
Enterprise *EnterpriseService
193-
Gists *GistsService
194-
Git *GitService
195-
Gitignores *GitignoresService
196-
Interactions *InteractionsService
197-
IssueImport *IssueImportService
198-
Issues *IssuesService
199-
Licenses *LicensesService
200-
Marketplace *MarketplaceService
201-
Migrations *MigrationService
202-
Organizations *OrganizationsService
203-
Projects *ProjectsService
204-
PullRequests *PullRequestsService
205-
Reactions *ReactionsService
206-
Repositories *RepositoriesService
207-
SCIM *SCIMService
208-
Search *SearchService
209-
SecretScanning *SecretScanningService
210-
Teams *TeamsService
211-
Users *UsersService
182+
Actions *ActionsService
183+
Activity *ActivityService
184+
Admin *AdminService
185+
Apps *AppsService
186+
Authorizations *AuthorizationsService
187+
Billing *BillingService
188+
Checks *ChecksService
189+
CodeScanning *CodeScanningService
190+
Codespaces *CodespacesService
191+
Dependabot *DependabotService
192+
Enterprise *EnterpriseService
193+
Gists *GistsService
194+
Git *GitService
195+
Gitignores *GitignoresService
196+
Interactions *InteractionsService
197+
IssueImport *IssueImportService
198+
Issues *IssuesService
199+
Licenses *LicensesService
200+
Marketplace *MarketplaceService
201+
Migrations *MigrationService
202+
Organizations *OrganizationsService
203+
Projects *ProjectsService
204+
PullRequests *PullRequestsService
205+
Reactions *ReactionsService
206+
Repositories *RepositoriesService
207+
SCIM *SCIMService
208+
Search *SearchService
209+
SecretScanning *SecretScanningService
210+
SecurityAdvisories *SecurityAdvisoriesService
211+
Teams *TeamsService
212+
Users *UsersService
212213
}
213214

214215
type service struct {
@@ -346,6 +347,7 @@ func NewClient(httpClient *http.Client) *Client {
346347
c.SCIM = (*SCIMService)(&c.common)
347348
c.Search = (*SearchService)(&c.common)
348349
c.SecretScanning = (*SecretScanningService)(&c.common)
350+
c.SecurityAdvisories = (*SecurityAdvisoriesService)(&c.common)
349351
c.Teams = (*TeamsService)(&c.common)
350352
c.Users = (*UsersService)(&c.common)
351353
return c

github/security_advisories.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2023 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
"fmt"
11+
)
12+
13+
type SecurityAdvisoriesService service
14+
15+
// RequestCVE requests a Common Vulnerabilities and Exposures (CVE) for a repository security advisory.
16+
// The ghsaID is the GitHub Security Advisory identifier of the advisory.
17+
//
18+
// GitHub API docs: https://docs.github.com/en/rest/security-advisories/repository-advisories#request-a-cve-for-a-repository-security-advisory
19+
func (s *SecurityAdvisoriesService) RequestCVE(ctx context.Context, owner, repo, ghsaID string) (*Response, error) {
20+
url := fmt.Sprintf("repos/%v/%v/security-advisories/%v/cve", owner, repo, ghsaID)
21+
22+
req, err := s.client.NewRequest("POST", url, nil)
23+
if err != nil {
24+
return nil, err
25+
}
26+
27+
resp, err := s.client.Do(ctx, req, nil)
28+
if err != nil {
29+
if _, ok := err.(*AcceptedError); ok {
30+
return resp, nil
31+
}
32+
33+
return resp, err
34+
}
35+
36+
return resp, nil
37+
}

github/security_advisories_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2023 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
"net/http"
11+
"testing"
12+
)
13+
14+
func TestSecurityAdvisoriesService_RequestCVE(t *testing.T) {
15+
client, mux, _, teardown := setup()
16+
defer teardown()
17+
18+
mux.HandleFunc("/repos/o/r/security-advisories/ghsa_id_ok/cve", func(w http.ResponseWriter, r *http.Request) {
19+
testMethod(t, r, "POST")
20+
w.WriteHeader(http.StatusOK)
21+
})
22+
23+
mux.HandleFunc("/repos/o/r/security-advisories/ghsa_id_accepted/cve", func(w http.ResponseWriter, r *http.Request) {
24+
testMethod(t, r, "POST")
25+
w.WriteHeader(http.StatusAccepted)
26+
})
27+
28+
ctx := context.Background()
29+
_, err := client.SecurityAdvisories.RequestCVE(ctx, "o", "r", "ghsa_id_ok")
30+
if err != nil {
31+
t.Errorf("SecurityAdvisoriesService.RequestCVE returned error: %v", err)
32+
}
33+
34+
_, err = client.SecurityAdvisories.RequestCVE(ctx, "o", "r", "ghsa_id_accepted")
35+
if err != nil {
36+
t.Errorf("SecurityAdvisoriesService.RequestCVE returned error: %v", err)
37+
}
38+
39+
const methodName = "RequestCVE"
40+
testBadOptions(t, methodName, func() (err error) {
41+
_, err = client.SecurityAdvisories.RequestCVE(ctx, "\n", "\n", "\n")
42+
return err
43+
})
44+
45+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
46+
resp, err := client.SecurityAdvisories.RequestCVE(ctx, "o", "r", "ghsa_id")
47+
if err == nil {
48+
t.Errorf("testNewRequestAndDoFailure %v should have return err", methodName)
49+
}
50+
return resp, err
51+
})
52+
}

0 commit comments

Comments
 (0)