From 09d044a824510273a23593dbb2a314befad14d38 Mon Sep 17 00:00:00 2001 From: kaxada Date: Thu, 21 Dec 2023 11:54:16 +0300 Subject: [PATCH] Fixes #7 --- providers/github/APICalls.js | 42 ++++++++++++++++++++++++++++++------ providers/gitlab/APICalls.js | 14 ++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/providers/github/APICalls.js b/providers/github/APICalls.js index 594bd0c..96b5c46 100644 --- a/providers/github/APICalls.js +++ b/providers/github/APICalls.js @@ -144,6 +144,15 @@ 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 */ + +/** + * Declaring the 'DEItempate variable as a global scrope + * so that it can be exported and used in comparing it to the + * DEI content in the gitlab repositories + */ + +let DEItemplate; + const scanRepositories = async (userId, name, email, repositoryIds) => { const octokit = new Octokit(); let results = []; @@ -169,13 +178,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 + 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 +208,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 +236,7 @@ const scanRepositories = async (userId, name, email, repositoryIds) => { } } } catch (error) { - console.error(error.message); + console.error("DEI.md file has no content"); } } @@ -228,4 +257,5 @@ module.exports = { getUserInfo, getUserRepositories, scanRepositories, + DEItemplate, }; diff --git a/providers/gitlab/APICalls.js b/providers/gitlab/APICalls.js index 2b91ae8..53bd400 100644 --- a/providers/gitlab/APICalls.js +++ b/providers/gitlab/APICalls.js @@ -2,6 +2,7 @@ const axios = require("axios"); const Repo = require("../../database/models/repo.model.js"); const bronzeBadge = require("../../badges/bronzeBadge.js"); const mailer = require("../../helpers/mailer.js"); +const { DEItemplate } = require("../github/APICalls.js"); /** * Calls the GitLab API to get the user info. @@ -191,6 +192,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(