This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·119 lines (100 loc) · 4.2 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2018 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause
const cache = require('./cache')
const { config } = require('./config')
const apiHelper = require('./github_api_helper')
const { runLinters } = require('./runner')
const yaml = require('js-yaml')
/**
* @param {import('probot').Application} app - Probot's Application class.
*/
module.exports = app => {
app.log('Starting app')
app.on('check_suite', async context => {
const action = context.payload.action
const checkSuite = context.payload.check_suite
const headSha = checkSuite.head_sha
const pullRequests = checkSuite.pull_requests
if (action === 'rerequested') {
// Check suite was manually rerequested by a user on the checks dashboard
// (previous run didn't complete)
return runLinterFromPRData(pullRequests, context, headSha)
}
})
app.on('check_run', async context => {
const action = context.payload.action
const headSha = context.payload.check_run.head_sha
const pullRequests = context.payload.check_run.pull_requests
if (action === 'rerequested') {
// Check suite was manually rerequested by a user on the checks dashboard
return runLinterFromPRData(pullRequests, context, headSha)
}
})
app.on(['pull_request.opened', 'pull_request.reopened', 'pull_request.synchronize'], async context => {
// Same thing but have to intercept the event because check suite is not triggered
const pullRequest = context.payload.pull_request
const headSha = pullRequest.head.sha
return runLinterFromPRData([pullRequest], context, headSha)
})
}
/**
* Retrieve files modified by pull request(s), run linter and send results
* @param {any[]} pullRequests object which contains information about the pull request
* @param {import('probot').Context} context
* @param {string} headSha
*/
async function runLinterFromPRData (pullRequests, context, headSha) {
const repoID = context.payload.repository.id
// Create check run on GitHub to send early feedback
const checkRunResponse = apiHelper.inProgressAPIresponse(context, headSha)
try {
// For now only deal with one PR
const PR = pullRequests[0]
// Loads the Precaution configuration file
const rawObj = await apiHelper.getConfigFile(context)
const configObj = rawObj ? yaml.safeLoad(rawObj.data) : null
// Process all pull requests associated with check suite
const PRsDownloadedPromise = pullRequests.map(pr => processPullRequest(pr, context, configObj))
const resolvedPRs = await Promise.all(PRsDownloadedPromise)
// Sometimes the resolverPR list contains undefined members
const inputFiles = resolvedPRs[0].filter((file) => file)
const report = await runLinters(inputFiles, repoID, PR.id)
const runID = (await checkRunResponse).data.id
apiHelper.sendResults(context, runID, report)
if (config.cleanupAfterRun) {
cache.clear(repoID, PR.id)
}
} catch (err) {
context.log.error(err)
const resolvedCheckRunResponse = await checkRunResponse
const runID = resolvedCheckRunResponse.data.id
// Send error to GitHub
apiHelper.errorResponse(context, runID, err)
}
}
/**
* Retrieve list of files modified by PR and download them to cache
* @param {import('probot').Context} context
* @param {Object} configObj configuration object populated by a config file
* @returns {Promise<string[]>} Paths to the downloaded PR files
*/
async function processPullRequest (pullRequest, context, configObj) {
const number = pullRequest.number
const ref = pullRequest.head.ref
const id = pullRequest.id
const repoID = context.payload.repository.id
const response = await apiHelper.getPRFiles(context, number, configObj)
const filesDownloadedPromise = response
.map(async filename => {
const headRevision = await apiHelper.getContents(context, filename, ref, pullRequest.head)
// TODO: merge this code with linter-specific path resolution
if (filename.endsWith('.go')) {
cache.saveFile(repoID, id, filename, headRevision.data, 'go')
} else {
cache.saveFile(repoID, id, filename, headRevision.data)
}
return filename
})
// Wait until all files have been downloaded
return Promise.all(filesDownloadedPromise)
}