-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
130 lines (118 loc) · 4.09 KB
/
index.js
File metadata and controls
130 lines (118 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const Confluence = require("confluence-api");
const core = require("@actions/core");
const parser = require("node-html-parser")
const path = require('path')
const axios = require('axios')
const filesStructure = require("./utils/files");
const SyncConfluence = require("./utils/confluence");
const markdownToHtml = require("./utils/markdownToHtml");
const root = "./" + core.getInput("folder", { required: true }) + "/";
const spaceKey = core.getInput("space-key", { required: true });
const rootParentPageId = core.getInput("parent-page-id", { required: true });
const config = {
username: core.getInput("username", { required: true }),
password: core.getInput("password", { required: true }),
baseUrl: core.getInput("confluence-base-url", { required: true }),
};
const confluenceAPI = new Confluence(config);
const syncConfluence = new SyncConfluence(
confluenceAPI,
spaceKey,
rootParentPageId
);
const cachedPageIdByTitle = {};
async function findOrCreatePage(pageTitle, parentPageId) {
let pageId;
if (cachedPageIdByTitle[pageTitle]) {
pageId = cachedPageIdByTitle[pageTitle];
} else {
pageId = await syncConfluence.getPageIdByTitle(pageTitle);
if (pageId) {
} else {
pageId = await syncConfluence.createEmptyParentPage(
pageTitle,
parentPageId
);
}
cachedPageIdByTitle[pageTitle] = pageId;
}
return pageId;
}
async function uploadAttachment(attachmentSource, pageId) {
attachmentSource = root + attachmentSource;
const existingAttachments = await syncConfluence.getAttachments(pageId)
if (existingAttachments) {
for (let attachment of existingAttachments) {
if (attachment.title === path.basename(attachmentSource)) {
return await syncConfluence.updateAttachment(pageId, attachment.id, attachmentSource);
}
}
}
return await syncConfluence.uploadAttachment(pageId, attachmentSource);
}
async function handleAttachments(contentPageId, data) {
const html = parser.parse(data);
const images = html.querySelectorAll("img")
for (var image of images) {
const attachmentSource = image.getAttribute("src");
// TODO handle remote images
if (attachmentSource.includes("http")) { continue; }
var attachment = await uploadAttachment(attachmentSource.replace("..", "."), contentPageId);
image.replaceWith(parser.parse('<ac:image><ri:attachment ri:filename=' + attachment.title +' /></ac:image>'));
}
return html.toString()
}
async function validateSubscription() {
const API_URL = `https://agent.api.stepsecurity.io/v1/github/${process.env.GITHUB_REPOSITORY}/actions/subscription`;
try {
await axios.get(API_URL, {timeout: 3000});
} catch (error) {
if (error.response && error.response.status === 403) {
console.error(
'Subscription is not valid. Reach out to support@stepsecurity.io'
);
process.exit(1);
} else {
core.info('Timeout or API not reachable. Continuing to next step.');
}
}
}
async function main() {
await validateSubscription();
const files = filesStructure(root);
if (!files.length) {
console.log("No markdown files found in %s", root);
}
for (const f of files) {
let path = f.join("/");
let currentParentPageId = rootParentPageId;
let pathsInRoot = root.split("/");
let newRoot= root;
if(pathsInRoot.length > 2){
newRoot = "./" + pathsInRoot[1] + "/"
console.log("Root for action includes subfolder. Assigning root as: " + newRoot)
}
for (const subPath of f) {
if (subPath.includes(".md")) {
let pageTitle = subPath.replace(".md", "");
let contentPageId = await findOrCreatePage(
pageTitle,
currentParentPageId
);
markdownToHtml(newRoot + path, async (err, data) => {
if(err) {
console.log(err);
}
let htmlContent = await handleAttachments(contentPageId, data);
syncConfluence.putContent(contentPageId, pageTitle, htmlContent);
});
} else {
currentParentPageId = await findOrCreatePage(
subPath,
currentParentPageId
);
}
}
}
}
main();