From cb8ee11ae1c5ef2ccdcfde2457ae65222fff2f79 Mon Sep 17 00:00:00 2001 From: Aditya Sharma <70089590+Aditya-eddy@users.noreply.github.com> Date: Mon, 7 Oct 2024 06:34:57 +0530 Subject: [PATCH] chore:adding a dropdown in appbar for login (#63) * chore:adding a dropdown in appbar for login Signed-off-by: Aditya-eddy * feat: add starring to keploy github while signing Signed-off-by: Hermione Dadheech --------- Signed-off-by: Aditya-eddy Signed-off-by: Hermione Dadheech Co-authored-by: Hermione Dadheech --- package.json | 76 +++++++++++++++++++++++------------------------- src/SignIn.ts | 29 ++++++++++++++++-- src/extension.ts | 69 ++++++++++++++++++++++++++----------------- 3 files changed, 105 insertions(+), 69 deletions(-) diff --git a/package.json b/package.json index 7043d60..e55967a 100644 --- a/package.json +++ b/package.json @@ -55,8 +55,20 @@ } ] }, + "submenus": [ + { + "icon": "$(account)", + "label": "Sign In Options", + "id": "sign_in_submenu" + } + ], "menus": { "view/title": [ + { + "submenu": "sign_in_submenu", + "when": "view == Keploy-Sidebar && keploy.signedIn != true", + "group": "navigation" + }, { "command": "keploy.updateKeploy", "when": "view == Keploy-Sidebar" @@ -77,23 +89,40 @@ "command": "keploy.viewDocumentation", "when": "view == Keploy-Sidebar" }, - { - "command": "keploy.SignInWithOthers", - "group": "navigation", - "when": "keploy.signedIn != true && view == Keploy-Sidebar" - }, { "command": "keploy.SignOut", "group": "navigation", "when": "keploy.signedOut != true && view == Keploy-Sidebar" } + ], + "sign_in_submenu": [ + { + "command": "keploy.SignInWithOthers", + "title": "Sign In with Google" + }, + { + "command": "keploy.SignInWithGithub", + "title": "Sign In with Github" + }, + { + "command": "keploy.SignInWithMicrosoft", + "title": "Sign In with Microsoft" + + } ] }, "commands": [ { "command": "keploy.SignInWithOthers", - "title": "Sign In", - "when": "keploy.signedIn != true && view == Keploy-Sidebar" + "title": "Sign In with Google" + }, + { + "command": "keploy.SignInWithGithub", + "title": "Sign In with GitHub" + }, + { + "command": "keploy.SignInWithMicrosoft", + "title": "Sign In with Microsoft" }, { "command": "keploy.SignOut", @@ -128,39 +157,6 @@ "command": "keploy.viewDocumentation", "title": "View Docs", "when": "view == Keploy-Sidebar" - }, - { - "command": "keploy.generateTests", - "title": "Do Something" - }, - { - "command": "keploy.updateKeploy", - "title": "Update your Keploy", - "when": "view == Keploy-Sidebar" - }, - { - "command": "keploy.KeployVersion", - "title": "View Your Keploy Version", - "when": "view == Keploy-Sidebar" - }, - { - "command": "keploy.getLatestVersion", - "title": "Get Latest Version", - "when": "view == Keploy-Sidebar" - }, - { - "command": "keploy.viewChangeLog", - "title": "View Change Log", - "when": "view == Keploy-Sidebar" - }, - { - "command": "keploy.viewDocumentation", - "title": "View Docs", - "when": "view == Keploy-Sidebar" - }, - { - "command": "keploy.generateTests", - "title": "Do Something" } ] }, diff --git a/src/SignIn.ts b/src/SignIn.ts index 9f04929..bd301b1 100644 --- a/src/SignIn.ts +++ b/src/SignIn.ts @@ -29,17 +29,40 @@ async function fetchGitHubEmail(accessToken: string): Promise { return null; } } +async function starGitHubRepo(accessToken: string, owner: string, repo: string): Promise { + console.log('Starring the repository:', owner, repo, accessToken); + try { + const response = await axios.put(`https://api.github.com/user/starred/${owner}/${repo}`, null, { + headers: { + 'Authorization': `Bearer ${accessToken}`, + 'Accept': 'application/vnd.github+json', + 'Content-Length': '0', + }, + }); + + if (response.status === 204) { + console.log(`Successfully starred the repository`); + } else { + throw new Error(`Failed to star repository. GitHub API responded with status ${response.status}`); + } + } catch (error) { + vscode.window.showErrorMessage(`Failed to star repository: ${error}`); + } +} export async function getGitHubAccessToken() { try { - const session = await vscode.authentication.getSession('github', ['user:email'], { createIfNone: true }); + const session = await vscode.authentication.getSession('github', ['user:email', 'public_repo'], { createIfNone: true }); + if (session) { const accessToken = session.accessToken; console.log('Access Token:', accessToken); // Fetch the user's email const email = await fetchGitHubEmail(accessToken); - console.log('Email:', email); + const owner = 'keploy'; + const repo = 'keploy'; + await starGitHubRepo(accessToken, owner, repo); return { accessToken, email }; } else { @@ -91,7 +114,7 @@ export default async function SignInWithGitHub() { console.log("Received code", code); if (receivedState === state) { // Make a POST request to the backend server to exchange the code for an access token - const backendUrl = `http://localhost:8083/auth/login`; + const backendUrl = `http://api.keploy.io/auth/login`; // vscode.env.openExternal(vscode.Uri.parse(backendUrl)); try { // Await the response from the backend diff --git a/src/extension.ts b/src/extension.ts index e68bda6..7da3aa6 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -215,39 +215,41 @@ export function activate(context: vscode.ExtensionContext) { } else { vscode.commands.executeCommand('setContext', 'keploy.signedOut', true); // Register the sign-in command if not signed in - // let signInCommand = vscode.commands.registerCommand('keploy.SignIn', async () => { - // try { - // const result = await getGitHubAccessToken(); + let signInCommand = vscode.commands.registerCommand('keploy.SignInWithGithub', async () => { + try { + const result = await getGitHubAccessToken(); - // if (result) { - // const { accessToken, email } = result; + if (result) { + const { accessToken, email } = result; - // getInstallationID(); + getInstallationID(); - // // Store the access token in global state - // await context.globalState.update('accessToken', accessToken); + // Store the access token in global state + await context.globalState.update('accessToken', accessToken); - // const { emailID, isValid, error , JwtToken } = await validateFirst(accessToken, "https://api.keploy.io"); + const { emailID, isValid, error , JwtToken } = await validateFirst(accessToken, "https://api.keploy.io"); - // await context.globalState.update('JwtToken', JwtToken); + console.log({emailID , isValid , error , JwtToken}); - // // if (isValid) { - // vscode.window.showInformationMessage('You are now signed in!'); - // vscode.commands.executeCommand('setContext', 'keploy.signedIn', true); - // vscode.commands.executeCommand('setContext', 'keploy.signedOut', false); - // // } else { - // // console.log('Validation failed for the user !'); - // // } + await context.globalState.update('JwtToken', JwtToken); - // } else { - // console.log('Failed to get the session or email.'); - // vscode.window.showInformationMessage('Failed to sign in Keploy!'); - // } - // } catch (error) { - // // console.error('Error during sign-in:', error); - // vscode.window.showInformationMessage('Failed to sign in Keploy!'); - // } - // }); + // if (isValid) { + vscode.window.showInformationMessage('You are now signed in!'); + vscode.commands.executeCommand('setContext', 'keploy.signedIn', true); + vscode.commands.executeCommand('setContext', 'keploy.signedOut', false); + // } else { + // console.log('Validation failed for the user !'); + // } + + } else { + console.log('Failed to get the session or email.'); + vscode.window.showInformationMessage('Failed to sign in Keploy!'); + } + } catch (error) { + // console.error('Error during sign-in:', error); + vscode.window.showInformationMessage('Failed to sign in Keploy!'); + } + }); } @@ -260,9 +262,24 @@ export function activate(context: vscode.ExtensionContext) { } }); + // context.subscriptions.push(signInCommand); context.subscriptions.push(signInWithOthersCommand); + //defining another function for microsoft to redirect because functions with same command name cannot be added in package.json + + let signInWithMicrosoft = vscode.commands.registerCommand('keploy.SignInWithMicrosoft', async () => { + try { + await SignInWithOthers(); // The result will now be handled in the URI handler + } catch (error) { + // console.error('Error during sign-in:', error); + vscode.window.showInformationMessage('Failed to sign in Keploy!'); + } + }); + + context.subscriptions.push(signInWithMicrosoft); + + let signout = vscode.commands.registerCommand('keploy.SignOut', async () => { console.log("logging out");