Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/publish/confluence/confluence-verify.ts
6446 views
1
import { AccountToken } from "../provider-types.ts";
2
import { ConfluenceClient } from "./api/index.ts";
3
import { getMessageFromAPIError } from "./confluence-helper.ts";
4
import { withSpinner } from "../../core/console.ts";
5
import { Confirm } from "cliffy/prompt/mod.ts";
6
import { ConfluenceParent, Space, User } from "./api/types.ts";
7
import { trace } from "./confluence-logger.ts";
8
import { CAN_SET_PERMISSIONS_DISABLED, CAN_SET_PERMISSIONS_ENABLED_CACHED } from "./constants.ts";
9
10
const verifyWithSpinner = async (
11
verifyCommand: () => Promise<void>,
12
message: string = "Verifying...",
13
) => {
14
return await withSpinner({ message }, verifyCommand);
15
};
16
17
export const verifyAccountToken = async (accountToken: AccountToken) => {
18
try {
19
const client = new ConfluenceClient(accountToken);
20
await client.getUser();
21
} catch (error) {
22
throw new Error(
23
`Unable to sign into Confluence account: ${
24
getMessageFromAPIError(error)
25
}`,
26
);
27
}
28
};
29
30
export const verifyLocation = async (locationURL: string) => {
31
return await verifyWithSpinner(() => verifyLocationExists(locationURL));
32
};
33
34
const verifyLocationExists = async (server: string) => {
35
try {
36
const result: Response = await fetch(server);
37
if (!result.ok) {
38
throw new Error("");
39
}
40
} catch (error) {
41
trace("server doesnt exist", error);
42
throw new Error(`${server} doesn't exist`);
43
}
44
};
45
46
const verifyParentExists = async (
47
parentId: string,
48
accountToken: AccountToken,
49
) => {
50
try {
51
const client = new ConfluenceClient(accountToken);
52
await client.getContent(parentId);
53
} catch (error) {
54
throw new Error(`Parent doesn't exist: ${getMessageFromAPIError(error)}`);
55
}
56
};
57
58
export const verifyConfluenceParent = async (
59
parentUrl: string,
60
parent: ConfluenceParent,
61
) => {
62
if (parent.space.length === 0) {
63
throw new Error("Invalid Confluence parent URL: " + parentUrl);
64
}
65
await verifyLocation(parentUrl);
66
};
67
68
export const verifyOrWarnManagePermissions = async (
69
client: ConfluenceClient,
70
space: Space,
71
parent: ConfluenceParent,
72
user: User,
73
) => {
74
const canManagePermissions = await client.canSetPermissions(
75
parent,
76
space,
77
user,
78
);
79
80
if (canManagePermissions) {
81
// bug/5622-too-many-confluence-permission-checks
82
// Cache success result
83
localStorage.setItem(CAN_SET_PERMISSIONS_ENABLED_CACHED, "true");
84
trace("Caching permission check success");
85
} else {
86
const confirmed: boolean = await Confirm.prompt(
87
"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?",
88
);
89
if (!confirmed) {
90
throw new Error("");
91
}
92
}
93
94
};
95
96