Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update post-results for new bot flow #963

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading