async getCircleCIArtifacts()

in functions/src/plugins/size.ts [361:404]


  async getCircleCIArtifacts(username: string, project: string, buildNumber: number, exclude?: string[], include?: string[]): Promise<BuildArtifact[]> {
    const artifactUrl = `https://circleci.com/api/v1.1/project/github/${username}/${project}/${buildNumber}/artifacts`;
    this.logDebug(`[size] Fetching artifacts for ${artifactUrl}`);

    const artifactsResponse = await fetch(artifactUrl);

    let artifacts = (await artifactsResponse.json() as CircleCiArtifact[]);
    if (include) {
      artifacts = artifacts.filter(ca => include.some(path => ca.path.startsWith(path)));
    }
    if (exclude && exclude.length > 0) {
      artifacts = artifacts.filter(ca => !exclude.some(path => ca.path.startsWith(path)));
    }

    const buildArtifacts = [];

    for (const artifact of artifacts) {
      let response = await fetch(artifact.url);
      if (response.status >= 500) {
        response = await fetch(artifact.url);
      }

      if (!response.ok) {
        throw new Error(`fetch for ${artifact.url} returned status [${response.status}]: ${response.statusText}`);
      }

      const data = await response.arrayBuffer();
      const size = data.byteLength;
      const pathParts = artifact.path.split('/');

      this.logDebug(`[size] Fetched artifact '${artifact.path}' with size ${size}`);

      buildArtifacts.push({
        path: artifact.path,
        url: artifact.url,
        size,
        projectName: pathParts.length > 1 ? pathParts[0] : undefined,
        context: pathParts.length > 2 ? pathParts.slice(1, -1).join('/') : undefined,
        filename: pathParts[pathParts.length - 1],
      });
    }

    return buildArtifacts;
  }