Create a snapshot of dependencies for a repository

Create a new snapshot of a repository's dependencies. You must authenticate using an access token with the `repo` scope to use this endpoint for a repository that the requesting user has access to.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 366 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Create a snapshot of dependencies for a repository
6
 * Create a new snapshot of a repository's dependencies. You must authenticate using an access token with the `repo` scope to use this endpoint for a repository that the requesting user has access to.
7
 */
8
export async function main(
9
  auth: Github,
10
  owner: string,
11
  repo: string,
12
  body: {
13
    detector: { name: string; url: string; version: string };
14
    job: { correlator: string; html_url?: string; id: string };
15
    manifests?: {
16
      [k: string]: {
17
        file?: { source_location?: string };
18
        metadata?: { [k: string]: string | number | boolean };
19
        name: string;
20
        resolved?: {
21
          [k: string]: {
22
            dependencies?: string[];
23
            metadata?: { [k: string]: string | number | boolean };
24
            package_url?: string;
25
            relationship?: "direct" | "indirect";
26
            scope?: "runtime" | "development";
27
          };
28
        };
29
      };
30
    };
31
    metadata?: { [k: string]: string | number | boolean };
32
    ref: string;
33
    scanned: string;
34
    sha: string;
35
    version: number;
36
  }
37
) {
38
  const url = new URL(
39
    `https://api.github.com/repos/${owner}/${repo}/dependency-graph/snapshots`
40
  );
41

42
  const response = await fetch(url, {
43
    method: "POST",
44
    headers: {
45
      "Content-Type": "application/json",
46
      Authorization: "Bearer " + auth.token,
47
    },
48
    body: JSON.stringify(body),
49
  });
50
  if (!response.ok) {
51
    const text = await response.text();
52
    throw new Error(`${response.status} ${text}`);
53
  }
54
  return await response.json();
55
}
56