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 1
/
index.js
60 lines (49 loc) · 2.01 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
const defaultsShape = {
existingCollaboratorMessage: 'string',
newCollaboratorMessage: '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.`));
}
/**
* @typedef {Object} Config
* @prop {string} existingCollaboratorMessage
* @prop {string} newCollaboratorMessage
*
* Anytime a user opens an issue, add them as a collaborator to the repository.
* @param {Object} robot
* @param {Config} defaults
* @param {string} [configFilename]
*/
module.exports = (robot, defaults, configFilename = 'add-collabs.yml') => {
checkForDefaults(defaults);
robot.on('issues.opened', async context => {
const {issue, action} = context.payload;
const issueOwner = issue.user.login;
const repo = context.repo({username: issueOwner});
let config;
try {
const {addCollaborators} = await context.config(configFilename);
config = Object.assign({}, defaults, addCollaborators);
} catch (err) {
config = defaults;
}
const isCollab = await context.github.repos.checkCollaborator(repo)
.catch(() => {
context.github.repos.addCollaborator(repo);
});
const params = {
number: issue.number,
body: isCollab ? config.existingCollaboratorMessage : config.newCollaboratorMessage,
};
// / When testing, uncomment the line below and comment the return line -- this avoids the bot getting marked as spam.
// robot.log("This is the params", params);
return context.github.issues.createComment(context.repo(params));
});
console.log('Yay, the teacher-bot/add-collabs 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
};