This repository was archived by the owner on Feb 1, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild.js
More file actions
141 lines (119 loc) · 3.58 KB
/
build.js
File metadata and controls
141 lines (119 loc) · 3.58 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
/* eslint-disable no-restricted-syntax */
import { Router } from 'express'
import asyncHandler from 'express-async-handler'
import { HttpError } from 'express-err'
import bodyParser from 'body-parser'
import { Build, Bundle, Repository } from 'models'
import buildJob from 'jobs/build'
import { getInstallationOctokit } from 'modules/github/client'
import { validateConfig } from 'modules/config'
import { notifyBuildGitHubStatus } from 'modules/build'
const router = new Router()
const getTokenRepository = asyncHandler(async function checkToken(
req,
res,
next,
) {
if (!req.body.token) {
throw new HttpError(400, 'token is required')
}
const repository = await Repository.query()
.where({ token: req.body.token })
.first()
if (!repository) {
throw new HttpError(400, 'token is not linked to a repository')
}
req.repository = repository
next()
})
const assertBodyValue = (...values) => (req, res, next) => {
for (const value of values) {
if (!req.body[value]) {
throw new HttpError(400, 'bundler is required')
}
}
next()
}
router.post(
'/bundles',
bodyParser.json(),
getTokenRepository,
assertBodyValue('bundler', 'stats'),
asyncHandler(async (req, res) => {
const bundle = await Bundle.query().insertAndFetch({
repositoryId: req.repository.id,
bundler: req.body.bundler,
stats: req.body.stats,
})
res.send({
id: bundle.id,
webpackStatsPutUrl: bundle.getWebpackStatsPutUrl(),
})
}),
)
router.post(
'/builds',
bodyParser.json(),
getTokenRepository,
assertBodyValue('commit', 'branch', 'providerMetadata', 'bundleId'),
asyncHandler(async (req, res) => {
const buildConfig = req.body.config || req.repository.config
const configValidation = validateConfig(buildConfig)
if (!configValidation.valid) {
res.status(400)
res.send({
error: { message: 'Invalid config', errors: configValidation.errors },
})
return
}
const bundle = await Bundle.query().findById(req.body.bundleId)
if (!bundle) {
throw new HttpError(400, 'Bundle not found')
}
const [installation] = await req.repository.$relatedQuery('installations')
if (!installation) {
throw new HttpError(400, `Installation not found for repository`)
}
const octokit = getInstallationOctokit(installation)
const owner = await req.repository.$relatedOwner()
let commitInfo
try {
;({ data: commitInfo } = await octokit.repos.getCommit({
owner: owner.login,
repo: req.repository.name,
ref: req.body.commit,
}))
} catch (error) {
const httpError = new HttpError(400, 'commit not found on GitHub')
httpError.code = 'unknown-commit'
throw httpError
}
if (!req.repository.active) {
await req.repository.$query().patch({ active: true })
}
const build = await Build.query().insertAndFetch({
jobStatus: 'queued',
bundleId: bundle.id,
repositoryId: req.repository.id,
branch: req.body.branch,
commit: req.body.commit,
stats: req.body.stats,
config: buildConfig,
providerMetadata: req.body.providerMetadata,
commitInfo: {
sha: commitInfo.sha,
message: commitInfo.commit.message,
author: {
id: commitInfo.author.id,
name: commitInfo.commit.author.name,
login: commitInfo.author.login,
avatarUrl: commitInfo.author.avatar_url,
},
},
})
await notifyBuildGitHubStatus(build, 'pending')
await buildJob.push(build.id)
res.send(build)
}),
)
export default router