This repository has been archived by the owner on Apr 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
222 lines (185 loc) · 5.5 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
require('nko')('z-wqAWVjbCSKa06J');
/**
* Module dependencies.
*/
var
express = require('express'),
routes = require('./routes'),
http = require('http'),
path = require('path'),
sass = require('node-sass'),
ejs = require('ejs'),
ejsLayouts = require('express-ejs-layouts'),
Utils = require('./classes/Utils.js'),
utils = new Utils(),
passport = require('passport'),
GithubStrat = require('passport-github').Strategy;
var isProduction = (process.env.NODE_ENV === 'production');
/* --------------------
// GITHUB SETUP
-----------------------*/
var GITHUB_CLIENT_ID;
var GITHUB_CLIENT_SECRET; // "Ignore Security"
if (!isProduction) {
GITHUB_CLIENT_ID = "546f4d5ead70fc95aa41";
GITHUB_CLIENT_SECRET = "1ddaecee7d8acdb427bb775ec9f8acf312609700"; // "Ignore Security"
} else {
GITHUB_CLIENT_ID = "edf9b749ff3da8d37514";
GITHUB_CLIENT_SECRET = "3ec13feb29ff46146a7d820c8686fb7be2102f5d";
}
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (obj, done) {
done(null, obj);
});
passport.use(new GithubStrat({
clientID: GITHUB_CLIENT_ID,
clientSecret: GITHUB_CLIENT_SECRET,
scope: 'public_repo'
}, function (accessToken, refreshToken, profile, done) {
profile.accessToken = accessToken;
done(null, profile);
}
));
var app = express();
var port = (isProduction ? 80 : 3000);
// development only
if (!isProduction) {
app.use(express.errorHandler());
app.use(
sass.middleware({
src: __dirname,
dest: __dirname + '/public',
debug: true
})
);
}
/* --------------------
// EXPRESS SETUP
-----------------------*/
app.set('port', process.env.PORT || port);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.set('layout', 'default');
app.use(ejsLayouts);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.cookieParser());
app.use(express.session({ secret: 'IgnoreSecurity!' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
var github = require('./routes/github/github'),
repos = require('./routes/github/repos'),
issues = require('./routes/github/issues'),
hooks = require('./routes/github/hooks'),
files = require('./routes/github/files'),
// Gitdo imports
gitdoUsers = require('./routes/users'),
gitdoRepos = require('./routes/repos'),
gitdoIssues = require('./routes/issues'),
// View routes
renderIssues = require('./routes/render/issues');
/**
* Github Routes
*/
// Gihub auth
app.get('/auth/github', passport.authenticate('github'), github.auth);
app.get('/auth/github/callback', passport.authenticate('github', {
failureRedirect: '/login'
}), github.authCallback);
app.get('/auth/logout', github.logout);
// Github api
app.get('/api/getRepos', repos.getReposRoute);
app.post('/api/getRepo', repos.getRepoRoute);
app.post('/api/createIssue', issues.createIssueRoute);
app.post('/api/getIssue', issues.getIssueRoute);
app.post('/api/closeIssue', issues.closeIssueRoute);
app.post('/api/addHook', hooks.addHookRoute);
app.post('/api/removeHook', hooks.removeHookRoute);
app.post('/api/commit', hooks.webHook);
/**
* Gitdo Routes
*/
// User
app.get('/api/user', gitdoUsers.get);
app.post('/api/user', gitdoUsers.create);
// Repos
app.get('/api/repos', gitdoRepos.getAllRoute);
app.get('/api/repo', gitdoRepos.getRoute);
app.post('/api/repo', gitdoRepos.createRoute);
app.delete('/api/repo/:name', gitdoRepos.deleteRoute);
// Issues
app.get('/api/issues', gitdoIssues.getAllRoute);
app.get('/api/issue', gitdoIssues.getRoute);
app.post('/api/issue', gitdoIssues.createRoute); // Also serves as update if number is passed
//app.delete('/api/issue/:name', gitdoIssues.closeRoute);
http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
/**
* Views
*/
app.get('/', function (req, res) {
res.render('index',
{
layout: 'landing-page'
}
);
});
app.get('/repos', function (req, res) {
if (req.user) {
// Get list of repos from GitHub
repos.getRepos(req.user.accessToken, function (err, repos) {
gitdoRepos.getAll(req, res, function (gdRepos) {
//this is the worst but Roman said its okay
var gdReposObj = utils.arrayToObj(gdRepos, "ghid");
for (var i = 0; i < repos.length; i++) {
var id = repos[i].id;
if (gdReposObj[id]) {
repos[i].gitdo = true;
}
}
res.render('repos', {
active: 'repos',
user: {
avatar: req.user._json.avatar_url,
name: req.user.displayName || req.user.username
},
repos: repos
});
});
});
} else {
res.redirect("/");
}
});
app.get('/dashboard', function (req, res) {
if (req.user) {
gitdoRepos.getAll(req, res, function (repos) {
res.render('dashboard', {
active: 'dash',
user: {
avatar: req.user._json.avatar_url,
name: req.user.displayName || req.user.username
},
repos: repos
});
});
} else {
res.redirect("/");
}
});
app.get('/dashboard/:repo', renderIssues);
// if run as root, downgrade to the owner of this file
if (process.getuid() === 0) {
require('fs').stat(__filename, function (err, stats) {
if (err) { return console.error(err); }
process.setuid(stats.uid);
});
}