Skip to content

Commit

Permalink
[ACHIEVEMENT] The Gladiator - achievement for getting all the reactio…
Browse files Browse the repository at this point in the history
…n to a comment (#51)

* [ACHIEVEMENT] used all reactions

This is not including inline comments Yet.
Also, I've noticed there is no collection of reactions for the PR itself. Can we?
The name will change. Also not sure about the beatles.

* used all reactions - now with returning value in function

* All reactions used achvmnt- not including inline comments

* Used all reactions - add inline and PR check(still problem with PR)

Can't distinguish between when trying to eliminate self reactions.

Also granting to creator is not set yet

* fix achievement + implement tests

* CR comments
  • Loading branch information
dunaevsky committed Feb 25, 2017
1 parent f6765b2 commit 591100a
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 0 deletions.
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'
}
};
}

0 comments on commit 591100a

Please sign in to comment.