Skip to content

Commit

Permalink
Merge pull request #11 from behaviorbot/spacing
Browse files Browse the repository at this point in the history
4 -> 2 spaces minor change
  • Loading branch information
Bex Warner authored Aug 17, 2017
2 parents b30d633 + 32ee6f8 commit 3b0aff3
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 108 deletions.
88 changes: 45 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
const googleapis = require('googleapis');

module.exports = robot => {
robot.on('issue_comment', async context => {
let codeOfConduct;
const config = await context.config('config.yml');
if (config && config.sentimentBotToxicityThreshold && config.sentimentBotReplyComment && !context.isBot) {
const toxicityThreshold = config.sentimentBotToxicityThreshold;
const body = context.payload.comment.body;
// Check for Code of Conduct
const repoData = await context.github.repos.get(context.repo({
headers: {
Accept: 'application/vnd.github.scarlet-witch-preview+json'
}
}));
if (repoData.data.code_of_conduct) {
codeOfConduct = Object.assign({}, repoData.data.code_of_conduct);
}
// Attempt to make perspective api requests template from google
const discoveryUrl = 'https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1';
googleapis.discoverAPI(discoveryUrl, (err, client) => {
if (err) {
throw err;
}
const analyzeRequest = {
comment: {text: body},
requestedAttributes: {TOXICITY: {}}
};
client.comments.analyze({key: process.env.PERSPECTIVE_API_KEY, resource: analyzeRequest}, (err, response) => {
if (err) {
throw err;
}
const toxicValue = response.attributeScores.TOXICITY.spanScores[0].score.value;
// If the comment is toxic, comment the comment
if (toxicValue >= toxicityThreshold) {
let comment;
if (codeOfConduct) {
comment = config.sentimentBotReplyComment + 'Keep in mind, this repository uses the [' + codeOfConduct.name + '](' + codeOfConduct.url + ').';
} else {
comment = config.sentimentBotReplyComment;
}
context.github.issues.createComment(context.issue({body: comment}));
}
});
});
robot.on('issue_comment', async context => {
let codeOfConduct;
const config = await context.config('config.yml');
if (config && !context.isBot) {
if (config.sentimentBotToxicityThreshold && config.sentimentBotReplyComment) {
const toxicityThreshold = config.sentimentBotToxicityThreshold;
const body = context.payload.comment.body;
// Check for Code of Conduct
const repoData = await context.github.repos.get(context.repo({
headers: {
Accept: 'application/vnd.github.scarlet-witch-preview+json'
}
}));
if (repoData.data.code_of_conduct) {
codeOfConduct = Object.assign({}, repoData.data.code_of_conduct);
}
});
// Attempt to make perspective api requests template from google
const discoveryUrl = 'https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1';
googleapis.discoverAPI(discoveryUrl, (err, client) => {
if (err) {
throw err;
}
const analyzeRequest = {
comment: {text: body},
requestedAttributes: {TOXICITY: {}}
};
client.comments.analyze({key: process.env.PERSPECTIVE_API_KEY, resource: analyzeRequest}, (err, response) => {
if (err) {
throw err;
}
const toxicValue = response.attributeScores.TOXICITY.spanScores[0].score.value;
// If the comment is toxic, comment the comment
if (toxicValue >= toxicityThreshold) {
let comment;
if (codeOfConduct) {
comment = config.sentimentBotReplyComment + 'Keep in mind, this repository uses the [' + codeOfConduct.name + '](' + codeOfConduct.url + ').';
} else {
comment = config.sentimentBotReplyComment;
}
context.github.issues.createComment(context.issue({body: comment}));
}
});
});
}
}
});
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"xo": "^0.18.2"
},
"xo": {
"space": 4
"space": 2
},
"engines": {
"node": ">= 7.7.0",
Expand Down
128 changes: 64 additions & 64 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,76 @@ const plugin = require('..');
const payload = require('./events/payload');

describe('sentiment-bot', () => {
let robot;
let github;
let robot;
let github;

beforeEach(() => {
robot = createRobot();
plugin(robot);
beforeEach(() => {
robot = createRobot();
plugin(robot);

github = {
repos: {
getContent: expect.createSpy().andReturn(Promise.resolve({
data: {
content: Buffer.from(`
sentimentBotToxicityThreshold: 0.3
sentimentBotReplyComment: "That comment was toxic"`).toString('base64')
}
})),
get: expect.createSpy().andReturn(Promise.resolve({
data: {
code_of_conduct: Buffer.from(`
name: 'Contributor Covenenant'
url: https://github.com/hiimbex/testing-things/blob/master/CODE_OF_CONDUCT.md`).toString('base64')
}
}))
},
issues: {
createComment: expect.createSpy()
}
};
github = {
repos: {
getContent: expect.createSpy().andReturn(Promise.resolve({
data: {
content: Buffer.from(`
sentimentBotToxicityThreshold: 0.3
sentimentBotReplyComment: "That comment was toxic"`).toString('base64')
}
})),
get: expect.createSpy().andReturn(Promise.resolve({
data: {
code_of_conduct: Buffer.from(`
name: 'Contributor Covenenant'
url: https://github.com/hiimbex/testing-things/blob/master/CODE_OF_CONDUCT.md`).toString('base64')
}
}))
},
issues: {
createComment: expect.createSpy()
}
};

robot.auth = () => Promise.resolve(github);
});
robot.auth = () => Promise.resolve(github);
});

describe('sentiment-bot success', () => {
it('posts a comment because the user was toxic', async () => {
await robot.receive(payload);
expect(github.repos.getContent).toHaveBeenCalledWith({
owner: 'hiimbex',
repo: 'testing-things',
path: '.github/config.yml'
});
expect(github.repos.get).toHaveBeenCalled();
expect(github.repos.get).toHaveBeenCalledWith({
owner: 'hiimbex',
repo: 'testing-things',
headers: {
Accept: 'application/vnd.github.scarlet-witch-preview+json'
}
});
// Imitate google api stuff
// expect(github.issues.createComment).toHaveBeenCalled();
});
describe('sentiment-bot success', () => {
it('posts a comment because the user was toxic', async () => {
await robot.receive(payload);
expect(github.repos.getContent).toHaveBeenCalledWith({
owner: 'hiimbex',
repo: 'testing-things',
path: '.github/config.yml'
});
expect(github.repos.get).toHaveBeenCalled();
expect(github.repos.get).toHaveBeenCalledWith({
owner: 'hiimbex',
repo: 'testing-things',
headers: {
Accept: 'application/vnd.github.scarlet-witch-preview+json'
}
});
// Imitate google api stuff
// expect(github.issues.createComment).toHaveBeenCalled();
});
});

describe('sentiment-bot fail', () => {
it('does not post a comment because the user was not toxic', async () => {
await robot.receive(payload);
describe('sentiment-bot fail', () => {
it('does not post a comment because the user was not toxic', async () => {
await robot.receive(payload);

expect(github.repos.getContent).toHaveBeenCalledWith({
owner: 'hiimbex',
repo: 'testing-things',
path: '.github/config.yml'
});
expect(github.repos.get).toHaveBeenCalledWith({
owner: 'hiimbex',
repo: 'testing-things',
headers: {
Accept: 'application/vnd.github.scarlet-witch-preview+json'
}
});
expect(github.issues.createComment).toNotHaveBeenCalled();
});
expect(github.repos.getContent).toHaveBeenCalledWith({
owner: 'hiimbex',
repo: 'testing-things',
path: '.github/config.yml'
});
expect(github.repos.get).toHaveBeenCalledWith({
owner: 'hiimbex',
repo: 'testing-things',
headers: {
Accept: 'application/vnd.github.scarlet-witch-preview+json'
}
});
expect(github.issues.createComment).toNotHaveBeenCalled();
});
});
});

0 comments on commit 3b0aff3

Please sign in to comment.