Skip to content

Instantly share code, notes, and snippets.

@ripla
Last active May 6, 2016 12:30
Show Gist options
  • Save ripla/73425086df410276cf66 to your computer and use it in GitHub Desktop.
Save ripla/73425086df410276cf66 to your computer and use it in GitHub Desktop.
+2, +1 style reviews for Reviewable.io
// Approval by username.
// +2, lgtm_strong or +1 +1 -> Reviewed and tested
// +1 or lgtm -> Reviewed
// -2 or -1 -> Remove previous review approval
// Review only valid if for the current version
// +1 and thumbsup are equal
var approvals = {};
var tested = {};
// Timestamp of the currently latest revision.
var lastRevisionTimestamp;
_.each(review.revisions, function(revision) {
if(revision.snapshotTimestamp) {
lastRevisionTimestamp = revision.snapshotTimestamp;
}
});
function hasTestedEmoji(emojis) {
return emojis['+2'] || emojis['+1'] >= 2 || emojis['lgtm_strong'] || emojis['thumbsup'] >= 2;
}
function hasNegativeEmoji(emojis) {
return emojis['-1'] || emojis['-2'] || emojis['thumbdown'];
}
function hasApprovedEmoji(emojis) {
return emojis['+1'] || emojis['lgtm'] || emojis['thumbsup'];
}
_.each(review.sentiments, function(sentiment) {
var emojis = _.countBy(sentiment.emojis);
if (hasNegativeEmoji(emojis)) {
delete approvals[sentiment.username];
delete tested[sentiment.username];
} else if (hasTestedEmoji(emojis)) {
approvals[sentiment.username] =
sentiment.timestamp >= lastRevisionTimestamp;
tested[sentiment.username] =
sentiment.timestamp >= lastRevisionTimestamp;
} else if (hasApprovedEmoji(emojis) && !approvals[sentiment.username]) {
approvals[sentiment.username] =
sentiment.timestamp >= lastRevisionTimestamp;
}
});
var numApprovals = _.countBy(approvals);
var numTesters = _.countBy(tested);
var description =
(numApprovals.true || 0) + ' of ' + 2 +
' approvals.';
var currentTesters = _.pick(tested, function(value, key, object) {
return value;
});
description += numTesters.true ? ' Tested by ' + Object.keys(currentTesters) : ' Not tested';
return {
completed: numApprovals.true >= 2 && numTesters.true >= 1,
description: description,
debug: {"approvals": approvals, "tested":tested, 'timestamp':lastRevisionTimestamp }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment