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
2 changes: 1 addition & 1 deletion src/commands/repo-list.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getCredentials } from "../auth";
import { createCommand, type CommandConfig } from "../lib/command";
import { stringify } from "../lib/json";
import { getRepositories } from "../lib/prismic/clients/legacy-user";
import { getRepositories } from "../lib/prismic/clients/user";
import { formatTable } from "../lib/string";

const config = {
Expand Down
27 changes: 0 additions & 27 deletions src/lib/prismic/clients/legacy-user.ts

This file was deleted.

26 changes: 26 additions & 0 deletions src/lib/prismic/clients/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ const ProfileSchema = z.object({
});
export type Profile = z.infer<typeof ProfileSchema>;

// A role is a string (e.g. "Owner") for a repository with one role, or a
// record of locale to role (e.g. { "de-de": "Writer" }) when roles are
// locale-scoped. A Write API token sends a string role, or omits it.
const RepositorySchema = z.object({
domain: z.string(),
name: z.optional(z.string()),
role: z.optional(z.union([z.string(), z.record(z.string(), z.string())])),
});

const RepositoryListSchema = z.object({
repositories: z.array(RepositorySchema),
});

export async function getProfile(config: {
token: string | undefined;
host: string;
Expand All @@ -20,3 +33,16 @@ export async function getProfile(config: {
unknownErrorMessage: "Failed to load your profile",
});
}

export async function getRepositories(config: {
token: string | undefined;
host: string;
}): Promise<z.infer<typeof RepositorySchema>[]> {
const url = new URL("user/repositories", `https://api.internal.${config.host}/`);
const response = await request(url, {
headers: { Authorization: `Bearer ${config.token}` },
schema: RepositoryListSchema,
unknownErrorMessage: "Failed to load your repositories",
});
return response.repositories;
}
22 changes: 22 additions & 0 deletions test/repo-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,25 @@ it("lists repositories as JSON", async ({ expect, prismic, repo }) => {
const parsed = JSON.parse(stdout);
expect(parsed).toEqual(expect.arrayContaining([expect.objectContaining({ domain: repo })]));
});

it("lists the one repository a Write API token can write to", async ({
expect,
prismic,
repo,
host,
writeToken,
}) => {
const { stdout, stderr, exitCode } = await prismic("repo", ["list", "--json"], {
nodeOptions: { env: { PRISMIC_TOKEN: writeToken } },
});
expect(exitCode, stderr).toBe(0);
const parsed: unknown = JSON.parse(stdout);
expect(parsed).toEqual([
{
domain: repo,
name: expect.any(String),
role: expect.any(String),
url: `https://${repo}.${host}/`,
},
]);
});
Loading