Skip to content

Commit 82cb3f1

Browse files
Merge pull request #50 from DesmondSanctity/event-badging-submit-api
feat: remove designate API for event and merge with project
2 parents 4400ca8 + 4c68a93 commit 82cb3f1

File tree

4 files changed

+49
-87
lines changed

4 files changed

+49
-87
lines changed

providers/event-github/auth.js

Lines changed: 0 additions & 69 deletions
This file was deleted.

providers/github/auth.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ require("dotenv").config();
44
const axios = require("axios");
55
const { saveUser } = require("../../database/controllers/user.controller.js");
66
const { getUserInfo, getUserRepositories } = require("./APICalls.js");
7+
const {
8+
encrypt,
9+
decrypt,
10+
convertToMarkdown,
11+
} = require("../../helpers/crypto.js");
712

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

29-
const scopes = ["user", "repo"];
30-
const url = `https://github.com/login/oauth/authorize?client_id=${
31-
process.env.GITHUB_AUTH_CLIENT_ID
32-
}&scope=${scopes.join(",")}`;
35+
if (type === "event-badging") {
36+
const scopes = ["repo"];
37+
const encryptedFormData = encrypt(JSON.stringify(req.body));
38+
const url = `https://github.com/login/oauth/authorize?client_id=${
39+
process.env.GITHUB_AUTH_CLIENT_ID
40+
}&scope=${scopes.join(",")}&state=${encryptedFormData}`;
3341

34-
res.redirect(url);
42+
res.send({ authorizationLink: url });
43+
} else {
44+
const scopes = ["user", "repo"];
45+
const url = `https://github.com/login/oauth/authorize?client_id=${
46+
process.env.GITHUB_AUTH_CLIENT_ID
47+
}&scope=${scopes.join(",")}`;
48+
49+
res.redirect(url);
50+
}
3551
};
3652

3753
/**
@@ -72,6 +88,17 @@ const requestAccessToken = async (code) => {
7288
const handleOAuthCallback = async (req, res) => {
7389
const code = req.body.code ?? req.query.code;
7490

91+
let issueTitle;
92+
let markdown;
93+
94+
if (req.query.state) {
95+
const encryptedState = req.query.state;
96+
const formData = decrypt(encryptedState);
97+
const parsedFormData = JSON.parse(formData);
98+
issueTitle = parsedFormData.title;
99+
markdown = convertToMarkdown(parsedFormData.body);
100+
}
101+
75102
const { access_token: accessToken, errors: accessTokenErrors } =
76103
await requestAccessToken(code);
77104
if (accessTokenErrors.length > 0) {
@@ -81,6 +108,18 @@ const handleOAuthCallback = async (req, res) => {
81108

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

111+
if (issueTitle && markdown) {
112+
const { data: issue } = await octokit.rest.issues.create({
113+
owner: "badging",
114+
repo: "event-diversity-and-inclusion",
115+
title: issueTitle,
116+
body: markdown,
117+
});
118+
119+
res.redirect(issue.html_url);
120+
return;
121+
}
122+
84123
// Authenticated user details
85124
const { user_info: userInfo, errors: userInfoErrors } = await getUserInfo(
86125
octokit

providers/index.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
const {
2-
issueCreationCallback,
3-
issueCreationAuth,
4-
} = require("./event-github/auth.js");
51
const {
62
githubAuth,
73
githubAuthCallback,
@@ -15,6 +11,4 @@ module.exports = {
1511
githubApp,
1612
gitlabAuth,
1713
gitlabAuthCallback,
18-
issueCreationAuth,
19-
issueCreationCallback,
2014
};

routes/index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ const {
1010
githubApp,
1111
gitlabAuth,
1212
gitlabAuthCallback,
13-
issueCreationCallback,
14-
issueCreationAuth,
1513
} = require("../providers/index.js");
1614

1715
/**
@@ -151,6 +149,11 @@ const setupRoutes = (app) => {
151149
githubAuth(req, res);
152150
});
153151

152+
// for event badging
153+
app.post("/api/auth/github", (req, res) => {
154+
githubAuth(req, res);
155+
});
156+
154157
app.get("/api/auth/gitlab", (req, res) => {
155158
gitlabAuth(req, res);
156159
});
@@ -161,7 +164,6 @@ const setupRoutes = (app) => {
161164
gitlabAuthCallback(app);
162165
app.get("/api/badgedRepos", badgedRepos);
163166
app.post("/api/repos-to-badge", reposToBadge);
164-
app.get("/api/issue-callback", issueCreationCallback);
165167

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

180-
app.post("/api/submit-form", (req, res) => {
181-
issueCreationAuth(req, res);
182-
});
183-
184182
// route to get all events
185183
app.get("/api/badged_events", getAllEvents);
186184

0 commit comments

Comments
 (0)