Skip to content
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
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,19 @@ Gitdog reads the source of every changed file. Files that do not fit the main
review are reviewed in further passes, and a review that still could not read
a file says so.

A pull request review with no findings needs no approval. It posts at once and,
when every changed file was reviewed and no Gitdog thread is open, approves the
reviewed commit. A later push, including updating the branch, withdraws that
approval until the new commits are reviewed.
A single review also reads the conversation: people's comments, review
summaries and open review threads. A change a reviewer asked for that the code
does not make yet is listed in the review as not yet addressed, and such a
review waits for approval like one with findings. A large pull request reviewed
in batches does not read the conversation; open threads and requested changes
still keep it from approving.

A pull request review with no findings and nothing left open needs no approval.
It posts at once and
approves the reviewed commit when every changed file was reviewed, no review
thread from anyone is open, no reviewer has requested changes, and nothing from
the discussion is left unaddressed. A later push, including updating the
branch, withdraws that approval until the new commits are reviewed.

## Pull request commands

Expand All @@ -120,8 +129,9 @@ After that, ask for more in a pull request comment or from the CLI:
works as shorthand, but it also notifies the GitHub user of that name, so
`@gitdogai` is preferred.
- **Status**: while a review runs, one status comment on the pull request shows
whether it is reviewing, waiting for approval, reviewed, or why it could not
finish. A review that failed or ran out of tokens can be requested again. For
that it is reviewing or waiting for approval. Once the review is posted the
comment is removed, so the review is the only result; if no review is posted,
the comment says why. A review that failed or ran out of tokens can be requested again. For
automatic reviews, Gitdog posts at most one failure comment a day per pull
request.
- **Limits**: someone who sends more than 10 commands an hour or 40 a day to
Expand Down
23 changes: 23 additions & 0 deletions convex/github/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,29 @@ export async function updateIssueComment(
await response.body?.cancel().catch(() => undefined);
}

// A comment already gone counts as deleted.
export async function deleteIssueComment(
owner: string,
repo: string,
commentId: number,
token: string,
): Promise<void> {
const response = await fetchGitHubApi(
`https://api.github.com/repos/${owner}/${repo}/issues/comments/${commentId}`,
{
method: "DELETE",
headers: {
Accept: "application/vnd.github.v3+json",
Authorization: `Bearer ${token}`,
"User-Agent": "Gitdog",
},
},
);
await response.body?.cancel().catch(() => undefined);
if (!response.ok && response.status !== 404)
throw new Error(`Failed to delete issue comment (HTTP ${response.status})`);
}

// Update issue title and/or body
export async function updateIssue(
owner: string,
Expand Down
192 changes: 181 additions & 11 deletions convex/pullRequests/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export type PullRequest = {
head: { sha: string };
base: { sha: string; repo: { id: number } };
changed_files: number;
// Conversation comments, for reading the newest page of them.
comments?: number;
};
export class PullRequestSizeLimitError extends Error {}
/** The whole diff is too large for one review; batches may still cover it. */
Expand Down Expand Up @@ -440,10 +442,186 @@ type ReviewThreadPage = {
* Whether a review thread Gitdog started is still unresolved. An answer that
* cannot be read completely counts as unresolved, so it never approves.
*/
export async function hasUnresolvedOwnThreads(
export type DiscussionEntry = {
author: string;
kind: "comment" | "review" | "thread";
text: string;
path?: string;
};

// The conversation shown to a reviewer is bounded: newest first, each entry
// trimmed, and the whole within this many characters.
const DISCUSSION_ENTRY_CHARS = 1_200;
const DISCUSSION_CHARS = 8_000;

function discussionText(body: unknown): string {
return typeof body === "string"
? body
// A command to Gitdog is not a request, but the rest of the comment
// may be one.
.replace(/^[ \t]*@gitdog\S*[^\n]*$/gim, " ")
.replace(/<!--[\s\S]*?(-->|$)/g, " ")
.replace(/<details[\s\S]*?(<\/details>|$)/gi, " ")
.replace(/<[^>]+>/g, " ")
.replace(/\s+/g, " ")
.trim()
: "";
}

/**
* What people and other reviewers said on the pull request: its comments,
* review summaries and unresolved review threads, without Gitdog's own posts
* or commands to it. A reviewer checks whether the change does what they
* asked; the text is untrusted data.
*/
export async function readDiscussion(
access: VerifiedRepositoryAccess,
number: number,
isGitdog: (login: string) => boolean,
// How many conversation comments the pull request has, so the newest
// page is read: GitHub lists them oldest first.
commentCount = 0,
): Promise<DiscussionEntry[]> {
type Item = { at: string; bot: boolean; entry: DiscussionEntry };
const items: Item[] = [];
const add = (
at: unknown,
login: unknown,
bot: boolean,
kind: DiscussionEntry["kind"],
body: unknown,
path?: unknown,
) => {
if (typeof login !== "string" || isGitdog(login)) return;
const text = discussionText(body);
if (text.length < 10) return;
items.push({
at: typeof at === "string" ? at : "",
bot: bot || /\[bot\]$/i.test(login),
entry: {
author: login.replace(/\[bot\]$/i, ""),
kind,
text: text.slice(0, DISCUSSION_ENTRY_CHARS),
...(typeof path === "string" ? { path } : {}),
},
});
};
// Each part is best effort: one failed read keeps the others.
const [commentsRead, reviewsRead, threadsRead] = await Promise.allSettled([
readReviewJson<
Array<{
created_at?: string;
user?: { login?: string; type?: string };
body?: string;
}>
>(
access,
`/issues/${number}/comments?per_page=100&page=${Math.max(1, Math.ceil(commentCount / 100))}`,
Comment thread
hyochan marked this conversation as resolved.
Resolved
"Read pull request comments",
),
readReviewJson<
Array<{
submitted_at?: string;
user?: { login?: string; type?: string };
body?: string;
}>
>(
access,
`/pulls/${number}/reviews?per_page=100`,
"Read pull request reviews",
),
runReviewRequest({ operation: "Read review threads" }, () =>
fetchGitHubAppJsonWithRetry<{
data?: {
repository?: {
pullRequest?: {
reviewThreads?: {
nodes?: Array<{
isResolved?: boolean;
path?: string;
comments?: {
nodes?: Array<{
createdAt?: string;
body?: string;
author?: { login?: string; __typename?: string };
}>;
};
}>;
};
};
};
};
}>(
"https://api.github.com/graphql",
access.token,
"Read review threads",
{
method: "POST",
maxAttempts: REVIEW_READ_ATTEMPTS,
jsonBody: {
query:
"query($owner:String!,$repo:String!,$number:Int!){repository(owner:$owner,name:$repo){pullRequest(number:$number){reviewThreads(last:100){nodes{isResolved path comments(first:1){nodes{createdAt body author{__typename login}}}}}}}}",
variables: { owner: access.owner, repo: access.repo, number },
},
},
),
),
]);
const comments =
commentsRead.status === "fulfilled" ? commentsRead.value : [];
const reviews = reviewsRead.status === "fulfilled" ? reviewsRead.value : [];
const threads =
threadsRead.status === "fulfilled" ? threadsRead.value : undefined;
for (const c of Array.isArray(comments) ? comments : [])
add(
c?.created_at,
c?.user?.login,
c?.user?.type === "Bot",
"comment",
c?.body,
);
for (const r of Array.isArray(reviews) ? reviews : [])
add(
r?.submitted_at,
r?.user?.login,
r?.user?.type === "Bot",
"review",
r?.body,
);
for (const t of threads?.data?.repository?.pullRequest?.reviewThreads
?.nodes ?? []) {
if (t?.isResolved !== false) continue;
const first = t.comments?.nodes?.[0];
add(
first?.createdAt,
first?.author?.login,
first?.author?.__typename === "Bot",
"thread",
first?.body,
t.path,
);
}
const result: DiscussionEntry[] = [];
let size = 0;
// People first, then bots, each newest first: bot summaries must not crowd
// out what a person asked for.
for (const { entry } of items.sort(
(a, b) => Number(a.bot) - Number(b.bot) || b.at.localeCompare(a.at),
)) {
size += entry.text.length;
if (size > DISCUSSION_CHARS) break;
result.push(entry);
}
return result;
}

/**
* Whether any review thread on the pull request is unresolved, whoever opened
* it: an approval must not talk over a reviewer's open request.
*/
export async function hasUnresolvedThreads(
access: VerifiedRepositoryAccess,
number: number,
botLogin: string,
): Promise<boolean> {
const page = await runReviewRequest(
{ operation: "Read review threads" },
Expand All @@ -464,21 +642,13 @@ export async function hasUnresolvedOwnThreads(
),
);
const threads = page.data?.repository?.pullRequest?.reviewThreads;
// GraphQL names an app by its slug, REST by `slug[bot]`.
const login = (value: string) => value.toLowerCase().replace(/\[bot\]$/, "");
const own = login(botLogin);
if (
!own ||
page.errors?.length ||
!threads?.nodes ||
threads.pageInfo?.hasNextPage !== false
)
return true;
return threads.nodes.some(
(thread) =>
thread.isResolved !== true &&
login(thread.comments?.nodes?.[0]?.author?.login ?? "") === own,
);
return threads.nodes.some((thread) => thread.isResolved !== true);
}

/**
Expand Down
Loading
Loading