Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.env
node_modules
*.pem
.DS_Store
44 changes: 44 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const moment = require('moment');
const perspectiveAPI = require('./lib/perspectiveAPI');

module.exports = robot => {
robot.on('issue_comment.created', async context => {
if (!context.isBot) {
const {body} = context.payload.comment;
const config = await context.config('config.yml');
const issue_comments = await context.github.issues.getComments(context.issue());
// TO DO: add since parameter with time condition
// probs use moment.js

if (config && config.lockThreads && issue_comments.data.length >= config.lockThreads.numComments) {
let toxicCommentCount = 0;
let commented = false;
issue_comments.data.forEach(function(comment) {
robot.log(comment.body)
if (!commented) {
perspectiveAPI.googleAPICall(comment.body, function (res) {
robot.log('res: ', res);
if (res > config.lockThreads.toxicityThreshold && !commented) {
// If the comment was toxic count it
toxicCommentCount += 1;
robot.log(toxicCommentCount, config.lockThreads.numComments)
if (toxicCommentCount >= config.lockThreads.numComments && !commented) {
// If there are too many toxic comments,
// Bot should comment with the maintainer set reply and lock the thread
robot.log(config.lockThreads.replyComment);
//context.github.issues.createComment(context.issue({body: config.lockThreads.replyComment}));
commented = true;
// Lock thread API call here
robot.log('issue should get locked!', context.issue());
return context.github.issues.lock(context.issue());
// SOBS UNCONTROLLABLY
😭
}
}
});
}
});
}
}
});
}
18 changes: 18 additions & 0 deletions lib/perspectiveAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const googleapis = require('googleapis');

exports.googleAPICall = async function (comment, callback) {
discovery_url = 'https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1';
let val;
return googleapis.discoverAPI(discovery_url, (err, client) => {
if (err) throw err;
var analyze_request = {
comment: {'text': comment},
requestedAttributes: {'TOXICITY': {}}
};
return client.comments.analyze({key: process.env.PERSPECTIVE_API_KEY, resource: analyze_request}, (err, response) => {
if (err) throw err;
val = response.attributeScores.TOXICITY.spanScores[0].score.value;
return callback(val);
});
});
};
Loading