Skip to content

Commit 5a5ddb7

Browse files
yulrizkaRachel Macfarlane
authored andcommitted
Fix invalid graphql for github enterprise, Fixes 1381
1 parent f39d815 commit 5a5ddb7

11 files changed

Lines changed: 442 additions & 40 deletions

File tree

preview-src/cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export interface PullRequest {
3838
mergeMethodsAvailability: MergeMethodsAvailability;
3939
supportsGraphQl: boolean;
4040
reviewers: ReviewState[];
41-
isDraft: boolean;
41+
isDraft?: boolean;
4242
}
4343

4444
export function getState(): PullRequest {

src/github/credentials.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { handler as uriHandler } from '../common/uri';
1818
import { createHttpLink } from 'apollo-link-http';
1919
import fetch from 'node-fetch';
2020
import { ITelemetry } from '../common/telemetry';
21+
const defaultSchema = require('./queries.gql');
22+
const enterpriseSchema = require('./enterprise.gql');
2123

2224
const TRY_AGAIN = 'Try again?';
2325
const SIGNIN_COMMAND = 'Sign in';
@@ -29,6 +31,7 @@ const AUTH_INPUT_TOKEN_CMD = 'auth.inputTokenCallback';
2931
export interface GitHub {
3032
octokit: Octokit;
3133
graphql: ApolloClient<NormalizedCacheObject> | null;
34+
schema: any | null;
3235
}
3336

3437
export class CredentialStore implements vscode.Disposable {
@@ -218,8 +221,28 @@ export class CredentialStore implements vscode.Disposable {
218221
}
219222
});
220223

224+
// detect which schema to use, currently only supports github.com & enterprise
225+
let version = '';
226+
const resp = await octokit.request('GET /');
227+
if ('x-github-enterprise-version' in resp.headers) {
228+
version = resp.headers['x-github-enterprise-version'];
229+
}
230+
let schema;
231+
if (version === '') {
232+
// use default github.com schema
233+
schema = defaultSchema;
234+
} else {
235+
schema = enterpriseSchema;
236+
}
237+
238+
let graphQLBaseURL = baseUrl;
239+
if (graphQLBaseURL.endsWith('/api/v3')) {
240+
// handles github enterprise
241+
graphQLBaseURL = baseUrl.replace('/v3', '');
242+
}
243+
221244
const graphql = new ApolloClient({
222-
link: link(baseUrl, creds.token || ''),
245+
link: link(graphQLBaseURL, creds.token || ''),
223246
cache: new InMemoryCache,
224247
defaultOptions: {
225248
query: {
@@ -231,15 +254,15 @@ export class CredentialStore implements vscode.Disposable {
231254
let supportsGraphQL = true;
232255
await graphql.query({ query: gql`query { viewer { login } }` })
233256
.then(result => {
234-
Logger.appendLine(`${baseUrl}: GraphQL support detected`);
257+
Logger.appendLine(`${graphQLBaseURL}: GraphQL support detected`);
235258

236259
/* __GDPR__
237260
"auth.graphql.supported" : {}
238261
*/
239262
this._telemetry.sendTelemetryEvent('auth.graphql.supported');
240263
})
241264
.catch(err => {
242-
Logger.appendLine(`${baseUrl}: GraphQL not supported (${err.message})`);
265+
Logger.appendLine(`${graphQLBaseURL}: GraphQL not supported (${err.message})`);
243266
/* __GDPR__
244267
"auth.graphql.unsupported" : {}
245268
*/
@@ -250,6 +273,7 @@ export class CredentialStore implements vscode.Disposable {
250273
return {
251274
octokit,
252275
graphql: supportsGraphQL ? graphql : null,
276+
schema: schema,
253277
};
254278
}
255279

0 commit comments

Comments
 (0)