Skip to content

Commit

Permalink
Merge pull request #50 from DesmondSanctity/event-badging-submit-api
Browse files Browse the repository at this point in the history
feat: remove designate API for event and merge with project
  • Loading branch information
adeyinkaoresanya committed Sep 24, 2024
2 parents 4400ca8 + 4c68a93 commit 82cb3f1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 87 deletions.
69 changes: 0 additions & 69 deletions providers/event-github/auth.js

This file was deleted.

49 changes: 44 additions & 5 deletions providers/github/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ require("dotenv").config();
const axios = require("axios");
const { saveUser } = require("../../database/controllers/user.controller.js");
const { getUserInfo, getUserRepositories } = require("./APICalls.js");
const {
encrypt,
decrypt,
convertToMarkdown,
} = require("../../helpers/crypto.js");

// instantiate Github App for event handling (webhooks)
const githubApp = new App({
Expand All @@ -21,17 +26,28 @@ const githubApp = new App({
* @param {*} res Response to send back to the caller
*/
const githubAuth = (req, res) => {
const { type } = req.body;
if (!process.env.GITHUB_AUTH_CLIENT_ID) {
res.status(500).send("GitHub provider is not configured");
return;
}

const scopes = ["user", "repo"];
const url = `https://github.com/login/oauth/authorize?client_id=${
process.env.GITHUB_AUTH_CLIENT_ID
}&scope=${scopes.join(",")}`;
if (type === "event-badging") {
const scopes = ["repo"];
const encryptedFormData = encrypt(JSON.stringify(req.body));
const url = `https://github.com/login/oauth/authorize?client_id=${
process.env.GITHUB_AUTH_CLIENT_ID
}&scope=${scopes.join(",")}&state=${encryptedFormData}`;

res.redirect(url);
res.send({ authorizationLink: url });
} else {
const scopes = ["user", "repo"];
const url = `https://github.com/login/oauth/authorize?client_id=${
process.env.GITHUB_AUTH_CLIENT_ID
}&scope=${scopes.join(",")}`;

res.redirect(url);
}
};

/**
Expand Down Expand Up @@ -72,6 +88,17 @@ const requestAccessToken = async (code) => {
const handleOAuthCallback = async (req, res) => {
const code = req.body.code ?? req.query.code;

let issueTitle;
let markdown;

if (req.query.state) {
const encryptedState = req.query.state;
const formData = decrypt(encryptedState);
const parsedFormData = JSON.parse(formData);
issueTitle = parsedFormData.title;
markdown = convertToMarkdown(parsedFormData.body);
}

const { access_token: accessToken, errors: accessTokenErrors } =
await requestAccessToken(code);
if (accessTokenErrors.length > 0) {
Expand All @@ -81,6 +108,18 @@ const handleOAuthCallback = async (req, res) => {

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

if (issueTitle && markdown) {
const { data: issue } = await octokit.rest.issues.create({
owner: "badging",
repo: "event-diversity-and-inclusion",
title: issueTitle,
body: markdown,
});

res.redirect(issue.html_url);
return;
}

// Authenticated user details
const { user_info: userInfo, errors: userInfoErrors } = await getUserInfo(
octokit
Expand Down
6 changes: 0 additions & 6 deletions providers/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
const {
issueCreationCallback,
issueCreationAuth,
} = require("./event-github/auth.js");
const {
githubAuth,
githubAuthCallback,
Expand All @@ -15,6 +11,4 @@ module.exports = {
githubApp,
gitlabAuth,
gitlabAuthCallback,
issueCreationAuth,
issueCreationCallback,
};
12 changes: 5 additions & 7 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ const {
githubApp,
gitlabAuth,
gitlabAuthCallback,
issueCreationCallback,
issueCreationAuth,
} = require("../providers/index.js");

/**
Expand Down Expand Up @@ -151,6 +149,11 @@ const setupRoutes = (app) => {
githubAuth(req, res);
});

// for event badging
app.post("/api/auth/github", (req, res) => {
githubAuth(req, res);
});

app.get("/api/auth/gitlab", (req, res) => {
gitlabAuth(req, res);
});
Expand All @@ -161,7 +164,6 @@ const setupRoutes = (app) => {
gitlabAuthCallback(app);
app.get("/api/badgedRepos", badgedRepos);
app.post("/api/repos-to-badge", reposToBadge);
app.get("/api/issue-callback", issueCreationCallback);

// github app routes
app.post("/api/event_badging", async (req, res) => {
Expand All @@ -177,10 +179,6 @@ const setupRoutes = (app) => {
res.send("ok");
});

app.post("/api/submit-form", (req, res) => {
issueCreationAuth(req, res);
});

// route to get all events
app.get("/api/badged_events", getAllEvents);

Expand Down

0 comments on commit 82cb3f1

Please sign in to comment.