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 1 commit
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
Prev Previous commit
Next Next commit
Feat : Added pagination logic to migrations API
  • Loading branch information
joyguptaa committed Jan 15, 2024
commit f1844d6e9b962170d6db8f9d786067e81f89aae3
10 changes: 3 additions & 7 deletions controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -857,20 +857,16 @@ async function usersPatchHandler(req, res) {
}
}
const migrations = async (req, res) => {
const { action } = req.query;
if (action !== "adds-github-id") {
return res.status(400).json({
message: "Invalid action type",
});
}
const { skip = 0, limit } = req.query;

try {
const result = await addGithubUserId();
const result = await addGithubUserId(parseInt(skip), parseInt(limit));
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);
}
};
Expand Down
72 changes: 35 additions & 37 deletions models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ const getNonNickNameSyncedUsers = async () => {
throw err;
}
};
const addGithubUserId = async () => {
const addGithubUserId = async (skip, limit) => {
try {
const usersNotFound = [];
let countUserFound = 0;
Expand All @@ -882,45 +882,43 @@ const addGithubUserId = async () => {
"Basic " + btoa(`${config.get("githubOauth.clientId")}:${config.get("githubOauth.clientSecret")}`),
},
};
const usersSnapshot = await firestore.collection("users").get();
const totalUsers = usersSnapshot.docs.length;
const batchCount = Math.ceil(totalUsers / 500);
const usersSnapshot = await firestore
.collection("users")
.limit(limit)
.offset(skip * limit)
.get();
// Create batch write operations for each batch of documents
for (let i = 0; i < batchCount; i++) {
const batchWrite = firestore.batch();
const batchWrites = [];
for (const userDoc of usersSnapshot.docs.slice(i * 500, (i + 1) * 500)) {
if (userDoc.data().github_user_id) continue;
const githubUsername = userDoc.data().github_id;
const username = userDoc.data().username;
const userId = userDoc.id;
batchWrite.update(userDoc.ref, { github_user_id: null });

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();
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: totalUsers,
totalUsers: usersSnapshot.docs.length,
usersUpdated: countUserFound,
usersNotUpdated: countUserNotFound,
invalidUsersDetails: usersNotFound,
Expand Down
4 changes: 2 additions & 2 deletions test/unit/models/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ describe("users", function () {
it("API should not be accessible to any regular user", function (done) {
chai
.request(app)
.post("/users/migrations?action=adds-github-id")
.post("/users/migrations?action=adds-github-id&skip=0&limit=10")
.set("cookie", `${cookieName}=${userToken}`)
.send()
.end((err, res) => {
Expand Down Expand Up @@ -523,7 +523,7 @@ describe("users", function () {

const res = await chai
.request(app)
.post("/users/migrations?action=adds-github-id")
.post("/users/migrations?action=adds-github-id&skip=0&limit=10")
.set("cookie", `${cookieName}=${superUserToken}`)
.send();

Expand Down