-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
103 lines (93 loc) · 2.97 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
const SpaceApiValidator = require('@spaceapi/validator-client')
const uuid = require('uuid')
const mongoose = require('mongoose')
const PullRequest = require('./schema')
const mongoUri = `mongodb://${process.env.DB_HOST}/spaceapi`
mongoose.connect(mongoUri, {
user: process.env.DB_USER,
pass: process.env.DB_PASS,
useUnifiedTopology: true,
useNewUrlParser: true
})
const validateSpaceUrl = url => {
const apiInstance = new SpaceApiValidator.V2Api()
const validateUrlV2 = new SpaceApiValidator.ValidateUrlV2(url)
return apiInstance.v2ValidateURLPost(validateUrlV2).then(res => {
return {
url,
result: res
}
})
}
async function createPullRequestStatus (pullRequest, context) {
const status = {
sha: pullRequest.sha,
state: 'pending',
target_url: `https://githubbot.spaceapi.community/pullrequest/${pullRequest.pullRequestNumber}`,
description: 'Checking for added url(s)',
context: 'Url check'
}
await context.github.repos.createStatus(context.repo(status))
.then(() => {
context.github.repos.createStatus(context.repo({
...status,
state: pullRequest.url.reduce((pre, cur) => pre && cur.result.valid, true)
? 'success'
: 'failure'
}))
pullRequest.save()
})
}
function validatePullRequest (pullRequest) {
return Promise.all(pullRequest.url.map(val => validateSpaceUrl(val.url)))
.then(res => {
pullRequest.url = res
pullRequest.save()
return pullRequest
})
}
module.exports = (app, { getRouter }) => {
app.log('app started')
const router = getRouter('/spaceapi')
router.get('/pullrequest/:number(\\d+)', (req, res) => {
PullRequest.find({ pullRequestNumber: req.params.number },
{ _id: 0, __v: 0, 'url._id': 0 }).exec().then(result => {
res.send(result)
})
})
app.on('pull_request', async context => {
const { sha } = context.payload.pull_request.head
const pullRequest = new PullRequest({ runId: uuid.v4(), sha })
const pull = context.issue()
app.log(`start checking pull request ${pull.number}`)
pullRequest.pullRequestNumber = pull.number
await pullRequest.save()
await context.github.pulls.listFiles(pull)
.then(changedFiles => {
return Promise.all(changedFiles.data.map(data => {
if (data.filename === 'directory.json') {
return data.patch.match(/^\+.*/gm).map((match) => {
return match.match(/https?[^"]*/gi).map(url => url)
})
.flat()
}
return null
})
.filter(res => res)
.flat())
})
.then(res => {
res.forEach(url => {
pullRequest.url.push({
url
})
})
})
await validatePullRequest(pullRequest).then(res => {
createPullRequestStatus(res, context)
app.log(`done checking pull request ${pull.number}`)
})
// await console.log(pullRequest)
// await createPullRequestStatus(pullRequest, context)
})
}