diff --git a/packages/github/RELEASES.md b/packages/github/RELEASES.md index b56ec46549..00db5e66f1 100644 --- a/packages/github/RELEASES.md +++ b/packages/github/RELEASES.md @@ -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) diff --git a/packages/github/package-lock.json b/packages/github/package-lock.json index 34a0a94afb..a075eac212 100644 --- a/packages/github/package-lock.json +++ b/packages/github/package-lock.json @@ -1,6 +1,6 @@ { "name": "@actions/github", - "version": "2.1.1", + "version": "2.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/packages/github/package.json b/packages/github/package.json index 45582ccc41..ef58523ba6 100644 --- a/packages/github/package.json +++ b/packages/github/package.json @@ -1,6 +1,6 @@ { "name": "@actions/github", - "version": "2.1.1", + "version": "2.2.0", "description": "Actions github lib", "keywords": [ "github", diff --git a/packages/github/src/github.ts b/packages/github/src/github.ts index 515f3700c1..699998dc69 100644 --- a/packages/github/src/github.ts +++ b/packages/github/src/github.ts @@ -61,6 +61,9 @@ 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) { @@ -68,7 +71,7 @@ export class GitHub extends Octokit { } // 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')) { + url = url.substr(0, url.length - '/graphql'.length) + } + return url + } }