Skip to content

Commit

Permalink
reference and scan DEI.md template ( Fixes #7 )
Browse files Browse the repository at this point in the history
  • Loading branch information
kaxada committed Dec 21, 2023
1 parent 93f2f5d commit 937a259
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
33 changes: 27 additions & 6 deletions providers/github/APICalls.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ const getFileContentAndSHA = async (octokit, repositoryFullName, filePath) => {
* @param {*} email User email used to send them emails with the results
* @param {*} repositoryIds List of repositories id to scan
*/

const scanRepositories = async (userId, name, email, repositoryIds) => {
const octokit = new Octokit();
let results = [];
Expand All @@ -169,13 +170,20 @@ const scanRepositories = async (userId, name, email, repositoryIds) => {
}

try {
// Check if the repo was badged before
const existingRepo = await Repo.findOne({
where: { githubRepoId: info.id, DEICommitSHA: file.sha },
});

if (file.content) {
// retrieve existing repo ID from Database
const existingRepo = await Repo.findOne({
where: { githubRepoId: info.id, DEICommitSHA: file.sha },
});

// retrieve DEI template as stored at => https://github.com/badging/badging/blob/main/Template.DEI.md
const DEItemplate = await getFileContentAndSHA(
octokit,
"badging/badging",
"Template.DEI.md"
);
if (existingRepo) {
// Check if the repo was badged before
// Compare the DEICommitSHA with the existing repo's DEICommitSHA
if (existingRepo.DEICommitSHA !== file.sha) {
bronzeBadge(
Expand All @@ -192,6 +200,19 @@ const scanRepositories = async (userId, name, email, repositoryIds) => {
// Handle case when DEI.md file is not changed
results.push(`${info.url} was already badged`);
}
} else if (DEItemplate) {
// check whether DEI file to be badged has content similar to DEI Template
try {
if (file.content === DEItemplate.file.content) {
results.push(
`Please provide project specific DEI content for ${info.url} different from the template information`
);
}
} catch (error) {
console.error(
"DEI template cannot be found at the original location 'https://github.com/badging/badging/blob/main/Template.DEI.md'"
);
}
} else {
// Repo not badged before, badge it
bronzeBadge(
Expand All @@ -207,7 +228,7 @@ const scanRepositories = async (userId, name, email, repositoryIds) => {
}
}
} catch (error) {
console.error(error.message);
console.error("DEI.md file has no content");
}
}

Expand Down
23 changes: 22 additions & 1 deletion providers/gitlab/APICalls.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const axios = require("axios");
const Repo = require("../../database/models/repo.model.js");
const bronzeBadge = require("../../badges/bronzeBadge.js");
const mailer = require("../../helpers/mailer.js");

/**
* Calls the GitLab API to get the user info.
* @param {*} access_token Token used to authorize the call to the GitLab API
Expand Down Expand Up @@ -173,6 +172,15 @@ const scanRepositories = async (userId, name, email, repositoryIds) => {
where: { gitlabRepoId: info.id, DEICommitSHA: file.sha },
});

// retrieve DEI template as stored at => https://github.com/badging/badging/blob/main/Template.DEI.md
const {
data: { content },
} = await axios.get(
"https://api.github.com/repos/badging/badging/contents/Template.DEI.md"
);

const DEItemplate = Buffer.from(content, "base64").toString();

if (file.content) {
if (existingRepo) {
// Compare the DEICommitSHA with the existing repo's DEICommitSHA
Expand All @@ -191,6 +199,19 @@ const scanRepositories = async (userId, name, email, repositoryIds) => {
// Handle case when DEI.md file is not changed
results.push(`${info.url} was already badged`);
}
} else if (DEItemplate) {
// check whether DEI file to be badged has content similar to DEI Template
try {
if (file.content === DEItemplate.file.content) {
results.push(
`Please provide project specific DEI content for ${info.url} different from the template information`
);
}
} catch (error) {
console.error(
"DEI template cannot be found at the original location 'https://github.com/badging/badging/blob/main/Template.DEI.md'"
);
}
} else {
// Repo not badged before, badge it
bronzeBadge(
Expand Down

0 comments on commit 937a259

Please sign in to comment.