github.com/google/go-github/v70@v70.0.0/github/dependency_graph_snapshots_test.go (about) 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 t.Parallel() 20 client, mux, _ := setup(t) 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/core@1.1.9","relationship":"direct","scope":"runtime","dependencies":["@actions/http-client"]},"@actions/http-client":{"package_url":"pkg:/npm/%40actions/http-client@1.0.7","relationship":"indirect","scope":"runtime","dependencies":["tunnel"]},"tunnel":{"package_url":"pkg:/npm/tunnel@0.0.6","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: Ptr("ce587453ced02b1526dfb4cb910479d431683101"), 32 Ref: Ptr("refs/heads/main"), 33 Job: &DependencyGraphSnapshotJob{ 34 Correlator: Ptr("yourworkflowname_youractionname"), 35 ID: Ptr("yourrunid"), 36 HTMLURL: Ptr("https://example.com"), 37 }, 38 Detector: &DependencyGraphSnapshotDetector{ 39 Name: Ptr("octo-detector"), 40 Version: Ptr("0.0.1"), 41 URL: Ptr("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: Ptr("package-lock.json"), 47 File: &DependencyGraphSnapshotManifestFile{SourceLocation: Ptr("src/package-lock.json")}, 48 Resolved: map[string]*DependencyGraphSnapshotResolvedDependency{ 49 "@actions/core": { 50 PackageURL: Ptr("pkg:/npm/%40actions/core@1.1.9"), 51 Relationship: Ptr("direct"), 52 Scope: Ptr("runtime"), 53 Dependencies: []string{"@actions/http-client"}, 54 }, 55 "@actions/http-client": { 56 PackageURL: Ptr("pkg:/npm/%40actions/http-client@1.0.7"), 57 Relationship: Ptr("indirect"), 58 Scope: Ptr("runtime"), 59 Dependencies: []string{"tunnel"}, 60 }, 61 "tunnel": { 62 PackageURL: Ptr("pkg:/npm/tunnel@0.0.6"), 63 Relationship: Ptr("indirect"), 64 Scope: Ptr("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: Ptr("Dependency results for the repo have been successfully updated."), 80 Result: Ptr("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 }