Path: blob/main/src/publish/confluence/confluence-verify.ts
6446 views
import { AccountToken } from "../provider-types.ts";1import { ConfluenceClient } from "./api/index.ts";2import { getMessageFromAPIError } from "./confluence-helper.ts";3import { withSpinner } from "../../core/console.ts";4import { Confirm } from "cliffy/prompt/mod.ts";5import { ConfluenceParent, Space, User } from "./api/types.ts";6import { trace } from "./confluence-logger.ts";7import { CAN_SET_PERMISSIONS_DISABLED, CAN_SET_PERMISSIONS_ENABLED_CACHED } from "./constants.ts";89const verifyWithSpinner = async (10verifyCommand: () => Promise<void>,11message: string = "Verifying...",12) => {13return await withSpinner({ message }, verifyCommand);14};1516export const verifyAccountToken = async (accountToken: AccountToken) => {17try {18const client = new ConfluenceClient(accountToken);19await client.getUser();20} catch (error) {21throw new Error(22`Unable to sign into Confluence account: ${23getMessageFromAPIError(error)24}`,25);26}27};2829export const verifyLocation = async (locationURL: string) => {30return await verifyWithSpinner(() => verifyLocationExists(locationURL));31};3233const verifyLocationExists = async (server: string) => {34try {35const result: Response = await fetch(server);36if (!result.ok) {37throw new Error("");38}39} catch (error) {40trace("server doesnt exist", error);41throw new Error(`${server} doesn't exist`);42}43};4445const verifyParentExists = async (46parentId: string,47accountToken: AccountToken,48) => {49try {50const client = new ConfluenceClient(accountToken);51await client.getContent(parentId);52} catch (error) {53throw new Error(`Parent doesn't exist: ${getMessageFromAPIError(error)}`);54}55};5657export const verifyConfluenceParent = async (58parentUrl: string,59parent: ConfluenceParent,60) => {61if (parent.space.length === 0) {62throw new Error("Invalid Confluence parent URL: " + parentUrl);63}64await verifyLocation(parentUrl);65};6667export const verifyOrWarnManagePermissions = async (68client: ConfluenceClient,69space: Space,70parent: ConfluenceParent,71user: User,72) => {73const canManagePermissions = await client.canSetPermissions(74parent,75space,76user,77);7879if (canManagePermissions) {80// bug/5622-too-many-confluence-permission-checks81// Cache success result82localStorage.setItem(CAN_SET_PERMISSIONS_ENABLED_CACHED, "true");83trace("Caching permission check success");84} else {85const confirmed: boolean = await Confirm.prompt(86"We've detected that your account is not able to manage the permissions for this destination.\n\nPublished pages will be directly editable within the Confluence web editor.\n\nThis means that if you republish the page from Quarto, you may be overwriting the web edits.\nWe recommend that you establish a clear policy about how this published page will be revised.\n\nAre you sure you want to continue?",87);88if (!confirmed) {89throw new Error("");90}91}9293};949596