Skip to content

Commit a178649

Browse files
authored
allow bot to operate with non "typescript" repos (#119)
1 parent 99fc645 commit a178649

1 file changed

Lines changed: 36 additions & 23 deletions

File tree

GithubCommentReader/index.js

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,23 @@ async function sleep(ms) {
6464
* pr: PR | undefined;
6565
* requestingUser: string;
6666
* statusCommentId: number; // TODO(jakebailey): rename this
67+
* owner: string;
68+
* repo: string;
6769
* }} RequestInfo
6870
* @typedef {(request: RequestInfo) => Promise<Run>} CommandFn
69-
* @typedef {{ fn: CommandFn; authorAssociations: AuthorAssociation[]; prOnly: boolean }} Command
71+
* @typedef {{ fn: CommandFn; authorAssociations: AuthorAssociation[]; prOnly: boolean, tsgoAllowed: boolean }} Command
7072
*/
7173
void 0;
7274

7375
/**
7476
* @param {CommandFn} fn
7577
* @param {AuthorAssociation[]} authorAssociations
7678
* @param {boolean} prOnly
79+
* @param {boolean} tsgoAllowed
7780
* @returns {Command}
7881
*/
79-
function createCommand(fn, authorAssociations = ["MEMBER", "OWNER", "COLLABORATOR"], prOnly = true) {
80-
return { fn, authorAssociations, prOnly };
82+
function createCommand(fn, authorAssociations = ["MEMBER", "OWNER", "COLLABORATOR"], prOnly = true, tsgoAllowed = false) {
83+
return { fn, authorAssociations, prOnly, tsgoAllowed };
8184
}
8285

8386
/**
@@ -107,6 +110,8 @@ function createParameters(info, inputs) {
107110
source_issue: `${info.issueNumber}`,
108111
requesting_user: info.requestingUser,
109112
status_comment: `${info.statusCommentId}`,
113+
source_owner: info.owner,
114+
source_repo: info.repo,
110115
};
111116

112117
const requiredParameters = Object.keys(parameters);
@@ -226,7 +231,7 @@ async function createWorkflowDispatch({ workflowId, info, inputs }) {
226231
const cli = getGHClient();
227232
await cli.actions.createWorkflowDispatch({
228233
owner: "microsoft",
229-
repo: "TypeScript",
234+
repo: info.repo,
230235
ref: "main",
231236
workflow_id: workflowId,
232237
inputs: parameters,
@@ -268,7 +273,7 @@ const commands = (/** @type {Map<RegExp, Command>} */ (new Map()))
268273
sourceBranch: `refs/pull/${request.issueNumber}/merge`,
269274
info: request,
270275
inputs: {
271-
DT_SHA: (await getGHClient().repos.getBranch({owner: "DefinitelyTyped", repo: "DefinitelyTyped", branch: "master"})).data.commit.sha
276+
DT_SHA: (await getGHClient().repos.getBranch({ owner: "DefinitelyTyped", repo: "DefinitelyTyped", branch: "master" })).data.commit.sha
272277
}
273278
})
274279
}))
@@ -314,7 +319,11 @@ const commands = (/** @type {Map<RegExp, Command>} */ (new Map()))
314319
repo_count: `${Math.max(+request.match[1], 400)}`,
315320
}
316321
})
317-
}))
322+
},
323+
/* authorAssociations */ undefined,
324+
/* prOnly */ undefined,
325+
/* tsgoAllowed */ true,
326+
))
318327
.set(/test tsserver top(\d{1,3})/, createCommand(async (request) => {
319328
assert(request.pr);
320329
return queueBuild({
@@ -540,7 +549,8 @@ const testItCommandToRun = [
540549
* commentIsFromIssue: boolean;
541550
* isPr: boolean;
542551
* commentUser: string;
543-
* authorAssociation: AuthorAssociation
552+
* authorAssociation: AuthorAssociation;
553+
* repo: string;
544554
* }} WebhookParams
545555
* @param {WebhookParams} params */
546556
async function webhook(params) {
@@ -561,8 +571,9 @@ async function webhook(params) {
561571
}
562572
lines = [...new Set(lines)];
563573

574+
const tsgo = params.repo.includes("typescript-go")
564575
const applicableCommands = Array.from(commands.entries()).filter(([, command]) => {
565-
if (!params.isPr && command.prOnly) {
576+
if (!params.isPr && command.prOnly && tsgo && !command.tsgoAllowed) {
566577
return false;
567578
}
568579
return command.authorAssociations.includes(params.authorAssociation);
@@ -605,7 +616,7 @@ async function webhook(params) {
605616
const createReaction = params.commentIsFromIssue ? cli.reactions.createForIssueComment : cli.reactions.createForPullRequestReviewComment;
606617
await createReaction({
607618
owner: "microsoft",
608-
repo: "TypeScript",
619+
repo: params.repo,
609620
comment_id: params.commentId,
610621
content: "+1",
611622
});
@@ -617,19 +628,19 @@ async function webhook(params) {
617628
let pr;
618629

619630
if (params.isPr) {
620-
pr = (await cli.pulls.get({ pull_number: params.issueNumber, owner: "microsoft", repo: "TypeScript" })).data;
631+
pr = (await cli.pulls.get({ pull_number: params.issueNumber, owner: "microsoft", repo: params.repo })).data;
621632

622633
if (!pr.merged && !pr.mergeable) {
623634
await cli.issues.createComment({
624635
owner: "microsoft",
625-
repo: "TypeScript",
636+
repo: params.repo,
626637
issue_number: params.issueNumber,
627638
body: `Hey @${params.commentUser}, this PR is in an unmergable state, so is missing a merge commit to run against; please resolve conflicts and try again.`,
628639
});
629640
return;
630641
}
631642
}
632-
643+
633644
const start = Date.now();
634645
const created = `>=${new Date(start).toISOString()}`;
635646

@@ -640,18 +651,17 @@ Starting jobs; this comment will be updated as builds start and complete.
640651
641652
| Command | Status | Results |
642653
| ------- | ------ | ------- |
643-
${
644-
commandInfos.map(({ name, distinctId }) =>
645-
`| \`${name}\` | ${getStatusPlaceholder(distinctId)} | ${getResultPlaceholder(distinctId)} |`
646-
)
654+
${commandInfos.map(({ name, distinctId }) =>
655+
`| \`${name}\` | ${getStatusPlaceholder(distinctId)} | ${getResultPlaceholder(distinctId)} |`
656+
)
647657
.join("\n")
648-
}
658+
}
649659
`.trim();
650660

651661
log("Creating status comment");
652662
const statusComment = await cli.issues.createComment({
653663
owner: "microsoft",
654-
repo: "TypeScript",
664+
repo: params.repo,
655665
issue_number: params.issueNumber,
656666
body: statusCommentBody,
657667
});
@@ -670,6 +680,8 @@ ${
670680
requestingUser: params.commentUser,
671681
pr,
672682
log: log,
683+
owner: "microsoft",
684+
repo: params.repo,
673685
});
674686
} catch (e) {
675687
// TODO: short error message
@@ -683,7 +695,7 @@ ${
683695
async function updateComment() {
684696
const comment = await cli.issues.getComment({
685697
owner: "microsoft",
686-
repo: "TypeScript",
698+
repo: params.repo,
687699
comment_id: statusCommentId,
688700
});
689701

@@ -720,7 +732,7 @@ ${
720732

721733
await cli.issues.updateComment({
722734
owner: "microsoft",
723-
repo: "TypeScript",
735+
repo: params.repo,
724736
comment_id: statusCommentId,
725737
body,
726738
});
@@ -740,7 +752,7 @@ ${
740752

741753
const response = await cli.actions.listWorkflowRunsForRepo({
742754
owner: "microsoft",
743-
repo: "TypeScript",
755+
repo: params.repo,
744756
created,
745757
exclude_pull_requests: true,
746758
});
@@ -789,6 +801,7 @@ async function handler(request, context) {
789801
return {};
790802
}
791803

804+
const repoName = event.repository.name;
792805
const commentIsFromIssue = "comment" in event;
793806
const comment = commentIsFromIssue ? event.comment : event.review;
794807
if (!comment.body) {
@@ -800,8 +813,7 @@ async function handler(request, context) {
800813
|| !!("issue" in event && event.issue && event.issue.pull_request);
801814

802815
const issueNumber = "issue" in event ? event.issue.number : event.pull_request.number;
803-
804-
context.log(`Processing comment ${comment.id} on ${isPr ? "PR" : "issue"} ${issueNumber} by ${comment.user.login} (${comment.author_association})`)
816+
context.log(`Processing comment ${comment.id} on microsoft/${repoName} ${isPr ? "PR" : "issue"} ${issueNumber} by ${comment.user.login} (${comment.author_association})`)
805817

806818
try {
807819
await webhook({
@@ -814,6 +826,7 @@ async function handler(request, context) {
814826
isPr,
815827
commentUser: comment.user.login,
816828
authorAssociation: comment.author_association,
829+
repo: repoName,
817830
});
818831
} catch (e) {
819832
context.log(`Error processing comment: ${e}`);

0 commit comments

Comments
 (0)