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
128 changes: 128 additions & 0 deletions mods/github/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions src/github/core/merge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export type MergeMethod = "merge" | "squash" | "rebase";
export interface MergeInfo {
methods: MergeMethod[];
reason: string | null;
sha: string;
headRepo: string | null;
headRef: string;
deletable: boolean;
}
export function mergeInfo(
p: {
mergeable?: boolean | null;
mergeable_state?: string;
draft?: boolean;
state: string;
merged?: boolean;
head: { sha: string; ref: string; repo: { full_name: string; default_branch: string } | null };
},
r: Record<string, unknown>,
): MergeInfo {
const methods = (["merge", "squash", "rebase"] as MergeMethod[]).filter((m) => r[`allow_${m}_merge`] === true);
const reasons: Record<string, string> = {
dirty: "This branch has conflicts that must be resolved",
blocked: "Merging is blocked by branch protection rules (required checks or reviews)",
behind: "This branch is out-of-date with the base branch",
unknown: "Checking mergeability…",
unstable: "Checks have not passed",
};
const reason = p.merged
? "Pull request successfully merged"
: p.state !== "open"
? "Pull request is closed"
: p.draft
? "This pull request is still a draft"
: p.mergeable !== true
? (reasons[p.mergeable_state ?? "unknown"] ?? "Checking mergeability…")
: p.mergeable_state !== "clean" && p.mergeable_state !== "has_hooks"
? (reasons[p.mergeable_state ?? "unknown"] ?? `Merging is blocked: ${p.mergeable_state}`)
: methods.length === 0
? "No merge methods are enabled"
: null;
return {
methods,
reason,
sha: p.head.sha,
headRepo: p.head.repo?.full_name ?? null,
headRef: p.head.ref,
deletable: Boolean(p.merged && p.head.repo && p.head.ref !== p.head.repo.default_branch),
};
}
1 change: 1 addition & 0 deletions src/github/core/pull-detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface Reviewer {
}

export interface PullDetail {
merge?: import("./merge.ts").MergeInfo;
number: number;
title: string;
state: PullState;
Expand Down
1 change: 1 addition & 0 deletions src/github/io/changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { IssueChange } from "../core/issues.ts";

/** One write to an issue: a new comment, or its new state. */
export interface Notice {
pull?: boolean;
repo: string;
number: number;
comment?: Comment;
Expand Down
35 changes: 35 additions & 0 deletions src/github/io/pulls.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Pull requests from GitHub, through gh on the folder's or reader tab's machine (ADR 0003).
import { type Comment, parseComment } from "../core/discussion.ts";
import { type ChangedFile, parseFiles } from "../core/files.ts";
import { type MergeInfo, type MergeMethod, mergeInfo } from "../core/merge.ts";
Comment thread
lw396 marked this conversation as resolved.
Resolved
import { type PullDetail, parsePullDetail } from "../core/pull-detail.ts";
import { parseChecks, pullTimeline } from "../core/pull-timeline.ts";
import {
Expand Down Expand Up @@ -39,6 +40,10 @@ export async function readPull(repo: string, number: number, on: string | null):
api(`repos/${repo}/pulls/${number}/reviews?per_page=100`, { on, paginate: true }),
]);
const detail = parsePullDetail(pull, issue, comments, reviews);
detail.merge = mergeInfo(
pull as Parameters<typeof mergeInfo>[0],
(await api(`repos/${repo}`, { on })) as Record<string, unknown>,
);
const sha = (pull as { head: { sha: string } }).head.sha;
const [events, inline, checks, statuses] = await Promise.all([
api(`repos/${repo}/issues/${number}/timeline?per_page=100`, { on, paginate: true }),
Expand All @@ -54,9 +59,39 @@ export async function readPull(repo: string, number: number, on: string | null):
detail.url,
);
detail.checks = parseChecks(Array.isArray(checks) ? checks : [checks], statuses as unknown[]);
if (detail.merge?.reason?.startsWith("Merging is blocked by")) {
const reasons: string[] = [];
if (detail.reviewers.some((r) => r.state === "changes_requested")) reasons.push("Changes requested");
if (detail.reviewers.some((r) => r.state === "requested")) reasons.push("Review required");
if (detail.checks.some((c) => /fail|pending|queued|progress|error/i.test(c.status)))
reasons.push("Checks have not passed");
if (reasons.length) detail.merge.reason += ": " + reasons.join("; ");
}
return detail;
}

export async function mergePull(
repo: string,
number: number,
method: MergeMethod,
sha: string,
on: string | null,
): Promise<void> {
const result = (await api(`repos/${repo}/pulls/${number}/merge`, {
on,
method: "PUT",
rawFields: { merge_method: method, sha },
})) as { merged: boolean; message: string };
if (!result.merged) throw new Error(result.message);
}
export async function deletePullBranch(info: MergeInfo, on: string | null): Promise<void> {
if (!info.deletable || !info.headRepo) throw new Error("This branch cannot be deleted");
await api(`repos/${info.headRepo}/git/refs/heads/${info.headRef.split("/").map(encodeURIComponent).join("/")}`, {
on,
method: "DELETE",
});
}

/** All changed-file pages; GitHub may omit a patch (binary, too large, or a pure rename). */
export async function readPullFiles(repo: string, number: number, on: string | null): Promise<ChangedFile[]> {
const files = parseFiles(await api(`repos/${repo}/pulls/${number}/files?per_page=100`, { on, paginate: true }));
Expand Down
1 change: 1 addition & 0 deletions src/github/views/panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export class Panel implements Role {
*/
private receive(cx: Context): void {
const notices = this.notices.take();
if (notices.some((n) => n.pull)) this.pulls.load(cx, true);
const reviews = notices.filter((n) => n.review);
for (const notice of reviews) known.pullReviewed(notice.repo, notice.review!.on);
const ready = this.ready();
Expand Down
Loading
Loading