-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.js
225 lines (184 loc) · 5.39 KB
/
action.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
import { diff } from 'deep-object-diff'
import * as assert from 'assert'
import { GitHubWrapper } from './libs/github-wrapper.js'
import { NpmWrapper } from './libs/npm-wrapper.js'
import { NotionWrapper } from './libs/notion-wrapper.js'
import * as utils from './libs/utils.js'
export { upsertStatusBoard }
async function upsertStatusBoard ({
logger,
githubToken,
githubRepositoryQuery,
githubIssueLabels,
notionToken,
databaseId,
options
}) {
const github = new GitHubWrapper({
auth: githubToken,
logger,
options
})
const npm = new NpmWrapper({
logger
})
const notion = new NotionWrapper({
auth: notionToken,
databaseId,
logger
})
await notion.prepareDatabase()
const githubRepos = await github.searchRepositories(githubRepositoryQuery, githubIssueLabels)
logger.info('Found %d repositories', githubRepos.length)
const npmPackages = await npm.searchPackages(githubRepos)
logger.info('Found %d npm packages', npmPackages.length)
const notionLines = await notion.readAllDatabase()
logger.info('Read %d records from notion', notionLines.length)
const todoList = buildActions({
githubRepos,
npmPackages,
notionLines,
notionClient: notion,
options
})
logger.info('Found %d actions to perform', todoList.length)
await Promise.all(todoList.map(executeAction))
async function executeAction (item) {
switch (item.action) {
case 'add':
{
const result = await notion.createItem(item.payload)
logger.info('Added item %s', result.url)
return
}
case 'update':
{
const result = await notion.updateItem(item.previousData, item.payload)
logger.info('Updated item %s', result.url)
return
}
case 'delete':
{
const result = await notion.deleteItem(item.previousData)
logger.info('Deleted item %s', result.id)
}
}
}
logger.info('Processed %d items', todoList.length)
return {
processedItems: todoList.length
}
}
function buildActions ({
githubRepos,
npmPackages,
notionLines,
notionClient,
options: { deleteAdditionalRows }
}) {
const githubReposMap = toMap(githubRepos, ghSharedKey)
const npmPackagesMap = toMap(npmPackages, npmSharedKey)
const notionLinesMap = toMap(notionLines, notionSharedKey)
const addAndUpdateActions = githubRepos
.map(github => ({ github }))
.map(decorateWith(npmPackagesMap, 'npm'))
.map(decorateWith(notionLinesMap, 'notion'))
.map(convertToAction)
.filter(removeUnchangedLines.bind(notionClient))
if (!deleteAdditionalRows) {
return addAndUpdateActions
}
const deleteActions = notionLines
.filter(removeOldLines(githubReposMap))
.map(line => ({ action: 'delete', previousData: line }))
return addAndUpdateActions.concat(deleteActions)
}
function removeOldLines (githubReposMap) {
return function removeOldLines (line) {
const key = notionSharedKey(line)
return !githubReposMap.has(key)
}
}
function removeUnchangedLines (item) {
if (item.action !== 'update') {
return true
}
const asLocal = this.toHumanProperties(item.previousData.properties)
const changedFields = diff(asLocal, item.payload)
assert.ok(!changedFields.title, 'title should not change')
// no changes
if (Object.keys(changedFields).length === 0) {
return false
}
// special case for array fields
// if the array has changed, the diff returns an indexed object
// since we want to align the whole array, we need to restore it
if (changedFields.topics) {
changedFields.topics = item.payload.topics
}
item.payload = {
title: item.payload.title,
...changedFields
}
return true
}
function convertToAction ({ github, npm, notion }) {
// all the fields must be listed here
let payload = {
title: github.name,
version: github.pkg?.version,
stars: github.stargazerCount,
repositoryUrl: github.url,
topics: github.repositoryTopics?.nodes?.map(node => node.topic.name),
prs: github.pullRequests.totalCount,
issues: github.issues.totalCount,
lastCommitAt: utils.toJsDateString(github.defaultBranchRef?.target?.history?.nodes?.[0]?.committedDate),
lastReleaseAt: utils.toJsDateString(github.releases?.nodes?.[0]?.publishedAt),
archived: github.isArchived,
packageUrl: undefined,
packageSizeBytes: undefined,
downloads: undefined
}
if (npm) {
payload = {
...payload,
version: npm.manifest.version,
packageUrl: `https://www.npmjs.com/package/${npm?.name}`,
packageSizeBytes: npm.manifest.dist.unpackedSize,
downloads: npm.downloads.downloads
}
}
return {
action: notion ? 'update' : 'add',
previousData: notion,
payload
}
}
function decorateWith (map, key) {
return function decorateWithNpmPackage (wrapper) {
wrapper[key] = map.get(ghSharedKey(wrapper.github))
return wrapper
}
}
function ghSharedKey (ghRepo) {
return `/${ghRepo.owner.login}/${ghRepo.name}`
}
function npmSharedKey (pkg) {
return `/${pkg.githubId}`
}
function notionSharedKey (line) {
if (!line.properties.GitHub.url) {
return null
}
return new URL(line.properties.GitHub.url).pathname
}
function toMap (array, genKey) {
return array.reduce((acc, item) => {
const key = genKey(item)
if (key) {
// the npm key may be undefined if the repo is not published
acc.set(genKey(item), item)
}
return acc
}, new Map())
}