-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add support for fetching SBOMs #2869
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
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
08342c2
Add support for fetching sboms
vandanrohatgi 7de20ac
Add support for fetching sboms
vandanrohatgi 6cf892d
Merge branch 'get-sbom' of github.com:vandanrohatgi/go-github into ge…
vandanrohatgi 57e5ce1
Add support for fetching sboms
vandanrohatgi 1157290
Update github/dependency_graph.go
vandanrohatgi 4d1aff4
Update github/dependency_graph.go
vandanrohatgi 06ddb1c
Update github/dependency_graph.go
vandanrohatgi 5e93a14
Update github/dependency_graph.go
vandanrohatgi f9225cd
Update github/dependency_graph.go
vandanrohatgi a294cff
Update github/dependency_graph.go
vandanrohatgi ee3ea27
Update github/dependency_graph.go
vandanrohatgi f7e6243
Apply suggestions from code review
vandanrohatgi 9f3a92c
Merge branch 'get-sbom' of github.com:vandanrohatgi/go-github into ge…
vandanrohatgi 7c083bd
Fix PR reviews
vandanrohatgi 7857903
Merge branch 'master' into get-sbom
vandanrohatgi 024b4a3
Apply suggestions from code review
vandanrohatgi c48702b
Update github/dependency_graph.go
gmlewis 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,80 @@ | ||
| // Copyright 2023 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" | ||
| ) | ||
|
|
||
| type DependencyGraphService service | ||
|
|
||
| // SBOM represents a software bill of materials, which describes the | ||
| // packages/libraries that a repository depends on. | ||
| type SBOM struct { | ||
| SBOM *SBOMInfo `json:"sbom,omitempty"` | ||
| } | ||
|
|
||
| // CreationInfo represents when the SBOM was created and who created it. | ||
| type CreationInfo struct { | ||
| Created *Timestamp `json:"created,omitempty"` | ||
| Creators []string `json:"creators,omitempty"` | ||
| } | ||
|
|
||
| // RepoDependencies represents the dependencies of a repo. | ||
| type RepoDependencies struct { | ||
| SPDXID *string `json:"SPDXID,omitempty"` | ||
| // Package name | ||
| Name *string `json:"name,omitempty"` | ||
| VersionInfo *string `json:"versionInfo,omitempty"` | ||
| DownloadLocation *string `json:"downloadLocation,omitempty"` | ||
| FilesAnalyzed *bool `json:"filesAnalyzed,omitempty"` | ||
| LicenseConcluded *string `json:"licenseConcluded,omitempty"` | ||
| LicenseDeclared *string `json:"licenseDeclared,omitempty"` | ||
| } | ||
|
|
||
| // SBOMInfo represents a software bill of materials (SBOM) using SPDX. | ||
| // SPDX is an open standard for SBOMs that | ||
| // identifies and catalogs components, licenses, copyrights, security | ||
| // references, and other metadata relating to software. | ||
| type SBOMInfo struct { | ||
| SPDXID *string `json:"SPDXID,omitempty"` | ||
| SPDXVersion *string `json:"spdxVersion,omitempty"` | ||
| CreationInfo *CreationInfo `json:"creationInfo,omitempty"` | ||
|
|
||
| // Repo name | ||
| Name *string `json:"name,omitempty"` | ||
| DataLicense *string `json:"dataLicense,omitempty"` | ||
| DocumentDescribes []string `json:"documentDescribes,omitempty"` | ||
| DocumentNamespace *string `json:"documentNamespace,omitempty"` | ||
|
|
||
| // List of packages dependencies | ||
| Packages []*RepoDependencies `json:"packages,omitempty"` | ||
| } | ||
|
|
||
| func (s SBOM) String() string { | ||
| return Stringify(s) | ||
| } | ||
|
|
||
| // GetSBOM fetches the software bill of materials for a repository. | ||
| // | ||
| // GitHub API docs: https://docs.github.com/en/rest/dependency-graph/sboms | ||
| func (s *DependencyGraphService) GetSBOM(ctx context.Context, owner, repo string) (*SBOM, *Response, error) { | ||
| u := fmt.Sprintf("repos/%v/%v/dependency-graph/sbom", owner, repo) | ||
|
|
||
| req, err := s.client.NewRequest("GET", u, nil) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| var sbom *SBOM | ||
| resp, err := s.client.Do(ctx, req, &sbom) | ||
| if err != nil { | ||
| return nil, resp, err | ||
| } | ||
|
|
||
| return sbom, 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,79 @@ | ||
| // Copyright 2023 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" | ||
| "time" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| ) | ||
|
|
||
| func TestDependencyGraphService_GetSBOM(t *testing.T) { | ||
| client, mux, _, teardown := setup() | ||
| defer teardown() | ||
|
|
||
| mux.HandleFunc("/repos/owner/repo/dependency-graph/sbom", func(w http.ResponseWriter, r *http.Request) { | ||
| testMethod(t, r, "GET") | ||
| fmt.Fprint(w, `{ | ||
| "sbom":{ | ||
| "creationInfo":{ | ||
| "created":"2021-09-01T00:00:00Z" | ||
| }, | ||
| "name":"owner/repo", | ||
| "packages":[ | ||
| { | ||
| "name":"rubygems:rails", | ||
| "versionInfo":"1.0.0" | ||
| } | ||
| ] | ||
| } | ||
| }`) | ||
| }) | ||
|
|
||
| ctx := context.Background() | ||
| sbom, _, err := client.DependencyGraph.GetSBOM(ctx, "owner", "repo") | ||
| if err != nil { | ||
| t.Errorf("DependencyGraph.GetSBOM returned error: %v", err) | ||
| } | ||
|
|
||
| testTime := time.Date(2021, 9, 1, 0, 0, 0, 0, time.UTC) | ||
| want := &SBOM{ | ||
| &SBOMInfo{ | ||
| CreationInfo: &CreationInfo{ | ||
| Created: &Timestamp{testTime}, | ||
| }, | ||
| Name: String("owner/repo"), | ||
| Packages: []*RepoDependencies{ | ||
| { | ||
| Name: String("rubygems:rails"), | ||
| VersionInfo: String("1.0.0"), | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| if !cmp.Equal(sbom, want) { | ||
| t.Errorf("DependencyGraph.GetSBOM returned %+v, want %+v", sbom, want) | ||
| } | ||
|
|
||
| const methodName = "GetSBOM" | ||
| testBadOptions(t, methodName, func() (err error) { | ||
| _, _, err = client.DependencyGraph.GetSBOM(ctx, "\n", "\n") | ||
| return err | ||
| }) | ||
|
|
||
| testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { | ||
| got, resp, err := client.DependencyGraph.GetSBOM(ctx, "owner", "repo") | ||
| if got != nil { | ||
| t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) | ||
| } | ||
| return resp, err | ||
| }) | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.