From 937a2597d5fde7e839a12eb898d5e129d29c73eb Mon Sep 17 00:00:00 2001 From: kaxada Date: Thu, 21 Dec 2023 11:54:16 +0300 Subject: [PATCH] reference and scan DEI.md template ( Fixes #7 ) --- providers/github/APICalls.js | 33 +++++++++++++++++++++++++++------ providers/gitlab/APICalls.js | 23 ++++++++++++++++++++++- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/providers/github/APICalls.js b/providers/github/APICalls.js index 594bd0c..1d72e6c 100644 --- a/providers/github/APICalls.js +++ b/providers/github/APICalls.js @@ -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 = []; @@ -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( @@ -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( @@ -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"); } } diff --git a/providers/gitlab/APICalls.js b/providers/gitlab/APICalls.js index 2b91ae8..5e8fc70 100644 --- a/providers/gitlab/APICalls.js +++ b/providers/gitlab/APICalls.js @@ -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 @@ -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 @@ -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(