Skip to content

Commit

Permalink
Adds initial workspace API
Browse files Browse the repository at this point in the history
  • Loading branch information
d13 committed Feb 23, 2023
1 parent 1d3345c commit b479590
Show file tree
Hide file tree
Showing 9 changed files with 725 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/container.ts
Expand Up @@ -27,6 +27,7 @@ import { GraphWebview } from './plus/webviews/graph/graphWebview';
import { TimelineWebview } from './plus/webviews/timeline/timelineWebview';
import { TimelineWebviewView } from './plus/webviews/timeline/timelineWebviewView';
import { WorkspacesWebview } from './plus/webviews/workspaces/workspacesWebview';
import { WorkspacesApi } from './plus/workspaces/workspaces';
import { StatusBarController } from './statusbar/statusBarController';
import type { Storage } from './storage';
import { executeCommand } from './system/command';
Expand Down Expand Up @@ -175,6 +176,7 @@ export class Container {
(this._subscriptionAuthentication = new SubscriptionAuthenticationProvider(this, server)),
);
context.subscriptions.push((this._subscription = new SubscriptionService(this, previousVersion)));
context.subscriptions.push((this._workspaces = new WorkspacesApi(this, server)));

context.subscriptions.push((this._git = new GitProviderService(this)));
context.subscriptions.push(new GitFileSystemProvider(this));
Expand Down Expand Up @@ -530,6 +532,11 @@ export class Container {
return this._searchAndCompareView;
}

private _workspaces: WorkspacesApi;
get workspaces() {
return this._workspaces;
}

private _subscription: SubscriptionService;
get subscription() {
return this._subscription;
Expand Down
3 changes: 2 additions & 1 deletion src/env/browser/fetch.ts
Expand Up @@ -11,7 +11,8 @@ declare global {
declare type _BodyInit = BodyInit;
declare type _RequestInit = RequestInit;
declare type _Response = Response;
export type { _BodyInit as BodyInit, _RequestInit as RequestInit, _Response as Response };
declare type _RequestInfo = RequestInfo;
export type { _BodyInit as BodyInit, _RequestInit as RequestInit, _Response as Response, _RequestInfo as RequestInfo };

export function getProxyAgent(_strictSSL?: boolean): HttpsProxyAgent | undefined {
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/env/node/fetch.ts
Expand Up @@ -6,7 +6,7 @@ import { configuration } from '../../configuration';
import { Logger } from '../../logger';

export { fetch };
export type { BodyInit, RequestInit, Response } from 'node-fetch';
export type { BodyInit, RequestInfo, RequestInit, Response } from 'node-fetch';

export function getProxyAgent(strictSSL?: boolean): HttpsProxyAgent | undefined {
let proxyUrl: string | undefined;
Expand Down
3 changes: 2 additions & 1 deletion src/git/remotes/richRemoteProvider.ts
Expand Up @@ -79,7 +79,8 @@ export abstract class RichRemoteProvider extends RemoteProvider {
}

protected _session: AuthenticationSession | null | undefined;
protected session() {
// TODO: exposing this is rough approach for workspaces
session() {
if (this._session === undefined) {
return this.ensureSession(false);
}
Expand Down
42 changes: 39 additions & 3 deletions src/plus/subscription/serverConnection.ts
@@ -1,7 +1,7 @@
import type { CancellationToken, Disposable, StatusBarItem } from 'vscode';
import { CancellationTokenSource, env, StatusBarAlignment, Uri, window } from 'vscode';
import { uuid } from '@env/crypto';
import type { Response } from '@env/fetch';
import type { RequestInfo, RequestInit, Response } from '@env/fetch';
import { fetch, getProxyAgent } from '@env/fetch';
import type { Container } from '../../container';
import { Logger } from '../../logger';
Expand All @@ -12,12 +12,20 @@ import type { DeferredEvent, DeferredEventExecutor } from '../../system/event';
import { promisifyDeferred } from '../../system/event';

export const AuthenticationUriPathPrefix = 'did-authenticate';
// TODO: What user-agent should we use?
const userAgent = 'Visual-Studio-Code-GitLens';

interface AccountInfo {
id: string;
accountName: string;
}

interface GraphQLRequest {
query: string;
operationName?: string;
variables?: Record<string, unknown>;
}

export class ServerConnection implements Disposable {
private _cancellationSource: CancellationTokenSource | undefined;
private _deferredCodeExchanges = new Map<string, DeferredEvent<string>>();
Expand Down Expand Up @@ -72,8 +80,7 @@ export class ServerConnection implements Disposable {
agent: getProxyAgent(),
headers: {
Authorization: `Bearer ${token}`,
// TODO: What user-agent should we use?
'User-Agent': 'Visual-Studio-Code-GitLens',
'User-Agent': userAgent,
},
});
} catch (ex) {
Expand Down Expand Up @@ -243,4 +250,33 @@ export class ServerConnection implements Disposable {
this._statusBarItem = undefined;
}
}

async fetchGraphql(data: GraphQLRequest, token: string, init?: RequestInit) {
return this.fetchCore(Uri.joinPath(this.baseAccountUri, 'api/projects/graphql').toString(), token, {
method: 'POST',
body: JSON.stringify(data),
...init,
});
}

private async fetchCore(url: RequestInfo, token: string, init?: RequestInit): Promise<Response> {
const scope = getLogScope();

try {
const options = {
agent: getProxyAgent(),
...init,
headers: {
Authorization: `Bearer ${token}`,
'User-Agent': userAgent,
'Content-Type': 'application/json',
...init?.headers,
},
};
return await fetch(url, options);
} catch (ex) {
Logger.error(ex, scope);
throw ex;
}
}
}
21 changes: 21 additions & 0 deletions src/plus/webviews/workspaces/workspacesWebview.ts
Expand Up @@ -34,4 +34,25 @@ export class WorkspacesWebview extends WebviewBase<State> {

void setContext(ContextKeys.WorkspacesFocused, focused);
}

private async getWorkspaces() {
try {
const rsp = await this.container.workspaces.getWorkspacesWithPullRequests();
console.log(rsp);
} catch (ex) {
console.log(ex);
}

return {};
}

private async getState(): Promise<State> {
return Promise.resolve({
workspaces: this.getWorkspaces(),
});
}

protected override async includeBootstrap(): Promise<State> {
return this.getState();
}
}

0 comments on commit b479590

Please sign in to comment.