Skip to content

Commit

Permalink
Merge pull request #292 from pantheon-systems/PCC-1523-add-cli-comman…
Browse files Browse the repository at this point in the history
…ds-for-managing-collection-user-permissions

PCC-1523 Add collaborator and visibility management commands.
  • Loading branch information
kevinstubbs authored Aug 19, 2024
2 parents 734f920 + bd39fb9 commit 1da1719
Show file tree
Hide file tree
Showing 7 changed files with 562 additions and 200 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-swans-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pantheon-systems/pcc-cli": minor
---

Added commands for managing collaborators and site visibility.
16 changes: 8 additions & 8 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@
},
"dependencies": {
"@pantheon-systems/pcc-sdk-core": "workspace:*",
"axios": "^1.6.8",
"axios": "^1.7.4",
"bluebird": "^3.7.2",
"boxen": "^7.1.1",
"chalk": "^5.3.0",
"dayjs": "^1.11.10",
"dayjs": "^1.11.12",
"dom-parser": "^1.1.5",
"fs-extra": "^11.2.0",
"get-port": "^7.0.0",
"google-auth-library": "^9.6.3",
"get-port": "^7.1.0",
"google-auth-library": "^9.13.0",
"googleapis": "^129.0.0",
"inquirer": "^8.2.6",
"nunjucks": "^3.2.4",
"octokit": "^3.1.2",
"octokit": "^3.2.1",
"open": "^9.1.0",
"ora": "^6.3.1",
"package-json": "^8.1.1",
Expand All @@ -65,17 +65,17 @@
"@types/fs-extra": "^11.0.4",
"@types/inquirer": "^9.0.7",
"@types/jest": "29.5.1",
"@types/node": "^20.11.21",
"@types/node": "^20.14.15",
"@types/nunjucks": "^3.2.6",
"@types/server-destroy": "^1.0.3",
"@types/showdown": "^2.0.6",
"@types/tmp": "^0.2.6",
"@types/yargs": "^17.0.32",
"@types/yargs": "^17.0.33",
"babel-jest": "^29.7.0",
"eslint": "^8.57.0",
"eslint-config-pcc-custom": "workspace:*",
"jest": "29.5.0",
"tmp": "^0.2.2",
"tmp": "^0.2.3",
"ts-jest": "29.1.0",
"tsup": "^7.2.0"
}
Expand Down
31 changes: 31 additions & 0 deletions packages/cli/src/cli/commands/sites/collaborators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import ora from "ora";
import AddOnApiHelper from "../../../lib/addonApiHelper";
import { errorHandler } from "../../exceptions";

type listCollaboratorsSchemaParams = { siteId: string };
export const listCollaborators = errorHandler<listCollaboratorsSchemaParams>(
async ({ siteId }: listCollaboratorsSchemaParams) => {
const spinner = ora("Retrieving Collaborators...").start();
const result = await AddOnApiHelper.listCollaborators(siteId);
spinner.succeed();
console.log(JSON.stringify(result, null, 4));
},
);

type removeCollaboratorschemaParams = { siteId: string; email: string };
export const removeCollaborator = errorHandler<removeCollaboratorschemaParams>(
async ({ siteId, email }: removeCollaboratorschemaParams) => {
const spinner = ora("Removing Collaborator...").start();
await AddOnApiHelper.removeCollaborator(siteId, email);
spinner.succeed();
},
);

type addCollaboratorschemaParams = { siteId: string; email: string };
export const addCollaborator = errorHandler<addCollaboratorschemaParams>(
async ({ siteId, email }: addCollaboratorschemaParams) => {
const spinner = ora("Adding Collaborator...").start();
await AddOnApiHelper.addCollaborator(siteId, email);
spinner.succeed();
},
);
9 changes: 9 additions & 0 deletions packages/cli/src/cli/commands/sites/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ export const configurableSiteProperties = [
type: "string",
},
},
{
id: "visibility",
command: {
name: "visibility <visibility>",
description:
"Set the collection's visibility (either 'private' or 'workspace')",
type: "string",
},
},
] as const;

export const SITE_EXAMPLES = [
Expand Down
100 changes: 100 additions & 0 deletions packages/cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ import {
listAdminsSchema,
removeAdminSchema,
} from "./commands/sites/admins";
import {
addCollaborator,
listCollaborators,
removeCollaborator,
} from "./commands/sites/collaborators";
import {
getComponentSchema,
printLiveComponentSchema,
Expand Down Expand Up @@ -609,6 +614,101 @@ yargs(hideBin(process.argv))
);
},
)
.command(
"publishing <cmd> [options]",
"Manage publishing permissions.",
(yargs) => {
yargs
.strictCommands()
.demandCommand()
.command(
"config [options]",
"Update the collection's visibility.",
(yargs) => {
yargs
.strictCommands()
.option("siteId", {
describe: "The id of the collection to modify.",
demandOption: true,
type: "string",
})
.option("mode", {
describe:
"The visibility of this collection (either 'private' or 'workspace').",
demandOption: true,
type: "string",
});
},
async (args) =>
await updateSiteConfig({
id: args.siteId as string,
visibility: args.mode as string,
}),
)
.command(
"list-user [options]",
"Print the users added as collaborators to this collection.",
(yargs) => {
yargs.strictCommands().option("siteId", {
describe: "The id of the collection to modify.",
demandOption: true,
type: "string",
});
},
async (args) =>
await listCollaborators({
siteId: args.siteId as string,
}),
)
.command(
"add-user [options]",
"Update the collection's visibility.",
(yargs) => {
yargs
.strictCommands()
.option("siteId", {
describe: "The id of the collection to modify.",
demandOption: true,
type: "string",
})
.option("user", {
describe: "The email of the user to add.",
demandOption: true,
type: "string",
});
},
async (args) =>
await addCollaborator({
siteId: args.siteId as string,
email: args.user as string,
}),
)
.command(
"remove-user [options]",
"Update the collection's visibility.",
(yargs) => {
yargs
.strictCommands()
.option("siteId", {
describe: "The id of the collection to modify.",
demandOption: true,
type: "string",
})
.option("user", {
describe: "The email of the user to remove.",
demandOption: true,
type: "string",
});
},
async (args) =>
await removeCollaborator({
siteId: args.siteId as string,
email: args.user as string,
}),
);
},
async (args) => await createSite(args.url as string),
)
.example(formatExamples(SITE_EXAMPLES));
},
async () => {
Expand Down
47 changes: 47 additions & 0 deletions packages/cli/src/lib/addonApiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,53 @@ class AddOnApiHelper {
});
}

static async listCollaborators(id: string): Promise<void> {
const idToken = await this.getIdToken();

return (
await axios.get(
`${(await getApiConfig()).SITE_ENDPOINT}/${id}/collaborators`,
{
headers: {
Authorization: `Bearer ${idToken}`,
},
},
)
).data;
}

static async addCollaborator(id: string, email: string): Promise<void> {
const idToken = await this.getIdToken();

await axios.patch(
`${(await getApiConfig()).SITE_ENDPOINT}/${id}/collaborators`,
{
email,
},
{
headers: {
Authorization: `Bearer ${idToken}`,
},
},
);
}

static async removeCollaborator(id: string, email: string): Promise<void> {
const idToken = await this.getIdToken();

await axios.delete(
`${(await getApiConfig()).SITE_ENDPOINT}/${id}/collaborators`,
{
headers: {
Authorization: `Bearer ${idToken}`,
},
data: {
email,
},
},
);
}

static async updateSiteConfig(
id: string,
{
Expand Down
Loading

0 comments on commit 1da1719

Please sign in to comment.