|
| 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 | +} |
0 commit comments