-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdangerfile.ts
284 lines (246 loc) · 6.68 KB
/
dangerfile.ts
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import flow from 'lodash/flow'
import curry from 'lodash/curry'
import flatten from 'lodash/flatten'
import map from 'lodash/map'
import partialRight from 'lodash/partialRight'
import { message, warn, danger } from 'danger'
import { build } from './bin/build'
import { preview } from './packages/sites/bin/preview'
const IS_TARGET_MASTER = danger.gitlab.mr.target_branch === 'master'
// pass through arg while execute side effects
function passThrough<T = any>(sideEffect: Function, arg: T): T {
sideEffect(arg)
return arg
}
class AsyncIO {
public value!: Promise<any>
constructor(value: any) {
this.value = Promise.resolve(value)
}
static of(value: any) {
return value instanceof AsyncIO ? value : new AsyncIO(value)
}
map(fn: Function) {
return AsyncIO.of(this.value.then(v => fn(v)))
}
}
function asyncFlow<T = any>(fns: Function[]) {
return function asyncFlowInternal(arg: T) {
let result: AsyncIO = AsyncIO.of(arg)
for (const fn of fns) {
result = result.map(fn)
}
return result
}
}
// side effects
// save input string in array
function accString(startStr: string = '') {
const acc: string[] = [startStr]
return function addStringToAcc(str: string = '') {
acc.push(str)
return acc
}
}
function join(sep: string, strList: string[]) {
return strList.join(sep)
}
function callIfTruthy(fn: Function, arg: any) {
if (Boolean(arg)) {
fn(arg)
}
}
function trim(input: string) {
return input.trim()
}
const cJoin = curry(join)
const cPassThrough = curry(passThrough)
const cCallIfTruthy = curry(callIfTruthy)
const messageIfTruthy = cCallIfTruthy(message)
function createMarkdownList(list: string[]) {
return list
.map(item => item.trim())
.filter(Boolean)
.map(item => `- ${item}\n`)
.join('')
}
const vueFiles = danger.git.fileMatch('*/vue/**/*.{js,md}')
const reactFiles = danger.git.fileMatch('*/react/**/*.{ts,tsx,md,mdx}')
const themeDefaultFiles = danger.git.fileMatch(
'*/theme-default/**/*.{scss,html}'
)
const helpersFiles = danger.git.fileMatch('*/helpers/**/*.ts')
const iconsFiles = danger.git.fileMatch('*packages/icons/icons/**/*.ts')
const sitesFiles = danger.git.fileMatch('*packages/sites/**/*.*')
const scopes = {
vue: vueFiles,
react: reactFiles,
themeDefault: themeDefaultFiles,
helpers: helpersFiles,
icons: iconsFiles,
sites: sitesFiles
} as const
const editedScopes: string[] = []
Object.entries(scopes).forEach(([scope, files]) => {
if (files.edited) {
editedScopes.push(scope)
}
})
const editFileAcc = accString()
const addEditedFilesHeader = cPassThrough((files: string[]) => {
if (files.length) {
editFileAcc(`**Edited files are:**`)
}
})
const printEditedFiles = flow([
addEditedFilesHeader,
createMarkdownList,
editFileAcc,
cJoin('\n'),
trim,
messageIfTruthy
])
const affectedScopeAcc = accString()
const addAffectedScopeHeader = cPassThrough((scopes: string[]) => {
if (scopes.length) {
affectedScopeAcc(`**Affected scopes are:**`)
}
})
const printAffectedScopes = flow([
addAffectedScopeHeader,
createMarkdownList,
affectedScopeAcc,
cJoin('\n'),
trim,
messageIfTruthy
])
type Commits = typeof danger.gitlab.commits
const filterOutCommitsHaveRelatedIssues = (commits: Commits) =>
commits.filter(({ message }) => {
return /ISSUES? CLOSED/.test(message)
})
const filterOutIssuesFromCommits = (commits: Commits) =>
commits.map(({ message }) => {
const [_, issues = ''] = message.match(/ISSUES? CLOSED:\s*([^\n]+)/) ?? []
return issues.trim().split(' ')
})
// make sure no repeated issues are printed
const unifiedIssues = (issues: string[]): string[] => {
const set = new Set(issues)
return Array.from(set)
}
const getIssues = flow([
filterOutCommitsHaveRelatedIssues,
filterOutIssuesFromCommits,
flatten,
unifiedIssues
])
const issuesAcc = accString()
const addIssuesHeader = cPassThrough((issues: string[]) => {
if (issues.length) {
issuesAcc('**Related issues are:**')
}
})
const warnIfNoIssues = cPassThrough((issues: string[]) => {
if (!issues.length) {
warn('**No related issue was found**, which is probably a bad thing')
}
})
const printIssues = flow([
getIssues,
warnIfNoIssues,
addIssuesHeader,
createMarkdownList,
issuesAcc,
cJoin('\n'),
trim,
messageIfTruthy
])
const sites = ['vue', 'react']
// based on edited scopes, gather sites to be previewed
// TODO: probably should preview sites as long as there is scope change
const getPreviewSites = (scopes: string[]) => {
const siteToBePreviewed = []
for (let scope of scopes) {
// impure
if (sites.includes(scope)) {
siteToBePreviewed.push(scope)
}
}
return siteToBePreviewed
}
type Site = string
type Url = string
type PreviewResult = [Site, Url] | void
type ValidPreviewResult = Exclude<PreviewResult, void>
const asyncPreBuildInternalDepsInSerial = async (sites: string[]) => {
await build(sites)
return sites
}
const asyncGetPreviewUrlForSite = async (
site: string
): Promise<PreviewResult> => {
// impure
if (sites.includes(site)) {
try {
const url = await preview(`uxc-${site}`)
if (typeof url === 'string') {
return [site, url]
}
} catch (e) {
return void 0
}
}
}
function waitForAllPreviewsDone(previewResults: Promise<PreviewResult>[]) {
return Promise.all(previewResults)
}
const generateUrlMessages = (urls: PreviewResult[]) => {
return (urls.filter(Boolean) as ValidPreviewResult[]).map(([site, url]) => {
return `You can preview **${site} site** in this 👉[**🔗URL**](${url})`
})
}
const pickUpMessage = (messageList: string) => {
if (messageList) {
return `**Preview links are:**\n${messageList}`
}
}
const printUrlMessages = flow([
createMarkdownList,
trim,
pickUpMessage,
messageIfTruthy
])
const printPreviewUrl = flow([
getPreviewSites,
asyncFlow([
asyncPreBuildInternalDepsInSerial,
partialRight(map, asyncGetPreviewUrlForSite),
waitForAllPreviewsDone,
generateUrlMessages,
printUrlMessages
])
])
const getMRAssignee = () => danger.gitlab.mr.assignee
const warnIfNoAssignee = (assignee?: string) => {
if (!assignee) {
warn('Please assign someone to review this mr')
}
}
const printAssigneeIssue = flow([getMRAssignee, warnIfNoAssignee])
printAssigneeIssue()
printEditedFiles([...danger.git.created_files, ...danger.git.modified_files])
printAffectedScopes(editedScopes)
printIssues(danger.gitlab.commits)
const conditionsToPreviewBothSites = [
['icons', 'helpers', 'sites', 'themeDefault'].some(scope =>
editedScopes.includes(scope)
),
IS_TARGET_MASTER
]
// preview both sites
if (conditionsToPreviewBothSites.some(condition => condition)) {
printPreviewUrl(sites)
} else {
printPreviewUrl(editedScopes)
}