Skip to content

Commit 016b4af

Browse files
authored
Update 'pack this' result script to update new status comment (#57696)
1 parent ddc417b commit 016b4af

File tree

1 file changed

+75
-31
lines changed

1 file changed

+75
-31
lines changed

scripts/post-vsts-artifact-comment.mjs

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,29 @@ import assert from "assert";
55
import ado from "azure-devops-node-api";
66
import fetch from "node-fetch";
77

8+
/**
9+
* @param {string} name
10+
*/
11+
function mustGetEnv(name) {
12+
const value = process.env[name];
13+
assert(value, `No ${name} specified`);
14+
return value;
15+
}
16+
17+
const REQUESTING_USER = mustGetEnv("REQUESTING_USER");
18+
const SOURCE_ISSUE = +mustGetEnv("SOURCE_ISSUE");
19+
const BUILD_BUILDID = +mustGetEnv("BUILD_BUILDID");
20+
const DISTINCT_ID = mustGetEnv("DISTINCT_ID");
21+
22+
const gh = new Octokit({
23+
auth: process.argv[2],
24+
});
25+
826
async function main() {
9-
if (!process.env.SOURCE_ISSUE) {
10-
throw new Error("No source issue specified");
11-
}
12-
if (!process.env.BUILD_BUILDID) {
13-
throw new Error("No build ID specified");
14-
}
1527
// The pipelines API does _not_ make getting the direct URL to a specific file _within_ an artifact trivial
1628
const cli = new ado.WebApi("https://typescript.visualstudio.com/defaultcollection", ado.getHandlerFromToken("")); // Empty token, anon auth
1729
const build = await cli.getBuildApi();
18-
const artifact = await build.getArtifact("typescript", +process.env.BUILD_BUILDID, "tgz");
30+
const artifact = await build.getArtifact("typescript", BUILD_BUILDID, "tgz");
1931
assert(artifact.resource?.url);
2032
const updatedUrl = new URL(artifact.resource.url);
2133
updatedUrl.search = `artifactName=tgz&fileId=${artifact.resource.data}&fileName=manifest`;
@@ -24,18 +36,11 @@ async function main() {
2436
const tgzUrl = new URL(artifact.resource.url);
2537
tgzUrl.search = `artifactName=tgz&fileId=${file.blob.id}&fileName=${file.path}`;
2638
const link = "" + tgzUrl;
27-
const gh = new Octokit({
28-
auth: process.argv[2],
29-
});
3039

3140
// Please keep the strings "an installable tgz" and "packed" in this message, as well as the devDependencies section,
3241
// so that the playgrounds deployment process can find these comments.
3342

34-
await gh.issues.createComment({
35-
issue_number: +process.env.SOURCE_ISSUE,
36-
owner: "Microsoft",
37-
repo: "TypeScript",
38-
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:
43+
const comment = `Hey @${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:
3944
\`\`\`
4045
{
4146
"devDependencies": {
@@ -44,26 +49,65 @@ async function main() {
4449
}
4550
\`\`\`
4651
and then running \`npm install\`.
47-
`,
48-
});
52+
`;
4953

5054
// Temporarily disable until we get access controls set up right
5155
// Send a ping to https://github.com/microsoft/typescript-make-monaco-builds#pull-request-builds
52-
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" } });
56+
await gh.request("POST /repos/microsoft/typescript-make-monaco-builds/dispatches", { event_type: `${SOURCE_ISSUE}`, headers: { Accept: "application/vnd.github.everest-preview+json" } });
57+
58+
return comment;
5359
}
5460

55-
main().catch(async e => {
61+
let newComment;
62+
let emoji;
63+
64+
try {
65+
newComment = await main();
66+
emoji = "✅";
67+
}
68+
catch (e) {
5669
console.error(e);
57-
process.exitCode = 1;
58-
if (process.env.SOURCE_ISSUE) {
59-
const gh = new Octokit({
60-
auth: process.argv[2],
61-
});
62-
await gh.issues.createComment({
63-
issue_number: +process.env.SOURCE_ISSUE,
64-
owner: "Microsoft",
65-
repo: "TypeScript",
66-
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)).`,
67-
});
68-
}
70+
newComment = `Hey @${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=${BUILD_BUILDID}&_a=summary)).`;
71+
emoji = "❌";
72+
}
73+
74+
const resultsComment = await gh.issues.createComment({
75+
issue_number: SOURCE_ISSUE,
76+
owner: "microsoft",
77+
repo: "TypeScript",
78+
body: newComment,
6979
});
80+
81+
const toReplace = `<!--result-${DISTINCT_ID}-->`;
82+
let posted = false;
83+
for (let i = 0; i < 5; i++) {
84+
// Get status comment contents
85+
const statusComment = await gh.rest.issues.getComment({
86+
comment_id: SOURCE_ISSUE,
87+
owner: "microsoft",
88+
repo: "TypeScript",
89+
});
90+
91+
const oldComment = statusComment.data.body;
92+
if (!oldComment?.includes(toReplace)) {
93+
posted = true;
94+
break;
95+
}
96+
97+
const newComment = oldComment.replace(toReplace, `[${emoji} Results](${resultsComment.data.html_url})`);
98+
99+
// Update status comment
100+
await gh.rest.issues.updateComment({
101+
comment_id: SOURCE_ISSUE,
102+
owner: "microsoft",
103+
repo: "TypeScript",
104+
body: newComment,
105+
});
106+
107+
// Repeat; someone may have edited the comment at the same time.
108+
await new Promise(resolve => setTimeout(resolve, 1000));
109+
}
110+
111+
if (!posted) {
112+
throw new Error("Failed to update status comment");
113+
}

0 commit comments

Comments
 (0)