Skip to content

Commit

Permalink
Merge pull request #26 from DesmondSanctity/clean-up-repo
Browse files Browse the repository at this point in the history
chore: clean up repository
  • Loading branch information
kaxada committed Feb 7, 2024
2 parents 23ce9f8 + 281548e commit fbcaa43
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 60 deletions.
4 changes: 2 additions & 2 deletions database/helpers/dbconnect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const sequelize = require("./sequelize");

const dbconnect = async () => {
const dbConnect = async () => {
try {
await sequelize.authenticate();
try {
Expand All @@ -17,4 +17,4 @@ const dbconnect = async () => {
}
};

module.exports = dbconnect;
module.exports = dbConnect;
4 changes: 2 additions & 2 deletions event_badging/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
updateReadme,
} = require("./logic/index.js");

const event_badging = async (name, octokit, payload) => {
const eventBadging = async (name, octokit, payload) => {
// perform actions on application issues only
if (payload.issue.title.match(/event/i)) {
// when applicant issue is open, welcome the applicant
Expand Down Expand Up @@ -49,4 +49,4 @@ const event_badging = async (name, octokit, payload) => {
}
};

module.exports = event_badging;
module.exports = eventBadging;
8 changes: 7 additions & 1 deletion event_badging/logic/calculate.badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ const calculateBadge = async (octokit, payload) => {
? (initialCheckCount = 4)
: initialCheckCount;

// get the list of comments on the event issue
const comments = await octokit.rest.issues.listComments({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.issue.number,
});

// filter out the comments that are checklists
let checklists = comments.data.filter((comment) => {
return (
comment.user.type == "Bot" &&
comment.body.substring(0, 15) == "# Checklist for"
);
});

// get the total number of checks for each checklist
let totalCheckCount = checklists.map(function (element) {
return (
(element.body.match(/\[x\]/g) || []).length +
Expand All @@ -31,13 +34,15 @@ const calculateBadge = async (octokit, payload) => {
return element - initialCheckCount;
});

// get the number of checks for each checklist that are positive
let positiveCheckCount = checklists.map(function (element) {
let checkCount =
+(element.body.match(/\[x\]/g) || []).length - initialCheckCount;
if (checkCount <= 0) return 0;
else return checkCount;
});

// get the percentage of checks for each checklist that are positive
let percentages = positiveCheckCount.map(function (element) {
let p = Math.floor((element / totalCheckCount[0]) * 100);
return p;
Expand All @@ -50,6 +55,7 @@ const calculateBadge = async (octokit, payload) => {
});
reviewResult /= reviewerCount;

// assign bagde based on review result
const badgeAssigned =
reviewResult < 40
? ["Pending", "D%26I-Pending-red"]
Expand All @@ -75,7 +81,7 @@ const calculateBadge = async (octokit, payload) => {
"<img src='" +
url +
"' alt='" +
"D&I Badging badge state: " +
"d&i-badging-badge-state: " +
badgeAssigned[0] +
"'/>";
messageObj = {
Expand Down
1 change: 1 addition & 0 deletions event_badging/logic/getResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const getResults = async (octokit, payload) => {
resultsArray.reviewerCount +
"\n";

// comment results and badge on the issue
await octokit.rest.issues
.createComment({
owner: payload.repository.owner.login,
Expand Down
1 change: 1 addition & 0 deletions event_badging/logic/welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const welcome = async (octokit, payload) => {
path: ".github/applicant-welcome.md",
});

// send welcome message on the issue
await octokit.rest.issues
.createComment({
owner: payload.repository.owner.login,
Expand Down
8 changes: 0 additions & 8 deletions helpers/awardBadge.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
// const fs = require("fs");
// const path = require("path");
// const badgePath = path.join(__dirname, "../../assets/badge.svg");
const badgeLink =
"https://raw.githubusercontent.com/badging/badging/main/src/assets/images/badges/bronze-badge.svg";

const awardBadge = async (octokit, owner, repoName) => {
try {
// Read the SVG file content
// const svgContent = fs.readFileSync(badgePath, "utf8");

// Read the existing README file content
const { data: existingFile } = await octokit.rest.repos.getContent({
owner: owner,
Expand All @@ -21,8 +15,6 @@ const awardBadge = async (octokit, owner, repoName) => {
"base64"
).toString("utf8");

// Combine the SVG content with the existing README content
// const updatedContent = `![Bronze badge](${svgContent})\n\n${existingContent}`;
const updatedContent = `![Bronze badge](${badgeLink})\n\n${existingContent}`;

// Create or update the README file with the modified content directly on the main branch
Expand Down
10 changes: 5 additions & 5 deletions helpers/email_templates/failure.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
margin-bottom: 20px;
}

.email_content {
.email-content {
color: #555555;
font-size: 16px;
line-height: 1.5;
Expand Down Expand Up @@ -64,17 +64,17 @@
<header>
<img
src="https://raw.githubusercontent.com/badging/BadgingAPI/main/assets/images/logo.svg"
alt="badging logo"
alt="badging-logo"
id="logo"
/>
<img
src="https://raw.githubusercontent.com/badging/BadgingAPI/main/assets/images/carousel.svg"
alt="email carousel"
alt="email-carousel"
id="carousel"
/>
</header>
<h1>Badging Updates</h1>
<p class="email_content">
<p class="email-content">
Dear {{recipientName}},
<br />
<br />
Expand All @@ -88,7 +88,7 @@ <h1>Badging Updates</h1>
<ul>
<li>{{results}}</li>
</ul>
<p class="email_content">
<p class="email-content">
Once you have the missing information, please resubmit your project for
review, and we'll be happy to reconsider it for the badge issuance.
<br />
Expand Down
4 changes: 2 additions & 2 deletions helpers/email_templates/success.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
margin-bottom: 20px;
}

.email_content {
.email-content {
color: #555555;
font-size: 16px;
line-height: 1.5;
Expand All @@ -58,7 +58,7 @@
<body>
<div class="container">
<h1>Congratulations on Your DEI Badge!</h1>
<p class="email_content">
<p class="email-content">
Dear {{recipientName}},
<br />
<br />
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const dbconnect = require("./database/helpers/dbconnect");
const dbConnect = require("./database/helpers/dbconnect");
const routes = require("./routes/index.js");
require("dotenv").config();

const app = express();

// middlewares
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(
Expand All @@ -20,7 +22,7 @@ routes.setupRoutes(app);

(async () => {
try {
await dbconnect().then(() => {
await dbConnect().then(() => {
app.listen(process.env.PORT, () => {
console.log(`server listening on port ${process.env.PORT}`);
});
Expand Down
28 changes: 14 additions & 14 deletions providers/github/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,28 @@ const requestAccessToken = async (code) => {
const handleOAuthCallback = async (req, res) => {
const code = req.body.code ?? req.query.code;

const { access_token, errors: access_token_errors } =
const { access_token: accessToken, errors: accessTokenErrors } =
await requestAccessToken(code);
if (access_token_errors.length > 0) {
res.status(500).send(access_token_errors.join());
if (accessTokenErrors.length > 0) {
res.status(500).send(accessTokenErrors.join());
return;
}

const octokit = new Octokit({ auth: `${access_token}` });
const octokit = new Octokit({ auth: `${accessToken}` });

// Authenticated user details
const { user_info, errors: user_info_errors } = await getUserInfo(octokit);
if (user_info_errors.length > 0) {
res.status(500).send(user_info_errors.join());
const { user_info: userInfo, errors: userInfoErrors } = await getUserInfo(octokit);
if (userInfoErrors.length > 0) {
res.status(500).send(userInfoErrors.join());
return;
}

// Save user to database
const savedUser = await saveUser(
user_info.login,
user_info.name,
user_info.email,
user_info.id,
userInfo.login,
userInfo.name,
userInfo.email,
userInfo.id,
null
);
if (!savedUser) {
Expand All @@ -102,10 +102,10 @@ const handleOAuthCallback = async (req, res) => {
}

// Public repos they maintain, administer, or own
const { repositories, errors: repositories_errors } =
const { repositories, errors: repositoriesErrors } =
await getUserRepositories(octokit);
if (repositories_errors.length > 0) {
res.status(500).send(repositories_errors.join());
if (repositoriesErrors.length > 0) {
res.status(500).send(repositoriesErrors.join());
return;
}

Expand Down
14 changes: 7 additions & 7 deletions providers/gitlab/APICalls.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,20 @@ const scanRepositories = async (userId, name, email, repositoryIds) => {

try {
for (const repositoryId of repositoryIds) {
const { info, errors: info_errors } = await getRepositoryInfo(
const { info, errors: infoErrors } = await getRepositoryInfo(
repositoryId
);
if (info_errors.length > 0) {
console.error(info_errors);
if (infoErrors.length > 0) {
console.error(infoErrors);
continue;
}

const { file, errors: file_errors } = await getFileContentAndSHA(
const { file, errors: fileErrors } = await getFileContentAndSHA(
repositoryId,
"DEI.md",
info.defaultBranch
);
if (file_errors.length > 0) {
if (fileErrors.length > 0) {
results.push(`${info.url} does not have a DEI.md file`);
continue;
}
Expand All @@ -188,11 +188,11 @@ const scanRepositories = async (userId, name, email, repositoryIds) => {
});

// retrieve DEI template
const template_content = await axios.get(
const templateContent = await axios.get(
"https://api.github.com/repos/badging/badging/contents/Template.DEI.md"
);
const template = Buffer.from(
template_content.data.content,
templateContent.data.content,
"base64"
).toString();

Expand Down
30 changes: 15 additions & 15 deletions providers/gitlab/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,40 +68,40 @@ const requestAccessToken = async (code) => {
const handleOAuthCallback = async (req, res) => {
const code = req.body.code ?? req.query.code;

const { access_token, errors: access_token_errors } =
const { access_token: accessToken, errors: accessTokenErrors } =
await requestAccessToken(code);
if (access_token_errors.length > 0) {
res.status(500).send(access_token_errors.join());
if (accessTokenErrors.length > 0) {
res.status(500).send(accessTokenErrors.join());
return;
}

// Authenticated user details
const { user_info, errors: user_info_errors } = await getUserInfo(
access_token
const { user_info: userInfo, errors: userInfoErrors } = await getUserInfo(
accessToken
);
if (user_info_errors.length > 0) {
res.status(500).send(user_info_errors.join());
if (userInfoErrors.length > 0) {
res.status(500).send(userInfoErrors.join());
return;
}

// Save user to database
const savedUser = await saveUser(
user_info.login,
user_info.name,
user_info.email,
userInfo.login,
userInfo.name,
userInfo.email,
null,
user_info.id
userInfo.id
);
if (!savedUser) {
res.status(500).send("Error saving user info");
return;
}

// Public repos they maintain, administer, or own
const { repositories, errors: repositories_errors } =
await getUserRepositories(access_token);
if (repositories_errors.length > 0) {
res.status(500).send(repositories_errors.join());
const { repositories, errors: repositoriesErrors } =
await getUserRepositories(accessToken);
if (repositoriesErrors.length > 0) {
res.status(500).send(repositoriesErrors.join());
return;
}

Expand Down
4 changes: 2 additions & 2 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { findUser } = require("../database/controllers/user.controller.js");
const Repo = require("../database/models/repo.model.js");
const event_badging = require("../event_badging/index.js");
const eventBadging = require("../event_badging/index.js");
const github_helpers = require("../providers/github/APICalls.js");
const gitlab_helpers = require("../providers/gitlab/APICalls.js");
const {
Expand Down Expand Up @@ -172,7 +172,7 @@ const setupRoutes = (app) => {
const octokit = await githubApp.getInstallationOctokit(
payload.installation.id
);
event_badging(name, octokit, payload);
eventBadging(name, octokit, payload);
console.info(`Received ${name} event from Github`);
res.send("ok");
});
Expand Down

0 comments on commit fbcaa43

Please sign in to comment.