-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
258 lines (228 loc) · 6.81 KB
/
index.js
File metadata and controls
258 lines (228 loc) · 6.81 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
This code is based on the github-sync example of the @notionhq/client package.
https://github.com/makenotion/notion-sdk-js/blob/ba873383d5416405798c66d0b47fed3717c14f6a/examples/notion-github-sync/index.js
*/
/**
* @typedef {import("./types/gitlab.d.ts").GitLabIssue} GitLabIssue
* @typedef {import("./types/gitlab.d.ts").GitLabLabel} GitLabLabel
* @typedef {import("./types/gitlab.d.ts").GitLabMilestone} GitLabMilestone
* @typedef {import("./types/notion.d.ts").NotionPage} NotionPage
*/
const { Client } = require("@notionhq/client");
const dotenv = require("dotenv");
dotenv.config();
const _ = require("lodash");
const {
getGitLabIssuesForRepository,
getGitLabLabelsForProject,
getPropertiesFromIssue,
getGitLabMilestonesForProject,
} = require("./gitlab.js");
const notion = new Client({ auth: process.env.NOTION_KEY });
const DATABASE_ID = process.env.NOTION_DATABASE_ID;
const OPERATION_BATCH_SIZE = 10;
/**
* Local map to store GitLab issue ID to its Notion pageId.
* @type { [issueId: string]: string }
*/
const gitLabIssuesIdToNotionPageId = {};
/**
* Initialize local data store.
* Then sync with GitLab.
*/
setInitialGitLabToNotionIdMap().then(syncNotionDatabaseWithGitLab);
/**
* Get and set the initial data store with issues currently in the database.
*/
async function setInitialGitLabToNotionIdMap() {
const currentIssues = await getIssuesFromNotionDatabase();
for (const { pageId, issueNumber } of currentIssues) {
gitLabIssuesIdToNotionPageId[issueNumber] = pageId;
}
}
async function syncNotionDatabaseWithGitLab() {
// Get all issues currently in the provided GitLab repository.
console.log("\nFetching issues from GitLab repository...");
const issues = await getGitLabIssuesForRepository();
console.log(`Fetched ${issues.length} issues from GitLab repository.`);
// Get all labels used in the GitLab projct
const labels = await getGitLabLabelsForProject();
console.log(`Fetched ${labels.length} lables from GitLab project.`);
// Get all milestones used in the GitLab projct
const milestones = await getGitLabMilestonesForProject();
console.log(`Fetched ${milestones.length} milestones from GitLab project.`);
// Update all the label options to reflect the projects tag
await updateMultiSelectOptions(labels, milestones);
// Group issues into those that need to be created or updated in the Notion database.
const { pagesToCreate, pagesToUpdate } = getNotionOperations(issues);
// Create pages for new issues.
console.log(`\n${pagesToCreate.length} new issues to add to Notion.`);
await createPages(pagesToCreate);
// Updates pages for existing issues.
console.log(`\n${pagesToUpdate.length} issues to update in Notion.`);
await updatePages(pagesToUpdate);
// Success!
console.log("\n✅ Notion database is synced with GitLab.");
}
/**
* Gets pages from the Notion database.
*
* @returns {Promise<Array<{ pageId: string, issueNumber: number }>>}
*/
async function getIssuesFromNotionDatabase() {
/** @type {NotionPage[]} */
const pages = [];
let cursor = undefined;
while (true) {
const { results, next_cursor } = await notion.databases.query({
database_id: DATABASE_ID,
start_cursor: cursor,
});
pages.push(...results);
if (!next_cursor) {
break;
}
cursor = next_cursor;
}
console.log(`${pages.length} issues successfully fetched from Notion.`);
const issues = [];
for (const page of pages) {
issues.push({
pageId: page.id,
issueNumber: page.properties.id.rich_text[0].plain_text.slice(1),
});
}
return issues;
}
/**
* Determines which issues already exist in the Notion database.
*
* @param {Array<GitLabIssue>} issues
* @returns {{
* pagesToCreate: Array<GitLabIssue>;
* pagesToUpdate: Array<{ pageId: string } & GitLabIssue>
* }}
*/
function getNotionOperations(issues) {
const pagesToCreate = [];
const pagesToUpdate = [];
for (const issue of issues) {
const pageId = gitLabIssuesIdToNotionPageId[issue.iid];
if (pageId) {
pagesToUpdate.push({
...issue,
pageId,
});
} else {
pagesToCreate.push(issue);
}
}
return { pagesToCreate, pagesToUpdate };
}
/**
* Creates new pages in Notion.
*
* https://developers.notion.com/reference/post-page
*
* @param {Array <GitLabIssue>} pagesToCreate
*/
async function createPages(pagesToCreate) {
const pagesToCreateChunks = _.chunk(pagesToCreate, OPERATION_BATCH_SIZE);
for (const pagesToCreateBatch of pagesToCreateChunks) {
await Promise.all(
pagesToCreateBatch.map(issue =>
notion.pages.create({
parent: { database_id: DATABASE_ID },
properties: getPropertiesFromIssue(issue),
})
)
);
console.log(`Completed batch size: ${pagesToCreateBatch.length}`);
}
}
/**
* Updates provided pages in Notion.
*
* https://developers.notion.com/reference/patch-page
*
* @param {Array <GitLabIssue>} pagesToUpdate
*/
async function updatePages(pagesToUpdate) {
const pagesToUpdateChunks = _.chunk(pagesToUpdate, OPERATION_BATCH_SIZE);
for (const pagesToUpdateBatch of pagesToUpdateChunks) {
await Promise.all(
pagesToUpdateBatch.map(({ pageId, ...issue }) =>
notion.pages.update({
page_id: pageId,
properties: getPropertiesFromIssue(issue),
})
)
);
console.log(`Completed batch size: ${pagesToUpdateBatch.length}`);
}
}
/*
* A list of the available colors for notion multi select options
*
* https://developers.notion.com/reference/property-object#multi-select
*/
const availableColors = [
"blue",
"brown",
"default",
"gray",
"green",
"orange",
"pink",
"purple",
"red",
"yellow",
];
/**
* Updates all the multi-select options to match the labels and milestones created on GitLab
*
* https://developers.notion.com/reference/patch-page
*
* @param {Array<GitLabLabel>} labels
* @param {Array<GitLabMilestone>} milestones
*/
async function updateMultiSelectOptions(labels, milestones) {
// Delete all tags, otherwise notion will return a 400 code when colors get changed
await notion.databases.update({
database_id: DATABASE_ID,
properties: {
tags: {
multi_select: {
options: [],
},
},
milestones: {
multi_select: {
options: []
},
},
},
});
// Reinsert all the labels
await notion.databases.update({
database_id: DATABASE_ID,
properties: {
tags: {
multi_select: {
options: labels.map((l, i) => ({
name: l.name,
color: availableColors[i % availableColors.length],
})),
},
},
milestones: {
multi_select: {
options: milestones.map((m, i) => ({
name: m.title,
color: availableColors[i % availableColors.length],
})),
},
},
},
});
}