Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
first version of bronze badging
Browse files Browse the repository at this point in the history
  • Loading branch information
kaxada committed Jul 5, 2023
1 parent b7d4c25 commit 8f07b29
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 92 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ jobs:
docker stop $(echo $IMAGE_NAME)
docker rm $(echo $IMAGE_NAME)
docker run -d \
-e CLIENT_ID=${{secrets.CLIENT_ID}} \
-e CLIENT_SECRET=${{ secrets.CLIENT_SECRET }} \
-e MAIL_PASSWORD=${{ secrets.MAIL_PASSWORD }} \
-e AUGUR_API_KEY=${{ secrets.AUGUR_API_KEY }} \
-p ${{secrets.PORT}}:${{secrets.PORT}} \
--restart always \
--name $(echo $IMAGE_NAME) \
Expand Down
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

# npm run format
npm run format
77 changes: 24 additions & 53 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ const express = require("express");
const { Octokit } = require("@octokit/rest");
const axios = require("axios");
require("dotenv").config();
const bodyParser = require('body-parser');
const scanner = require('./src/scanner.js')
const bodyParser = require("body-parser");
const scanner = require("./src/scanner.js");

const app = express();
app.use(express.static('public'));
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());


app.get("/login", (req, res) => {
app.get("/api/login", (req, res) => {
const scopes = ["user", "repo"];
const url = `https://github.com/login/oauth/authorize?client_id=${
process.env.CLIENT_ID
Expand All @@ -20,10 +19,12 @@ app.get("/login", (req, res) => {
res.redirect(url);
});

app.get("/callback", async (req, res) => {
app.get("/api/callback", async (req, res) => {
try {
const { code } = req.query;
const {data:{access_token}} = await axios.post(
const {
data: { access_token },
} = await axios.post(
"https://github.com/login/oauth/access_token",
{
client_id: process.env.CLIENT_ID,
Expand All @@ -40,15 +41,15 @@ app.get("/callback", async (req, res) => {
const octokit = new Octokit({ auth: `${access_token}` });

// authenticated user details
({
const {
data: { login, name, email },
} = await octokit.users.getAuthenticated());
} = await octokit.users.getAuthenticated();

// Public repos they maintain, administer or own
let repos = [];
let page = 1;
let response = await octokit.repos.listForAuthenticatedUser({
visibility: 'public',
visibility: "public",
per_page: 100,
page,
});
Expand All @@ -64,57 +65,27 @@ app.get("/callback", async (req, res) => {
}

// let repoList = repos.filter(repo=>repo.permissions.admin === true)
const repoList = repos.map(repo=>repo.full_name)

// res.status(200).json({
// name:name,
// username: login,
// email: email,
// repos: repoList
// })
const repoList = repos.map((repo) => repo.full_name);

res.status(200).send(`
<html>
<head>
<title>Repo List</title>
</head>
<body>
<h1>Welcome ${name}</h1>
<h2>Username: ${login}</h2>
<h2>Email: ${email}</h2>
<form action="/repos-to-badge" method="post">
<input type="hidden" name="name" value="${name}">
<input type="hidden" name="email" value="${email}">
<h2>Select Repositories:</h2>
${repoList
.map(
(repo) => `
<div>
<input type="checkbox" name="repos[]" value="${repo}">
<label for="${repo}">${repo}</label>
</div>
`
)
.join('')}
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
`);
res.status(200).json({
name,
username: login,
email,
repos: repoList,
});
} catch (error) {
console.error("Error:", error.message);
res.status(500).send("Internal Server Error");
}
});

app.post('/repos-to-badge', async (req, res) => {
const selectedRepos = await req.body.repos || [];
const name = req.body.name || '';
const email = req.body.email || '';
app.post("/api/repos-to-badge", async (req, res) => {
const selectedRepos = (await req.body.repos) || [];
const name = req.body.name || "";
const email = req.body.email || "";
// Process the selected repos as needed
const results = await scanner(name, email, selectedRepos)
res.status(200).json({results});
const results = await scanner(name, email, selectedRepos);
res.status(200).json({ results });
});

app.listen(4040, () => {
Expand Down
2 changes: 0 additions & 2 deletions src/badges/badges.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const { Octokit } = require("@octokit/rest");
const { scanne, awardBadge } = require("../helpers");
const bronzeBadge = require("./bronzeBadge");

const badges = async (req, res, login, name, email, octokit) => {
Expand Down
16 changes: 10 additions & 6 deletions src/badges/bronzeBadge.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const augurAPI = require('../helpers/augurAPI')
// const augurAPI = require("../helpers/augurAPI");

const bronzeBadge = async (id,url,content) => {
const bronzeBadge = async (id, url, content) => {
const results = [];
// Check for specific titles
const titlesToCheck = [
Expand All @@ -22,15 +22,19 @@ const bronzeBadge = async (id,url,content) => {
}

if (hasAllTitles) {
const augur=augurAPI(id,{level:"bronze"},url)
/**
* augur configs here
*/

// const augur=augurAPI(id,{level:"bronze"},url)
// console.log(augur)
// return results
console.log(augur)
return(results)
return results;
} else {
results.push(
"some fields are missing. Please refer to this link for more information"
);
return results
return results;
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/helpers/augurAPI.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const axios = require('axios');
const axios = require("axios");
require("dotenv").config();

const augurAPI = async (id, level, url) => {
try {
const response = await axios.post(
'https://projectbadge.chaoss.io/api/unstable/dei/repo',
"https://projectbadge.chaoss.io/api/unstable/dei/repo",
{ id, level, url },
{
headers: {
Expand All @@ -15,7 +15,7 @@ const augurAPI = async (id, level, url) => {

return response;
} catch (error) {
console.error('Error:', error.message);
console.error("Error:", error.message);
throw error;
}
};
Expand Down
8 changes: 4 additions & 4 deletions src/helpers/awardBadge.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const fs = require("fs");
const path = require("path");
const badgePath = path.join(__dirname, "../../assets/badge.svg");
// const fs = require("fs");
// const path = require("path");
// const badgePath = path.join(__dirname, "../../assets/badge.svg");
const badgeLink =
"https://raw.githubusercontent.com/AllInOpenSource/ProjectBadging/main/src/assets/images/badges/badge.svg";

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

// Read the existing README file content
const { data: existingFile } = await octokit.rest.repos.getContent({
Expand Down
21 changes: 10 additions & 11 deletions src/helpers/mailer.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
const nodemailer = require("nodemailer");
require("dotenv").config();

const mailer = (email, results)=>{

// Create a transporter using your email service provider's SMTP settings
const transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "ekaxada@gmail.com",
pass: process.env.MAIL_PASSWORD,
},
});
const mailer = (email, results) => {
// Create a transporter using your email service provider's SMTP settings
const transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "ekaxada@gmail.com",
pass: process.env.MAIL_PASSWORD,
},
});

// Define the email options
const mailOptions = {
from: "ekaxada@gmail.com",
to: email,
subject: "DEI report",
subject: "DEI Badging report",
text: results.join("\n"),
};

Expand Down
26 changes: 14 additions & 12 deletions src/scanner.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Octokit } = require("@octokit/rest");
const mailer = require('./helpers/mailer.js')
const bronzeBadge = require('./badges/bronzeBadge.js')
const mailer = require("./helpers/mailer.js");
const bronzeBadge = require("./badges/bronzeBadge.js");

const scanner = async (name, email, selectedRepos) => {
const octokit = new Octokit();
Expand All @@ -13,27 +13,29 @@ const scanner = async (name, email, selectedRepos) => {
repo: repo.split("/")[1],
});

const id = repoResponse.data.id
const url = repoResponse.data.html_url
const id = repoResponse.data.id;
const url = repoResponse.data.html_url;
const contentResponse = await octokit.repos.getContent({
owner: repo.split("/")[0],
repo: repo.split("/")[1],
path: "DEI.md",
});
const content = Buffer.from(contentResponse.data.content, 'base64').toString();
const bronzeResults = await bronzeBadge(id,url,content)
results.push("✅", repo, bronzeResults)
const content = Buffer.from(
contentResponse.data.content,
"base64"
).toString();
const bronzeResults = await bronzeBadge(id, url, content);
results.push("✅", repo, bronzeResults);
} catch (error) {
console.error(error)
console.error(error);
// results.push("❌", repo, "does not have DEI.md file");
}
}
} catch (error) {
console.error('Error:', error.message);
console.error("Error:", error.message);
}
mailer(email, results)
mailer(email, results);
return results;
}
};

module.exports = scanner;

0 comments on commit 8f07b29

Please sign in to comment.