-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
216 lines (194 loc) · 6.44 KB
/
app.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
// Dependencies
const atob = require('atob');
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const GitHubApi = require('github');
const _ = require('lodash');
const ESLintCLIEngine = require('eslint').CLIEngine;
// Config variables
import {REPOSITORY_OWNER, REPOSITORY_NAME, FILE_FILTER} from './config';
const GITHUB_USERNAME = process.env.GITHUB_USERNAME;
const GITHUB_PASSWORD = process.env.GITHUB_PASSWORD;
// Github configuration
const github = new GitHubApi({
version: '3.0.0',
headers: {
'user-agent': 'webos-bot'
}
});
github.authenticate({
type: 'basic',
username: GITHUB_USERNAME,
password: GITHUB_PASSWORD
});
// Eslint configuration
const eslint = new ESLintCLIEngine();
// Functions
/**
* Get commits from a payload object.
* @param {Object} payload the payload sent by Github
* @return {Array} the commits pushed to Github
*/
const getCommitsFromPayload = payload => payload.commits;
/**
* Get modified files from a commit.
* @param {Function} callback callback called when the files are fetched
* @param {Object} {id} the commit object
*/
const getFilesFromCommit = (callback, {id: sha}) => {
github.repos.getCommit({
user: REPOSITORY_OWNER,
repo: REPOSITORY_NAME,
sha
}, (error, {files}) => {
if (error) {
console.log(error);
}
callback(files, sha);
});
};
/**
* Filter files to keep only Javascript files. ESLint is for Javascript. No kidding.
* @param {Array} files every files contained in the commit
* @return {Array} Filtered files, which matched the FILE_FILTER regex (set in the config)
*/
const filterJavascriptFiles = files => files.filter(({filename}) => filename.match(FILE_FILTER));
/**
* Download a file from its url, then call the callback with its content.
* @param {Function} callback Download success callback
* @param {String} filename File filename
* @param {String} patch The commit's patch string.
* @param {String} raw_url File URL
* @param {String} sha Commit id
*/
const downloadFile = (callback, {filename, patch, raw_url}, sha) => { // eslint-disable-line
github.repos.getContent({
user: REPOSITORY_OWNER,
repo: REPOSITORY_NAME,
path: filename,
ref: sha
}, (error, data) => {
if (error) {
console.log(error);
} else{
callback(filename, patch, atob(data.content), sha);
}
});
};
/**
* Compute a mapping object for the relationship 'file line number' <-> 'Github's diff view line number'.
* This is necessary for the comments, as Github API asks to specify the line number in the diff view to attach an inline comment to.
* If a file line is not modified, then it will not appear in the diff view, so it is not taken into account here.
* The linter will therefore only mention warnings for modified lines.
* @param {String} patchString The git patch string.
* @return {Object} An object shaped as follows : {'file line number': 'diff view line number'}.
*/
const getLineMapFromPatchString = patchString => {
let diffLineIndex = 0;
let fileLineIndex = 0;
return patchString.split('\n').reduce((lineMap, line) => {
if (line.match(/^@@.*/)) {
fileLineIndex = line.match(/\+[0-9]+/)[0].slice(1) - 1;
} else {
diffLineIndex++;
if ('-' !== line[0]) {
fileLineIndex++;
if ('+' === line[0]) {
lineMap[fileLineIndex] = diffLineIndex;
}
}
}
return lineMap;
}, {});
};
/**
* Lint a raw content passed as a string, then return the linting messages.
* @param {String} filename File filename
* @param {String} patch Commit's Git patch
* @param {String} content File content
* @param {String} sha Commit's id
* @return {Array} Linting messages
*/
const lintContent = (filename, patch, content, sha) => {
return {
filename,
lineMap: getLineMapFromPatchString(patch),
messages: _.get(eslint.executeOnText(content, filename),
'results[0].messages'),
sha
};
};
/**
* Send a comment to Github's commit view
* @param {String} filename File filename
* @param {Object} lineMap The map between file and diff view line numbers
* @param {String} ruleId ESLint rule id
* @param {String} message ESLint message
* @param {Integer} line Line number (in the file)
* @param {String} sha Commit's id
*/
const sendSingleComment = (
filename,
lineMap,
{
ruleId='Eslint',
message,
line
},
sha
) => {
const diffLinePosition = lineMap[line];
console.log('Log ::: DiffLinePosition ', diffLinePosition);
console.log('Log ::: lineMap, line ', lineMap, line);
if (diffLinePosition) { // By testing this, we skip the linting messages related to non-modified lines.
github.repos.createCommitComment({
user: REPOSITORY_OWNER,
repo: REPOSITORY_NAME,
sha,
path: filename,
commit_id: sha, // eslint-disable-line
body: `**${ruleId}**: ${message}`,
position: diffLinePosition
});
}
};
/**
* Send the comments for all the linting messages, to Github
* @param {String} filename File filename
* @param {Object} lineMap The map between file and diff view line numbers
* @param {Array} messages ESLint messages
* @param {String} sha Commit's id
*/
const sendComments = ({filename, lineMap, messages, sha}) => {
messages.map(message => {
sendSingleComment(filename, lineMap, message, sha);
});
};
/**
* Main function, that treats the payload sent by Github.
* First it gets the commits, the it extracts the filenames from the commit, downloads and filters the files.
* The remaining files are analyzed by the linter, and the resulting linting messages are sent to Github as inline comments.
* @param {Object} payload Push event's payload sent by Github.
*/
const treatPayload = payload => {
getCommitsFromPayload(payload).map(commit => {
getFilesFromCommit((files, sha) => {
filterJavascriptFiles(files).map(file => {
downloadFile(_.compose(sendComments, lintContent), file, sha);
});
}, commit);
});
};
// Server
app.use(bodyParser.json());
app.set('port', (process.env.PORT || 3002));
app.post('/', ({body: payload}, response) => {
if (payload && payload.commits) {
treatPayload(payload);
}
response.end();
});
app.listen(app.get('port'), () => {
console.log('connect');
});