-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
set base URL for GHES #449
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,14 +61,17 @@ 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 | ||
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} : {} | ||
|
@@ -82,6 +85,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] | ||
|
||
|
@@ -94,7 +98,7 @@ export class GitHub extends Octokit { | |
} | ||
|
||
// Proxy | ||
const agent = GitHub.getProxyAgent(options) | ||
const agent = GitHub.getProxyAgent(defaults.baseUrl, options) | ||
if (agent) { | ||
defaults.request = {agent} | ||
} | ||
|
@@ -119,16 +123,36 @@ 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' | ||
|
||
// Shouldn't be a trailing slash, but remove if so | ||
if (url.endsWith('/')) { | ||
url = url.substr(0, url.length - 1) | ||
} | ||
|
||
// Remove trailing "/graphql" | ||
if (url.toUpperCase().endsWith('/GRAPHQL')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no possibility of a trailing slash is there? Alternative https://stackoverflow.com/a/16750711/775184 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldnt be a trailing slash, i'll trim on launch side when setting the urls There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also not bad to add resiliency here, i added code to trim trailing slash if detected |
||
url = url.substr(0, url.length - '/graphql'.length) | ||
} | ||
return url | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll look before merging whether any unit test can be added. Might be able to verify the expected baseUrl is set and whether proxy is set based on the correct URL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unfortunately doesnt appear to be a way to get the options after it's constructed