Skip to content

Commit c90f032

Browse files
authored
Update postGithubComments for new bot flow (#132)
1 parent a183054 commit c90f032

File tree

3 files changed

+42
-24
lines changed

3 files changed

+42
-24
lines changed

azure-pipelines-userTests.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ parameters:
2323
displayName: Old head reference
2424
type: string
2525
default: main
26+
- name: DISTINCT_ID
27+
displayName: Distinct ID
28+
type: string
2629
- name: REQUESTING_USER
2730
displayName: User to tag when the results are ready
2831
type: string
@@ -119,7 +122,7 @@ jobs:
119122
- script: |
120123
npm ci
121124
npm run build
122-
node dist/postGithubComments ${{ parameters.REQUESTING_USER }} ${{ parameters.SOURCE_ISSUE }} ${{ parameters.STATUS_COMMENT }} ${{ parameters.TOP_REPOS }} '$(Pipeline.Workspace)' '$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_build/results?buildId=$(Build.BuildId)&view=artifacts&type=publishedArtifacts' ${{ parameters.POST_RESULT }}
125+
node dist/postGithubComments ${{ parameters.REQUESTING_USER }} ${{ parameters.SOURCE_ISSUE }} ${{ parameters.STATUS_COMMENT }} ${{ parameters.DISTINCT_ID }} ${{ parameters.TOP_REPOS }} '$(Pipeline.Workspace)' '$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_build/results?buildId=$(Build.BuildId)&view=artifacts&type=publishedArtifacts' ${{ parameters.POST_RESULT }}
123126
displayName: 'Update PR comment with new errors'
124127
env:
125128
GITHUB_PAT: $(GITHUB_PAT)

src/postGithubComments.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import pu = require("./utils/packageUtils");
77
const { argv } = process;
88

99
if (argv.length !== 9) {
10-
console.error(`Usage: ${path.basename(argv[0])} ${path.basename(argv[1])} <user_to_tag> <pr_number> <comment_number> <is_top_repos_run> <result_dir_path> <artifacts_uri> <post_result>`);
10+
console.error(`Usage: ${path.basename(argv[0])} ${path.basename(argv[1])} <user_to_tag> <pr_number> <comment_number> <distinct_id> <is_top_repos_run> <result_dir_path> <artifacts_uri> <post_result>`);
1111
process.exit(-1);
1212
}
1313

14-
const [, , userToTag, prNumber, commentNumber, isTop, resultDirPath, artifactsUri, post] = argv;
14+
const [, , userToTag, prNumber, commentNumber, distinctId, isTop, resultDirPath, artifactsUri, post] = argv;
1515
const isTopReposRun = isTop.toLowerCase() === "true";
1616
const postResult = post.toLowerCase() === "true";
1717

@@ -78,7 +78,7 @@ let header = `@${userToTag} Here are the results of running the ${suiteDescripti
7878
${summary.join("\n")}`;
7979

8080
if (!outputs.length) {
81-
git.createComment(+prNumber, +commentNumber, postResult, [header]);
81+
git.createComment(+prNumber, +commentNumber, distinctId, postResult, [header], true);
8282
}
8383
else {
8484
const oldErrorHeader = `<h2>:warning: Old server errors :warning:</h2>`;
@@ -117,5 +117,5 @@ else {
117117
console.log(`Chunk of size ${chunk.length}`);
118118
}
119119

120-
git.createComment(+prNumber, +commentNumber, postResult, bodyChunks);
120+
git.createComment(+prNumber, +commentNumber, distinctId, postResult, bodyChunks, somethingChanged);
121121
}

src/utils/gitUtils.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export async function createIssue(postResult: boolean, title: string, bodyChunks
149149
}
150150
}
151151

152-
export async function createComment(prNumber: number, statusComment: number, postResult: boolean, bodyChunks: readonly string[]): Promise<void> {
152+
export async function createComment(prNumber: number, statusComment: number, distinctId: string, postResult: boolean, bodyChunks: readonly string[], somethingChanged: boolean): Promise<void> {
153153
const newComments = bodyChunks.map(body => ({
154154
...repoProperties,
155155
issue_number: prNumber,
@@ -181,26 +181,41 @@ export async function createComment(prNumber: number, statusComment: number, pos
181181
newCommentUrls.push(newCommentUrl);
182182
}
183183

184-
// Update typescript-bot comment
185-
const comment = await kit.issues.getComment({
186-
...repoProperties,
187-
comment_id: statusComment
188-
});
189184

190-
let newBody = `${comment.data.body}\n\n`;
191-
if (newCommentUrls.length === 1) {
192-
newBody += `Update: [The results are in!](${newCommentUrls[0]})`;
193-
}
194-
else {
195-
newBody += `Update: The results are in! `;
196-
newBody += newCommentUrls.map((url, i) => `[Part ${i + 1}](${url})`).join(", ");
197-
}
185+
const emoji = !somethingChanged ? "✅" : "👀";
198186

199-
await kit.issues.updateComment({
200-
...repoProperties,
201-
comment_id: statusComment,
202-
body: newBody
203-
});
187+
const toReplace = `<!--result-${distinctId}-->`;
188+
let posted = false;
189+
for (let i = 0; i < 5; i++) {
190+
// Get status comment contents
191+
const statusCommentResp = await kit.issues.getComment({
192+
comment_id: statusComment,
193+
owner: "Microsoft",
194+
repo: "TypeScript",
195+
});
196+
197+
const oldComment = statusCommentResp.data.body;
198+
if (!oldComment?.includes(toReplace)) {
199+
posted = true;
200+
break;
201+
}
202+
203+
const newComment = oldComment.replace(
204+
toReplace,
205+
`[${emoji} Results](${newCommentUrls[0]})`,
206+
);
207+
208+
// Update status comment
209+
await kit.issues.updateComment({
210+
comment_id: statusComment,
211+
owner: "Microsoft",
212+
repo: "TypeScript",
213+
body: newComment,
214+
});
215+
216+
// Repeat; someone may have edited the comment at the same time.
217+
await new Promise(resolve => setTimeout(resolve, 1000));
218+
}
204219
}
205220

206221
export async function checkout(cwd: string, branch: string) {

0 commit comments

Comments
 (0)