feat(web): apply and remove labels from the pull request tab (#9313)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Fable 5 <[email protected]>
This commit is contained in:
19 files changed
+1025
-114
No files matched your search
@@ -2726,7 +2726,12 @@ layer("GitHubPullRequestCli.layer", (it) => {
|
||||
// One request, because both answers hang off the same repository object.
|
||||
assert.strictEqual(mockedExecute.mock.calls.length, 1);
|
||||
expect(callAt(0).args).toContain("number=7");
|
||||
expect(access).toEqual({ canWrite: false, canUpdate: true, didAuthor: true });
|
||||
expect(access).toEqual({
|
||||
canWrite: false,
|
||||
canTriage: false,
|
||||
canUpdate: true,
|
||||
didAuthor: true,
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -2889,7 +2894,12 @@ layer("GitHubPullRequestCli.layer", (it) => {
|
||||
});
|
||||
|
||||
assert.strictEqual(mockedExecute.mock.calls.length, 2);
|
||||
expect(access).toEqual({ canWrite: false, canUpdate: true, didAuthor: true });
|
||||
expect(access).toEqual({
|
||||
canWrite: false,
|
||||
canTriage: false,
|
||||
canUpdate: true,
|
||||
didAuthor: true,
|
||||
});
|
||||
yield* TestClock.setTime(Date.parse("2100-01-01T00:00:00Z"));
|
||||
}),
|
||||
);
|
||||
@@ -3029,4 +3039,56 @@ layer("GitHubPullRequestCli.layer", (it) => {
|
||||
]);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("puts labels on by posting to the issue's own collection, all at once", () =>
|
||||
Effect.gen(function* () {
|
||||
mockedExecute.mockReturnValue(Effect.succeed(output("[]")));
|
||||
const cli = yield* GitHubPullRequestCli.GitHubPullRequestCli;
|
||||
|
||||
yield* cli.setLabels({
|
||||
cwd: "/w",
|
||||
repository: "acme/web",
|
||||
host: "github.com",
|
||||
number: 7,
|
||||
labels: ["bug", "size:XL"],
|
||||
applied: true,
|
||||
});
|
||||
|
||||
assert.strictEqual(mockedExecute.mock.calls.length, 1);
|
||||
const call = callAt(0);
|
||||
expect(call.args).toEqual([
|
||||
"api",
|
||||
"--method",
|
||||
"POST",
|
||||
"--hostname",
|
||||
"github.com",
|
||||
"repos/acme/web/issues/7/labels",
|
||||
"--input",
|
||||
"-",
|
||||
]);
|
||||
// @effect-diagnostics-next-line preferSchemaOverJson:off - asserting the raw gh request body.
|
||||
expect(JSON.parse(call.stdin ?? "")).toEqual({ labels: ["bug", "size:XL"] });
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("takes labels off one at a time, naming each in the path encoded", () =>
|
||||
Effect.gen(function* () {
|
||||
mockedExecute.mockReturnValue(Effect.succeed(output("[]")));
|
||||
const cli = yield* GitHubPullRequestCli.GitHubPullRequestCli;
|
||||
|
||||
yield* cli.setLabels({
|
||||
cwd: "/w",
|
||||
repository: "acme/web",
|
||||
host: "github.com",
|
||||
number: 7,
|
||||
labels: ["good first issue", "area/web"],
|
||||
applied: false,
|
||||
});
|
||||
|
||||
assert.strictEqual(mockedExecute.mock.calls.length, 2);
|
||||
expect(callAt(0).args).toContain("repos/acme/web/issues/7/labels/good%20first%20issue");
|
||||
expect(callAt(0).args).toContain("DELETE");
|
||||
expect(callAt(1).args).toContain("repos/acme/web/issues/7/labels/area%2Fweb");
|
||||
}),
|
||||
);
|
||||
});
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
type PullRequestReviewVerdict,
|
||||
type PullRequestReviewerCandidateList,
|
||||
type PullRequestReviewerKind,
|
||||
type PullRequestLabelCandidateList,
|
||||
type PullRequestThreadCommentsResult,
|
||||
type PullRequestUpdateMethod,
|
||||
} from "@t3tools/contracts";
|
||||
@@ -42,6 +43,9 @@ import {
|
||||
decodeReactionSubjectScopeJson,
|
||||
decodeRepositoryAccessJson,
|
||||
decodeReviewerCandidatesJson,
|
||||
decodeLabelCandidatesJson,
|
||||
buildLabelRequestJson,
|
||||
LABEL_CANDIDATES_GRAPHQL_QUERY,
|
||||
decodeReviewDismissalsJson,
|
||||
decodeReviewThreadCommentsJson,
|
||||
decodeReviewThreadsJson,
|
||||
@@ -597,6 +601,24 @@ export class GitHubPullRequestCli extends Context.Service<
|
||||
readonly requested: boolean;
|
||||
}) => Effect.Effect<void, GitHubPullRequestCliError>;
|
||||
|
||||
/** The repository's labels, and which of them this pull request already wears. */
|
||||
readonly listLabelCandidates: (input: {
|
||||
readonly cwd: string;
|
||||
readonly repository: string;
|
||||
readonly host: string;
|
||||
readonly number: number;
|
||||
}) => Effect.Effect<PullRequestLabelCandidateList, GitHubPullRequestCliError>;
|
||||
|
||||
readonly setLabels: (input: {
|
||||
readonly cwd: string;
|
||||
readonly repository: string;
|
||||
readonly host: string;
|
||||
readonly number: number;
|
||||
readonly labels: ReadonlyArray<string>;
|
||||
/** False takes each label off; true adds each to whatever is already there. */
|
||||
readonly applied: boolean;
|
||||
}) => Effect.Effect<void, GitHubPullRequestCliError>;
|
||||
|
||||
readonly runPullRequestAction: (input: {
|
||||
readonly cwd: string;
|
||||
readonly repository: string;
|
||||
@@ -1977,6 +1999,56 @@ export const make = Effect.gen(function* () {
|
||||
.pipe(Effect.asVoid);
|
||||
},
|
||||
|
||||
listLabelCandidates: (input) => {
|
||||
const { owner, name } = parseRepositorySelector(input.repository);
|
||||
return graphqlRead({
|
||||
cwd: input.cwd,
|
||||
host: input.host,
|
||||
operation: "listLabelCandidates",
|
||||
allowReserve: true,
|
||||
variables: [
|
||||
["-f", `owner=${owner}`],
|
||||
["-f", `name=${name}`],
|
||||
["-F", `number=${input.number}`],
|
||||
],
|
||||
query: LABEL_CANDIDATES_GRAPHQL_QUERY,
|
||||
decode: decodeLabelCandidatesJson,
|
||||
});
|
||||
},
|
||||
|
||||
setLabels: (input) => {
|
||||
const { owner, name } = parseRepositorySelector(input.repository);
|
||||
// A pull request is an issue to the labels API. Adding posts a list and leaves what was
|
||||
// already there; taking off is one delete per label, since the endpoint names one in its
|
||||
// path. The name goes into the path encoded, because a label may carry a space or a slash.
|
||||
const issue = `repos/${owner}/${name}/issues/${input.number}/labels`;
|
||||
if (input.applied) {
|
||||
return github
|
||||
.execute({
|
||||
cwd: input.cwd,
|
||||
args: ["api", "--method", "POST", "--hostname", input.host, issue, "--input", "-"],
|
||||
stdin: buildLabelRequestJson(input.labels),
|
||||
})
|
||||
.pipe(Effect.asVoid);
|
||||
}
|
||||
return Effect.forEach(
|
||||
input.labels,
|
||||
(label) =>
|
||||
github.execute({
|
||||
cwd: input.cwd,
|
||||
args: [
|
||||
"api",
|
||||
"--method",
|
||||
"DELETE",
|
||||
"--hostname",
|
||||
input.host,
|
||||
`${issue}/${encodeURIComponent(label)}`,
|
||||
],
|
||||
}),
|
||||
{ concurrency: 1, discard: true },
|
||||
);
|
||||
},
|
||||
|
||||
runPullRequestAction: (input) => {
|
||||
if (input.action === "revert") {
|
||||
return pullRequestNodeId({ ...input, operation: "revertPullRequest" }).pipe(
|
||||
|
||||
@@ -47,7 +47,14 @@ it.effect("uses one narrow read for a linked pull request summary", () =>
|
||||
|
||||
describe("gitHubViewerPermissions", () => {
|
||||
it("offers everything to a viewer who can write to the repository", () => {
|
||||
expect(gitHubViewerPermissions({ canWrite: true, canUpdate: true, didAuthor: false })).toEqual({
|
||||
expect(
|
||||
gitHubViewerPermissions({
|
||||
canWrite: true,
|
||||
canTriage: true,
|
||||
canUpdate: true,
|
||||
didAuthor: false,
|
||||
}),
|
||||
).toEqual({
|
||||
// Arming a merge for later is the merge, so it travels with it.
|
||||
actions: [
|
||||
"merge",
|
||||
@@ -64,6 +71,7 @@ describe("gitHubViewerPermissions", () => {
|
||||
resolve: true,
|
||||
verdicts: ["comment", "approve", "request-changes"],
|
||||
requestReviewers: true,
|
||||
labels: true,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -71,7 +79,12 @@ describe("gitHubViewerPermissions", () => {
|
||||
// Every open-source pull request somebody else opened: GitHub says no to all five actions
|
||||
// and to resolving, and yes to commenting and to every verdict.
|
||||
expect(
|
||||
gitHubViewerPermissions({ canWrite: false, canUpdate: false, didAuthor: false }),
|
||||
gitHubViewerPermissions({
|
||||
canWrite: false,
|
||||
canTriage: false,
|
||||
canUpdate: false,
|
||||
didAuthor: false,
|
||||
}),
|
||||
).toEqual({
|
||||
actions: [],
|
||||
comment: true,
|
||||
@@ -79,11 +92,31 @@ describe("gitHubViewerPermissions", () => {
|
||||
verdicts: ["comment", "approve", "request-changes"],
|
||||
// Asking somebody else to review is the one thing read access never stretches to.
|
||||
requestReviewers: false,
|
||||
labels: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("lets a triager label without letting them merge or ask for a review", () => {
|
||||
const permissions = gitHubViewerPermissions({
|
||||
canWrite: false,
|
||||
canTriage: true,
|
||||
canUpdate: false,
|
||||
didAuthor: false,
|
||||
});
|
||||
expect(permissions.labels).toBe(true);
|
||||
expect(permissions.requestReviewers).toBe(false);
|
||||
expect(permissions.actions).toEqual([]);
|
||||
});
|
||||
|
||||
it("keeps an author's own pull request theirs to close, with read access and no more", () => {
|
||||
expect(gitHubViewerPermissions({ canWrite: false, canUpdate: true, didAuthor: true })).toEqual({
|
||||
expect(
|
||||
gitHubViewerPermissions({
|
||||
canWrite: false,
|
||||
canTriage: false,
|
||||
canUpdate: true,
|
||||
didAuthor: true,
|
||||
}),
|
||||
).toEqual({
|
||||
// Merging is the one thing writing is needed for, now or later; the rest an author may do.
|
||||
actions: ["ready", "draft", "close", "reopen"],
|
||||
comment: true,
|
||||
@@ -91,6 +124,7 @@ describe("gitHubViewerPermissions", () => {
|
||||
// GitHub refuses an author's approval of their own change, so the page does not offer one.
|
||||
verdicts: ["comment"],
|
||||
requestReviewers: false,
|
||||
labels: false,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -110,6 +144,7 @@ describe("gitHubViewerPermissions", () => {
|
||||
resolve: false,
|
||||
verdicts: ["comment", "approve", "request-changes"],
|
||||
requestReviewers: false,
|
||||
labels: false,
|
||||
});
|
||||
expect(detail.workflowApprovalsRequired).toBeUndefined();
|
||||
expect(detail.checks).toContainEqual({
|
||||
@@ -158,7 +193,12 @@ describe("gitHubViewerPermissions", () => {
|
||||
mergeCapabilities: { merge: true, squash: true, rebase: true },
|
||||
}),
|
||||
getViewerAccess: () =>
|
||||
Effect.succeed({ canWrite: false, canUpdate: true, didAuthor: false }),
|
||||
Effect.succeed({
|
||||
canWrite: false,
|
||||
canTriage: false,
|
||||
canUpdate: true,
|
||||
didAuthor: false,
|
||||
}),
|
||||
}),
|
||||
),
|
||||
),
|
||||
@@ -254,7 +294,7 @@ describe("gitHubViewerPermissions", () => {
|
||||
mergeCapabilities: { merge: true, squash: true, rebase: true },
|
||||
}),
|
||||
getViewerAccess: () =>
|
||||
Effect.succeed({ canWrite: true, canUpdate: true, didAuthor: false }),
|
||||
Effect.succeed({ canWrite: true, canTriage: true, canUpdate: true, didAuthor: false }),
|
||||
}),
|
||||
),
|
||||
),
|
||||
@@ -318,7 +358,7 @@ it.effect("does not classify same-repository gates as fork workflow approvals",
|
||||
mergeCapabilities: { merge: true, squash: true, rebase: true },
|
||||
}),
|
||||
getViewerAccess: () =>
|
||||
Effect.succeed({ canWrite: true, canUpdate: true, didAuthor: false }),
|
||||
Effect.succeed({ canWrite: true, canTriage: true, canUpdate: true, didAuthor: false }),
|
||||
}),
|
||||
),
|
||||
),
|
||||
@@ -365,7 +405,7 @@ it.effect("keeps an unsafe workflow approval scope visible as unknown", () =>
|
||||
mergeCapabilities: { merge: true, squash: true, rebase: true },
|
||||
}),
|
||||
getViewerAccess: () =>
|
||||
Effect.succeed({ canWrite: true, canUpdate: true, didAuthor: false }),
|
||||
Effect.succeed({ canWrite: true, canTriage: true, canUpdate: true, didAuthor: false }),
|
||||
}),
|
||||
),
|
||||
),
|
||||
@@ -404,7 +444,7 @@ it.effect("propagates workflow discovery rate limits", () =>
|
||||
mergeCapabilities: { merge: true, squash: true, rebase: true },
|
||||
}),
|
||||
getViewerAccess: () =>
|
||||
Effect.succeed({ canWrite: true, canUpdate: true, didAuthor: false }),
|
||||
Effect.succeed({ canWrite: true, canTriage: true, canUpdate: true, didAuthor: false }),
|
||||
}),
|
||||
),
|
||||
),
|
||||
@@ -420,7 +460,8 @@ describe("getViewerPermissions", () => {
|
||||
Layer.mock(GitHubPullRequestCli.GitHubPullRequestCli)({
|
||||
getPullRequestDetail: () => Effect.succeed(openDetail),
|
||||
getPullRequestBaseComparison: () => comparison,
|
||||
getViewerAccess: () => Effect.succeed({ canWrite: true, canUpdate: true, didAuthor: false }),
|
||||
getViewerAccess: () =>
|
||||
Effect.succeed({ canWrite: true, canTriage: true, canUpdate: true, didAuthor: false }),
|
||||
});
|
||||
|
||||
it.effect("offers update-branch when the comparison grants it", () =>
|
||||
@@ -466,7 +507,7 @@ describe("getViewerPermissions", () => {
|
||||
getViewerAccess: (input) =>
|
||||
Effect.sync(() => {
|
||||
viewerAllowReserve = input.allowReserve;
|
||||
return { canWrite: true, canUpdate: true, didAuthor: false };
|
||||
return { canWrite: true, canTriage: true, canUpdate: true, didAuthor: false };
|
||||
}),
|
||||
}),
|
||||
),
|
||||
@@ -501,7 +542,7 @@ describe("getViewerPermissions", () => {
|
||||
}),
|
||||
),
|
||||
getViewerAccess: () =>
|
||||
Effect.succeed({ canWrite: true, canUpdate: true, didAuthor: false }),
|
||||
Effect.succeed({ canWrite: true, canTriage: true, canUpdate: true, didAuthor: false }),
|
||||
}),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -47,6 +47,7 @@ const CAPABILITIES: PullRequestCapabilities = {
|
||||
},
|
||||
reviewers: { request: true, listCandidates: true },
|
||||
edit: { changeRequest: true, comment: true },
|
||||
labels: true,
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -93,6 +94,8 @@ export function gitHubViewerPermissions(access: GitHubViewerAccess): PullRequest
|
||||
verdicts: access.didAuthor ? (["comment"] as const) : CAPABILITIES.review.verdicts,
|
||||
requestReviewers: access.canWrite,
|
||||
...(access.canUpdateBranch === true ? { updateMethods: CAPABILITIES.updateMethods } : {}),
|
||||
// Triage is the one role that labels without writing, which is what triage is for.
|
||||
labels: access.canTriage,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -526,6 +529,21 @@ export const make = Effect.gen(function* () {
|
||||
})
|
||||
.pipe(Effect.mapError(fail("setReviewerRequest"))),
|
||||
|
||||
listLabelCandidates: (input) =>
|
||||
cli.listLabelCandidates(input).pipe(Effect.mapError(fail("listLabelCandidates"))),
|
||||
|
||||
setLabels: (input) =>
|
||||
cli
|
||||
.setLabels({
|
||||
cwd: input.cwd,
|
||||
repository: input.repository,
|
||||
host: input.host,
|
||||
number: input.number,
|
||||
labels: input.labels,
|
||||
applied: input.applied,
|
||||
})
|
||||
.pipe(Effect.mapError(fail("setLabels"))),
|
||||
|
||||
runAction: (input) =>
|
||||
cli
|
||||
.runPullRequestAction({
|
||||
|
||||
@@ -26,6 +26,7 @@ import type {
|
||||
PullRequestReviewVerdict,
|
||||
PullRequestReviewerCandidateList,
|
||||
PullRequestReviewerKind,
|
||||
PullRequestLabelCandidateList,
|
||||
PullRequestState,
|
||||
PullRequestUpdateMethod,
|
||||
PullRequestViewerPermissions,
|
||||
@@ -469,6 +470,23 @@ export interface PullRequestProviderApi {
|
||||
},
|
||||
) => Effect.Effect<void, PullRequestProviderError>;
|
||||
|
||||
/**
|
||||
* The repository's labels, with the ones already on the change request marked. Present with
|
||||
* `setLabels` only where `capabilities.labels` is true; the service refuses both without it.
|
||||
*/
|
||||
readonly listLabelCandidates?: (
|
||||
input: ProviderRepositoryRef & { readonly number: number },
|
||||
) => Effect.Effect<PullRequestLabelCandidateList, PullRequestProviderError>;
|
||||
|
||||
/** Puts labels on the change request, or takes them off. One call for both directions. */
|
||||
readonly setLabels?: (
|
||||
input: ProviderRepositoryRef & {
|
||||
readonly number: number;
|
||||
readonly labels: ReadonlyArray<string>;
|
||||
readonly applied: boolean;
|
||||
},
|
||||
) => Effect.Effect<void, PullRequestProviderError>;
|
||||
|
||||
/** Only called when `capabilities.review.reply` is true. */
|
||||
readonly replyToThread: (
|
||||
input: ProviderRepositoryRef & {
|
||||
|
||||
@@ -2284,6 +2284,125 @@ it.effect("hands the host's own candidate list back, and asks for it with the ch
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("refuses a label change on a host that has not said it takes one", () =>
|
||||
Effect.gen(function* () {
|
||||
let changed = false;
|
||||
const service = yield* makeService({
|
||||
projects: [project({ id: "p1", title: "web", workspaceRoot: "/a", repository: "acme/web" })],
|
||||
providers: [
|
||||
fakeProvider("github", {
|
||||
// The method is there; the capability that would let it be called is not.
|
||||
setLabels: () => {
|
||||
changed = true;
|
||||
return Effect.void;
|
||||
},
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
const error = yield* Effect.flip(
|
||||
service.setLabels({
|
||||
projectId: "p1" as ProjectId,
|
||||
repository: "acme/web",
|
||||
number: 1,
|
||||
labels: ["bug"],
|
||||
applied: true,
|
||||
}),
|
||||
);
|
||||
|
||||
assert.strictEqual(error._tag, "PullRequestOperationError");
|
||||
assert.include(error.message, "cannot change the labels");
|
||||
assert.isFalse(changed);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("refuses a label change this viewer may not make, and says what access it takes", () =>
|
||||
Effect.gen(function* () {
|
||||
let changed = false;
|
||||
const service = yield* makeService({
|
||||
projects: [project({ id: "p1", title: "web", workspaceRoot: "/a", repository: "acme/web" })],
|
||||
providers: [
|
||||
fakeProvider("github", {
|
||||
capabilities: { ...fakeProvider("github").capabilities, labels: true },
|
||||
getViewerPermissions: () =>
|
||||
Effect.succeed({
|
||||
actions: [],
|
||||
comment: true,
|
||||
resolve: false,
|
||||
verdicts: ["comment", "approve", "request-changes"],
|
||||
requestReviewers: false,
|
||||
labels: false,
|
||||
}),
|
||||
listLabelCandidates: () => Effect.die("must not be called"),
|
||||
setLabels: () => {
|
||||
changed = true;
|
||||
return Effect.void;
|
||||
},
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
const listError = yield* Effect.flip(
|
||||
service.labelCandidates({ projectId: "p1" as ProjectId, repository: "acme/web", number: 1 }),
|
||||
);
|
||||
assert.include(listError.message, "You need triage access on this repository");
|
||||
|
||||
const error = yield* Effect.flip(
|
||||
service.setLabels({
|
||||
projectId: "p1" as ProjectId,
|
||||
repository: "acme/web",
|
||||
number: 1,
|
||||
labels: ["bug"],
|
||||
applied: true,
|
||||
}),
|
||||
);
|
||||
assert.include(error.message, "You need triage access on this repository");
|
||||
assert.isFalse(changed);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("hands a label change to the host, and reads the labels back for the menu", () =>
|
||||
Effect.gen(function* () {
|
||||
let received: { labels: ReadonlyArray<string>; applied: boolean } | null = null;
|
||||
const service = yield* makeService({
|
||||
projects: [project({ id: "p1", title: "web", workspaceRoot: "/a", repository: "acme/web" })],
|
||||
providers: [
|
||||
fakeProvider("github", {
|
||||
capabilities: { ...fakeProvider("github").capabilities, labels: true },
|
||||
listLabelCandidates: () =>
|
||||
Effect.succeed({
|
||||
candidates: [{ name: "bug", color: null, description: null, isApplied: false }],
|
||||
truncated: false,
|
||||
}),
|
||||
setLabels: (input) => {
|
||||
received = { labels: input.labels, applied: input.applied };
|
||||
return Effect.void;
|
||||
},
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
const list = yield* service.labelCandidates({
|
||||
projectId: "p1" as ProjectId,
|
||||
repository: "acme/web",
|
||||
number: 4,
|
||||
});
|
||||
assert.deepStrictEqual(
|
||||
list.candidates.map((label) => label.name),
|
||||
["bug"],
|
||||
);
|
||||
|
||||
yield* service.setLabels({
|
||||
projectId: "p1" as ProjectId,
|
||||
repository: "acme/web",
|
||||
number: 4,
|
||||
labels: ["bug"],
|
||||
applied: false,
|
||||
});
|
||||
assert.deepStrictEqual(received, { labels: ["bug"], applied: false });
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("answers a repeated listing from cache, and concurrent readers share one request", () =>
|
||||
Effect.gen(function* () {
|
||||
let hostCalls = 0;
|
||||
|
||||
@@ -38,6 +38,8 @@ import {
|
||||
type PullRequestReviewVerdict,
|
||||
type PullRequestReviewerCandidateList,
|
||||
type PullRequestReviewerRequestInput,
|
||||
type PullRequestLabelCandidateList,
|
||||
type PullRequestLabelChangeInput,
|
||||
type PullRequestSubmitReviewInput,
|
||||
type PullRequestSummary,
|
||||
type PullRequestThreadReplyInput,
|
||||
@@ -170,6 +172,12 @@ export class PullRequestService extends Context.Service<
|
||||
readonly requestReviewers: (
|
||||
input: PullRequestReviewerRequestInput,
|
||||
) => Effect.Effect<void, PullRequestError>;
|
||||
readonly labelCandidates: (
|
||||
input: PullRequestRef,
|
||||
) => Effect.Effect<PullRequestLabelCandidateList, PullRequestError>;
|
||||
readonly setLabels: (
|
||||
input: PullRequestLabelChangeInput,
|
||||
) => Effect.Effect<void, PullRequestError>;
|
||||
readonly invalidate: (input: PullRequestInvalidateInput) => Effect.Effect<void>;
|
||||
}
|
||||
>()("t3/pullRequest/PullRequestService") {}
|
||||
@@ -213,6 +221,7 @@ const ACTION_ACCESS_REFUSALS: Record<PullRequestAction, string> = {
|
||||
* sentence is only ever the answer where a host said no.
|
||||
*/
|
||||
const REVIEWER_REQUEST_REFUSAL = "You need write access on this repository to ask for a review.";
|
||||
const LABEL_CHANGE_REFUSAL = "You need triage access on this repository to change its labels.";
|
||||
|
||||
/** A project this page can read: its remote is on a host with an implementation. */
|
||||
interface SupportedProject {
|
||||
@@ -473,6 +482,10 @@ function withRateLimitBackoff(
|
||||
submitReview: interactive("submitReview", api.submitReview),
|
||||
listReviewerCandidates: interactive("listReviewerCandidates", api.listReviewerCandidates),
|
||||
setReviewerRequest: interactive("setReviewerRequest", api.setReviewerRequest),
|
||||
...(api.listLabelCandidates === undefined
|
||||
? {}
|
||||
: { listLabelCandidates: interactive("listLabelCandidates", api.listLabelCandidates) }),
|
||||
...(api.setLabels === undefined ? {} : { setLabels: interactive("setLabels", api.setLabels) }),
|
||||
replyToThread: interactive("replyToThread", api.replyToThread),
|
||||
setReaction: interactive("setReaction", api.setReaction),
|
||||
setThreadResolution: interactive("setThreadResolution", api.setThreadResolution),
|
||||
@@ -1843,6 +1856,78 @@ export const make = Effect.gen(function* () {
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* The labels, like the reviewer candidates, are wanted only by somebody about to change them,
|
||||
* so the same permission guards the list and the change.
|
||||
*/
|
||||
const labelCandidates: PullRequestService["Service"]["labelCandidates"] = (input) =>
|
||||
requireProject(input).pipe(
|
||||
Effect.flatMap((project): Effect.Effect<PullRequestLabelCandidateList, PullRequestError> => {
|
||||
const list = project.api.listLabelCandidates;
|
||||
if (project.api.capabilities.labels !== true || list === undefined) {
|
||||
return Effect.fail(
|
||||
new PullRequestOperationError({
|
||||
operation: "labelCandidates",
|
||||
detail: "This host cannot change the labels on a change request.",
|
||||
}),
|
||||
);
|
||||
}
|
||||
return viewerPermissionsOf(project, input, "labelCandidates").pipe(
|
||||
Effect.flatMap(
|
||||
(viewer): Effect.Effect<PullRequestLabelCandidateList, PullRequestError> =>
|
||||
viewer.labels === false
|
||||
? Effect.fail(
|
||||
new PullRequestOperationError({
|
||||
operation: "labelCandidates",
|
||||
detail: LABEL_CHANGE_REFUSAL,
|
||||
}),
|
||||
)
|
||||
: list({
|
||||
cwd: project.project.workspaceRoot,
|
||||
repository: project.repository,
|
||||
host: project.host,
|
||||
number: input.number,
|
||||
}).pipe(Effect.mapError(toPullRequestError("labelCandidates"))),
|
||||
),
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
const setLabels: PullRequestService["Service"]["setLabels"] = (input) =>
|
||||
requireProject(input).pipe(
|
||||
Effect.flatMap((project): Effect.Effect<void, PullRequestError> => {
|
||||
const change = project.api.setLabels;
|
||||
if (project.api.capabilities.labels !== true || change === undefined) {
|
||||
return Effect.fail(
|
||||
new PullRequestOperationError({
|
||||
operation: "setLabels",
|
||||
detail: "This host cannot change the labels on a change request.",
|
||||
}),
|
||||
);
|
||||
}
|
||||
return viewerPermissionsOf(project, input, "setLabels").pipe(
|
||||
Effect.flatMap(
|
||||
(viewer): Effect.Effect<void, PullRequestError> =>
|
||||
viewer.labels === false
|
||||
? Effect.fail(
|
||||
new PullRequestOperationError({
|
||||
operation: "setLabels",
|
||||
detail: LABEL_CHANGE_REFUSAL,
|
||||
}),
|
||||
)
|
||||
: change({
|
||||
cwd: project.project.workspaceRoot,
|
||||
repository: project.repository,
|
||||
host: project.host,
|
||||
number: input.number,
|
||||
labels: input.labels,
|
||||
applied: input.applied,
|
||||
}).pipe(Effect.mapError(toPullRequestError("setLabels"))),
|
||||
),
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
/**
|
||||
* The line counts for rows already on the page, which the listing left out because on GitHub
|
||||
* they cost more than everything else on the row put together.
|
||||
@@ -2336,6 +2421,8 @@ export const make = Effect.gen(function* () {
|
||||
// The candidate list is deliberately read fresh per menu-open, so it stays uncached.
|
||||
reviewerCandidates,
|
||||
requestReviewers: invalidatedByMutation(requestReviewers),
|
||||
labelCandidates,
|
||||
setLabels: invalidatedByMutation(setLabels),
|
||||
invalidate,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
decodePullRequestListJson,
|
||||
decodePullRequestNodeIdJson,
|
||||
decodePullRequestSearchJson,
|
||||
decodeLabelCandidatesJson,
|
||||
decodeRepositoryAccessJson,
|
||||
decodeReviewerCandidatesJson,
|
||||
decodeReviewThreadCommentsJson,
|
||||
@@ -832,7 +833,7 @@ describe("viewer permission decoding", () => {
|
||||
}),
|
||||
),
|
||||
),
|
||||
).toEqual({ canWrite: false, canUpdate: true, didAuthor: true });
|
||||
).toEqual({ canWrite: false, canTriage: false, canUpdate: true, didAuthor: true });
|
||||
});
|
||||
|
||||
it("says no to a passer-by on a repository they can only read", () => {
|
||||
@@ -845,7 +846,7 @@ describe("viewer permission decoding", () => {
|
||||
}),
|
||||
),
|
||||
),
|
||||
).toEqual({ canWrite: false, canUpdate: false, didAuthor: false });
|
||||
).toEqual({ canWrite: false, canTriage: false, canUpdate: false, didAuthor: false });
|
||||
});
|
||||
|
||||
it("reads silence as permission, but not as authorship", () => {
|
||||
@@ -854,10 +855,79 @@ describe("viewer permission decoding", () => {
|
||||
// and claiming it for someone who did not is how an author's own rules get handed out.
|
||||
expect(expectSuccess(decodeViewerPermissionsJson(viewerJson({ pullRequest: null })))).toEqual({
|
||||
canWrite: false,
|
||||
canTriage: false,
|
||||
canUpdate: true,
|
||||
didAuthor: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("reads triage as enough to label, and not enough to write", () => {
|
||||
const access = expectSuccess(
|
||||
decodeViewerPermissionsJson(
|
||||
viewerJson({
|
||||
viewerPermission: "TRIAGE",
|
||||
pullRequest: { viewerCanUpdate: false, viewerDidAuthor: false },
|
||||
}),
|
||||
),
|
||||
);
|
||||
expect(access.canTriage).toBe(true);
|
||||
expect(access.canWrite).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("label candidate decoding", () => {
|
||||
const labelsJson = (input: {
|
||||
readonly defined: ReadonlyArray<Record<string, unknown>>;
|
||||
readonly applied?: ReadonlyArray<string>;
|
||||
readonly hasNextPage?: boolean;
|
||||
}) =>
|
||||
JSON.stringify({
|
||||
data: {
|
||||
repository: {
|
||||
labels: {
|
||||
pageInfo: { hasNextPage: input.hasNextPage ?? false },
|
||||
nodes: input.defined,
|
||||
},
|
||||
pullRequest: { labels: { nodes: (input.applied ?? []).map((name) => ({ name })) } },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
it("marks the labels the pull request already wears", () => {
|
||||
const list = expectSuccess(
|
||||
decodeLabelCandidatesJson(
|
||||
labelsJson({
|
||||
defined: [
|
||||
{ name: "bug", color: "d73a4a", description: "Something is broken" },
|
||||
{ name: "size:XL", color: "e4572e", description: null },
|
||||
],
|
||||
applied: ["size:XL"],
|
||||
}),
|
||||
),
|
||||
);
|
||||
expect(list.candidates).toEqual([
|
||||
{ name: "bug", color: "d73a4a", description: "Something is broken", isApplied: false },
|
||||
{ name: "size:XL", color: "e4572e", description: null, isApplied: true },
|
||||
]);
|
||||
expect(list.truncated).toBe(false);
|
||||
});
|
||||
|
||||
it("keeps a worn label the repository no longer defines, so it can be taken off", () => {
|
||||
const list = expectSuccess(
|
||||
decodeLabelCandidatesJson(labelsJson({ defined: [{ name: "bug" }], applied: ["legacy"] })),
|
||||
);
|
||||
expect(list.candidates.map((label) => [label.name, label.isApplied])).toEqual([
|
||||
["legacy", true],
|
||||
["bug", false],
|
||||
]);
|
||||
});
|
||||
|
||||
it("says so when the repository defines more labels than the read asked for", () => {
|
||||
expect(
|
||||
expectSuccess(decodeLabelCandidatesJson(labelsJson({ defined: [], hasNextPage: true })))
|
||||
.truncated,
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("review thread decoding", () => {
|
||||
|
||||
@@ -24,6 +24,8 @@ import type {
|
||||
PullRequestReviewerCandidate,
|
||||
PullRequestReviewerCandidateList,
|
||||
PullRequestReviewerKind,
|
||||
PullRequestLabelCandidate,
|
||||
PullRequestLabelCandidateList,
|
||||
PullRequestState,
|
||||
PullRequestThreadComment,
|
||||
} from "@t3tools/contracts";
|
||||
@@ -1994,6 +1996,11 @@ function toCanWrite(viewerPermission: string | null | undefined): boolean {
|
||||
}
|
||||
}
|
||||
|
||||
/** Triage is the least role GitHub lets label a pull request; it is not a write. */
|
||||
function toCanTriage(viewerPermission: string | null | undefined): boolean {
|
||||
return viewerPermission?.trim().toUpperCase() === "TRIAGE" || toCanWrite(viewerPermission);
|
||||
}
|
||||
|
||||
export function decodeRepositoryAccessJson(
|
||||
raw: string,
|
||||
): Result.Result<GitHubRepositoryAccess, DecodeFailure> {
|
||||
@@ -2222,6 +2229,99 @@ export function buildReviewerRequestJson(
|
||||
});
|
||||
}
|
||||
|
||||
export const LABEL_CANDIDATES_GRAPHQL_QUERY = `query($owner: String!, $name: String!, $number: Int!) {
|
||||
repository(owner: $owner, name: $name) {
|
||||
labels(first: ${GRAPHQL_PAGE_SIZE}, orderBy: { field: NAME, direction: ASC }) {
|
||||
pageInfo { hasNextPage }
|
||||
nodes { name color description }
|
||||
}
|
||||
pullRequest(number: $number) {
|
||||
labels(first: ${GRAPHQL_PAGE_SIZE}) { nodes { name } }
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
const RawLabelCandidatesSchema = Schema.Struct({
|
||||
data: Schema.Struct({
|
||||
repository: Schema.Struct({
|
||||
labels: Schema.optional(
|
||||
Schema.NullOr(
|
||||
Schema.Struct({
|
||||
pageInfo: Schema.optional(RawPageInfoSchema),
|
||||
nodes: Schema.Array(
|
||||
Schema.NullOr(
|
||||
Schema.Struct({
|
||||
...RawLabelSchema.fields,
|
||||
description: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
}),
|
||||
),
|
||||
),
|
||||
}),
|
||||
),
|
||||
),
|
||||
/** Null for a number that names no pull request the viewer can see. */
|
||||
pullRequest: Schema.NullOr(
|
||||
Schema.Struct({
|
||||
labels: Schema.optional(
|
||||
Schema.NullOr(Schema.Struct({ nodes: Schema.Array(Schema.NullOr(RawLabelSchema)) })),
|
||||
),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
}),
|
||||
});
|
||||
|
||||
const decodeLabelCandidates = decodeJsonResult(RawLabelCandidatesSchema);
|
||||
|
||||
/**
|
||||
* The repository's labels, with the ones already on this pull request marked. A label the pull
|
||||
* request wears that the repository no longer defines — deleted since, or past the page — leads
|
||||
* the list anyway, because a label that cannot be seen cannot be taken off.
|
||||
*/
|
||||
export function decodeLabelCandidatesJson(
|
||||
raw: string,
|
||||
): Result.Result<PullRequestLabelCandidateList, DecodeFailure> {
|
||||
const decoded = decodeLabelCandidates(raw);
|
||||
if (!Result.isSuccess(decoded)) {
|
||||
return Result.fail(decoded.failure);
|
||||
}
|
||||
const repository = decoded.success.data.repository;
|
||||
const applied = new Set(
|
||||
(repository.pullRequest?.labels?.nodes ?? []).flatMap((label) => {
|
||||
const name = trimmed(label?.name);
|
||||
return name === null ? [] : [name];
|
||||
}),
|
||||
);
|
||||
const candidates = new Map<string, PullRequestLabelCandidate>();
|
||||
for (const node of repository.labels?.nodes ?? []) {
|
||||
const name = trimmed(node?.name);
|
||||
if (name === null) continue;
|
||||
candidates.set(name, {
|
||||
name,
|
||||
color: trimmed(node?.color),
|
||||
description: trimmed(node?.description),
|
||||
isApplied: applied.has(name),
|
||||
});
|
||||
}
|
||||
const missing = [...applied].filter((name) => !candidates.has(name));
|
||||
return Result.succeed({
|
||||
candidates: [
|
||||
...missing.map((name) => ({ name, color: null, description: null, isApplied: true })),
|
||||
...candidates.values(),
|
||||
],
|
||||
truncated: repository.labels?.pageInfo?.hasNextPage === true,
|
||||
});
|
||||
}
|
||||
|
||||
/** The body of `POST /repos/{owner}/{repo}/issues/{number}/labels`, which adds to what is there. */
|
||||
const LabelRequestSchema = Schema.Struct({ labels: Schema.Array(Schema.String) });
|
||||
|
||||
const encodeLabelRequest = Schema.encodeSync(Schema.fromJsonString(LabelRequestSchema));
|
||||
|
||||
export function buildLabelRequestJson(labels: ReadonlyArray<string>): string {
|
||||
return encodeLabelRequest({ labels });
|
||||
}
|
||||
|
||||
/**
|
||||
* Everything GitHub says about what the signed-in account may do here. `canWrite` is about the
|
||||
* repository, the other two about this pull request in particular — which is why an author with
|
||||
@@ -2229,6 +2329,11 @@ export function buildReviewerRequestJson(
|
||||
*/
|
||||
export interface GitHubViewerAccess {
|
||||
readonly canWrite: boolean;
|
||||
/**
|
||||
* The viewer's role reaches triage, which is the least that may label. Everyone who can write
|
||||
* can triage; a triager is the one role that can label without being able to merge.
|
||||
*/
|
||||
readonly canTriage: boolean;
|
||||
/** GitHub's own `viewerCanUpdate`, true for the author as well as for anyone with write. */
|
||||
readonly canUpdate: boolean;
|
||||
readonly didAuthor: boolean;
|
||||
@@ -2275,6 +2380,7 @@ export function decodeViewerPermissionsJson(
|
||||
const repository = decoded.success.data.repository;
|
||||
return Result.succeed({
|
||||
canWrite: toCanWrite(repository.viewerPermission),
|
||||
canTriage: toCanTriage(repository.viewerPermission),
|
||||
...toPullRequestViewerFields(repository.pullRequest),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user