Skip to content

Commit 807d5cf

Browse files
authored
Add support for Dependency Graph Snapshots endpoint (#2856)
1 parent 5868a66 commit 807d5cf

4 files changed

+558
-0
lines changed

github/dependency_graph_snapshots.go

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
// DependencyGraphSnapshotResolvedDependency represents a resolved dependency in a dependency graph snapshot.
14+
//
15+
// GitHub API docs: https://docs.github.com/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository
16+
type DependencyGraphSnapshotResolvedDependency struct {
17+
PackageURL *string `json:"package_url,omitempty"`
18+
// Represents whether the dependency is requested directly by the manifest or is a dependency of another dependency.
19+
// Can have the following values:
20+
// - "direct": indicates that the dependency is requested directly by the manifest.
21+
// - "indirect": indicates that the dependency is a dependency of another dependency.
22+
Relationship *string `json:"relationship,omitempty"`
23+
// Represents whether the dependency is required for the primary build artifact or is only used for development.
24+
// Can have the following values:
25+
// - "runtime": indicates that the dependency is required for the primary build artifact.
26+
// - "development": indicates that the dependency is only used for development.
27+
Scope *string `json:"scope,omitempty"`
28+
Dependencies []string `json:"dependencies,omitempty"`
29+
}
30+
31+
// DependencyGraphSnapshotJob represents the job that created the snapshot.
32+
//
33+
// GitHub API docs: https://docs.github.com/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository
34+
type DependencyGraphSnapshotJob struct {
35+
Correlator *string `json:"correlator,omitempty"`
36+
ID *string `json:"id,omitempty"`
37+
HTMLURL *string `json:"html_url,omitempty"`
38+
}
39+
40+
// DependencyGraphSnapshotDetector represents a description of the detector used.
41+
//
42+
// GitHub API docs: https://docs.github.com/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository
43+
type DependencyGraphSnapshotDetector struct {
44+
Name *string `json:"name,omitempty"`
45+
Version *string `json:"version,omitempty"`
46+
URL *string `json:"url,omitempty"`
47+
}
48+
49+
// DependencyGraphSnapshotManifestFile represents the file declaring the repository's dependencies.
50+
//
51+
// GitHub API docs: https://docs.github.com/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository
52+
type DependencyGraphSnapshotManifestFile struct {
53+
SourceLocation *string `json:"source_location,omitempty"`
54+
}
55+
56+
// DependencyGraphSnapshotManifest represents a collection of related dependencies declared in a file or representing a logical group of dependencies.
57+
//
58+
// GitHub API docs: https://docs.github.com/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository
59+
type DependencyGraphSnapshotManifest struct {
60+
Name *string `json:"name,omitempty"`
61+
File *DependencyGraphSnapshotManifestFile `json:"file,omitempty"`
62+
Resolved map[string]*DependencyGraphSnapshotResolvedDependency `json:"resolved,omitempty"`
63+
}
64+
65+
// DependencyGraphSnapshot represent a snapshot of a repository's dependencies.
66+
//
67+
// GitHub API docs: https://docs.github.com/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository
68+
type DependencyGraphSnapshot struct {
69+
Version int `json:"version"`
70+
Sha *string `json:"sha,omitempty"`
71+
Ref *string `json:"ref,omitempty"`
72+
Job *DependencyGraphSnapshotJob `json:"job,omitempty"`
73+
Detector *DependencyGraphSnapshotDetector `json:"detector,omitempty"`
74+
Scanned *Timestamp `json:"scanned,omitempty"`
75+
Manifests map[string]*DependencyGraphSnapshotManifest `json:"manifests,omitempty"`
76+
}
77+
78+
// DependencyGraphSnapshotCreationData represents the dependency snapshot's creation result.
79+
//
80+
// GitHub API docs: https://docs.github.com/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository
81+
type DependencyGraphSnapshotCreationData struct {
82+
ID int64 `json:"id"`
83+
CreatedAt *Timestamp `json:"created_at,omitempty"`
84+
Message *string `json:"message,omitempty"`
85+
// Represents the snapshot creation result.
86+
// Can have the following values:
87+
// - "SUCCESS": indicates that the snapshot was successfully created and the repository's dependencies were updated.
88+
// - "ACCEPTED": indicates that the snapshot was successfully created, but the repository's dependencies were not updated.
89+
// - "INVALID": indicates that the snapshot was malformed.
90+
Result *string `json:"result,omitempty"`
91+
}
92+
93+
// CreateSnapshot creates a new snapshot of a repository's dependencies.
94+
//
95+
// GitHub API docs: https://docs.github.com/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository
96+
//
97+
//meta:operation POST /repos/{owner}/{repo}/dependency-graph/snapshots
98+
func (s *DependencyGraphService) CreateSnapshot(ctx context.Context, owner, repo string, dependencyGraphSnapshot *DependencyGraphSnapshot) (*DependencyGraphSnapshotCreationData, *Response, error) {
99+
url := fmt.Sprintf("repos/%v/%v/dependency-graph/snapshots", owner, repo)
100+
101+
req, err := s.client.NewRequest("POST", url, dependencyGraphSnapshot)
102+
if err != nil {
103+
return nil, nil, err
104+
}
105+
106+
var snapshotCreationData *DependencyGraphSnapshotCreationData
107+
resp, err := s.client.Do(ctx, req, &snapshotCreationData)
108+
if err != nil {
109+
return nil, resp, err
110+
}
111+
112+
return snapshotCreationData, resp, nil
113+
}
+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
"net/http"
12+
"testing"
13+
"time"
14+
15+
"github.com/google/go-cmp/cmp"
16+
)
17+
18+
func TestDependencyGraphService_CreateSnapshot(t *testing.T) {
19+
client, mux, _, teardown := setup()
20+
defer teardown()
21+
22+
mux.HandleFunc("/repos/o/r/dependency-graph/snapshots", func(w http.ResponseWriter, r *http.Request) {
23+
testMethod(t, r, "POST")
24+
testBody(t, r, `{"version":0,"sha":"ce587453ced02b1526dfb4cb910479d431683101","ref":"refs/heads/main","job":{"correlator":"yourworkflowname_youractionname","id":"yourrunid","html_url":"https://example.com"},"detector":{"name":"octo-detector","version":"0.0.1","url":"https://github.com/octo-org/octo-repo"},"scanned":"2022-06-14T20:25:00Z","manifests":{"package-lock.json":{"name":"package-lock.json","file":{"source_location":"src/package-lock.json"},"resolved":{"@actions/core":{"package_url":"pkg:/npm/%40actions/[email protected]","relationship":"direct","scope":"runtime","dependencies":["@actions/http-client"]},"@actions/http-client":{"package_url":"pkg:/npm/%40actions/[email protected]","relationship":"indirect","scope":"runtime","dependencies":["tunnel"]},"tunnel":{"package_url":"pkg:/npm/[email protected]","relationship":"indirect","scope":"runtime"}}}}}`+"\n")
25+
fmt.Fprint(w, `{"id":12345,"created_at":"2022-06-14T20:25:01Z","message":"Dependency results for the repo have been successfully updated.","result":"SUCCESS"}`)
26+
})
27+
28+
ctx := context.Background()
29+
snapshot := &DependencyGraphSnapshot{
30+
Version: 0,
31+
Sha: String("ce587453ced02b1526dfb4cb910479d431683101"),
32+
Ref: String("refs/heads/main"),
33+
Job: &DependencyGraphSnapshotJob{
34+
Correlator: String("yourworkflowname_youractionname"),
35+
ID: String("yourrunid"),
36+
HTMLURL: String("https://example.com"),
37+
},
38+
Detector: &DependencyGraphSnapshotDetector{
39+
Name: String("octo-detector"),
40+
Version: String("0.0.1"),
41+
URL: String("https://github.com/octo-org/octo-repo"),
42+
},
43+
Scanned: &Timestamp{time.Date(2022, time.June, 14, 20, 25, 00, 0, time.UTC)},
44+
Manifests: map[string]*DependencyGraphSnapshotManifest{
45+
"package-lock.json": {
46+
Name: String("package-lock.json"),
47+
File: &DependencyGraphSnapshotManifestFile{SourceLocation: String("src/package-lock.json")},
48+
Resolved: map[string]*DependencyGraphSnapshotResolvedDependency{
49+
"@actions/core": {
50+
PackageURL: String("pkg:/npm/%40actions/[email protected]"),
51+
Relationship: String("direct"),
52+
Scope: String("runtime"),
53+
Dependencies: []string{"@actions/http-client"},
54+
},
55+
"@actions/http-client": {
56+
PackageURL: String("pkg:/npm/%40actions/[email protected]"),
57+
Relationship: String("indirect"),
58+
Scope: String("runtime"),
59+
Dependencies: []string{"tunnel"},
60+
},
61+
"tunnel": {
62+
PackageURL: String("pkg:/npm/[email protected]"),
63+
Relationship: String("indirect"),
64+
Scope: String("runtime"),
65+
},
66+
},
67+
},
68+
},
69+
}
70+
71+
snapshotCreationData, _, err := client.DependencyGraph.CreateSnapshot(ctx, "o", "r", snapshot)
72+
if err != nil {
73+
t.Errorf("DependencyGraph.CreateSnapshot returned error: %v", err)
74+
}
75+
76+
want := &DependencyGraphSnapshotCreationData{
77+
ID: 12345,
78+
CreatedAt: &Timestamp{time.Date(2022, time.June, 14, 20, 25, 01, 0, time.UTC)},
79+
Message: String("Dependency results for the repo have been successfully updated."),
80+
Result: String("SUCCESS"),
81+
}
82+
if !cmp.Equal(snapshotCreationData, want) {
83+
t.Errorf("DependencyGraph.CreateSnapshot returned %+v, want %+v", snapshotCreationData, want)
84+
}
85+
86+
const methodName = "CreateSnapshot"
87+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
88+
got, resp, err := client.DependencyGraph.CreateSnapshot(ctx, "o", "r", snapshot)
89+
if got != nil {
90+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
91+
}
92+
return resp, err
93+
})
94+
}

github/github-accessors.go

+160
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)