Skip to content
This repository was archived by the owner on Mar 22, 2024. It is now read-only.

Commit 16d38c7

Browse files
committed
ESLint cleanup and .env fix and ip fix
1 parent b6a5731 commit 16d38c7

File tree

4 files changed

+35
-56
lines changed

4 files changed

+35
-56
lines changed

.env.example

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,5 @@ MAIL_PASSWORD=123
3030
MAIL_KEY=1a2b3c
3131

3232
# Github
33-
GITHUB_INSTALLATION_ID=
3433
GITHUB_APP_ID=
35-
GITHUB_APP_CLIENT_ID=
3634
GITHUB_APP_SECRET=

public/scripts/githubAuth.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ window.onload = () => {
33
const url = new URL(window.location)
44
const code = url.searchParams.get('code') // SUysVwZaIztTpdkvVzNe6RCZn3fRqC
55
const state = url.searchParams.get('state')
6-
6+
77
// before auth
88
if (!code) {
99
const randomString = generateRandomString()
1010
localStorage.setItem('oauth-state', randomString)
11-
11+
1212
document.getElementById('login').href += `&state=${btoa(randomString)}`
1313
document.getElementById('login').style.display = 'block'
1414
return
@@ -20,15 +20,15 @@ window.onload = () => {
2020
return
2121
}
2222
document.getElementById('info').innerText = 'Github account linked'
23-
23+
2424
function generateRandomString () {
2525
let randomString = ''
2626
const randomNumber = Math.floor(Math.random() * 10)
27-
27+
2828
for (let i = 0; i < 20 + randomNumber; i++) {
2929
randomString += String.fromCharCode(33 + Math.floor(Math.random() * 94))
3030
}
31-
31+
3232
return randomString
3333
}
3434
}

src/app/Http/GithubController.js

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
const fetch = require('node-fetch')
21
const User = require('../Models/User')
32
const Project = require('../Models/Project')
43
const gitHub = require('../../github/githubApi')
5-
const { request } = require("@octokit/request");
6-
const { Octokit } = require('octokit');
74
const authLink = 'https://github.com/login/oauth/authorize?client_id=7864fe4bf9aed444e764&scope=repo'
85
const TEMP_currentproject = '627380b04dd41fde76c9bf66' // TODO: use URL instead
96

107
async function page (req, res) {
11-
if (req.session.user)
12-
{
8+
if (req.session.user) {
139
if (req.query.code) await handleAuth(req, res)
1410
const project = await Project.findById(TEMP_currentproject)
15-
if (project.categories.development.services && project.categories.development.services.github)
16-
{
11+
if (project.categories.development.services && project.categories.development.services.github) {
1712
const github = project.categories.development.services.github
1813
res.render('project/githubtemp', {
1914
githubName: github.name,
@@ -23,47 +18,45 @@ async function page (req, res) {
2318
return
2419
}
2520
}
26-
res.render('project/githubtemp', {AuthLink: authLink})
21+
res.render('project/githubtemp', { AuthLink: authLink })
2722
}
28-
async function webHookReceiver(req) {
23+
async function webHookReceiver (req) {
2924
try {
3025
// TODO: implement security for github webhook
3126
const push = JSON.parse(req.body.read)
3227
console.log(push)
33-
const project = await Project.findOne({services: {github: push.repository_id}})
28+
const project = await Project.findOne({ services: { github: push.repository_id } })
3429
if (project) {
35-
project.categories.development.services.github.hookMessages.push({ event: push.event, action: push.action})
36-
if (project.categories.development.services.github.hookMessages.length > 10)
37-
project.categories.development.services.github.hookMessages.shift()
30+
project.categories.development.services.github.hookMessages.push({ event: push.event, action: push.action })
31+
if (project.categories.development.services.github.hookMessages.length > 10) { project.categories.development.services.github.hookMessages.shift() }
3832
project.markModified('categories.development.services')
3933
project.save()
4034
} else console.log("Couldn't find a project with " + push.repository_id)
41-
}
42-
catch (e) {
43-
console.log("Webhook error: " + e)
35+
} catch (e) {
36+
console.log('Webhook error: ' + e)
4437
}
4538
}
46-
async function handleAuth(req, res) {
39+
async function handleAuth (req, res) {
4740
try {
4841
const userAuth = await gitHub.auth({
49-
type: "oauth-user",
42+
type: 'oauth-user',
5043
code: req.query.code,
51-
state: req.query.state,
52-
});
44+
state: req.query.state
45+
})
5346
console.log(userAuth)
5447
await putUserInDB(userAuth, req.session.user.username)
5548
await gitHub.setupProject(userAuth, req.session.user, TEMP_currentproject)
5649
// TODO: Handle case where project with that name exists
57-
}
58-
catch (e) {
59-
console.log("Failed discord auth: " + e)
50+
} catch (e) {
51+
console.log('Failed discord auth: ' + e)
6052
}
6153
}
6254

63-
async function putUserInDB(auth, username) {
55+
async function putUserInDB (auth, username) {
6456
const user = await User.findOne({ username: username })
65-
user.services = { ...user.services,
66-
github: {clientId: auth.clientId, clientSecret: auth.clientSecret, token: auth.token}
57+
user.services = {
58+
...user.services,
59+
github: { clientId: auth.clientId, clientSecret: auth.clientSecret, token: auth.token }
6760
}
6861
await user.save()
6962
console.log(`Sucessfully linked ${username} with github account ${auth.clientId}`)

src/github/githubApi.js

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,16 @@ const path = require('path')
22
require('dotenv').config({ path: path.resolve(__dirname, '../.env') })
33
const appID = process.env.GITHUB_APP_ID
44
const appSecret = process.env.GITHUB_APP_SECRET
5-
const { Octokit, App } = require("octokit");
5+
const { Octokit } = require('octokit')
66
const Project = require('../app/Models/Project')
7-
const { createAppAuth } = require("@octokit/auth-app");
8-
const { createOAuthAppAuth } = require("@octokit/auth-oauth-app");
9-
const User = require('../app/Models/User');
10-
const { request } = require('@octokit/request');
7+
const { createOAuthAppAuth } = require('@octokit/auth-oauth-app')
118
const auth = createOAuthAppAuth({
12-
clientType: "oauth-app",
9+
clientType: 'oauth-app',
1310
clientId: appID,
14-
clientSecret: appSecret,
15-
});
11+
clientSecret: appSecret
12+
})
1613

17-
/*start()
18-
async function start() {
19-
20-
const appAuthentication = await auth({
21-
type: "oauth-app",
22-
});
23-
}*/
24-
async function setupProject(userAuthenticationFromWebFlow, user, projectID) {
14+
async function setupProject (userAuthenticationFromWebFlow, user, projectID) {
2515
const project = await Project.findById(projectID)
2616
const octokit = new Octokit({
2717
auth: userAuthenticationFromWebFlow.token
@@ -36,10 +26,9 @@ async function setupProject(userAuthenticationFromWebFlow, user, projectID) {
3626
has_wiki: true
3727
})
3828
console.log(resp.data)
39-
// await createWebHook(resp.data.hooks_url, octokit) //TODO: CANT BE LOCALHOST!!!
4029
await putGitHubInDB(project, resp.data)
4130
}
42-
async function putGitHubInDB(project, data) {
31+
async function putGitHubInDB (project, data) {
4332
const github = {
4433
id: data.id,
4534
name: data.name,
@@ -53,19 +42,18 @@ async function putGitHubInDB(project, data) {
5342
project.markModified('categories.development.services')
5443
project.save()
5544
}
56-
async function createWebHook(hookUrl, octokit) {
57-
await octokit.request('POST ' + hookUrl.split("com")[1], {
45+
async function createWebHook (hookUrl, octokit) {
46+
await octokit.request('POST ' + hookUrl.split('com')[1], {
5847
active: true,
5948
events: [
6049
'push',
6150
'pull_request'
6251
],
6352
config: {
64-
url: 'https://localhost:4000/api/github/webhook', // TODO: THIS CANT BE LOCALHOST!!!
53+
url: 'http://178.128.202.47/api/github/webhook',
6554
content_type: 'json',
6655
insecure_ssl: '0'
6756
}
6857
})
6958
}
70-
module.exports = { auth, setupProject };
71-
59+
module.exports = { auth, setupProject }

0 commit comments

Comments
 (0)