forked from szenius/notion-update-page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (127 loc) · 3.42 KB
/
index.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const core = require("@actions/core");
const { Client } = require("@notionhq/client");
require("dotenv").config();
const SUPPORTED_PROPERTY_TYPES = {
URL: "url",
RICH_TEXT: "rich_text",
MULTI_SELECT: "multi_select",
};
const SUPPORTED_EXISTING_VALUE_ACTIONS = {
OVERWRITE: "overwrite",
APPEND: "append",
};
const generateUpdateProps = (
propertyType,
existingValueAction,
propertyName,
newValue,
pageDetails
) => {
if (propertyType === SUPPORTED_PROPERTY_TYPES.MULTI_SELECT) {
const selectValues = pageDetails.properties[propertyName].multi_select;
selectValues.push({ name: newValue });
return {
multi_select: selectValues,
};
}
if (propertyType === SUPPORTED_PROPERTY_TYPES.URL) {
return {
url: newValue,
};
}
if (existingValueAction === SUPPORTED_EXISTING_VALUE_ACTIONS.OVERWRITE) {
return {
rich_text: [{ type: "text", text: { content: newValue } }],
};
}
const richTextValues = pageDetails.properties[propertyName].rich_text;
richTextValues.push(newValue);
return {
rich_text: [{ type: "text", text: { content: richTextValues.join(",") } }],
};
};
const updateNotionStory = async (
notionKey,
notionPageId,
propertyName,
value,
propertyType,
existingValueAction
) => {
const notion = new Client({ auth: notionKey });
const pageDetails = await notion.pages.retrieve({ page_id: notionPageId });
const updateProps = generateUpdateProps(
propertyType,
existingValueAction,
propertyName,
value,
pageDetails
);
await notion.pages.update({
page_id: notionPageId,
properties: {
[propertyName]: updateProps,
},
});
};
const getConfig = () => {
const isOffline = process.env.NODE_ENV === "offline";
if (isOffline) {
return {
notionKey: process.env.NOTION_KEY,
notionPageId: process.env.NOTION_PAGE_ID,
notionPropertyName: process.env.NOTION_PROPERTY_NAME,
notionPropertyType:
process.env.NOTION_PROPERTY_TYPE || SUPPORTED_PROPERTY_TYPES.RICH_TEXT,
notionUpdateValue: process.env.NOTION_UPDATE_VALUE,
existingValueAction:
process.env.EXISTING_VALUE ||
SUPPORTED_EXISTING_VALUE_ACTIONS.OVERWRITE,
};
}
return {
notionKey: core.getInput("notion-key"),
notionPageId: core.getInput("notion-page-id"),
notionPropertyName: core.getInput("notion-property-name"),
notionPropertyType:
core.getInput("notion-property-type") ||
SUPPORTED_PROPERTY_TYPES.RICH_TEXT,
notionUpdateValue: core.getInput("notion-update-value"),
existingValueAction:
core.getInput("existing-value") ||
SUPPORTED_EXISTING_VALUE_ACTIONS.OVERWRITE,
};
};
const run = async () => {
const {
notionKey,
notionPageId,
notionPropertyName,
notionPropertyType,
notionUpdateValue,
existingValueAction,
} = getConfig();
if (
!SUPPORTED_PROPERTY_TYPES.hasOwnProperty(notionPropertyType.toUpperCase())
) {
core.setFailed(
`Type of Notion Page property ${notionPropertyType} is not supported.`
);
}
try {
await updateNotionStory(
notionKey,
notionPageId,
notionPropertyName,
notionUpdateValue,
notionPropertyType,
existingValueAction
);
} catch (error) {
core.setFailed(`Error updating Notion page ${notionPageId}: ${error}`);
}
console.info(
`Updated Notion page ${notionPageId} | ${notionPropertyName}: ${notionUpdateValue}`
);
};
run();