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

fixed repo_config bug on setup #293

Merged
merged 19 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
811d6ee
fixed repo_config bug on setup
MuskanPaliwal Mar 29, 2024
69d6df3
removed unnecessory new lines
MuskanPaliwal Mar 29, 2024
d6ac835
added `on conflict` condition in insert repo_config query
MuskanPaliwal Mar 29, 2024
20ad19a
Addressed pR comments, tested the operation
MuskanPaliwal Apr 1, 2024
f40340e
removed logs
MuskanPaliwal Apr 1, 2024
b730e21
removed unnecessory return statements
MuskanPaliwal Apr 1, 2024
efaaa0c
created function to remove repo_config for repos that are not in the …
MuskanPaliwal Apr 4, 2024
7add7ca
created just one function to save setup repos in db, handled cases wh…
MuskanPaliwal Apr 4, 2024
690cec3
return 0th element of queryReuslt rows in case of multiple users
MuskanPaliwal Apr 4, 2024
18d2bdd
fixed `install_id` error
MuskanPaliwal Apr 4, 2024
b230788
removed unused import
MuskanPaliwal Apr 4, 2024
2d8c447
Add comment in the query of removeRepoconfigForInstallId function to …
avikalpg Apr 8, 2024
01fc8ff
minor: never nesting in `getUserIdByTopicName`
avikalpg Apr 8, 2024
00c4ddb
minor: Formatting changes and snake_case to camelCase conversion for …
avikalpg Apr 8, 2024
707b1b0
addressed pr comments
MuskanPaliwal Apr 8, 2024
65fd488
Merge branch 'mkp/repo_config_bug_fix_on_setup' of github.com:Alokit-…
MuskanPaliwal Apr 8, 2024
84a2d2d
add catch block to insert repos query in setupRepos.ts
avikalpg Apr 8, 2024
8ce86fe
Merge branch 'mkp/repo_config_bug_fix_on_setup' of github.com:Alokit-…
MuskanPaliwal Apr 8, 2024
afc09ff
Merge branch 'main' of github.com:Alokit-Innovations/vibinex-server i…
avikalpg Apr 8, 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
34 changes: 30 additions & 4 deletions pages/api/dpu/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { SetupReposArgs, removePreviousInstallations, saveSetupReposInDb } from '../../../utils/db/setupRepos';
import { getUserIdByTopicName } from '../../../utils/db/users';
import { insertRepoConfig } from '../../../utils/db/repos';

const setupHandler = async (req: NextApiRequest, res: NextApiResponse) => {
console.info("[setupHandler]Saving setup info in db...");
Expand All @@ -10,17 +12,26 @@ const setupHandler = async (req: NextApiRequest, res: NextApiResponse) => {
return;
}
try {
await removePreviousInstallations(jsonBody.installationId)
await removePreviousInstallations(jsonBody.installationId)
} catch (err) {
console.error(`[setupHandler] Unable to remove previous installations for ${jsonBody.installationId}`, err);
res.status(500).json({"error": "Internal Server Error"});
res.status(500).json({ "error": "Internal Server Error" });
return;
}
// get user_id for the given install_id
const userId = await getUserIdByTopicName(jsonBody.installationId).catch((error: any) => {
console.error("[setupHandler/getUserIdByTopicName] Failed to fetch userId from the database.", error);
});
avikalpg marked this conversation as resolved.
Show resolved Hide resolved
if (!userId) {
console.error(`[setupHandler/getUserIdByTopicName] NO userId found for topic name: ${jsonBody.installationId} from database.`);
res.status(404).json({ "error": "No userId found for given installationId" });
return;
}
const allSetupReposPromises = [];
for (const ownerInfo of jsonBody.info) {
let setupReposArgs: SetupReposArgs = {
repo_owner: ownerInfo.owner,
repo_provider: ownerInfo.provider,
repo_provider: ownerInfo.provider,
repo_names: ownerInfo.repos,
install_id: jsonBody.installationId
}
Expand All @@ -30,8 +41,23 @@ const setupHandler = async (req: NextApiRequest, res: NextApiResponse) => {
});
allSetupReposPromises.push(saveSetupReposPromises);
}
await Promise.all(allSetupReposPromises).then((values) => {
await Promise.all(allSetupReposPromises).then(async (values) => {
console.info("[setupHandler] All setup info saved succesfully...")
// for repos in ownerInfo, save default repo_config value in repo_config table
for (const ownerInfo of jsonBody.info) {
await insertRepoConfig(ownerInfo.owner, ownerInfo.repos, userId, ownerInfo.provider)
.then((queryResponse: any) => {
if (queryResponse) {
console.info(`[setupHandler/insertRepoConfigOnSetup] repo config info saved succesfully for repos: ${ownerInfo.repos} for owner: ${ownerInfo.owner}`);
}
else {
console.error(`[setupHandler/insertRepoConfigOnSetup] Failed to save repo config info for repos: ${ownerInfo.repos} for owner: ${ownerInfo.owner}`);
}
})
.catch((error: Error) => {
console.error(`[setupHandler/insertRepoConfigOnSetup] Failed to save repo config for repos: ${ownerInfo.repos} for owner: ${ownerInfo.owner}`, error);
})
avikalpg marked this conversation as resolved.
Show resolved Hide resolved
}
res.status(200).send("Ok");
return;
}).catch((error) => {
Expand Down
26 changes: 26 additions & 0 deletions utils/db/repos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,29 @@ export const getRepoConfigByUserAndRepo = async (provider: string, repoName: str
}
return userRows[0].config;
}
export const insertRepoConfig = async (repoOwner: string, repoNames: string[], userId: string, provider: string) => {
// Construct the VALUES clause dynamically
const valuesClause = repoNames.map(repoName => `('${repoName}', '${repoOwner}', '${provider}')`).join(',');
const query = `
INSERT INTO repo_config (repo_id, user_id, auto_assign, comment_setting)
SELECT id, $1, true, true
FROM public.repos
WHERE (repo_name, repo_owner, repo_provider) IN (${valuesClause})
ON CONFLICT DO NOTHING
MuskanPaliwal marked this conversation as resolved.
Show resolved Hide resolved
`;
const params = [userId];

const queryIsSuccessful = await conn.query(query, params)
MuskanPaliwal marked this conversation as resolved.
Show resolved Hide resolved
.then((dbResponse) => {
if (dbResponse.rowCount == 0) {
MuskanPaliwal marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
return true;
})
.catch((err: Error) => {
console.error(`[db/insertRepoConfigOnSetup] Could not insert repo config for the repos: ${repoNames}`, { pg_query: query }, err);
return false;
})
return queryIsSuccessful;
}

22 changes: 21 additions & 1 deletion utils/db/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,24 @@ export const getAuthInfoFromDb = async function (user_id: string): Promise<AuthI
return { authInfo: null };
});
return authinfo_promise.authInfo;
}
}

export const getUserIdByTopicName = async function(topic_name: string) {
const query = `SELECT id FROM users WHERE topic_name = $1`;
const params = [topic_name];

const queryResult = await conn.query(query, params).catch((err) => {
console.error(`[getUserIdByTopicName] Query failed: Select from users where topic_name = ${topic_name}`, { pg_query: query }, err);
});

if (queryResult && queryResult.rowCount === 0) {
console.error(`[getUserIdByTopicName] No user found for topicName ${topic_name}`);
return null;
} else if (queryResult && queryResult.rowCount > 1) {
console.error(`[getUserIdByTopicName] Multiple users found for topicName ${topic_name}`);
return null;
MuskanPaliwal marked this conversation as resolved.
Show resolved Hide resolved
} else {
return queryResult?.rows[0].id;
}
}