Skip to content

Commit

Permalink
Merge pull request #53 from DesmondSanctity/event-badging-submit-api
Browse files Browse the repository at this point in the history
Event badging submit api
  • Loading branch information
adeyinkaoresanya authored Oct 7, 2024
2 parents ad93744 + 430bc62 commit 5fae27e
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 139 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/deploy-staging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: CI/CD

on:
push:
branches:
- "staging"

env:
IMAGE_NAME: "project_badging_staging"

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Remove old Docker images
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
script: |
if docker inspect ${{ env.IMAGE_NAME }} >/dev/null 2>&1; then
docker stop ${{ env.IMAGE_NAME }}
docker rm ${{ env.IMAGE_NAME }}
docker images --filter "reference=${{ env.IMAGE_NAME }}*" -q | xargs docker rmi -f || true
fi
- name: Build Docker image
run: docker build -t ${{ env.IMAGE_NAME }}:${{ github.sha }} .

- name: Create image archive
run: docker save ${{ env.IMAGE_NAME }}:${{ github.sha }} -o badging-staging.tar

- name: Upload image archive using appleboy/scp-action
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
source: badging-staging.tar
target: ~/

- name: Extract and load image on droplet
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
script: |
docker load -i ~/badging-staging.tar
docker run -d \
-p ${{ secrets.PORT }}:${{ secrets.PORT }} \
--env-file /home/${{ secrets.USERNAME }}/staging/.env \
--network host \
--restart=always \
--name ${{ env.IMAGE_NAME }} \
${{ env.IMAGE_NAME }}:${{ github.sha }}
112 changes: 104 additions & 8 deletions providers/github/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,117 @@ 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) {
res.status(500).send(accessTokenErrors.join());
return;
}

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
);
if (userInfoErrors.length > 0) {
res.status(500).send(userInfoErrors.join());
return;
}

// Save user to database
const savedUser = await saveUser(
userInfo.login,
userInfo.name,
userInfo.email,
userInfo.id,
null
);
if (!savedUser) {
res.status(500).send("Error saving user info");
return;
}

// Public repos they maintain, administer, or own
const { repositories, errors: repositoriesErrors } =
await getUserRepositories(octokit);
if (repositoriesErrors.length > 0) {
res.status(500).send(repositoriesErrors.join());
return;
}

/**
* Sets up the provided Express app routes for GitLab
* @param {*} app Express application instance
*/
const githubAuthCallback = (app) => {
if (process.env.NODE_ENV === "production") {
app.post("/api/callback/github", handleOAuthCallback);
res.status(200).json({
userId: savedUser.id,
name: savedUser.name,
username: savedUser.login,
email: savedUser.email,
repos: repositories,
provider: "github",
});
} else if (process.env.NODE_ENV === "development") {
app.get("/api/callback/github", handleOAuthCallback);
res.status(200).send(`
<html>
<head>
<title>Repo List</title>
</head>
<body>
<h1>Welcome ${savedUser.name}</h1>
<h2>Username: ${savedUser.login}</h2>
<h2>Email: ${savedUser.email}</h2>
<form action="/api/repos-to-badge" method="post">
<input type="hidden" name="provider" value="github">
<input type="hidden" name="userId" value="${savedUser.id}">
<h2>Select Repositories:</h2>
${repositories
.map(
(repo) => `
<div>
<input type="checkbox" name="repos[]" value="${repo.id}">
<label for="${repo.id}">${repo.fullName}</label>
</div>
`
)
.join("")}
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
`);
} else {
res.status(500).send("Unknown process mode");
}
};

module.exports = {
githubAuth,
githubAuthCallback,
handleOAuthCallback,
githubApp,
};
16 changes: 2 additions & 14 deletions providers/gitlab/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const requestAccessToken = async (code) => {
* @returns A json object with `user_info` and `errors`
* */

const handleOAuthCallback = async (req, res) => {
const handleOAuthCallbackGitlab = async (req, res) => {
const code = req.body.code ?? req.query.code;

const { access_token: accessToken, errors: accessTokenErrors } =
Expand Down Expand Up @@ -152,19 +152,7 @@ const handleOAuthCallback = async (req, res) => {
}
};

/**
* Sets up the provided Express app routes for GitLab
* @param {*} app Express application instance
*/
const gitlabAuthCallback = (app) => {
if (process.env.NODE_ENV === "production") {
app.post("/api/callback/gitlab", handleOAuthCallback);
} else if (process.env.NODE_ENV === "development") {
app.get("/api/callback/gitlab", handleOAuthCallback);
}
};

module.exports = {
gitlabAuth,
gitlabAuthCallback,
handleOAuthCallbackGitlab,
};
132 changes: 15 additions & 117 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ const eventBadging = require("../event_badging/index.js");
const github_helpers = require("../providers/github/APICalls.js");
const gitlab_helpers = require("../providers/gitlab/APICalls.js");
const { getAllEvents } = require("../database/controllers/event.controller.js");
const {
githubAuth,
githubAuthCallback,
githubApp,
gitlabAuth,
gitlabAuthCallback,
} = require("../providers/index.js");
const { githubAuth, githubApp, gitlabAuth } = require("../providers/index.js");
const { handleOAuthCallback } = require("../providers/github/auth.js");
const { handleOAuthCallbackGitlab } = require("../providers/gitlab/auth.js");

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

//callbacks
app.get("/api/github/callback", 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) {
res.status(500).send(accessTokenErrors.join());
return;
}

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
);
if (userInfoErrors.length > 0) {
res.status(500).send(userInfoErrors.join());
return;
}

// Save user to database
const savedUser = await saveUser(
userInfo.login,
userInfo.name,
userInfo.email,
userInfo.id,
null
);
if (!savedUser) {
res.status(500).send("Error saving user info");
return;
}

// Public repos they maintain, administer, or own
const { repositories, errors: repositoriesErrors } =
await getUserRepositories(octokit);
if (repositoriesErrors.length > 0) {
res.status(500).send(repositoriesErrors.join());
return;
}

if (process.env.NODE_ENV === "production") {
res.status(200).json({
userId: savedUser.id,
name: savedUser.name,
username: savedUser.login,
email: savedUser.email,
repos: repositories,
provider: "github",
});
} else if (process.env.NODE_ENV === "development") {
res.status(200).send(`
<html>
<head>
<title>Repo List</title>
</head>
<body>
<h1>Welcome ${savedUser.name}</h1>
<h2>Username: ${savedUser.login}</h2>
<h2>Email: ${savedUser.email}</h2>
<form action="/api/repos-to-badge" method="post">
<input type="hidden" name="provider" value="github">
<input type="hidden" name="userId" value="${savedUser.id}">
<h2>Select Repositories:</h2>
${repositories
.map(
(repo) => `
<div>
<input type="checkbox" name="repos[]" value="${repo.id}">
<label for="${repo.id}">${repo.fullName}</label>
</div>
`
)
.join("")}
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
`);
} else {
res.status(500).send("Unknown process mode");
}
});
githubAuthCallback(app);
gitlabAuthCallback(app);
if (process.env.NODE_ENV === "production") {
app.post("/api/callback/github", handleOAuthCallback);
} else if (process.env.NODE_ENV === "development") {
app.get("/api/callback/github", handleOAuthCallback);
}

if (process.env.NODE_ENV === "production") {
app.post("/api/callback/gitlab", handleOAuthCallbackGitlab);
} else if (process.env.NODE_ENV === "development") {
app.get("/api/callback/gitlab", handleOAuthCallbackGitlab);
}

app.get("/api/badgedRepos", badgedRepos);
app.post("/api/repos-to-badge", reposToBadge);

Expand Down

0 comments on commit 5fae27e

Please sign in to comment.