Skip to content

Commit

Permalink
set base URL for GHES
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsciple committed May 6, 2020
1 parent 6a744be commit 888957c
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions packages/github/src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,19 @@ export class GitHub extends Octokit {
const token = args[0]
const options = {...args[1]} // Shallow clone - don't mutate the object provided by the caller

// Base URL - GHES or Dotcom
if (!options.baseUrl) {
options.baseUrl = this.getApiBaseUrl()
}

// Auth
const auth = GitHub.getAuthString(token, options)
if (auth) {
options.auth = auth
}

// Proxy
const agent = GitHub.getProxyAgent(options)
const agent = GitHub.getProxyAgent(options.baseUrl, options)
if (agent) {
// Shallow clone - don't mutate the object provided by the caller
options.request = options.request ? {...options.request} : {}
Expand All @@ -82,6 +87,7 @@ export class GitHub extends Octokit {

private static getGraphQL(args: [string, Octokit.Options]): GraphQL {
const defaults: GraphQLRequestParameters = {}
defaults.baseUrl = this.getGraphQLBaseUrl()
const token = args[0]
const options = args[1]

Expand All @@ -94,7 +100,7 @@ export class GitHub extends Octokit {
}

// Proxy
const agent = GitHub.getProxyAgent(options)
const agent = GitHub.getProxyAgent(defaults.baseUrl, options)
if (agent) {
defaults.request = {agent}
}
Expand All @@ -119,16 +125,31 @@ export class GitHub extends Octokit {
}

private static getProxyAgent(
destinationUrl: string,
options: Octokit.Options
): http.Agent | undefined {
if (!options.request?.agent) {
const serverUrl = 'https://api.github.com'
if (httpClient.getProxyUrl(serverUrl)) {
if (httpClient.getProxyUrl(destinationUrl)) {
const hc = new httpClient.HttpClient()
return hc.getAgent(serverUrl)
return hc.getAgent(destinationUrl)
}
}

return undefined
}

private static getApiBaseUrl(): string {
return process.env['GITHUB_API_URL'] || 'https://api.github.com'
}

private static getGraphQLBaseUrl(): string {
let url =
process.env['GITHUB_GRAPHQL_URL'] || 'https://api.github.com/graphql'

// Remove trailing "/graphql"
if (url.toUpperCase().endsWith('/GRAPHQL')) {
url = url.substr(0, url.length - '/graphql'.length)
}
return url
}
}

0 comments on commit 888957c

Please sign in to comment.