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

fix: login and install jest if not present #51

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 17 additions & 2 deletions scripts/utg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,23 @@ extension="${sourceFilePath##*.}"

# If the file is a Python file, install pytest-cov
if [ "$extension" = "py" ]; then
echo "Installing pytest-cov..."
pip3 install pytest-cov --break-system-packages
echo "Checking if pytest-cov is installed..."
if ! pip3 show pytest-cov > /dev/null 2>&1; then
echo "pytest-cov is not installed. Installing pytest-cov..."
pip3 install pytest-cov --break-system-packages
else
echo "pytest-cov is already installed."
fi
fi

if [ "$extension" = "js" ] || [ "$extension" = "ts" ]; then
echo "Checking if Jest is installed..."
if ! npm list -g jest > /dev/null 2>&1; then
echo "Jest is not installed. Installing Jest..."
npm install --global jest
else
echo "Jest is already installed."
fi
fi

if [ "$extension" = "go" ]; then
Expand Down
51 changes: 0 additions & 51 deletions src/SignIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,57 +123,6 @@ export async function SignInWithOthers() {
const state = generateRandomState(); // Generate a secure random state
const authUrl = `https://app.keploy.io/signin?vscode=true&state=${state}`;
vscode.env.openExternal(vscode.Uri.parse(authUrl));

return new Promise((resolve, reject) => {
const server = http.createServer(async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}

if (req && req.url && req.url.startsWith('/login/keploy/callback')) {
const url = new URL(req.url, `http://${req.headers.host}`);
const receivedState = url.searchParams.get('state');
const token = url.searchParams.get('token');
console.log("Received state:", receivedState);

if (!receivedState || !token) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Missing state or token' }));
reject(new Error('Missing state or token'));
server.close();
return;
}

try {
// Simulate processing the token
console.log("Processing token...");

res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Token received and processed', token, receivedState }));

// Resolve the promise with the token
resolve(token.toString());
} catch (err) {
console.error('Error processing token:', err);
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Internal Server Error' }));
reject(err);
} finally {
server.close(); // Close the server once the request is handled
}
} else {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Not Found' }));
}
}).listen(12408, () => {
console.log('Server listening on port 12408');
});
});
}


Expand Down
46 changes: 25 additions & 21 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,31 @@ export function activate(context: vscode.ExtensionContext) {
const sidebarProvider = new SidebarProvider(context.extensionUri , context);
context.subscriptions.push(
vscode.window.registerUriHandler({
handleUri(uri: vscode.Uri): vscode.ProviderResult<void> {
vscode.window.showInformationMessage(`URI handler called: ${uri.toString()}`);
async handleUri(uri) {
// Extract the token and state from the URI query parameters
const token = uri.query.split('token=')[1]?.split('&')[0];
const state = uri.query.split('state=')[1];

if (token) {
vscode.window.showInformationMessage(`You are now logged in!`);

await context.globalState.update('JwtToken', token);
await context.globalState.update('SignedOthers', true);

const response = await ValidateSignInWithOthers(token);

if (response) {
vscode.commands.executeCommand('setContext', 'keploy.signedIn', true);
vscode.commands.executeCommand('setContext', 'keploy.signedOut', false);
} else {
vscode.window.showInformationMessage('Token validation failed!');
}
} else {
vscode.window.showInformationMessage('Login failed');
}
}
}),
}),

vscode.window.registerWebviewViewProvider(
"Keploy-Sidebar",
sidebarProvider
Expand Down Expand Up @@ -232,24 +253,7 @@ export function activate(context: vscode.ExtensionContext) {
}
let signInWithOthersCommand = vscode.commands.registerCommand('keploy.SignInWithOthers', async () => {
try {
const result = await SignInWithOthers();
// console.log("result from the Sign in with other , ",result);
const accessToken = result as string; // Assert that result is a string
// console.log('Jwt token:', accessToken);
await context.globalState.update('JwtToken', accessToken);
await context.globalState.update('SignedOthers', true);
const reponse = await ValidateSignInWithOthers(accessToken);



if (Boolean(accessToken)) {
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 !');
vscode.window.showInformationMessage('Failed to sign in Keploy!');
}
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!');
Expand Down
Loading