-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
234 lines (206 loc) · 5.86 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
const axios = require("axios");
const btoa = require("btoa");
const core = require("@actions/core");
const github = require("@actions/github");
const { Client } = require("@notionhq/client");
require("dotenv").config();
const SUPPORTED_PROPERTY_TYPES = {
RICH_TEXT: "rich_text",
MULTI_SELECT: "multi_select",
};
const SUPPORTED_EXISTING_VALUE_ACTIONS = {
OVERWRITE: "overwrite",
APPEND: "append",
};
const getGitHubRequestHeaders = (username, accessToken) => ({
headers: { Authorization: `Basic ${btoa(`${username}:${accessToken}`)}` },
});
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 (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 extractFirstNotionPageId = (prDescription) => {
const notionURLs = prDescription.match(
/(https?:\/\/)?(www\.)?notion\.so\/([A-Za-z0-9\-\/]+)/gi
);
if (notionURLs === null || notionURLs.length === 0) {
return null;
}
const firstNotionURLTokens = notionURLs[0].split("/");
const notionURLPath = firstNotionURLTokens[firstNotionURLTokens.length - 1];
const notionURLPathTokens = notionURLPath.split("-");
return notionURLPathTokens[notionURLPathTokens.length - 1];
};
const fetchPRDescription = async (prURL, username, accessToken) => {
const response = await axios.get(
prURL,
getGitHubRequestHeaders(username, accessToken)
);
const { body } = response.data;
return body;
};
const fetchPRURL = async (
commitHash,
username,
accessToken,
repoOwner,
repoName
) => {
const response = await axios.get(
`https://api.github.com/search/issues?q=hash:${commitHash}`,
getGitHubRequestHeaders(username, accessToken)
);
const { items: issues } = response.data;
if (issues === null || issues.length === 0) {
return null;
}
const prURLRegex = new RegExp(
`https://api.github.com/repos/${repoOwner}/${repoName}/pulls/[0-9]+`
);
const { pull_request } = issues.find((issue) =>
issue.pull_request.url.match(prURLRegex)
);
return pull_request.url;
};
const getConfig = () => {
const isOffline = process.env.NODE_ENV === "offline";
if (isOffline) {
return {
commitHash: process.env.COMMIT_HASH,
repoOwner: process.env.REPO_OWNER,
repoName: process.env.REPO_NAME,
username: process.env.GH_USERNAME,
accessToken: process.env.GH_ACCESS_TOKEN,
notionKey: process.env.NOTION_KEY,
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 {
commitHash: github.context.sha,
repoOwner: github.context.repo.owner,
repoName: github.context.repo.repo,
username: core.getInput("gh-username"),
accessToken: core.getInput("gh-token"),
notionKey: core.getInput("notion-key"),
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 {
commitHash,
repoOwner,
repoName,
username,
accessToken,
notionKey,
notionPropertyName,
notionPropertyType,
notionUpdateValue,
existingValueAction,
} = getConfig();
if (
!SUPPORTED_PROPERTY_TYPES.hasOwnProperty(notionPropertyType.toUpperCase())
) {
core.setFailed(
`Type of Notion Page property ${notionPropertyType} is not supported.`
);
}
let prDescription = "";
try {
const prURL = await fetchPRURL(
commitHash,
username,
accessToken,
repoOwner,
repoName
);
if (prURL === null) {
core.setFailed(
`Error get PR URL for hash ${commitHash}. Commit may not be created by merging a PR.`
);
}
prDescription = await fetchPRDescription(prURL, username, accessToken);
} catch (error) {
core.setFailed(
`Error fetch PR description with hash ${commitHash}: ${error}`
);
}
const notionPageId = extractFirstNotionPageId(prDescription);
if (notionPageId === null) {
core.setFailed("No Notion URL found.");
}
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();