Skip to content

Commit 5fae27e

Browse files
Merge pull request #53 from DesmondSanctity/event-badging-submit-api
Event badging submit api
2 parents ad93744 + 430bc62 commit 5fae27e

File tree

4 files changed

+179
-139
lines changed

4 files changed

+179
-139
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: CI/CD
2+
3+
on:
4+
push:
5+
branches:
6+
- "staging"
7+
8+
env:
9+
IMAGE_NAME: "project_badging_staging"
10+
11+
jobs:
12+
deploy:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v3
16+
- name: Remove old Docker images
17+
uses: appleboy/[email protected]
18+
with:
19+
host: ${{ secrets.HOST }}
20+
username: ${{ secrets.USERNAME }}
21+
password: ${{ secrets.PASSWORD }}
22+
script: |
23+
if docker inspect ${{ env.IMAGE_NAME }} >/dev/null 2>&1; then
24+
docker stop ${{ env.IMAGE_NAME }}
25+
docker rm ${{ env.IMAGE_NAME }}
26+
docker images --filter "reference=${{ env.IMAGE_NAME }}*" -q | xargs docker rmi -f || true
27+
fi
28+
29+
- name: Build Docker image
30+
run: docker build -t ${{ env.IMAGE_NAME }}:${{ github.sha }} .
31+
32+
- name: Create image archive
33+
run: docker save ${{ env.IMAGE_NAME }}:${{ github.sha }} -o badging-staging.tar
34+
35+
- name: Upload image archive using appleboy/scp-action
36+
uses: appleboy/[email protected]
37+
with:
38+
host: ${{ secrets.HOST }}
39+
username: ${{ secrets.USERNAME }}
40+
password: ${{ secrets.PASSWORD }}
41+
source: badging-staging.tar
42+
target: ~/
43+
44+
- name: Extract and load image on droplet
45+
uses: appleboy/[email protected]
46+
with:
47+
host: ${{ secrets.HOST }}
48+
username: ${{ secrets.USERNAME }}
49+
password: ${{ secrets.PASSWORD }}
50+
script: |
51+
docker load -i ~/badging-staging.tar
52+
docker run -d \
53+
-p ${{ secrets.PORT }}:${{ secrets.PORT }} \
54+
--env-file /home/${{ secrets.USERNAME }}/staging/.env \
55+
--network host \
56+
--restart=always \
57+
--name ${{ env.IMAGE_NAME }} \
58+
${{ env.IMAGE_NAME }}:${{ github.sha }}

providers/github/auth.js

Lines changed: 104 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,117 @@ const requestAccessToken = async (code) => {
8585
}
8686
};
8787

88+
const handleOAuthCallback = async (req, res) => {
89+
const code = req.body.code ?? req.query.code;
90+
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+
102+
const { access_token: accessToken, errors: accessTokenErrors } =
103+
await requestAccessToken(code);
104+
if (accessTokenErrors.length > 0) {
105+
res.status(500).send(accessTokenErrors.join());
106+
return;
107+
}
108+
109+
const octokit = new Octokit({ auth: `${accessToken}` });
110+
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+
123+
// Authenticated user details
124+
const { user_info: userInfo, errors: userInfoErrors } = await getUserInfo(
125+
octokit
126+
);
127+
if (userInfoErrors.length > 0) {
128+
res.status(500).send(userInfoErrors.join());
129+
return;
130+
}
131+
132+
// Save user to database
133+
const savedUser = await saveUser(
134+
userInfo.login,
135+
userInfo.name,
136+
userInfo.email,
137+
userInfo.id,
138+
null
139+
);
140+
if (!savedUser) {
141+
res.status(500).send("Error saving user info");
142+
return;
143+
}
144+
145+
// Public repos they maintain, administer, or own
146+
const { repositories, errors: repositoriesErrors } =
147+
await getUserRepositories(octokit);
148+
if (repositoriesErrors.length > 0) {
149+
res.status(500).send(repositoriesErrors.join());
150+
return;
151+
}
88152

89-
/**
90-
* Sets up the provided Express app routes for GitLab
91-
* @param {*} app Express application instance
92-
*/
93-
const githubAuthCallback = (app) => {
94153
if (process.env.NODE_ENV === "production") {
95-
app.post("/api/callback/github", handleOAuthCallback);
154+
res.status(200).json({
155+
userId: savedUser.id,
156+
name: savedUser.name,
157+
username: savedUser.login,
158+
email: savedUser.email,
159+
repos: repositories,
160+
provider: "github",
161+
});
96162
} else if (process.env.NODE_ENV === "development") {
97-
app.get("/api/callback/github", handleOAuthCallback);
163+
res.status(200).send(`
164+
<html>
165+
<head>
166+
<title>Repo List</title>
167+
</head>
168+
<body>
169+
<h1>Welcome ${savedUser.name}</h1>
170+
<h2>Username: ${savedUser.login}</h2>
171+
<h2>Email: ${savedUser.email}</h2>
172+
<form action="/api/repos-to-badge" method="post">
173+
<input type="hidden" name="provider" value="github">
174+
<input type="hidden" name="userId" value="${savedUser.id}">
175+
<h2>Select Repositories:</h2>
176+
${repositories
177+
.map(
178+
(repo) => `
179+
<div>
180+
<input type="checkbox" name="repos[]" value="${repo.id}">
181+
<label for="${repo.id}">${repo.fullName}</label>
182+
</div>
183+
`
184+
)
185+
.join("")}
186+
<br>
187+
<input type="submit" value="Submit">
188+
</form>
189+
</body>
190+
</html>
191+
`);
192+
} else {
193+
res.status(500).send("Unknown process mode");
98194
}
99195
};
100196

101197
module.exports = {
102198
githubAuth,
103-
githubAuthCallback,
199+
handleOAuthCallback,
104200
githubApp,
105201
};

providers/gitlab/auth.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const requestAccessToken = async (code) => {
6565
* @returns A json object with `user_info` and `errors`
6666
* */
6767

68-
const handleOAuthCallback = async (req, res) => {
68+
const handleOAuthCallbackGitlab = async (req, res) => {
6969
const code = req.body.code ?? req.query.code;
7070

7171
const { access_token: accessToken, errors: accessTokenErrors } =
@@ -152,19 +152,7 @@ const handleOAuthCallback = async (req, res) => {
152152
}
153153
};
154154

155-
/**
156-
* Sets up the provided Express app routes for GitLab
157-
* @param {*} app Express application instance
158-
*/
159-
const gitlabAuthCallback = (app) => {
160-
if (process.env.NODE_ENV === "production") {
161-
app.post("/api/callback/gitlab", handleOAuthCallback);
162-
} else if (process.env.NODE_ENV === "development") {
163-
app.get("/api/callback/gitlab", handleOAuthCallback);
164-
}
165-
};
166-
167155
module.exports = {
168156
gitlabAuth,
169-
gitlabAuthCallback,
157+
handleOAuthCallbackGitlab,
170158
};

routes/index.js

Lines changed: 15 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@ const eventBadging = require("../event_badging/index.js");
44
const github_helpers = require("../providers/github/APICalls.js");
55
const gitlab_helpers = require("../providers/gitlab/APICalls.js");
66
const { getAllEvents } = require("../database/controllers/event.controller.js");
7-
const {
8-
githubAuth,
9-
githubAuthCallback,
10-
githubApp,
11-
gitlabAuth,
12-
gitlabAuthCallback,
13-
} = require("../providers/index.js");
7+
const { githubAuth, githubApp, gitlabAuth } = require("../providers/index.js");
8+
const { handleOAuthCallback } = require("../providers/github/auth.js");
9+
const { handleOAuthCallbackGitlab } = require("../providers/gitlab/auth.js");
1410

1511
/**
1612
* Redirects the user to the GitHub OAuth login page for authentication.
@@ -160,116 +156,18 @@ const setupRoutes = (app) => {
160156
app.get("/api/login", login);
161157

162158
//callbacks
163-
app.get("/api/github/callback", async (req, res) => {
164-
const code = req.body.code ?? req.query.code;
165-
166-
let issueTitle;
167-
let markdown;
168-
169-
if (req.query.state) {
170-
const encryptedState = req.query.state;
171-
const formData = decrypt(encryptedState);
172-
const parsedFormData = JSON.parse(formData);
173-
issueTitle = parsedFormData.title;
174-
markdown = convertToMarkdown(parsedFormData.body);
175-
}
176-
177-
const { access_token: accessToken, errors: accessTokenErrors } =
178-
await requestAccessToken(code);
179-
if (accessTokenErrors.length > 0) {
180-
res.status(500).send(accessTokenErrors.join());
181-
return;
182-
}
183-
184-
const octokit = new Octokit({ auth: `${accessToken}` });
185-
186-
if (issueTitle && markdown) {
187-
const { data: issue } = await octokit.rest.issues.create({
188-
owner: "badging",
189-
repo: "event-diversity-and-inclusion",
190-
title: issueTitle,
191-
body: markdown,
192-
});
193-
194-
res.redirect(issue.html_url);
195-
return;
196-
}
197-
198-
// Authenticated user details
199-
const { user_info: userInfo, errors: userInfoErrors } = await getUserInfo(
200-
octokit
201-
);
202-
if (userInfoErrors.length > 0) {
203-
res.status(500).send(userInfoErrors.join());
204-
return;
205-
}
206-
207-
// Save user to database
208-
const savedUser = await saveUser(
209-
userInfo.login,
210-
userInfo.name,
211-
userInfo.email,
212-
userInfo.id,
213-
null
214-
);
215-
if (!savedUser) {
216-
res.status(500).send("Error saving user info");
217-
return;
218-
}
219-
220-
// Public repos they maintain, administer, or own
221-
const { repositories, errors: repositoriesErrors } =
222-
await getUserRepositories(octokit);
223-
if (repositoriesErrors.length > 0) {
224-
res.status(500).send(repositoriesErrors.join());
225-
return;
226-
}
227-
228-
if (process.env.NODE_ENV === "production") {
229-
res.status(200).json({
230-
userId: savedUser.id,
231-
name: savedUser.name,
232-
username: savedUser.login,
233-
email: savedUser.email,
234-
repos: repositories,
235-
provider: "github",
236-
});
237-
} else if (process.env.NODE_ENV === "development") {
238-
res.status(200).send(`
239-
<html>
240-
<head>
241-
<title>Repo List</title>
242-
</head>
243-
<body>
244-
<h1>Welcome ${savedUser.name}</h1>
245-
<h2>Username: ${savedUser.login}</h2>
246-
<h2>Email: ${savedUser.email}</h2>
247-
<form action="/api/repos-to-badge" method="post">
248-
<input type="hidden" name="provider" value="github">
249-
<input type="hidden" name="userId" value="${savedUser.id}">
250-
<h2>Select Repositories:</h2>
251-
${repositories
252-
.map(
253-
(repo) => `
254-
<div>
255-
<input type="checkbox" name="repos[]" value="${repo.id}">
256-
<label for="${repo.id}">${repo.fullName}</label>
257-
</div>
258-
`
259-
)
260-
.join("")}
261-
<br>
262-
<input type="submit" value="Submit">
263-
</form>
264-
</body>
265-
</html>
266-
`);
267-
} else {
268-
res.status(500).send("Unknown process mode");
269-
}
270-
});
271-
githubAuthCallback(app);
272-
gitlabAuthCallback(app);
159+
if (process.env.NODE_ENV === "production") {
160+
app.post("/api/callback/github", handleOAuthCallback);
161+
} else if (process.env.NODE_ENV === "development") {
162+
app.get("/api/callback/github", handleOAuthCallback);
163+
}
164+
165+
if (process.env.NODE_ENV === "production") {
166+
app.post("/api/callback/gitlab", handleOAuthCallbackGitlab);
167+
} else if (process.env.NODE_ENV === "development") {
168+
app.get("/api/callback/gitlab", handleOAuthCallbackGitlab);
169+
}
170+
273171
app.get("/api/badgedRepos", badgedRepos);
274172
app.post("/api/repos-to-badge", reposToBadge);
275173

0 commit comments

Comments
 (0)