-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from presubmit/dev
Added Pull Request Comment Handling Infrastructure
- Loading branch information
Showing
9 changed files
with
426 additions
and
139 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { Octokit } from "@octokit/action"; | ||
import { COMMENT_SIGNATURE } from "./messages"; | ||
|
||
export type ReviewComment = { | ||
path: string; | ||
body: string; | ||
diff_hunk?: string; | ||
line?: number; | ||
in_reply_to_id?: number; | ||
id: number; | ||
start_line?: number | null; | ||
user: { | ||
login: string; | ||
}; | ||
}; | ||
|
||
export type ReviewCommentThread = { | ||
file: string; | ||
comments: ReviewComment[]; | ||
}; | ||
|
||
export async function listPullRequestCommentThreads( | ||
octokit: Octokit, | ||
{ | ||
owner, | ||
repo, | ||
pull_number, | ||
}: { owner: string; repo: string; pull_number: number } | ||
): Promise<ReviewCommentThread[]> { | ||
let { data: comments } = await octokit.rest.pulls.listReviewComments({ | ||
owner, | ||
repo, | ||
pull_number, | ||
}); | ||
|
||
comments = comments.map((c) => ({ | ||
...c, | ||
user: { | ||
...c.user, | ||
login: isOwnComment(c.body) ? "presubmit" : c.user.login, | ||
}, | ||
})); | ||
|
||
return generateCommentThreads(comments); | ||
} | ||
|
||
export async function getCommentThread( | ||
octokit: Octokit, | ||
{ | ||
owner, | ||
repo, | ||
pull_number, | ||
comment_id, | ||
}: { owner: string; repo: string; pull_number: number; comment_id: number } | ||
): Promise<ReviewCommentThread | null> { | ||
const threads = await listPullRequestCommentThreads(octokit, { | ||
owner, | ||
repo, | ||
pull_number, | ||
}); | ||
return ( | ||
threads.find((t) => t.comments.some((c) => c.id === comment_id)) || null | ||
); | ||
} | ||
|
||
export function isThreadRelevant(thread: ReviewCommentThread): boolean { | ||
return thread.comments.some( | ||
(c) => | ||
c.body.includes(COMMENT_SIGNATURE) || | ||
c.body.includes("@presubmitai") || | ||
c.body.includes("@presubmit") | ||
); | ||
} | ||
|
||
function generateCommentThreads( | ||
reviewComments: ReviewComment[] | ||
): ReviewCommentThread[] { | ||
const topLevelComments = reviewComments.filter( | ||
(c) => !c.in_reply_to_id && c.body.length && !!c.line | ||
); | ||
|
||
return topLevelComments.map((topLevelComment) => { | ||
return { | ||
file: topLevelComment.path, | ||
comments: [ | ||
topLevelComment, | ||
...reviewComments.filter( | ||
(c) => c.in_reply_to_id === topLevelComment.id | ||
), | ||
], | ||
}; | ||
}); | ||
} | ||
|
||
export function isOwnComment(comment: string): boolean { | ||
return comment.includes(COMMENT_SIGNATURE); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.