Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/GitHub duplication #1818

Merged
merged 22 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4017e99
Refactor : Modified callback api to have github user id
joyguptaa Dec 28, 2023
7dacd3e
Feat : Added API to add github_user_id to all users
joyguptaa Dec 28, 2023
8407103
Test : Added test for github_user_id script
joyguptaa Dec 28, 2023
d4ae3ac
Bug : Fixed logic while for checking github_user_id
joyguptaa Dec 28, 2023
9f0cc96
Chore : Minor Fixes
joyguptaa Jan 4, 2024
3a3d361
Refactor : Moved code to models
joyguptaa Jan 5, 2024
3a4c604
Merge branch 'develop' into feat/github-duplication
joyguptaa Jan 5, 2024
a8f3d7e
Test : Added failing mock data
joyguptaa Jan 5, 2024
483527c
Merge branch 'feat/github-duplication' of https://github.com/Real-Dev…
joyguptaa Jan 5, 2024
5787ca3
Merge branch 'develop' into feat/github-duplication
joyguptaa Jan 6, 2024
788f63d
Merge branch 'develop' into feat/github-duplication
joyguptaa Jan 7, 2024
8d70a3a
Merge branch 'develop' into feat/github-duplication
joyguptaa Jan 7, 2024
8aa5d6f
Feat : Updated API params
joyguptaa Jan 15, 2024
f1844d6
Feat : Added pagination logic to migrations API
joyguptaa Jan 15, 2024
78e6600
Merge branch 'feat/github-duplication' of https://github.com/Real-Dev…
joyguptaa Jan 15, 2024
61881fb
Merge branch 'develop' into feat/github-duplication
joyguptaa Jan 16, 2024
eded930
Test : Updated the test case
joyguptaa Jan 16, 2024
8f29087
Feat : Updated query params
joyguptaa Jan 16, 2024
8e1eb5f
Test : Updated test cases for migration api
joyguptaa Jan 16, 2024
1c3af94
Merge branch 'feat/github-duplication' of https://github.com/Real-Dev…
joyguptaa Jan 16, 2024
eacc712
Merge branch 'develop' into feat/github-duplication
joyguptaa Jan 17, 2024
86ae835
Merge branch 'develop' into feat/github-duplication
Ajeyakrishna-k Jan 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const githubAuthCallback = (req, res, next) => {
github_id: user.username,
github_display_name: user.displayName,
github_created_at: Number(new Date(user._json.created_at).getTime()),
github_user_id: user.id,
created_at: Date.now(),
updated_at: Date.now(),
};
Expand Down
17 changes: 16 additions & 1 deletion controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { getPaginationLink, getUsernamesFromPRs, getRoleToUpdate } = require("../
const { setInDiscordFalseScript, setUserDiscordNickname } = require("../services/discordService");
const { generateDiscordProfileImageUrl } = require("../utils/discord-actions");
const { addRoleToUser, getDiscordMembers } = require("../services/discordService");
const { fetchAllUsers } = require("../models/users");
const { fetchAllUsers, addGithubUserId } = require("../models/users");
const { getOverdueTasks } = require("../models/tasks");
const { getQualifiers } = require("../utils/helper");
const { parseSearchQuery } = require("../utils/users");
Expand Down Expand Up @@ -856,6 +856,20 @@ async function usersPatchHandler(req, res) {
return res.boom.badImplementation(INTERNAL_SERVER_ERROR);
}
}
const migrations = async (req, res) => {
const { page = 0, size } = req.query;

try {
const result = await addGithubUserId(parseInt(page), parseInt(size));
return res.status(200).json({
message: "Result of migration",
data: result,
});
} catch (error) {
logger.error(`Internal Server Error: ${error}`);
return res.boom.badImplementation(INTERNAL_SERVER_ERROR);
}
};

module.exports = {
verifyUser,
Expand Down Expand Up @@ -886,4 +900,5 @@ module.exports = {
updateDiscordUserNickname,
archiveUserIfNotInDiscord,
usersPatchHandler,
migrations,
};
20 changes: 19 additions & 1 deletion middlewares/validators/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,24 @@ const validateGenerateUsernameQuery = async (req, res, next) => {
res.boom.badRequest("Invalid Query Parameters Passed");
}
};

const migrationsValidator = async (req, res, next) => {
Ajeyakrishna-k marked this conversation as resolved.
Show resolved Hide resolved
const { action, page, size } = req.query;
const schema = joi
.object()
.strict()
.keys({
page: joi.number(),
action: joi.string().valid("adds-github-id").required(),
size: joi.number().min(1).max(500).required(),
});
try {
await schema.validateAsync({ action, page: parseInt(page), size: parseInt(size) });
next();
} catch (error) {
logger.error("Invalid Query Parameters Passed", error);
res.boom.badRequest("Invalid Query Parameters Passed");
}
};
module.exports = {
updateUser,
updateProfileURL,
Expand All @@ -331,4 +348,5 @@ module.exports = {
validateUpdateRoles,
validateUsersPatchHandler,
validateGenerateUsernameQuery,
migrationsValidator,
};
72 changes: 69 additions & 3 deletions models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ const addOrUpdate = async (userData, userId = null) => {
}

// userId is null, Add or Update user
const user = await userModel.where("github_id", "==", userData.github_id).limit(1).get();
if (!user.empty) {
let user;
if (userData.github_user_id) {
user = await userModel.where("github_user_id", "==", userData.github_user_id).limit(1).get();
}
if (!user || (user && user.empty)) {
user = await userModel.where("github_id", "==", userData.github_id).limit(1).get();
}
if (user && !user.empty) {
Ajeyakrishna-k marked this conversation as resolved.
Show resolved Hide resolved
await userModel.doc(user.docs[0].id).set(userData, { merge: true });

return {
isNewUser: false,
userId: user.docs[0].id,
Expand Down Expand Up @@ -863,6 +868,66 @@ const getNonNickNameSyncedUsers = async () => {
throw err;
}
};
const addGithubUserId = async (page, size) => {
try {
const usersNotFound = [];
let countUserFound = 0;
let countUserNotFound = 0;

const requestOptions = {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization:
"Basic " + btoa(`${config.get("githubOauth.clientId")}:${config.get("githubOauth.clientSecret")}`),
},
};
const usersSnapshot = await firestore
.collection("users")
.limit(size)
.offset(page * size)
.get();
// Create batch write operations for each batch of documents
const batchWrite = firestore.batch();
const batchWrites = [];
for (const userDoc of usersSnapshot.docs) {
if (userDoc.data().github_user_id) continue;
const githubUsername = userDoc.data().github_id;
const username = userDoc.data().username;
const userId = userDoc.id;
const getUserDetails = fetch(`https://api.github.com/users/${githubUsername}`, requestOptions)
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => {
const githubUserId = data.id;
batchWrite.update(userDoc.ref, { github_user_id: `${githubUserId}` });
countUserFound++;
})
.catch((error) => {
countUserNotFound++;
const invalidUsers = { userId, username, githubUsername };
usersNotFound.push(invalidUsers);
logger.error("An error occurred at fetch:", error);
});
batchWrites.push(getUserDetails);
}
await Promise.all(batchWrites);
await batchWrite.commit();
return {
totalUsers: usersSnapshot.docs.length,
usersUpdated: countUserFound,
usersNotUpdated: countUserNotFound,
invalidUsersDetails: usersNotFound,
};
} catch (error) {
logger.error(`Error while Updating all users: ${error}`);
throw Error(error);
}
};

module.exports = {
addOrUpdate,
Expand Down Expand Up @@ -893,4 +958,5 @@ module.exports = {
fetchUsersListForMultipleValues,
fetchUserForKeyValue,
getNonNickNameSyncedUsers,
addGithubUserId,
};
8 changes: 8 additions & 0 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,12 @@
router.patch("/rejectDiff", authenticate, authorizeRoles([SUPERUSER]), users.rejectProfileDiff);
router.patch("/:userId", authenticate, authorizeRoles([SUPERUSER]), users.updateUser);
router.get("/suggestedUsers/:skillId", authenticate, authorizeRoles([SUPERUSER]), users.getSuggestedUsers);
// WARNING!! - One time Script/Route to do migration
router.post(
"/migrations",
authenticate,

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.
This route handler performs
authorization
, but is not rate-limited.
This route handler performs
authorization
, but is not rate-limited.
authorizeRoles([SUPERUSER]),
userValidator.migrationsValidator,
users.migrations
);
module.exports = router;
191 changes: 191 additions & 0 deletions test/fixtures/user/prodUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
const prodUsers = [
{
profileURL: "https://my.realdevsquad.com/identity",
incompleteUserDetails: false,
discordJoinedAt: "2023-04-02T18:30:30.476000+00:00",
discordId: "1040700289348542566",
roles: {
archived: false,
in_discord: true,
member: true,
super_user: true,
},
last_name: "Prakash",
linkedin_id: "dev-amit-prakash",
picture: {
publicId: "profile/65QiTlqudZfDk3i5W9WO/iiti1pudbvbo9huvbqmk",
url: "https://res.cloudinary.com/realdevsquad/image/upload/v1691542131/profile/65QiTlqudZfDk3i5W9WO/iiti1pudbvbo9huvbqmk.jpg",
},
github_created_at: 1514318626000,
github_display_name: "Amit Prakash",
github_id: "iamitprakash",
company: "Boeing",
designation: "SDE-2",
twitter_id: "twts_tejas",
first_name: "Amit",
username: "amitprakash",
updated_at: 1702144542601,
created_at: 1702144542601,
},
{
incompleteUserDetails: false,
discordJoinedAt: "2023-04-13T05:49:42.784000+00:00",
website: "www.google.com",
discordId: "1095941231403597854",
roles: {
archived: false,
maven: true,
in_discord: true,
},
last_name: "Karanth",
linkedin_id: "ajeyakrishna-karanth-a229a8191",
yoe: 1,
company: "VTU",
designation: "SDE",
twitter_id: "AjeyakrishnaK",
first_name: "Ajeyakrishna",
username: "ajeyakrishna",
github_id: "Ajeyakrishna-k",
github_created_at: 1643691501000,
github_display_name: "Ajeyakrishna",
updated_at: 1702964506393,
created_at: 1702964506393,
},
{
profileURL: "https://ajoys-profile-service.onrender.com",
incompleteUserDetails: false,
website: "https://ajoykumardas.vercel.app",
discordJoinedAt: "2023-08-24T12:45:12.013000+00:00",
discordId: "661243030065643530",
roles: {
archived: false,
in_discord: true,
},
last_name: "Das",
linkedin_id: "https://www.linkedin.com/in/ajoy-kumar-das/",
picture: {
publicId: "profile/NMKS92IoTCGZzPopZu7b/s7pevs5aw7oh6yh2pj03",
url: "https://res.cloudinary.com/realdevsquad/image/upload/v1693214755/profile/NMKS92IoTCGZzPopZu7b/s7pevs5aw7oh6yh2pj03.png",
},
github_created_at: 1625806111000,
github_display_name: "Ajoy Kumar Das",
github_id: "ajoykumardas12",
company: "Government College of Engineering and Textile Technology, Serampore",
designation: "Graduate",
twitter_id: "ajoykdas",
first_name: "Ajoy Kumar",
username: "ajoy-kumar",
profileStatus: "VERIFIED",
updated_at: 1702905921930,
created_at: 1702905921930,
},

{
incompleteUserDetails: false,
discordJoinedAt: "2023-02-26T09:24:14.587000+00:00",
discordId: "799600467218923521",
roles: {
archived: false,
in_discord: true,
},
last_name: "Pawaskar",
linkedin_id: "Anish Pawaskar",
github_created_at: 1473931782000,
github_display_name: "Anish Pawaskar",
github_id: "anishpawaskar",
company: "Kirti M. Doongursee College",
designation: "Student",
twitter_id: "anish_pawaskar",
first_name: "Anish",
username: "anish-pawaskar",
updated_at: 1703474831637,
created_at: 1703474831637,
},
{
profileURL: "https://my.realdevsquad.com/identity",
discordJoinedAt: "2020-02-01T08:33:38.278000+00:00",
roles: {
archived: false,
in_discord: true,
member: true,
super_user: true,
admin: true,
},
yoe: "8",
github_created_at: 1341655281000,
company: "Amazon",
twitter_id: "ankushdharkar",
first_name: "Ankush",
" instagram_id": "ankushdharkar",
website: "NA",
incompleteUserDetails: false,
discordId: "154585730465660929",
linkedin_id: "ankushdharkar",
last_name: "Dharkar",
picture: {
publicId: "profile/XAF7rSUvk4p0d098qWYS/me40uk7taytbjaa67mhe",
url: "https://res.cloudinary.com/realdevsquad/image/upload/v1692058952/profile/XAF7rSUvk4p0d098qWYS/me40uk7taytbjaa67mhe.jpg",
},
github_display_name: "Ankush Dharkar",
company_name: "Amazon",
github_id: "ankushdharkar",
designation: "SDE",
status: "idle",
username: "ankush",
updated_at: 1703548515652,
created_at: 1703548515652,
},
{
profileURL: "https://my-profile-service-m5d2.onrender.com",
incompleteUserDetails: false,
discordJoinedAt: "2023-08-28T06:04:20.282000+00:00",
discordId: "751127804678242364",
roles: {
archived: false,
in_discord: true,
},
last_name: "Gupta",
linkedin_id: "ardourApex",
picture: {
publicId: "profile/AAM0MZxZXEfWKmfdYOUp/rsa1twaw1movhmmlyfsd",
url: "https://res.cloudinary.com/realdevsquad/image/upload/v1693248423/profile/AAM0MZxZXEfWKmfdYOUp/rsa1twaw1movhmmlyfsd.jpg",
},
github_created_at: 1570647377000,
github_display_name: "Joy",
github_id: "ardourApeX",
company: "Test Unity",
designation: "SDE1",
twitter_id: "ardourApex",
first_name: "Joy",
username: "joy-gupta",
profileStatus: "VERIFIED",
updated_at: 1703486457963,
created_at: 1703486457963,
},
{
id: "4CtxyyUtdwwMKkrdY6pK",
incompleteUserDetails: false,
discordJoinedAt: "2023-04-06T04:06:30.483000+00:00",
discordId: "498037866707025932",
roles: {
archived: false,
in_discord: true,
},
last_name: "kamat",
linkedin_id: "https://www.linkedin.com/in/vishal-kamat-312b02267/",
yoe: 0,
picture: {
publicId: "profile/4CtxyyUtdwwMKkrdY6pK/ov3b7begsmlgpncqu0ey",
url: "https://res.cloudinary.com/realdevsquad/image/upload/v1685934397/profile/4CtxyyUtdwwMKkrdY6pK/ov3b7begsmlgpncqu0ey.jpg",
},
github_created_at: 1552124195000,
github_display_name: "Vishal kamat",
github_id: "meanwhile7",
twitter_id: "vishalkrkamat",
first_name: "Vishal",
username: "vishal-#34",
updated_at: 1702893387292,
created_at: 1702893387292,
},
];
module.exports = prodUsers;
Loading
Loading