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
1,462 changes: 1,085 additions & 377 deletions packages/api-client/src/schema.ts

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion packages/cli/src/commands/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import { registerAccountDomain } from "./domain";
import { registerAccountGet } from "./get";
import { registerAccountInvite } from "./invite";
import { registerAccountMember } from "./member";
import { registerAccountProjectToken } from "./project-token";
import { registerAccountUpdate } from "./update";

export function accountCommand(program: Command) {
const account = program
.command("account")
.description(
"Inspect an account's plan and usage, and manage a team's members, invites, and email domains",
"Inspect an account's plan and usage, manage its project tokens, and a team's members, invites, and email domains",
);
registerAccountGet(account);
registerAccountUpdate(account);
registerAccountMember(account);
registerAccountInvite(account);
registerAccountDomain(account);
registerAccountProjectToken(account);
}
112 changes: 112 additions & 0 deletions packages/cli/src/commands/account/project-token/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { Option, type Command } from "commander";
import { unwrap, unwrapEmpty } from "../../../lib/api";
import {
formatCreatedProjectToken,
formatProjectTokens,
} from "../../../lib/format";
import {
buildCreateTokenBody,
tokenExpireAtOption,
tokenExpiresInOption,
tokenNameOption,
tokenPermissionsOption,
type CreateProjectTokenOptions,
} from "../../../lib/project-token";
import { accountSlugOption, jsonOption, tokenOption } from "../../../options";
import { runAccountAction, type AccountCommandOptions } from "../_run";

export function registerAccountProjectToken(account: Command) {
const projectToken = account
.command("project-token")
.description(
"Manage every project token of an account, including the ones linked to several projects. Requires administrator access to the account",
);

projectToken
.command("list")
.description(
"List an account's project tokens, whatever projects they are linked to",
)
.addOption(accountSlugOption)
.addOption(tokenOption)
.addOption(jsonOption)
.action((options: AccountCommandOptions) =>
runAccountAction({
options,
handler: async ({ client, accountSlug }) =>
unwrap(
await client.GET("/accounts/{accountSlug}/project-tokens", {
params: { path: { accountSlug } },
}),
),
format: formatProjectTokens,
}),
);

projectToken
.command("create")
.description(
"Create a project token linked to one or more projects of the account and print it. It is shown only once: store it right away. CI using a token linked to several projects must set ARGOS_PROJECT",
)
.addOption(
new Option(
"--projects <names...>",
"Names of the account's projects to link, e.g. web docs",
).makeOptionMandatory(),
)
.addOption(tokenNameOption)
.addOption(tokenPermissionsOption)
.addOption(tokenExpireAtOption)
.addOption(tokenExpiresInOption)
.addOption(accountSlugOption)
.addOption(tokenOption)
.addOption(jsonOption)
.action(
(
options: AccountCommandOptions &
CreateProjectTokenOptions & { projects: string[] },
) =>
runAccountAction({
options,
handler: async ({ client, accountSlug }) =>
unwrap(
await client.POST("/accounts/{accountSlug}/project-tokens", {
params: { path: { accountSlug } },
body: {
...buildCreateTokenBody(options),
projects: options.projects,
},
}),
),
format: formatCreatedProjectToken,
}),
);

projectToken
.command("revoke")
.description(
"Revoke a project token of the account. CI using it is rejected from its next request",
)
.argument(
"<tokenId>",
"Token ID, taken from `argos account project-token list`",
)
.addOption(accountSlugOption)
.addOption(tokenOption)
.addOption(jsonOption)
.action((tokenId: string, options: AccountCommandOptions) =>
runAccountAction({
options,
handler: async ({ client, accountSlug }) => {
unwrapEmpty(
await client.DELETE(
"/accounts/{accountSlug}/project-tokens/{tokenId}",
{ params: { path: { accountSlug, tokenId } } },
),
);
return { revoked: true, tokenId };
},
format: ({ tokenId: revoked }) => `Revoked token ${revoked}.`,
}),
);
}
12 changes: 1 addition & 11 deletions packages/cli/src/commands/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { fail } from "../lib/cli-error";
import { formatAnalytics } from "../lib/format";
import { handleCliError, output } from "../lib/run";
import { resolveToken } from "../lib/target";
import { jsonOption, tokenOption } from "../options";
import { DAY_MS, jsonOption, toIsoOrFail, tokenOption } from "../options";

type GroupBy = "day" | "week" | "month";

Expand All @@ -19,16 +19,6 @@ type AnalyticsOptions = {
};

const DEFAULT_PERIOD_DAYS = 30;
const DAY_MS = 24 * 60 * 60 * 1000;

/** Coerce a user-supplied date into an ISO 8601 datetime, or fail cleanly. */
function toIsoOrFail(value: string, option: string): string {
const date = new Date(value);
if (Number.isNaN(date.getTime())) {
fail(`Invalid --${option} value: "${value}". Use an ISO 8601 datetime.`);
}
return date.toISOString();
}

/** Collect a repeatable `--project` flag into an array of project names. */
function collectProject(value: string, previous: string[] = []): string[] {
Expand Down
34 changes: 28 additions & 6 deletions packages/cli/src/commands/baseline/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ import {
} from "../../lib/download";
import { formatSnapshotDownload } from "../../lib/format";
import { handleCliError, output } from "../../lib/run";
import { resolveOptionalToken } from "../../lib/target";
import { resolveOptionalToken, resolveProjectPath } from "../../lib/target";
import {
buildProjectPathOption,
jsonOption,
tokenOption,
type JsonOption,
type ProjectPathOption,
type TokenOption,
} from "../../options";

type DownloadOptions = TokenOption &
ProjectPathOption &
JsonOption & {
output?: string | undefined;
branch?: string | undefined;
Expand All @@ -32,7 +35,8 @@ type DownloadOptions = TokenOption &
};

// The baseline lookup (`POST /baseline`) only accepts a project token, and the
// project it searches is the token's own.
// project it searches is the token's own — the one named by --project when the
// token is linked to several.
const PROJECT_TOKEN_HINT =
"Finding the baseline takes the project's token: pass it with --token or set ARGOS_TOKEN. The token stored by `argos login` cannot be used.";

Expand All @@ -43,9 +47,14 @@ const FAILURE_STATUSES = new Set<SnapshotDiff["status"]>([
"retryFailure",
]);

async function fetchTokenProject(client: ArgosAPIClient) {
async function fetchTokenProject(
client: ArgosAPIClient,
project: string | undefined,
) {
try {
return unwrap(await client.GET("/project"));
return unwrap(
await client.GET("/project", { params: { query: { project } } }),
);
} catch (error) {
if (
error instanceof APIError &&
Expand Down Expand Up @@ -85,6 +94,7 @@ export function registerBaselineDownload(baseline: Command) {
.choices(["ci", "monitoring"])
.default("ci"),
)
.addOption(buildProjectPathOption)
.addOption(tokenOption)
.addOption(jsonOption)
.action(async (options: DownloadOptions) => {
Expand All @@ -99,7 +109,14 @@ export function registerBaselineDownload(baseline: Command) {
);
}
const client = createApiClient(token);
const project = await fetchTokenProject(client);
// Named on the token's endpoints only when set: a token linked to a
// single project needs no more, one linked to several is rejected
// without it.
const projectPath = resolveProjectPath(options);
const projectSlug = projectPath
? `${projectPath.owner}/${projectPath.project}`
: undefined;
const project = await fetchTokenProject(client, projectSlug);
const path = { owner: project.account.slug, project: project.name };
const branch = options.branch ?? project.defaultBaseBranch;

Expand All @@ -120,7 +137,12 @@ export function registerBaselineDownload(baseline: Command) {
findBaseline: async (commits) =>
unwrap(
await client.POST("/baseline", {
body: { commits, name: options.buildName, mode: options.mode },
body: {
project: projectSlug,
commits,
name: options.buildName,
mode: options.mode,
},
}),
).baseline,
});
Expand Down
16 changes: 12 additions & 4 deletions packages/cli/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ import type { Command } from "commander";
import { Option } from "commander";
import { deploy } from "@argos-ci/core";
import ora from "ora";
import { tokenOption, type TokenOption } from "../options";
import {
projectOption,
tokenOption,
type ProjectOption,
type TokenOption,
} from "../options";

type DeployOptions = TokenOption & {
prod?: boolean;
};
type DeployOptions = TokenOption &
ProjectOption & {
prod?: boolean;
};

export function deployCommand(program: Command) {
program
Expand All @@ -17,6 +23,7 @@ export function deployCommand(program: Command) {
"Deploy a static build (Storybook or any static site) to Argos",
)
.addOption(tokenOption)
.addOption(projectOption)
.addOption(
new Option("--prod", "Deploy as a production deployment").default(false),
)
Expand All @@ -25,6 +32,7 @@ export function deployCommand(program: Command) {
try {
const result = await deploy({
token: options.token,
project: options.project,
root: resolve(directory),
environment: options.prod ? "production" : undefined,
});
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/commands/finalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import { finalize } from "@argos-ci/core";
import {
buildNameOption,
parallelNonceOption,
projectOption,
tokenOption,
type BuildNameOption,
type ParallelNonceOption,
type ProjectOption,
type TokenOption,
} from "../options";

type FinalizeOptions = ParallelNonceOption &
TokenOption &
ProjectOption &
BuildNameOption & { skipIfEmpty?: boolean | undefined };

const skipIfEmptyOption = new Option(
Expand All @@ -25,13 +28,15 @@ export function finalizeCommand(program: Command) {
.description("Finalize pending parallel builds")
.addOption(parallelNonceOption)
.addOption(tokenOption)
.addOption(projectOption)
.addOption(skipIfEmptyOption)
.addOption(buildNameOption)
.action(async (options: FinalizeOptions) => {
const spinner = ora("Finalizing builds").start();
try {
const result = await finalize({
token: options.token,
project: options.project,
parallel: options.parallelNonce
? { nonce: options.parallelNonce }
: undefined,
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/commands/project/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ import { registerProjectContributor } from "./contributor";
import { registerProjectDeployments } from "./deployments";
import { registerProjectDomain } from "./domain";
import { registerProjectGet } from "./get";
import { registerProjectToken } from "./token";
import { registerProjectTransfer } from "./transfer";
import { registerProjectUpdate } from "./update";

export function projectCommand(program: Command) {
const project = program
.command("project")
.description(
"Inspect and configure a project: settings, contributors, deployments, and its deployment domain",
"Inspect and configure a project: settings, contributors, tokens, deployments, and its deployment domain",
);
registerProjectGet(project);
registerProjectUpdate(project);
registerProjectTransfer(project);
registerProjectContributor(project);
registerProjectToken(project);
registerProjectDeployments(project);
registerProjectDomain(project);
}
Loading
Loading