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
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# list-bot-test
# badging-bot

**Bot Function**
## About

This is a bot made for helping out with CHAOSS D&I Badging reviews.

## Installation

Visit the public page [here](https://github.com/apps/badging-bot) to install this App on one of your own repositories.

## Bot Function

- Posts contents of `.github/applicant-welcome.md` when an issue is opened
- Posts contents of `.github/reviewer-welcome.md` + `.github/checklist.md` when a issue is assigned
Expand Down
24 changes: 12 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const postWelcome = require('./src/postWelcome');
const postChecklist = require('./src/postChecklist');
const commandResponse = require('./src/commandResponse');
const endReview = require('./src/endReview');
const commands = require('probot-commands');
const postHelp = require('./src/postHelp');
const assignReviewers = require('./src/assignReviewers');
const postWelcome = require("./src/postWelcome");
const postChecklist = require("./src/postChecklist");
const commandResponse = require("./src/commandResponse");
const endReview = require("./src/endReview");
const commands = require("probot-commands");
const postHelp = require("./src/postHelp");
const assignReviewers = require("./src/assignReviewers");

module.exports = (app) => {
module.exports = app => {
app.on("issues.opened", postWelcome);
app.on("issues.opened", assignReviewers);
app.on("issues.assigned", postChecklist);
commands(app, 'result', commandResponse);
commands(app, 'end', endReview);
commands(app, 'help', postHelp);
};
commands(app, "result", commandResponse);
commands(app, "end", endReview);
commands(app, "help", postHelp);
};
20 changes: 10 additions & 10 deletions src/assignReviewers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ async function assignReviewers(context) {
).toString();
reviewerList = reviewerList.split("\n");


let filteredList = reviewerList.filter(function(element) {
return element[0] == "-";
});
return element[0] == "-";
});

length = filteredList.length;
usernameIndex = getRandomIndexes(0, length - 1);
length = filteredList.length;
let usernameIndex = getRandomIndexes(0, length - 1);

let list = filteredList.map(function(element) {
return element.replace(/-|\s/g, "");
return element.substring(2);
});

console.log(list);
let assigneeList = [list[usernameIndex[0]], list[usernameIndex[1]]];

const issueComment = context.issue({ assignees: assigneeList });
return context.github.issues.addAssignees(issueComment);
const commentContent = context.issue({ assignees: assigneeList });
return context.github.issues.addAssignees(commentContent);
}

function getRandomIndexes(min, max) {
indexOne = Math.floor(Math.random() * (max - min + 1)) + min;
indexTwo = Math.floor(Math.random() * (max - min + 1)) + min;
let indexOne = Math.floor(Math.random() * (max - min + 1)) + min;
let indexTwo = Math.floor(Math.random() * (max - min + 1)) + min;

while (indexOne == indexTwo) {
indexTwo = Math.floor(Math.random() * (max - min + 1)) + min;
Expand Down
132 changes: 77 additions & 55 deletions src/calculateBadge.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,83 @@
let initialCheckCount = 6;
async function calculateBadge(context) {
const issueObject = await context.github.issues.listComments({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
issue_number: context.payload.issue.number
});
const issueURL = context.payload.issue.html_url;

const issueObject = await context.github.issues.listComments({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
issue_number: context.payload.issue.number
});
const issueURL = context.payload.issue.html_url;

if (context.payload.repository.name == "event-diversity-and-inclusion")
{
initialCheckCount = 4;
}

let checklists = issueObject.data.filter(function (comment) {
return (comment.user.type == "Bot") && (comment.body.substring(0, 15) == "# Checklist for");
})
if (context.payload.repository.name == "event-diversity-and-inclusion") {
initialCheckCount = 4;
}

let checklists = issueObject.data.filter(function(comment) {
return (
comment.user.type == "Bot" &&
comment.body.substring(0, 15) == "# Checklist for"
);
});
//mapping not required for total check calculation
let totalCheckCount = checklists.map(function (element) {
return ((element.body.match(/\[x\]/g) || []).length) + ((element.body.match(/\[ \]/g) || []).length);
})

console.log(totalCheckCount);
totalCheckCount = totalCheckCount.map(function (element) {
return element - initialCheckCount;
});

let positiveCheckCount = checklists.map(function (element) {

let checkCount = +((element.body.match(/\[x\]/g) || []).length) - initialCheckCount;
if (checkCount <= 0)
return 0;
else
return checkCount;
})

let percentages = positiveCheckCount.map(function (element) {
let p = Math.floor((element/totalCheckCount[0]) * 100);
return p;
})

let reviewerCount = percentages.length;
let reviewResult = 0;
percentages.forEach(element => {
reviewResult += element;
});
reviewResult /= reviewerCount;
console.log(reviewResult);
const badgeAssigned = (reviewResult < 40) ? ["pending", "D%26I-Pending-red"] : ((reviewResult < 60) ? ["passing", "D%26I-Passing-passing"] : ((reviewResult < 80) ? ["silver", "D%26I-Silver-silver"] : ((reviewResult <= 100) ? ["gold", "D%26I-Gold-yellow"] : ["pending", "D%26I-Pending-red"])));

const url = "https://img.shields.io/badge/" + badgeAssigned[1] + "?style=flat-square&labelColor=583586&&link=" + issueURL + "/&logo=data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDI1MCAyNTAiPgo8cGF0aCBmaWxsPSIjMUM5QkQ2IiBkPSJNOTcuMSw0OS4zYzE4LTYuNywzNy44LTYuOCw1NS45LTAuMmwxNy41LTMwLjJjLTI5LTEyLjMtNjEuOC0xMi4yLTkwLjgsMC4zTDk3LjEsNDkuM3oiLz4KPHBhdGggZmlsbD0iIzZBQzdCOSIgZD0iTTE5NC42LDMyLjhMMTc3LjIsNjNjMTQuOCwxMi4zLDI0LjcsMjkuNSwyNy45LDQ4LjVoMzQuOUMyMzYuMiw4MC4yLDIxOS45LDUxLjcsMTk0LjYsMzIuOHoiLz4KPHBhdGggZmlsbD0iI0JGOUNDOSIgZD0iTTIwNC45LDEzOS40Yy03LjksNDMuOS00OS45LDczLTkzLjgsNjUuMWMtMTMuOC0yLjUtMjYuOC04LjYtMzcuNS0xNy42bC0yNi44LDIyLjQKCWM0Ni42LDQzLjQsMTE5LjUsNDAuOSwxNjIuOS01LjdjMTYuNS0xNy43LDI3LTQwLjIsMzAuMS02NC4ySDIwNC45eiIvPgo8cGF0aCBmaWxsPSIjRDYxRDVGIiBkPSJNNTUuNiwxNjUuNkMzNS45LDEzMS44LDQzLjMsODguOCw3My4xLDYzLjVMNTUuNywzMy4yQzcuNSw2OS44LTQuMiwxMzcuNCwyOC44LDE4OEw1NS42LDE2NS42eiIvPgo8L3N2Zz4K";

const markdownBadgeImage = "![Assigned badge: " + badgeAssigned[0] + "](" + url + ")";

const htmlBadgeImage = "<img src='" + url + "' alt='" + "D&I Badging badge state: " + badgeAssigned[0] + "'/>";
return [markdownBadgeImage, htmlBadgeImage, reviewResult, reviewerCount];

let totalCheckCount = checklists.map(function(element) {
return (
(element.body.match(/\[x\]/g) || []).length +
(element.body.match(/\[ \]/g) || []).length
);
});

console.log(totalCheckCount);
totalCheckCount = totalCheckCount.map(function(element) {
return element - initialCheckCount;
});

let positiveCheckCount = checklists.map(function(element) {
let checkCount =
+(element.body.match(/\[x\]/g) || []).length - initialCheckCount;
if (checkCount <= 0) return 0;
else return checkCount;
});

let percentages = positiveCheckCount.map(function(element) {
let p = Math.floor((element / totalCheckCount[0]) * 100);
return p;
});

let reviewerCount = percentages.length;
let reviewResult = 0;
percentages.forEach(element => {
reviewResult += element;
});
reviewResult /= reviewerCount;
console.log(reviewResult);
const badgeAssigned =
reviewResult < 40
? ["pending", "D%26I-Pending-red"]
: reviewResult < 60
? ["passing", "D%26I-Passing-passing"]
: reviewResult < 80
? ["silver", "D%26I-Silver-silver"]
: reviewResult <= 100
? ["gold", "D%26I-Gold-yellow"]
: ["pending", "D%26I-Pending-red"];

const url =
"https://img.shields.io/badge/" +
badgeAssigned[1] +
"?style=flat-square&labelColor=583586&&link=" +
issueURL +
"/&logo=data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDI1MCAyNTAiPgo8cGF0aCBmaWxsPSIjMUM5QkQ2IiBkPSJNOTcuMSw0OS4zYzE4LTYuNywzNy44LTYuOCw1NS45LTAuMmwxNy41LTMwLjJjLTI5LTEyLjMtNjEuOC0xMi4yLTkwLjgsMC4zTDk3LjEsNDkuM3oiLz4KPHBhdGggZmlsbD0iIzZBQzdCOSIgZD0iTTE5NC42LDMyLjhMMTc3LjIsNjNjMTQuOCwxMi4zLDI0LjcsMjkuNSwyNy45LDQ4LjVoMzQuOUMyMzYuMiw4MC4yLDIxOS45LDUxLjcsMTk0LjYsMzIuOHoiLz4KPHBhdGggZmlsbD0iI0JGOUNDOSIgZD0iTTIwNC45LDEzOS40Yy03LjksNDMuOS00OS45LDczLTkzLjgsNjUuMWMtMTMuOC0yLjUtMjYuOC04LjYtMzcuNS0xNy42bC0yNi44LDIyLjQKCWM0Ni42LDQzLjQsMTE5LjUsNDAuOSwxNjIuOS01LjdjMTYuNS0xNy43LDI3LTQwLjIsMzAuMS02NC4ySDIwNC45eiIvPgo8cGF0aCBmaWxsPSIjRDYxRDVGIiBkPSJNNTUuNiwxNjUuNkMzNS45LDEzMS44LDQzLjMsODguOCw3My4xLDYzLjVMNTUuNywzMy4yQzcuNSw2OS44LTQuMiwxMzcuNCwyOC44LDE4OEw1NS42LDE2NS42eiIvPgo8L3N2Zz4K";

const markdownBadgeImage =
"![Assigned badge: " + badgeAssigned[0] + "](" + url + ")";

const htmlBadgeImage =
"<img src='" +
url +
"' alt='" +
"D&I Badging badge state: " +
badgeAssigned[0] +
"'/>";
return [markdownBadgeImage, htmlBadgeImage, reviewResult, reviewerCount];
}

module.exports = calculateBadge;
module.exports = calculateBadge;
24 changes: 24 additions & 0 deletions src/checkModerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
async function checkModerator(context) {
let moderatorUsername = context.payload.issue.user.login;
let moderatorListDocument = await context.github.repos.getContents(
context.repo({ path: ".github/moderators.md" })
);

let moderatorList = Buffer.from(
moderatorListDocument.data.content,
"base64"
).toString();
moderatorList = moderatorList.split("\n");

let list = moderatorList.filter(element => {
return element[0] == "-";
});

list = list.map(function(element) {
return element.substring(2);
});

return list.includes(moderatorUsername);
}

module.exports = checkModerator;
16 changes: 10 additions & 6 deletions src/commandResponse.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
const calculateBadge = require('./calculateBadge');
const calculateBadge = require("./calculateBadge");
async function commandResponse(context) {

let reviewDetails = await calculateBadge(context);

const message = "\nReview percentage: " + reviewDetails[2] + "\n"
+ "\nNumber of reviewers: " + reviewDetails[3] + "\n";
const message =
"\nReview percentage: " +
reviewDetails[2] +
"\n" +
"\nNumber of reviewers: " +
reviewDetails[3] +
"\n";

return context.github.issues.createComment(
context.issue({ body: reviewDetails[0] + message}));

context.issue({ body: reviewDetails[0] + message })
);
}

module.exports = commandResponse;
30 changes: 19 additions & 11 deletions src/endReview.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
const calculateBadge = require('./calculateBadge');
const calculateBadge = require("./calculateBadge");
const checkModerator = require("./checkModerator");

async function endReview(context) {
let reviewDetails = await calculateBadge(context);
if ((await checkModerator(context)) == true)
context.github.issues.update(context.issue({ state: "closed" }));

let reviewDetails = await calculateBadge(context);
context.github.issues.update(context.issue({state: "closed"}));
context.github.issues.removeLabel(context.issue({name: ["review-begin"]}));
context.github.issues.addLabels(context.issue({labels: ["review-end"]}));
context.github.issues.removeLabel(context.issue({ name: ["review-begin"] }));
context.github.issues.addLabels(context.issue({ labels: ["review-end"] }));

const message = "\n**Markdown Badge Link:**\n```\n" + reviewDetails[0] + "\n```"
+ "\n**HTML Badge Link:**\n```\n" + reviewDetails[1] + "\n```";

return context.github.issues.createComment(
context.issue({ body: reviewDetails[0] + message}));
const message =
"\n**Markdown Badge Link:**\n```\n" +
reviewDetails[0] +
"\n```" +
"\n**HTML Badge Link:**\n```\n" +
reviewDetails[1] +
"\n```";

return context.github.issues.createComment(
context.issue({ body: reviewDetails[0] + message })
);
}

module.exports = endReview;
module.exports = endReview;
40 changes: 25 additions & 15 deletions src/postChecklist.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
async function postChecklist(context) {
const heading = `# Checklist for @${context.payload.assignee.login}`;

const heading = `# Checklist for ${context.payload.assignee.login}`;
const reviewerWelcome = await context.github.repos.getContents(
context.repo({ path: ".github/reviewer-welcome.md" })
);

const reviewerWelcome = await context.github.repos.getContents(
context.repo({path: '.github/reviewer-welcome.md'})
);
const title = context.payload.issue.title;

let checklist = await context.github.repos.getContents(
context.repo({ path: ".github/checklist.md" })
);

const checklist = await context.github.repos.getContents(
context.repo({path: '.github/checklist.md'})
if (title.substring(0, 15) == "[Virtual Event]") {
checklist = await context.github.repos.getContents(
context.repo({ path: ".github/checklist-virtual.md" })
);
}

const reviewerMessage = heading + "\n" + Buffer.from(reviewerWelcome.data.content, 'base64').toString() +
Buffer.from(checklist.data.content, 'base64').toString();
const reviewerMessage =
heading +
"\n" +
Buffer.from(reviewerWelcome.data.content, "base64").toString() +
Buffer.from(checklist.data.content, "base64").toString();
console.log(reviewerMessage);

context.github.issues.createComment(
context.issue({ body: reviewerMessage })
);
context.github.issues.createComment(context.issue({ body: reviewerMessage }));

if ((context.payload.issue.assignees.length) == 2)
{
context.github.issues.addLabels(context.issue({labels: ["review-begin"]}));
}
if (context.payload.issue.assignees.length == 2) {
context.github.issues.addLabels(
context.issue({ labels: ["review-begin"] })
);
}
}

module.exports = postChecklist;
21 changes: 11 additions & 10 deletions src/postHelp.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
async function endReview(context) {
const applicantWelcome = await context.github.repos.getContents(
context.repo({path: '.github/review-help.md'})
);

const helpMessage = Buffer.from(applicantWelcome.data.content, 'base64').toString();

context.github.issues.createComment(
context.issue({ body: helpMessage })
);
}
const applicantWelcome = await context.github.repos.getContents(
context.repo({ path: ".github/review-help.md" })
);

const helpMessage = Buffer.from(
applicantWelcome.data.content,
"base64"
).toString();

context.github.issues.createComment(context.issue({ body: helpMessage }));
}
Loading