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 7, 2020
1 parent 6a744be commit f872dae
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
4 changes: 4 additions & 0 deletions packages/github/RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @actions/github Releases

### 2.2.0

- [Support GHES: Use GITHUB_API_URL and GITHUB_GRAPHQL_URL to determine baseUrl](https://github.com/actions/toolkit/pull/449)

### 2.1.1

- [Use import {Octokit}](https://github.com/actions/toolkit/pull/332)
Expand Down
2 changes: 1 addition & 1 deletion packages/github/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/github/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@actions/github",
"version": "2.1.1",
"version": "2.2.0",
"description": "Actions github lib",
"keywords": [
"github",
Expand Down
29 changes: 24 additions & 5 deletions packages/github/src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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} : {}
Expand All @@ -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]

Expand All @@ -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}
}
Expand All @@ -119,16 +123,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 f872dae

Please sign in to comment.