This repository has been archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (65 loc) · 2.44 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
const defaultsShape = {
name: 'string',
path: 'string',
detailsURL: 'string',
success: 'string',
failure: 'string'
};
function checkForDefaults(defaults) {
const errors = Object.keys(defaultsShape).filter(key => !Object.prototype.hasOwnProperty.call(defaults, key));
if (errors.length > 0) errors.forEach(err => console.error(`Key \`${err}\` of type \`${defaultsShape[err]}\` is missing.`));
}
/**
* Checks to see if the filenames of the files in a PR start with `_pins/`.
* @param {object} context - Context argument from a Robot .on() hook.
* @returns {object}
*/
const checkPaths = async (robot, context, config) => {
const {number, pull_request: pr} = context.payload;
const {data: files} = await context.github.pullRequests.getFiles(context.repo({
number
}));
// Check that all the filenames of files in the PR start with `_pins/`.
// Returns true if there are no issues, false if there are files that do
// not start with `_pins/`.
const properPath = files.filter(file => !file.filename.startsWith( config.path)).length === 0;
return context.repo({
sha: pr.head.sha,
context: config.name,
state: properPath ? 'success' : 'failure',
target_url: config.detailsURL,
description: properPath ? config.success : config.failure
});
};
/**
* @typedef {Object} Config
* @prop {string} message
*
* Anytime a user merges a pull request, they are reminded to delete their branch.
* @param {Object} robot
* @param {Config} defaults
* @param {String} [configFilename]
*/
module.exports = (robot, defaults, configFilename = 'checkpath.yml') => {
checkForDefaults(defaults);
const runTests = async context => {
let config;
try {
const {checkPath} = await context.config(configFilename);
config = Object.assign({}, defaults, checkPath);
} catch (err) {
config = defaults;
}
const pinCheck = await checkPaths(robot, context, config);
return context.github.repos.createStatus(pinCheck);
}
robot.on('pull_request.opened', runTests);
robot.on('pull_request.edited', runTests);
robot.on('pull_request.reopened', runTests);
robot.on('pull_request.synchronize', runTests);
console.log('Yay, the teacher-bot/tests plugin was loaded!');
// For more information on building plugins:
// https://github.com/probot/probot/blob/master/docs/plugins.md
// To get your plugin running against GitHub, see:
// https://github.com/probot/probot/blob/master/docs/development.md
};