File size: 2,159 Bytes
db5855f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// @ts-check
/* eslint-env node */

import { execSync } from 'child_process';
import decompress from 'decompress';
import { parse } from 'path';

import { NOTEBOOKS_STATUS_FILE_NAME } from './constants.js';

/**

 * @typedef {{ artifacts: { archive_download_url: string }[] }} ArtifactsResponse

 */

/**

 * @returns {string}

 */
function getLatestNotebooksStatusArtifactUrl() {
  const artifactsResponse = execSync(
    `curl -L "https://api.github.com/repos/openvinotoolkit/openvino_notebooks/actions/artifacts?per_page=1&name=${NOTEBOOKS_STATUS_FILE_NAME}"`
  ).toString();
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
  const artifactsResponseJson = /** @type {ArtifactsResponse} */ (JSON.parse(artifactsResponse));
  if (!artifactsResponseJson || !artifactsResponseJson?.artifacts?.length) {
    throw Error(
      `Unable to fetch latest artifact "${NOTEBOOKS_STATUS_FILE_NAME}" via GitHub API. Response: ${artifactsResponse}.`
    );
  }
  return artifactsResponseJson.artifacts[0].archive_download_url;
}

/**

 * @param {string} distPath

 * @returns {Promise<void>}

 */
export async function fetchNotebooksStatusFile(distPath) {
  const { GITHUB_TOKEN } = process.env;
  if (!GITHUB_TOKEN) {
    throw Error(`"GITHUB_TOKEN" env varible is not set. Please provide it to fetch notebooks statuses.`);
  }
  console.info(`Fetching latest notebooks status file...`);

  let artifactUrl;
  try {
    artifactUrl = getLatestNotebooksStatusArtifactUrl();
  } catch (error) {
    console.warn(error);
    console.warn('Notebooks status file is not downloaded.');
    return;
  }
  const artifactArchiveFileName = `${parse(NOTEBOOKS_STATUS_FILE_NAME).name}.zip`;
  execSync(
    `curl -H "Accept: application/vnd.github+json" -H "Authorization: token ${GITHUB_TOKEN}" -L --fail -o ${artifactArchiveFileName} "${artifactUrl}"`
  );
  console.info(`Fetched "${artifactArchiveFileName}". Extracting...`);
  await decompress(artifactArchiveFileName, distPath);
  execSync(`rm ${artifactArchiveFileName}`);
  console.info(`Extracted "${artifactArchiveFileName}" to "${distPath}".`);
}