Skip to content

Commit

Permalink
Update post-results for new bot flow (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey authored Mar 2, 2024
1 parent 74419b5 commit 138c83e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .changeset/good-badgers-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
55 changes: 39 additions & 16 deletions packages/dtslint-runner/src/post-results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import glob = require("glob");

type Errors = { path: string; error: string }[];

// Args: [auth token] [buildId] [status comment] [user to tag] [issue] [job status] [?main errors file] [?branch errors file]
// Args: [auth token] [buildId] [status comment] [user to tag] [issue] [job status] [distinct id] [?main errors file] [?branch errors file]
async function main() {
const [auth, buildId, statusCommentId, userToTag, issue, status, mainErrorsPath, branchErrorsPath] =
const [auth, buildId, statusCommentId, userToTag, issue, status, distinctId, mainErrorsPath, branchErrorsPath] =
process.argv.slice(2);
if (!auth) throw new Error("First argument must be a GitHub auth token.");
if (!buildId) throw new Error("Second argument must be a build id.");
if (!statusCommentId) throw new Error("Third argument must be a GitHub comment id.");
if (!userToTag) throw new Error("Fourth argument must be a GitHub username.");
if (!issue) throw new Error("Fifth argument must be a TypeScript issue/PR number.");
if (!status) throw new Error("Sixth argument must be a status ('ok' or 'fail').");
if (!distinctId) throw new Error("Seventh argument must be a distinct ID.");

const gh = new Octokit({ auth });
const checkLogsMessage = `\n[You can check the log here](https://typescript.visualstudio.com/TypeScript/_build/index?buildId=${buildId}&_a=summary).`;
Expand Down Expand Up @@ -55,29 +56,51 @@ async function main() {
newComment += checkLogsMessage;
}

const response = await gh.issues.createComment({
const resultsComment = await gh.issues.createComment({
issue_number: +issue,
owner: "Microsoft",
repo: "TypeScript",
body: newComment,
});

const newCommentUrl = response.data.html_url;
const statusComment = await gh.issues.getComment({
owner: "Microsoft",
repo: "TypeScript",
comment_id: +statusCommentId,
});
const emoji = status !== "fail" ? "✅" : "👀";

const toReplace = `<!--result-${distinctId}-->`;
let posted = false;
for (let i = 0; i < 5; i++) {
// Get status comment contents
const statusComment = await gh.rest.issues.getComment({
comment_id: +statusCommentId,
owner: "Microsoft",
repo: "TypeScript",
});

const oldComment = statusComment.data.body;
if (!oldComment?.includes(toReplace)) {
posted = true;
break;
}

const newBody = `${statusComment.data.body}\n\nUpdate: [The results are in!](${newCommentUrl})`;
await gh.issues.updateComment({
owner: "Microsoft",
repo: "TypeScript",
comment_id: +statusCommentId,
body: newBody,
});
const newComment = oldComment.replace(toReplace, `[${emoji} Results](${resultsComment.data.html_url})`);

// Update status comment
await gh.rest.issues.updateComment({
comment_id: +statusCommentId,
owner: "Microsoft",
repo: "TypeScript",
body: newComment,
});

// Repeat; someone may have edited the comment at the same time.
await new Promise((resolve) => setTimeout(resolve, 1000));
}

if (!posted) {
throw new Error("Failed to update status comment");
}
} catch (e) {
console.error(e);
// TODO(jakebailey): is this a good idea? all that can really fail here is the GH API.
await gh.issues.createComment({
issue_number: +issue,
owner: "Microsoft",
Expand Down

0 comments on commit 138c83e

Please sign in to comment.