Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ACHIEVEMENT] The Gladiator - achievement for getting all the reaction to a comment #51

Merged
merged 6 commits into from Feb 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions achievements/template.achievement.js
@@ -0,0 +1,23 @@


//var template = {
// name: ,
// check: function(pullRequest, shall) {
// if (...) {
//
// var achievement = {
// / avatar : '',
// name: '',
// short: '',
// description: '',
// relatedPullRequest: pullRequest._id
// };

//shall.grant(pullRequest.creator.username, achievement);
// }
// }
//};



//module.exports = template;
65 changes: 65 additions & 0 deletions achievements/usedAllReactionsInComment.achievement.js
@@ -0,0 +1,65 @@
var _ = require('lodash');
// Gives achievement to comment authors who got all reactions
// without reacting themselves

var usedAllReactionsInComment = {
name: 'Gladiator',
check: function(pullRequest, shall) {
var topAuthorsUsernames = getCommentAuthorsWithAllReactions(pullRequest);
if (!_.isEmpty(topAuthorsUsernames)) {
var achievement = {
avatar: 'images/achievements/gladiator.achievement.gif',
name: 'Gladiator',
short: 'Are you not ENTERTAINED?!',
description: [
'Your message got all the possible reactions. ',
'Enjoy your 15 minutes of fame'
].join(''),
relatedPullRequest: pullRequest._id
};

_.forEach(topAuthorsUsernames, function(author) {
shall.grant(author, achievement);
});
}
}
};

function getCommentAuthorsWithAllReactions(pullRequest) {
var allComments = _.concat(pullRequest.comments, pullRequest.inlineComments);
var authors = _.map(allComments, 'author.username');
var AllCommentsReactions = _.map(allComments, 'reactions');

// also add pull request description reactions
authors.push(pullRequest.creator.username);
AllCommentsReactions.push(pullRequest.reactions);

getOnlyUniqueReactionsWithoutAuthors(AllCommentsReactions, authors);

return onlyUsersWithAllReactions(authors, AllCommentsReactions);
}

function reactionsWithoutAuthor(reactions, author) {
return _.map(_.reject(reactions, ['user.username', author]), 'reaction');
}

function getOnlyUniqueReactionsWithoutAuthors(AllCommentsReactions, authors) {
_.forEach(AllCommentsReactions, function(reactions, index) {
AllCommentsReactions[index] =
_.uniq(reactionsWithoutAuthor(reactions, authors[index]));

});
}

function onlyUsersWithAllReactions(authors, AllCommentsReactions) {
var commentAuthorsWithAllReactions = [];

_.forEach(authors, function(author, index) {
if (AllCommentsReactions[index].length === 6) {
commentAuthorsWithAllReactions.push(author);
}
});

return commentAuthorsWithAllReactions;
}
module.exports = usedAllReactionsInComment;
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
170 changes: 170 additions & 0 deletions test/usedAllReactionsInComment.specs.js
@@ -0,0 +1,170 @@
var usedAllReactionsInComment =
require('../achievements/usedAllReactionsInComment.achievement');
var expect = require('chai').expect;

describe('usedAllReactionsInComment achievement', function() {
describe('should be granted if PR has a message with all reactions',
function() {
it('should work with inline comments', function() {
var testShall = new Shall();
var pullRequest = new PullRequest();

pullRequest.inlineComments = [ createQualifiedComment() ];

usedAllReactionsInComment.check(pullRequest, testShall);
expect(testShall.grantedToUsername).to.be.a('string');
expect(testShall.grantedToUsername).to.equal('comment_author');
expect(testShall.grantedAchievement).to.be.an('object');
});

it('should work with PR comments', function() {
var testShall = new Shall();
var pullRequest = new PullRequest();

pullRequest.comments = [ createQualifiedComment() ];

usedAllReactionsInComment.check(pullRequest, testShall);
expect(testShall.grantedToUsername).to.be.a('string');
expect(testShall.grantedToUsername).to.equal('comment_author');
expect(testShall.grantedAchievement).to.be.an('object');
});

it('should work with PR description', function() {
var testShall = new Shall();
var pullRequest = new PullRequest();

pullRequest.reactions = createQualifiedReactions();

usedAllReactionsInComment.check(pullRequest, testShall);
expect(testShall.grantedToUsername).to.be.a('string');
expect(testShall.grantedToUsername).to.equal('creator');
expect(testShall.grantedAchievement).to.be.an('object');
});
it('should work if all reactions + author reaction', function() {
var testShall = new Shall();
var pullRequest = new PullRequest();

pullRequest.reactions = createQualifiedReactions();
pullRequest.reactions.push({
'reaction': '+1',
'user': {
'username': 'creator'
}
});

usedAllReactionsInComment.check(pullRequest, testShall);
expect(testShall.grantedToUsername).to.be.a('string');
expect(testShall.grantedToUsername).to.equal('creator');
expect(testShall.grantedAchievement).to.be.an('object');
});
});

it('should not grant if comment with less than all reactions', function() {
var testShall = new Shall();
var pullRequest = new PullRequest();

pullRequest.inlineComments = [ createQualifiedComment() ];
pullRequest.inlineComments[0].reactions.pop(); //remove 1 reaction

usedAllReactionsInComment.check(pullRequest, testShall);
expect(testShall.grantedToUsername).to.not.exist;
expect(testShall.grantedAchievement).to.not.exist;
});

it('should not grant if author reacted', function() {
var testShall = new Shall();
var pullRequest = new PullRequest();

pullRequest.comments = [ createQualifiedComment() ];
pullRequest.comments[0].reactions[0].user.username = 'comment_author';

usedAllReactionsInComment.check(pullRequest, testShall);
expect(testShall.grantedToUsername).to.not.exist;
expect(testShall.grantedAchievement).to.not.exist;
});

it('should not grant if 6 reactions with a duplicate', function() {
var testShall = new Shall();
var pullRequest = new PullRequest();

pullRequest.comments = [ createQualifiedComment() ];
pullRequest.comments[0].reactions[0].reaction = '-1';

usedAllReactionsInComment.check(pullRequest, testShall);
expect(testShall.grantedToUsername).to.not.exist;
expect(testShall.grantedAchievement).to.not.exist;
});
});

function Shall() {
var self = this;

self.grantedAchievement = undefined;
self.grantedToUsername = undefined;

self.grant = function(username, achievementObject) {
self.grantedToUsername = username;
self.grantedAchievement = achievementObject;
};
}

function createQualifiedComment() {
return {
'author': {
'username': 'comment_author'
},
'reactions': createQualifiedReactions()
};
}

function createQualifiedReactions() {
return [
{
'reaction': '+1',
'user': {
'username': 'Thatkookooguy'
}
},
{
'reaction': '-1',
'user': {
'username': 'Thatkookooguy'
}
},
{
'reaction': 'laugh',
'user': {
'username': 'Thatkookooguy'
}
},
{
'reaction': 'hooray',
'user': {
'username': 'Thatkookooguy'
}
},
{
'reaction': 'confused',
'user': {
'username': 'Thatkookooguy'
}
},
{
'reaction': 'heart',
'user': {
'username': 'Thatkookooguy'
}
}
];
}

function PullRequest() {
return {
'id': 'test',
'url': 'url',
'number': 3,
'creator': {
'username': 'creator'
}
};
}