1import { Octokit } from "@octokit/rest";
2import assert from "assert";
3import ado from "azure-devops-node-api";
4import fetch from "node-fetch";
5
6async function main() {
7    if (!process.env.SOURCE_ISSUE) {
8        throw new Error("No source issue specified");
9    }
10    if (!process.env.BUILD_BUILDID) {
11        throw new Error("No build ID specified");
12    }
13    // The pipelines API does _not_ make getting the direct URL to a specific file _within_ an artifact trivial
14    const cli = new ado.WebApi("https://typescript.visualstudio.com/defaultcollection", ado.getHandlerFromToken("")); // Empty token, anon auth
15    const build = await cli.getBuildApi();
16    const artifact = await build.getArtifact("typescript", +process.env.BUILD_BUILDID, "tgz");
17    assert(artifact.resource?.url);
18    const updatedUrl = new URL(artifact.resource.url);
19    updatedUrl.search = `artifactName=tgz&fileId=${artifact.resource.data}&fileName=manifest`;
20    const resp = await (await fetch(`${updatedUrl}`)).json();
21    const file = /** @type {any} */ (resp).items[0];
22    const tgzUrl = new URL(artifact.resource.url);
23    tgzUrl.search = `artifactName=tgz&fileId=${file.blob.id}&fileName=${file.path}`;
24    const link = "" + tgzUrl;
25    const gh = new Octokit({
26        auth: process.argv[2]
27    });
28
29    // Please keep the strings "an installable tgz" and "packed" in this message, as well as the devDependencies section,
30    // so that the playgrounds deployment process can find these comments.
31
32    await gh.issues.createComment({
33        issue_number: +process.env.SOURCE_ISSUE,
34        owner: "Microsoft",
35        repo: "TypeScript",
36        body: `Hey @${process.env.REQUESTING_USER}, I've packed this into [an installable tgz](${link}). You can install it for testing by referencing it in your \`package.json\` like so:
37\`\`\`
38{
39    "devDependencies": {
40        "typescript": "${link}"
41    }
42}
43\`\`\`
44and then running \`npm install\`.
45`
46    });
47
48    // Temporarily disable until we get access controls set up right
49    // Send a ping to https://github.com/microsoft/typescript-make-monaco-builds#pull-request-builds
50    await gh.request("POST /repos/microsoft/typescript-make-monaco-builds/dispatches", { event_type: process.env.SOURCE_ISSUE, headers: { Accept: "application/vnd.github.everest-preview+json" } });
51}
52
53main().catch(async e => {
54    console.error(e);
55    process.exitCode = 1;
56    if (process.env.SOURCE_ISSUE) {
57        const gh = new Octokit({
58            auth: process.argv[2]
59        });
60        await gh.issues.createComment({
61            issue_number: +process.env.SOURCE_ISSUE,
62            owner: "Microsoft",
63            repo: "TypeScript",
64            body: `Hey @${process.env.REQUESTING_USER}, something went wrong when looking for the build artifact. ([You can check the log here](https://typescript.visualstudio.com/TypeScript/_build/index?buildId=${process.env.BUILD_BUILDID}&_a=summary)).`
65        });
66    }
67});
68