-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
189 lines (169 loc) · 5.25 KB
/
index.js
File metadata and controls
189 lines (169 loc) · 5.25 KB
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
const { findUser } = require("../database/controllers/user.controller.js");
const Repo = require("../database/models/repo.model.js");
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, 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.
* @param {*} req - object containing the client req details.
* @param {*} res - object used to send a redirect response.
*/
const login = (req, res) => {
const provider = req.query.provider;
if (provider === "github") {
githubAuth(req, res);
} else if (provider === "gitlab") {
gitlabAuth(req, res);
} else {
res.status(400).send(`Unknown provider: ${provider}`);
}
};
const reposToBadge = async (req, res) => {
const selectedRepos = (await req.body.repos) || [];
const userId = req.body.userId;
const provider = req.body.provider;
const repositoryIds = selectedRepos.map((repo) => repo.id);
if (!provider) {
res.status(400).send("provider missing");
return;
}
if (!userId) {
res.status(400).send("userId missing");
return;
}
let user = null;
try {
user = await findUser(userId);
if (!user) {
res.status(404).json("User not found");
return;
}
} catch (error) {
res.status(500).json("Error fetching user data");
return;
}
// Process the selected repos as needed
if (process.env.NODE_ENV === "development") {
if (provider === "github") {
const results = await github_helpers.scanRepositories(
user.id,
user.name,
user.email,
selectedRepos
);
res.status(200).json({ results });
} else if (provider === "gitlab") {
const results = await gitlab_helpers.scanRepositories(
user.id,
user.name,
user.email,
selectedRepos
);
res.status(200).json({ results });
}
} else if (process.env.NODE_ENV === "production") {
// process the selected repositories in production
if (provider === "github") {
const results = await github_helpers.scanRepositories(
user.id,
user.name,
user.email,
repositoryIds
);
res.status(200).json({ results });
} else if (provider === "gitlab") {
const results = await gitlab_helpers.scanRepositories(
user.id,
user.name,
user.email,
repositoryIds
);
res.status(200).json({ results });
}
} else {
res.status(400).send(`Unknown provider: ${provider}`);
}
};
const badgedRepos = async (req, res) => {
try {
// Use Sequelize to find all repos, excluding the DEICommitSHA field
const repos = await Repo.findAll({
attributes: { exclude: ["DEICommitSHA"] },
});
// Extract the relevant information from the repos
const formattedRepos = repos.map((repo) => ({
id: repo.id,
githubRepoId: repo.githubRepoId,
repoLink: repo.repoLink,
badgeType: repo.badgeType,
attachment: repo.attachment,
createdAt: repo.createdAt,
updatedAt: repo.updatedAt,
userId: repo.userId,
}));
res.json(formattedRepos);
} catch (error) {
res.status(500).json({ message: "Error retrieving repos", error });
}
};
const setupRoutes = (app) => {
app.get("/api", (req, res) => {
try {
res.json({ message: "Project Badging server up and running" });
} catch (error) {
console.error(error);
if (error.statusCode && error.statusCode !== 200) {
res.status(error.statusCode).json({
error: "Error",
message: "our bad, something is wrong with the server configuration",
});
} else {
res.status(500).json({
error: "Internal Server Error",
message: "An unexpected error occurred at our end",
});
}
}
});
app.get("/api/auth/github", (req, res) => {
githubAuth(req, res);
});
// for event badging
app.post("/api/auth/github", (req, res) => {
githubAuth(req, res);
});
app.get("/api/auth/gitlab", (req, res) => {
gitlabAuth(req, res);
});
app.get("/api/login", login);
//callbacks
app.get("/api/callback/github", handleOAuthCallback);
app.get("/api/callback/gitlab", handleOAuthCallbackGitlab);
app.get("/api/badgedRepos", badgedRepos);
app.post("/api/repos-to-badge", reposToBadge);
// github app routes
app.post("/api/event_badging", async (req, res) => {
const {
headers: { "x-github-event": name },
body: payload,
} = req;
const octokit = await githubApp.getInstallationOctokit(
payload.installation.id
);
eventBadging(name, octokit, payload);
console.info(`Received ${name} event from Github`);
res.send("ok");
});
// route to get all events
app.get("/api/badged_events", getAllEvents);
app.get("*", (req, res) => {
res.status(404).send("Endpoint not found or unresponsive");
});
};
module.exports = {
setupRoutes,
};