From 2202a283a7caa2ec6437b141bb32760b1716ecc2 Mon Sep 17 00:00:00 2001
From: GABeech <george@brokenhaze.com>
Date: Tue, 14 Jun 2022 22:12:53 -0400
Subject: [PATCH 1/2] add config option to set github host

---
 src/git-auth-helper.ts     | 2 +-
 src/git-source-settings.ts | 6 ++++++
 src/url-helper.ts          | 8 ++++++--
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/src/git-auth-helper.ts b/src/git-auth-helper.ts
index 8a1c7c3..48044b6 100644
--- a/src/git-auth-helper.ts
+++ b/src/git-auth-helper.ts
@@ -52,7 +52,7 @@ class GitAuthHelper {
     this.settings = gitSourceSettings || (({} as unknown) as IGitSourceSettings)
 
     // Token auth header
-    const serverUrl = urlHelper.getServerUrl()
+    const serverUrl = urlHelper.getServerUrl(gitSourceSettings?.setHost)
     this.tokenConfigKey = `http.${serverUrl.origin}/.extraheader` // "origin" is SCHEME://HOSTNAME[:PORT]
     const basicCredential = Buffer.from(
       `x-access-token:${this.settings.authToken}`,
diff --git a/src/git-source-settings.ts b/src/git-source-settings.ts
index 6fa3960..b49df90 100644
--- a/src/git-source-settings.ts
+++ b/src/git-source-settings.ts
@@ -83,4 +83,10 @@ export interface IGitSourceSettings {
    * Indicates whether to add repositoryPath as safe.directory in git global config
    */
   setSafeDirectory: boolean
+
+  /**
+   * Set a host to override the automatic detection. Useful when you need to clone
+   * from cloud when running an action on an on prem server
+   */
+  setHost: string | undefined
 }
diff --git a/src/url-helper.ts b/src/url-helper.ts
index 05f1cbd..c703e39 100644
--- a/src/url-helper.ts
+++ b/src/url-helper.ts
@@ -1,6 +1,7 @@
 import * as assert from 'assert'
 import {IGitSourceSettings} from './git-source-settings'
 import {URL} from 'url'
+import { settings } from 'cluster'
 
 export function getFetchUrl(settings: IGitSourceSettings): string {
   assert.ok(
@@ -8,7 +9,7 @@ export function getFetchUrl(settings: IGitSourceSettings): string {
     'settings.repositoryOwner must be defined'
   )
   assert.ok(settings.repositoryName, 'settings.repositoryName must be defined')
-  const serviceUrl = getServerUrl()
+  const serviceUrl = getServerUrl(settings.setHost)
   const encodedOwner = encodeURIComponent(settings.repositoryOwner)
   const encodedName = encodeURIComponent(settings.repositoryName)
   if (settings.sshKey) {
@@ -19,7 +20,10 @@ export function getFetchUrl(settings: IGitSourceSettings): string {
   return `${serviceUrl.origin}/${encodedOwner}/${encodedName}`
 }
 
-export function getServerUrl(): URL {
+export function getServerUrl(configHost: string|undefined = undefined): URL {
+  if (configHost) {
+    return new URL(configHost)
+  }
   // todo: remove GITHUB_URL after support for GHES Alpha is no longer needed
   return new URL(
     process.env['GITHUB_SERVER_URL'] ||

From ea0806b2ec48a0e7669fb8f1ce63a9b5b53297a4 Mon Sep 17 00:00:00 2001
From: GABeech <george@brokenhaze.com>
Date: Tue, 14 Jun 2022 22:18:07 -0400
Subject: [PATCH 2/2] add input for action to override action host

---
 action.yml | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/action.yml b/action.yml
index 96c535e..fd05602 100644
--- a/action.yml
+++ b/action.yml
@@ -71,6 +71,13 @@ inputs:
   set-safe-directory:
     description: Add repository path as safe.directory for Git global config by running `git config --global --add safe.directory <path>`
     default: true
+  
+    setHost:
+      description: >
+        override the github host that is set automatically to the github instance the action was called from. 
+        This is useful if you need to clone from an instance that is not the one your action is running from.  
+        For example, you need to clone a cloud hosted repo from an action run by an on prem instance.
+      required: false
 runs:
   using: node16
   main: dist/index.js