From adc6552966798c10a05a0ea274afec7005aa7992 Mon Sep 17 00:00:00 2001 From: Johannes Nicolai Date: Mon, 25 Jan 2021 18:25:20 +0100 Subject: [PATCH 1/2] Support GitHub Enterprise Server * parse GITHUB_SERVER_URL if present * accept proper server remotes as well --- dist/index.js | 12 ++++++++++-- src/utils.ts | 20 ++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index 798d6b7..03bd6fc 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1157,8 +1157,16 @@ exports.getRepoPath = getRepoPath; function getRemoteDetail(remoteUrl) { // Parse the protocol and github repository from a URL // e.g. HTTPS, peter-evans/create-pull-request - const httpsUrlPattern = /^https:\/\/.*@?github.com\/(.+\/.+)$/i; - const sshUrlPattern = /^git@github.com:(.+\/.+).git$/i; + let githubServerUrl = process.env['GITHUB_SERVER_URL']; + if (!githubServerUrl) { + githubServerUrl = 'https://github.com'; + } + const githubServerMatch = githubServerUrl.match(/^https?:\/\/(.+)$/i); + if (!githubServerMatch) { + throw new Error('Could not parse GitHub Server name'); + } + const httpsUrlPattern = new RegExp('^https?://.*@?' + githubServerMatch[1] + '/(.+/.+)$', 'i'); + const sshUrlPattern = new RegExp('^git@' + githubServerMatch[1] + ':(.+/.+).git$', 'i'); const httpsMatch = remoteUrl.match(httpsUrlPattern); if (httpsMatch) { return { diff --git a/src/utils.ts b/src/utils.ts index 8b1fde1..eb6aeb7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -39,8 +39,24 @@ interface RemoteDetail { export function getRemoteDetail(remoteUrl: string): RemoteDetail { // Parse the protocol and github repository from a URL // e.g. HTTPS, peter-evans/create-pull-request - const httpsUrlPattern = /^https:\/\/.*@?github.com\/(.+\/.+)$/i - const sshUrlPattern = /^git@github.com:(.+\/.+).git$/i + let githubServerUrl = process.env['GITHUB_SERVER_URL'] + if (!githubServerUrl) { + githubServerUrl = 'https://github.com' + } + + const githubServerMatch = githubServerUrl.match(/^https?:\/\/(.+)$/i) + if (!githubServerMatch) { + throw new Error('Could not parse GitHub Server name') + } + + const httpsUrlPattern = new RegExp( + '^https?://.*@?' + githubServerMatch[1] + '/(.+/.+)$', + 'i' + ) + const sshUrlPattern = new RegExp( + '^git@' + githubServerMatch[1] + ':(.+/.+).git$', + 'i' + ) const httpsMatch = remoteUrl.match(httpsUrlPattern) if (httpsMatch) { From 05bc46786e0035e55e8436f92cdaedb443b80a0c Mon Sep 17 00:00:00 2001 From: Johannes Nicolai Date: Mon, 25 Jan 2021 19:16:19 +0100 Subject: [PATCH 2/2] Support GitHub Server API URL * pass GitHub Server API in Octokkit constructor --- dist/index.js | 8 +++----- src/github-helper.ts | 1 + src/utils.ts | 7 ++----- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/dist/index.js b/dist/index.js index 03bd6fc..8bc520a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -901,6 +901,7 @@ class GitHubHelper { if (token) { options.auth = `${token}`; } + options.baseUrl = process.env['GITHUB_API_URL'] || 'https://api.github.com'; this.octokit = new octokit_client_1.Octokit(options); } parseRepository(repository) { @@ -1157,11 +1158,8 @@ exports.getRepoPath = getRepoPath; function getRemoteDetail(remoteUrl) { // Parse the protocol and github repository from a URL // e.g. HTTPS, peter-evans/create-pull-request - let githubServerUrl = process.env['GITHUB_SERVER_URL']; - if (!githubServerUrl) { - githubServerUrl = 'https://github.com'; - } - const githubServerMatch = githubServerUrl.match(/^https?:\/\/(.+)$/i); + const githubUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com'; + const githubServerMatch = githubUrl.match(/^https?:\/\/(.+)$/i); if (!githubServerMatch) { throw new Error('Could not parse GitHub Server name'); } diff --git a/src/github-helper.ts b/src/github-helper.ts index f031230..db8e015 100644 --- a/src/github-helper.ts +++ b/src/github-helper.ts @@ -23,6 +23,7 @@ export class GitHubHelper { if (token) { options.auth = `${token}` } + options.baseUrl = process.env['GITHUB_API_URL'] || 'https://api.github.com' this.octokit = new Octokit(options) } diff --git a/src/utils.ts b/src/utils.ts index eb6aeb7..4659b04 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -39,12 +39,9 @@ interface RemoteDetail { export function getRemoteDetail(remoteUrl: string): RemoteDetail { // Parse the protocol and github repository from a URL // e.g. HTTPS, peter-evans/create-pull-request - let githubServerUrl = process.env['GITHUB_SERVER_URL'] - if (!githubServerUrl) { - githubServerUrl = 'https://github.com' - } + const githubUrl = process.env['GITHUB_SERVER_URL'] || 'https://github.com' - const githubServerMatch = githubServerUrl.match(/^https?:\/\/(.+)$/i) + const githubServerMatch = githubUrl.match(/^https?:\/\/(.+)$/i) if (!githubServerMatch) { throw new Error('Could not parse GitHub Server name') }