From 3ede035fe45c42d11725788d5fb3442ebdd6eed4 Mon Sep 17 00:00:00 2001 From: be0x74a Date: Sun, 28 Apr 2024 23:01:01 +0100 Subject: [PATCH] Address PR review comments --- github/dependency_graph_snapshots.go | 74 ++++++++++++++--------- github/dependency_graph_snapshots_test.go | 14 ++--- github/github-accessors.go | 24 ++++++++ github/github-accessors_test.go | 30 +++++++++ 4 files changed, 105 insertions(+), 37 deletions(-) diff --git a/github/dependency_graph_snapshots.go b/github/dependency_graph_snapshots.go index 0aec48a6ded..b2b61304f55 100644 --- a/github/dependency_graph_snapshots.go +++ b/github/dependency_graph_snapshots.go @@ -10,57 +10,61 @@ import ( "fmt" ) -// DependencyGraphSnapshotResolvedDependencyRelationship represents whether the dependency is requested directly by the manifest or is a dependency of another dependency. +// DependencyGraphSnapshotResolvedDependency represents a resolved dependency in a dependency graph snapshot. // -// Can have the following values: -// - "direct": indicates that the dependency is requested directly by the manifest. -// - "indirect": indicates that the dependency is a dependency of another dependency. -type DependencyGraphSnapshotResolvedDependencyRelationship string - -// DependencyGraphSnapshotResolvedDependencyScope represents whether the dependency is required for the primary build artifact or is only used for development. -// -// Can have the following values: -// - "runtime": indicates that the dependency is required for the primary build artifact. -// - "development": indicates that the dependency is only used for development. -type DependencyGraphSnapshotResolvedDependencyScope string - -// DependencyGraphSnapshotCreationResult represents the snapshot creation result. -// -// Can have the following values: -// - "SUCCESS": indicates that the snapshot was successfully created and the repository's dependencies were updated. -// - "ACCEPTED": indicates that the snapshot was successfully created, but the repository's dependencies were not updated. -// - "INVALID": indicates that the snapshot was malformed. -type DependencyGraphSnapshotCreationResult string - +// GitHub API docs: https://docs.github.com/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository type DependencyGraphSnapshotResolvedDependency struct { - PackageURL *string `json:"package_url,omitempty"` - Relationship DependencyGraphSnapshotResolvedDependencyRelationship `json:"relationship,omitempty"` - Scope DependencyGraphSnapshotResolvedDependencyScope `json:"scope,omitempty"` - Dependencies []string `json:"dependencies,omitempty"` + PackageURL *string `json:"package_url,omitempty"` + // Represents whether the dependency is requested directly by the manifest or is a dependency of another dependency. + // Can have the following values: + // - "direct": indicates that the dependency is requested directly by the manifest. + // - "indirect": indicates that the dependency is a dependency of another dependency. + Relationship *string `json:"relationship,omitempty"` + // Represents whether the dependency is required for the primary build artifact or is only used for development. + // Can have the following values: + // - "runtime": indicates that the dependency is required for the primary build artifact. + // - "development": indicates that the dependency is only used for development. + Scope *string `json:"scope,omitempty"` + Dependencies []string `json:"dependencies,omitempty"` } +// DependencyGraphSnapshotJob represents the job that created the snapshot. +// +// GitHub API docs: https://docs.github.com/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository type DependencyGraphSnapshotJob struct { Correlator *string `json:"correlator,omitempty"` ID *string `json:"id,omitempty"` HTMLURL *string `json:"html_url,omitempty"` } +// DependencyGraphSnapshotDetector represents a description of the detector used. +// +// GitHub API docs: https://docs.github.com/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository type DependencyGraphSnapshotDetector struct { Name *string `json:"name,omitempty"` Version *string `json:"version,omitempty"` URL *string `json:"url,omitempty"` } +// DependencyGraphSnapshotManifestFile represents the file declaring the repository's dependencies. +// +// GitHub API docs: https://docs.github.com/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository type DependencyGraphSnapshotManifestFile struct { SourceLocation *string `json:"source_location,omitempty"` } +// DependencyGraphSnapshotManifest represents a collection of related dependencies declared in a file or representing a logical group of dependencies. +// +// GitHub API docs: https://docs.github.com/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository type DependencyGraphSnapshotManifest struct { Name *string `json:"name,omitempty"` File *DependencyGraphSnapshotManifestFile `json:"file,omitempty"` Resolved map[string]*DependencyGraphSnapshotResolvedDependency `json:"resolved,omitempty"` } +// DependencyGraphSnapshot represent a snapshot of a repository's dependencies. +// +// GitHub API docs: https://docs.github.com/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository type DependencyGraphSnapshot struct { Version int `json:"version"` Sha *string `json:"sha,omitempty"` @@ -71,16 +75,26 @@ type DependencyGraphSnapshot struct { Manifests map[string]*DependencyGraphSnapshotManifest `json:"manifests,omitempty"` } +// DependencyGraphSnapshotCreationData represents the dependency snapshot's creation result. +// +// GitHub API docs: https://docs.github.com/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository type DependencyGraphSnapshotCreationData struct { - ID int `json:"id"` - CreatedAt *Timestamp `json:"created_at"` - Message *string `json:"message"` - Result DependencyGraphSnapshotCreationResult `json:"result"` + ID int `json:"id"` + CreatedAt *Timestamp `json:"created_at"` + Message *string `json:"message"` + // Represents the snapshot creation result. + // Can have the following values: + // - "SUCCESS": indicates that the snapshot was successfully created and the repository's dependencies were updated. + // - "ACCEPTED": indicates that the snapshot was successfully created, but the repository's dependencies were not updated. + // - "INVALID": indicates that the snapshot was malformed. + Result *string `json:"result"` } // CreateSnapshot creates a new snapshot of a repository's dependencies. // -// GitHub API docs: https://docs.github.com/en/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository +// GitHub API docs: https://docs.github.com/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository +// +//meta:operation POST /repos/{owner}/{repo}/dependency-graph/snapshots func (s *DependencyGraphService) CreateSnapshot(ctx context.Context, owner, repo string, dependencyGraphSnapshot *DependencyGraphSnapshot) (*DependencyGraphSnapshotCreationData, *Response, error) { url := fmt.Sprintf("repos/%v/%v/dependency-graph/snapshots", owner, repo) diff --git a/github/dependency_graph_snapshots_test.go b/github/dependency_graph_snapshots_test.go index c9a2cc8345a..ed70ed574d2 100644 --- a/github/dependency_graph_snapshots_test.go +++ b/github/dependency_graph_snapshots_test.go @@ -48,20 +48,20 @@ func TestDependencyGraphService_CreateSnapshot(t *testing.T) { Resolved: map[string]*DependencyGraphSnapshotResolvedDependency{ "@actions/core": { PackageURL: String("pkg:/npm/%40actions/core@1.1.9"), - Relationship: "direct", - Scope: "runtime", + Relationship: String("direct"), + Scope: String("runtime"), Dependencies: []string{"@actions/http-client"}, }, "@actions/http-client": { PackageURL: String("pkg:/npm/%40actions/http-client@1.0.7"), - Relationship: "indirect", - Scope: "runtime", + Relationship: String("indirect"), + Scope: String("runtime"), Dependencies: []string{"tunnel"}, }, "tunnel": { PackageURL: String("pkg:/npm/tunnel@0.0.6"), - Relationship: "indirect", - Scope: "runtime", + Relationship: String("indirect"), + Scope: String("runtime"), }, }, }, @@ -77,7 +77,7 @@ func TestDependencyGraphService_CreateSnapshot(t *testing.T) { ID: 12345, CreatedAt: &Timestamp{time.Date(2022, time.June, 14, 20, 25, 01, 0, time.UTC)}, Message: String("Dependency results for the repo have been successfully updated."), - Result: "SUCCESS", + Result: String("SUCCESS"), } if !cmp.Equal(snapshotCreationData, want) { t.Errorf("DependencyGraph.CreateSnapshot returned %+v, want %+v", snapshotCreationData, want) diff --git a/github/github-accessors.go b/github/github-accessors.go index 6acb5b3150a..fc83a9509b4 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -5262,6 +5262,14 @@ func (d *DependencyGraphSnapshotCreationData) GetMessage() string { return *d.Message } +// GetResult returns the Result field if it's non-nil, zero value otherwise. +func (d *DependencyGraphSnapshotCreationData) GetResult() string { + if d == nil || d.Result == nil { + return "" + } + return *d.Result +} + // GetName returns the Name field if it's non-nil, zero value otherwise. func (d *DependencyGraphSnapshotDetector) GetName() string { if d == nil || d.Name == nil { @@ -5342,6 +5350,22 @@ func (d *DependencyGraphSnapshotResolvedDependency) GetPackageURL() string { return *d.PackageURL } +// GetRelationship returns the Relationship field if it's non-nil, zero value otherwise. +func (d *DependencyGraphSnapshotResolvedDependency) GetRelationship() string { + if d == nil || d.Relationship == nil { + return "" + } + return *d.Relationship +} + +// GetScope returns the Scope field if it's non-nil, zero value otherwise. +func (d *DependencyGraphSnapshotResolvedDependency) GetScope() string { + if d == nil || d.Scope == nil { + return "" + } + return *d.Scope +} + // GetAction returns the Action field if it's non-nil, zero value otherwise. func (d *DeployKeyEvent) GetAction() string { if d == nil || d.Action == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index f861002ab95..ee15610f9c2 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -6176,6 +6176,16 @@ func TestDependencyGraphSnapshotCreationData_GetMessage(tt *testing.T) { d.GetMessage() } +func TestDependencyGraphSnapshotCreationData_GetResult(tt *testing.T) { + var zeroValue string + d := &DependencyGraphSnapshotCreationData{Result: &zeroValue} + d.GetResult() + d = &DependencyGraphSnapshotCreationData{} + d.GetResult() + d = nil + d.GetResult() +} + func TestDependencyGraphSnapshotDetector_GetName(tt *testing.T) { var zeroValue string d := &DependencyGraphSnapshotDetector{Name: &zeroValue} @@ -6273,6 +6283,26 @@ func TestDependencyGraphSnapshotResolvedDependency_GetPackageURL(tt *testing.T) d.GetPackageURL() } +func TestDependencyGraphSnapshotResolvedDependency_GetRelationship(tt *testing.T) { + var zeroValue string + d := &DependencyGraphSnapshotResolvedDependency{Relationship: &zeroValue} + d.GetRelationship() + d = &DependencyGraphSnapshotResolvedDependency{} + d.GetRelationship() + d = nil + d.GetRelationship() +} + +func TestDependencyGraphSnapshotResolvedDependency_GetScope(tt *testing.T) { + var zeroValue string + d := &DependencyGraphSnapshotResolvedDependency{Scope: &zeroValue} + d.GetScope() + d = &DependencyGraphSnapshotResolvedDependency{} + d.GetScope() + d = nil + d.GetScope() +} + func TestDeployKeyEvent_GetAction(tt *testing.T) { var zeroValue string d := &DeployKeyEvent{Action: &zeroValue}