Skip to content
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@
"devDependencies": {
"@types/chai": "^4.1.4",
"@types/debounce": "^3.0.0",
"@types/graphql": "^14.0.5",
"@types/keytar": "^4.0.1",
"@types/lodash": "^4.14.106",
"@types/markdown-it": "^0.0.5",
Expand Down
41 changes: 27 additions & 14 deletions src/github/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import * as Octokit from '@octokit/rest';
import { ApolloClient, InMemoryCache, NormalizedCacheObject } from 'apollo-boost';
import { ApolloClient, InMemoryCache, NormalizedCacheObject, gql } from 'apollo-boost';
import { setContext } from 'apollo-link-context';
import * as vscode from 'vscode';
import { agent } from '../common/net';
Expand All @@ -25,7 +25,7 @@ const AUTH_INPUT_TOKEN_CMD = 'auth.inputTokenCallback';

export interface GitHub {
octokit: Octokit;
graphql: ApolloClient<NormalizedCacheObject>;
graphql: ApolloClient<NormalizedCacheObject> | null;
}

export class CredentialStore {
Expand Down Expand Up @@ -74,7 +74,7 @@ export class CredentialStore {

if (token) {
if (await server.validate(token)) {
octokit = this.createHub({ host, token });
octokit = await this.createHub({ host, token });
} else {
Logger.debug(`Token is no longer valid for host ${host}.`, 'Authentication');
}
Expand Down Expand Up @@ -137,7 +137,7 @@ export class CredentialStore {
this.willStartLogin(authority);
const login = await server.login();
if (login && login.token) {
octokit = this.createHub(login);
octokit = await this.createHub(login);
await setToken(login.host, login.token, { emit: false });
vscode.window.showInformationMessage(`You are now signed in to ${authority}`);
}
Expand Down Expand Up @@ -174,7 +174,7 @@ export class CredentialStore {
return octokit && (octokit as any).currentUser && (octokit as any).currentUser.login === username;
}

private createHub(creds: IHostConfiguration): GitHub {
private async createHub(creds: IHostConfiguration): Promise<GitHub> {
const baseUrl = `${HostHelper.getApiHost(creds).toString().slice(0, -1)}${HostHelper.getApiPath(creds, '')}`;
let octokit = new Octokit({
agent,
Expand All @@ -187,17 +187,30 @@ export class CredentialStore {
token: creds.token || '',
});

return {
octokit,
graphql: new ApolloClient({
link: link(baseUrl, creds.token || ''),
cache: new InMemoryCache,
defaultOptions: {
query: {
fetchPolicy: 'no-cache'
}
const graphql = new ApolloClient({
link: link(baseUrl, creds.token || ''),
cache: new InMemoryCache,
defaultOptions: {
query: {
fetchPolicy: 'no-cache'
}
}
});

let supportsGraphQL = true;
await graphql.query({ query: gql `query { viewer { login } }` })
.then(result => {
Logger.appendLine(`${baseUrl}: GraphQL support detected`);
Logger.appendLine(JSON.stringify(result, null, 2));
})
.catch(err => {
Logger.appendLine(`${baseUrl}: GraphQL not supported (${err.message})`);
supportsGraphQL = false;
});

return {
octokit,
graphql: supportsGraphQL ? graphql : null,
};
}

Expand Down
8 changes: 4 additions & 4 deletions src/github/githubRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class GitHubRepository implements IGitHubRepository, vscode.Disposable {
}

await this.ensure();
this.commentsProvider = this.supportsGraphQl() ? new PRDocumentCommentProviderGraphQL() : new PRDocumentCommentProvider();
this.commentsProvider = this.supportsGraphQl ? new PRDocumentCommentProviderGraphQL() : new PRDocumentCommentProvider();
this._toDispose.push(vscode.workspace.registerDocumentCommentProvider(this.commentsProvider));
} catch (e) {
console.log(e);
Expand All @@ -72,7 +72,7 @@ export class GitHubRepository implements IGitHubRepository, vscode.Disposable {
constructor(public remote: Remote, private readonly _credentialStore: CredentialStore) {
}

supportsGraphQl(): boolean {
get supportsGraphQl(): boolean {
return !!(this.hub && this.hub.graphql);
}

Expand Down Expand Up @@ -279,7 +279,7 @@ export class GitHubRepository implements IGitHubRepository, vscode.Disposable {
async getPullRequest(id: number): Promise<PullRequestModel | undefined> {
try {
Logger.debug(`Fetch pull request ${id} - enter`, GitHubRepository.ID);
const { octokit, query, remote } = await this.ensure();
const { octokit, query, remote, supportsGraphQl } = await this.ensure();
let prsResult = await octokit.pullRequests.get({
owner: remote.owner,
repo: remote.repositoryName,
Expand All @@ -292,7 +292,7 @@ export class GitHubRepository implements IGitHubRepository, vscode.Disposable {
return;
}

if (this.supportsGraphQl()) {
if (supportsGraphQl) {
const { data } = await query<PullRequestResponse>({
query: queries.PullRequest,
variables: {
Expand Down
6 changes: 3 additions & 3 deletions src/github/pullRequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -666,10 +666,10 @@ export class PullRequestManager {

async getTimelineEvents(pullRequest: PullRequestModel): Promise<TimelineEvent[]> {
Logger.debug(`Fetch timeline events of PR #${pullRequest.prNumber} - enter`, PullRequestManager.ID);
const { octokit, query, remote } = await pullRequest.githubRepository.ensure();
const { octokit, query, remote, supportsGraphQl } = await pullRequest.githubRepository.ensure();

let ret = [];
if (pullRequest.githubRepository.supportsGraphQl()) {
if (supportsGraphQl) {
try {
const { data } = await query<TimelineEventsResponse>({
query: queries.TimelineEvents,
Expand Down Expand Up @@ -794,7 +794,7 @@ export class PullRequestManager {
return undefined;
}

if (!pullRequest.githubRepository.supportsGraphQl()) {
if (!pullRequest.githubRepository.supportsGraphQl) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/github/pullRequestOverview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class PullRequestOverviewPanel {
const isCurrentlyCheckedOut = pullRequestModel.equals(this._pullRequestManager.activePullRequest);
const canEdit = this._pullRequestManager.canEditPullRequest(this._pullRequest);
const defaultMergeMethod = vscode.workspace.getConfiguration('githubPullRequests').get<string>('defaultMergeMethod');
const supportsGraphQl = pullRequestModel.githubRepository.supportsGraphQl();
const supportsGraphQl = pullRequestModel.githubRepository.supportsGraphQl;

this._postMessage({
command: 'pr.initialize',
Expand Down
2 changes: 1 addition & 1 deletion src/view/reviewManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ export class ReviewManager implements vscode.DecorationProvider {
}

private registerCommentProvider() {
const supportsGraphQL = this._prManager.activePullRequest && (this._prManager.activePullRequest as PullRequestModel).githubRepository.supportsGraphQl();
const supportsGraphQL = this._prManager.activePullRequest && (this._prManager.activePullRequest as PullRequestModel).githubRepository.supportsGraphQl;
if (supportsGraphQL) {
this._localToDispose.push(onDidSubmitReview(submittedComments => {
this.updateCommentPendingState(submittedComments);
Expand Down