Skip to content

Commit

Permalink
Merge branch 'main' of github.com:penja/scalr-vscode
Browse files Browse the repository at this point in the history
  • Loading branch information
emocharnik committed Sep 11, 2024
2 parents 2ca72d7 + ab06019 commit 7483975
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 18 deletions.
13 changes: 10 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

## [Unreleased]

## [0.0.4]

### Fixed

- Add better error handling and reduce api call on create vscode session ([#28](https://github.com/Scalr/scalr-vscode/pull/28/files))

## [0.0.3]

### Features
Expand All @@ -14,11 +20,11 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

### Improvements

- Added logs streaming for active runs. ([#22](https://github.com/Scalr/scalr-vscode/pull/22))
- Added logs streaming for active runs. ([#22](https://github.com/Scalr/scalr-vscode/pull/22))

## [0.0.2]

- Updated the Workspaces widget: now users can filter workspaces by environments and perform a search by workspace name or ID. Filters are kept as long as the session is active and applied automatically upon the next launch of VSCode.
- Updated the Workspaces widget: now users can filter workspaces by environments and perform a search by workspace name or ID. Filters are kept as long as the session is active and applied automatically upon the next launch of VSCode.

## [0.0.1]

Expand All @@ -32,6 +38,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

---

[Unreleased]: https://github.com/Scalr/scalr-vscode/compare/v0.0.3...HEAD
[Unreleased]: https://github.com/Scalr/scalr-vscode/compare/v0.0.4...HEAD
[0.0.4]: https://github.com/Scalr/scalr-vscode/releases/tag/v0.0.4
[0.0.3]: https://github.com/Scalr/scalr-vscode/releases/tag/v0.0.3
[0.0.2]: https://github.com/Scalr/scalr-vscode/releases/tag/v0.0.2
28 changes: 28 additions & 0 deletions src/api/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ErrorDocument } from './types.gen';
import * as vscode from 'vscode';

export function getErrorMessage(error: unknown): string {
if (typeof error === 'string') {
return error;
}

if (typeof error === 'object' && error !== null && 'errors' in error) {
const errorDocument = error as ErrorDocument;
if (errorDocument.errors) {
//TODO:ape add the titile to the error type in scalr api
//@ts-expect-error the title is not exposed in the error type but it is in the api
return errorDocument.errors.map((e) => e.title || e.detail).join('\n');
}
}

return 'Unknown error';
}

export function showErrorMessage(error: unknown, prefix: string | undefined = undefined): void {
if (prefix) {
vscode.window.showErrorMessage(prefix + '. ' + getErrorMessage(error));
return;
}

vscode.window.showErrorMessage(getErrorMessage(error));
}
22 changes: 15 additions & 7 deletions src/providers/authenticationProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as vscode from 'vscode';
import { initClient } from '../api/init';
import { getErrorMessage } from '../api/error';
import { getAccounts } from '../api/services.gen';
import { AccountListingDocument, Account, User } from '../api/types.gen';

/* import { getRemoteRepoIdentifiers } from '../git'; TODO: uncomment when we'll be implementing Git-based filters */

export class ScalrSession implements vscode.AuthenticationSession {
Expand Down Expand Up @@ -94,7 +96,7 @@ export class ScalrAuthenticationProvider implements vscode.AuthenticationProvide

return session;
} catch (error) {
await vscode.window.showErrorMessage('Failed to load log: ' + error);
await vscode.window.showErrorMessage('Failed to log in: ' + error);
throw error;
}
}
Expand Down Expand Up @@ -123,28 +125,34 @@ export class ScalrAuthenticationProvider implements vscode.AuthenticationProvide
filter: {
name: accountName,
},
fields: {
accounts: 'name',
},
include: ['owner'],
},
});

if (error || !data) {
throw new Error('Failed to load accounts: ' + error);
throw new Error(getErrorMessage(error));
}

const accounts = data as AccountListingDocument;

if (!accounts.data || accounts.data.length === 0) {
throw new Error('No accounts found');
throw new Error('No accounts found with the provided name ' + accountName);
}

const account = accounts.data[0] as Account;
let email = 'unknown';
let fullName = 'unknown';

if (!accounts.included || accounts.included.length === 0) {
throw new Error('No owner found');
if (accounts.included && accounts.included.length !== 0) {
const owner = accounts.included[0] as User;
email = owner.attributes.email;
fullName = owner.attributes['full-name'] as string;
}

const owner = accounts.included[0] as User;
return new ScalrSession(token, owner.attributes['full-name'] as string, owner.attributes.email, {
return new ScalrSession(token, fullName, email, {
id: account.id as string,
label: account.attributes.name,
});
Expand Down
3 changes: 2 additions & 1 deletion src/providers/logProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vscode from 'vscode';
import { getPlanLog, getApply, getApplyLog, getPlan } from '../api/services.gen';
import { ScalrAuthenticationProvider } from '../providers/authenticationProvider';
import { showErrorMessage } from '../api/error';

export class LogProvider implements vscode.TextDocumentContentProvider, vscode.Disposable {
_onDidChange = new vscode.EventEmitter<vscode.Uri>();
Expand Down Expand Up @@ -65,7 +66,7 @@ export class LogProvider implements vscode.TextDocumentContentProvider, vscode.D
}

if (error || !data || !data.data) {
vscode.window.showErrorMessage(`Failed to fetch status: ${error}`);
showErrorMessage(error, 'Failed to fetch status');
return;
}

Expand Down
9 changes: 5 additions & 4 deletions src/providers/runProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getPlanStatusIcon, getPlanLabel } from './planProvider';
import { WorkspaceItem } from './workspaceProvider';
import { formatDate } from '../date-utils';
import { Pagination } from '../@types/api';
import { showErrorMessage } from '../api/error';

export type RunTreeItem = RunItem | ApplyItem | PlanItem | LoadMoreItem;

Expand Down Expand Up @@ -105,13 +106,13 @@ export class RunTreeDataProvider implements vscode.TreeDataProvider<RunTreeItem>
});

if (error || !data || !data.data) {
vscode.window.showErrorMessage('Failed to queue the run: ' + error);
showErrorMessage(error, 'Failed to queue the run');
return;
}

const planId = data.data.relationships?.plan?.data?.id as string;
if (!planId) {
vscode.window.showErrorMessage('Failed to retrieve the plan ID.');
showErrorMessage(error, 'Failed to retrieve the plan ID');
return;
}

Expand All @@ -122,7 +123,7 @@ export class RunTreeDataProvider implements vscode.TreeDataProvider<RunTreeItem>
});

if (planError || !planData || !planData.data) {
vscode.window.showErrorMessage('Failed to retrieve the plan: ' + planError);
showErrorMessage(error, 'Failed to retrieve the plan');
return;
}

Expand Down Expand Up @@ -195,7 +196,7 @@ export class RunTreeDataProvider implements vscode.TreeDataProvider<RunTreeItem>
});

if (error) {
vscode.window.showErrorMessage('Failed to fetch runs');
showErrorMessage(error, 'Failed to load runs');
return [];
}

Expand Down
14 changes: 11 additions & 3 deletions src/providers/workspaceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ScalrAuthenticationProvider, ScalrSession } from './authenticationProvi
import { getRunStatusIcon, RunTreeDataProvider } from './runProvider';
import { Pagination } from '../@types/api';
import { formatDate } from '../date-utils';
import { showErrorMessage } from '../api/error';

class QuickPickItem implements vscode.QuickPickItem {
constructor(
Expand Down Expand Up @@ -89,6 +90,11 @@ export class WorkspaceTreeDataProvider implements vscode.TreeDataProvider<vscode
this.nextPage = null;
}

resetFilters(): void {
this.filters.clear();
this.applyFilters();
}

refresh(): void {
this.didChangeTreeData.fire();
}
Expand Down Expand Up @@ -132,7 +138,7 @@ export class WorkspaceTreeDataProvider implements vscode.TreeDataProvider<vscode
});

if (error || !data) {
vscode.window.showErrorMessage('Failed to fetch workspaces: ' + error);
showErrorMessage(error, 'Unable to get workspaces');
return [];
}

Expand Down Expand Up @@ -249,7 +255,7 @@ export class WorkspaceTreeDataProvider implements vscode.TreeDataProvider<vscode
});

if (error || !data) {
vscode.window.showErrorMessage('Unable to get environments: ' + error);
showErrorMessage(error, 'Unable to get environments');
return [];
}

Expand All @@ -263,7 +269,9 @@ export class WorkspaceTreeDataProvider implements vscode.TreeDataProvider<vscode
}));
}

dispose() {}
dispose() {
this.ctx.workspaceState.update('workspaceFilters', undefined);
}
}

export class WorkspaceItem extends vscode.TreeItem {
Expand Down
1 change: 1 addition & 0 deletions src/scalr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class ScalrFeature implements vscode.Disposable {

vscode.authentication.onDidChangeSessions((e) => {
if (e.provider.id === ScalrAuthenticationProvider.id) {
workspaceDataProvider.resetFilters();
workspaceDataProvider.reset();
workspaceDataProvider.refresh();
runProvider.reset();
Expand Down

0 comments on commit 7483975

Please sign in to comment.