Skip to content
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

Inject GitHub host to be able to clone from another GitHub instance #922

Merged
merged 3 commits into from Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,4 +1,5 @@
__test__/_temp
_temp/
lib/
node_modules/
node_modules/
.vscode/
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -97,6 +97,11 @@ When Git 2.18 or higher is not in your PATH, falls back to the REST API to downl
# config --global --add safe.directory <path>`
# Default: true
set-safe-directory: ''

# The base URL for the GitHub instance that you are trying to clone from, will use
# environment defaults to fetch from the same instance that the workflow is
# running from unless specified.
Link- marked this conversation as resolved.
Show resolved Hide resolved
github-server-url: ''
```
<!-- end usage -->

Expand Down
50 changes: 36 additions & 14 deletions __test__/git-auth-helper.test.ts
Expand Up @@ -5,8 +5,8 @@ import * as io from '@actions/io'
import * as os from 'os'
import * as path from 'path'
import * as stateHelper from '../lib/state-helper'
import {IGitCommandManager} from '../lib/git-command-manager'
import {IGitSourceSettings} from '../lib/git-source-settings'
import { IGitCommandManager } from '../lib/git-command-manager'
import { IGitSourceSettings } from '../lib/git-source-settings'

const isWindows = process.platform === 'win32'
const testWorkspace = path.join(__dirname, '_temp', 'git-auth-helper')
Expand All @@ -17,9 +17,10 @@ let localGitConfigPath: string
let globalGitConfigPath: string
let runnerTemp: string
let tempHomedir: string
let git: IGitCommandManager & {env: {[key: string]: string}}
let git: IGitCommandManager & { env: { [key: string]: string } }
let settings: IGitSourceSettings
let sshPath: string
let githubServerUrl: string

describe('git-auth-helper tests', () => {
beforeAll(async () => {
Expand All @@ -32,7 +33,7 @@ describe('git-auth-helper tests', () => {

beforeEach(() => {
// Mock setSecret
jest.spyOn(core, 'setSecret').mockImplementation((secret: string) => {})
jest.spyOn(core, 'setSecret').mockImplementation((secret: string) => { })

// Mock error/warning/info/debug
jest.spyOn(core, 'error').mockImplementation(jest.fn())
Expand Down Expand Up @@ -67,11 +68,15 @@ describe('git-auth-helper tests', () => {
}
})

const configureAuth_configuresAuthHeader =
'configureAuth configures auth header'
it(configureAuth_configuresAuthHeader, async () => {
async function testAuthHeader(testName: string, serverUrl: string | undefined = undefined) {
// Arrange
await setup(configureAuth_configuresAuthHeader)
let expectedServerUrl = 'https://github.com'
if (serverUrl) {
githubServerUrl = serverUrl
expectedServerUrl = githubServerUrl
}

await setup(testName)
expect(settings.authToken).toBeTruthy() // sanity check
const authHelper = gitAuthHelper.createAuthHelper(git, settings)

Expand All @@ -88,9 +93,25 @@ describe('git-auth-helper tests', () => {
).toString('base64')
expect(
configContent.indexOf(
`http.https://github.com/.extraheader AUTHORIZATION: basic ${basicCredential}`
`http.${expectedServerUrl}/.extraheader AUTHORIZATION: basic ${basicCredential}`
)
).toBeGreaterThanOrEqual(0)
}

const configureAuth_configuresAuthHeader =
'configureAuth configures auth header'
it(configureAuth_configuresAuthHeader, async () => {
await testAuthHeader(configureAuth_configuresAuthHeader);
})

const configureAuth_AcceptsGitHubServerUrl = 'inject https://my-ghes-server.com as github server url'
it(configureAuth_AcceptsGitHubServerUrl, async () => {
await testAuthHeader(configureAuth_AcceptsGitHubServerUrl, 'https://my-ghes-server.com');
})

const configureAuth_AcceptsGitHubServerUrlSetToGHEC = 'inject https://github.com as github server url'
it(configureAuth_AcceptsGitHubServerUrlSetToGHEC, async () => {
await testAuthHeader(configureAuth_AcceptsGitHubServerUrl, 'https://github.com');
})

const configureAuth_configuresAuthHeaderEvenWhenPersistCredentialsFalse =
Expand Down Expand Up @@ -678,17 +699,17 @@ async function setup(testName: string): Promise<void> {
workspace = path.join(testWorkspace, testName, 'workspace')
runnerTemp = path.join(testWorkspace, testName, 'runner-temp')
tempHomedir = path.join(testWorkspace, testName, 'home-dir')
await fs.promises.mkdir(workspace, {recursive: true})
await fs.promises.mkdir(runnerTemp, {recursive: true})
await fs.promises.mkdir(tempHomedir, {recursive: true})
await fs.promises.mkdir(workspace, { recursive: true })
await fs.promises.mkdir(runnerTemp, { recursive: true })
await fs.promises.mkdir(tempHomedir, { recursive: true })
process.env['RUNNER_TEMP'] = runnerTemp
process.env['HOME'] = tempHomedir

// Create git config
globalGitConfigPath = path.join(tempHomedir, '.gitconfig')
await fs.promises.writeFile(globalGitConfigPath, '')
localGitConfigPath = path.join(workspace, '.git', 'config')
await fs.promises.mkdir(path.dirname(localGitConfigPath), {recursive: true})
await fs.promises.mkdir(path.dirname(localGitConfigPath), { recursive: true })
await fs.promises.writeFile(localGitConfigPath, '')

git = {
Expand Down Expand Up @@ -778,7 +799,8 @@ async function setup(testName: string): Promise<void> {
sshKnownHosts: '',
sshStrict: true,
workflowOrganizationId: 123456,
setSafeDirectory: true
setSafeDirectory: true,
githubServerUrl: githubServerUrl
}
}

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Expand Up @@ -71,6 +71,9 @@ 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
github-server-url:
description: The base URL for the GitHub instance that you are trying to clone from, will use environment defaults to fetch from the same instance that the workflow is running from unless specified.
required: false
runs:
using: node16
main: dist/index.js
Expand Down
114 changes: 87 additions & 27 deletions dist/index.js
Expand Up @@ -1295,6 +1295,51 @@ module.exports._parse = parse;
module.exports._enoent = enoent;


/***/ }),

/***/ 24:
/***/ (function(__unusedmodule, exports, __webpack_require__) {

"use strict";

var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOctokit = exports.Octokit = void 0;
const github = __importStar(__webpack_require__(469));
const url_helper_1 = __webpack_require__(81);
// Centralize all Octokit references by re-exporting
var rest_1 = __webpack_require__(0);
Object.defineProperty(exports, "Octokit", { enumerable: true, get: function () { return rest_1.Octokit; } });
function getOctokit(authToken, opts) {
const options = {
baseUrl: (0, url_helper_1.getServerApiUrl)(opts.baseUrl)
};
if (opts.userAgent) {
options.userAgent = opts.userAgent;
}
return new github.GitHub(authToken, options);
}
exports.getOctokit = getOctokit;


/***/ }),

/***/ 26:
Expand Down Expand Up @@ -1935,13 +1980,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getServerUrl = exports.getFetchUrl = void 0;
exports.isGhes = exports.getServerApiUrl = exports.getServerUrl = exports.getFetchUrl = void 0;
const assert = __importStar(__webpack_require__(357));
const url_1 = __webpack_require__(835);
function getFetchUrl(settings) {
assert.ok(settings.repositoryOwner, 'settings.repositoryOwner must be defined');
assert.ok(settings.repositoryName, 'settings.repositoryName must be defined');
const serviceUrl = getServerUrl();
const serviceUrl = getServerUrl(settings.githubServerUrl);
const encodedOwner = encodeURIComponent(settings.repositoryOwner);
const encodedName = encodeURIComponent(settings.repositoryName);
if (settings.sshKey) {
Expand All @@ -1951,13 +1996,27 @@ function getFetchUrl(settings) {
return `${serviceUrl.origin}/${encodedOwner}/${encodedName}`;
}
exports.getFetchUrl = getFetchUrl;
function getServerUrl() {
// todo: remove GITHUB_URL after support for GHES Alpha is no longer needed
return new url_1.URL(process.env['GITHUB_SERVER_URL'] ||
process.env['GITHUB_URL'] ||
'https://github.com');
function getServerUrl(url) {
let urlValue = url && url.trim().length > 0
? url
: process.env['GITHUB_SERVER_URL'] || 'https://github.com';
return new url_1.URL(urlValue);
}
exports.getServerUrl = getServerUrl;
function getServerApiUrl(url) {
let apiUrl = 'https://api.github.com';
if (isGhes(url)) {
const serverUrl = getServerUrl(url);
apiUrl = new url_1.URL(`${serverUrl.origin}/api/v3`).toString();
}
return apiUrl;
}
exports.getServerApiUrl = getServerApiUrl;
function isGhes(url) {
const ghUrl = getServerUrl(url);
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}
exports.isGhes = isGhes;


/***/ }),
Expand Down Expand Up @@ -4279,9 +4338,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkCommitInfo = exports.testRef = exports.getRefSpec = exports.getRefSpecForAllHistory = exports.getCheckoutInfo = exports.tagsRefSpec = void 0;
const url_1 = __webpack_require__(835);
const core = __importStar(__webpack_require__(470));
const github = __importStar(__webpack_require__(469));
const octokit_helper_1 = __webpack_require__(24);
const url_helper_1 = __webpack_require__(81);
exports.tagsRefSpec = '+refs/tags/*:refs/tags/*';
function getCheckoutInfo(git, ref, commit) {
return __awaiter(this, void 0, void 0, function* () {
Expand Down Expand Up @@ -4431,12 +4491,12 @@ function testRef(git, ref, commit) {
});
}
exports.testRef = testRef;
function checkCommitInfo(token, commitInfo, repositoryOwner, repositoryName, ref, commit) {
function checkCommitInfo(token, commitInfo, repositoryOwner, repositoryName, ref, commit, baseUrl) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
try {
// GHES?
if (isGhes()) {
if ((0, url_helper_1.isGhes)(baseUrl)) {
return;
}
// Auth token?
Expand Down Expand Up @@ -4481,7 +4541,8 @@ function checkCommitInfo(token, commitInfo, repositoryOwner, repositoryName, ref
const actualHeadSha = match[1];
if (actualHeadSha !== expectedHeadSha) {
core.debug(`Expected head sha ${expectedHeadSha}; actual head sha ${actualHeadSha}`);
const octokit = new github.GitHub(token, {
const octokit = (0, octokit_helper_1.getOctokit)(token, {
baseUrl: baseUrl,
userAgent: `actions-checkout-tracepoint/1.0 (code=STALE_MERGE;owner=${repositoryOwner};repo=${repositoryName};pr=${fromPayload('number')};run_id=${process.env['GITHUB_RUN_ID']};expected_head_sha=${expectedHeadSha};actual_head_sha=${actualHeadSha})`
});
yield octokit.repos.get({ owner: repositoryOwner, repo: repositoryName });
Expand All @@ -4507,10 +4568,6 @@ function select(obj, path) {
const key = path.substr(0, i);
return select(obj[key], path.substr(i + 1));
}
function isGhes() {
const ghUrl = new url_1.URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}


/***/ }),
Expand Down Expand Up @@ -6561,7 +6618,7 @@ class GitAuthHelper {
this.git = gitCommandManager;
this.settings = gitSourceSettings || {};
// Token auth header
const serverUrl = urlHelper.getServerUrl();
const serverUrl = urlHelper.getServerUrl(this.settings.githubServerUrl);
this.tokenConfigKey = `http.${serverUrl.origin}/.extraheader`; // "origin" is SCHEME://HOSTNAME[:PORT]
const basicCredential = Buffer.from(`x-access-token:${this.settings.authToken}`, 'utf8').toString('base64');
core.setSecret(basicCredential);
Expand Down Expand Up @@ -7382,7 +7439,7 @@ function getSource(settings) {
else if (settings.sshKey) {
throw new Error(`Input 'ssh-key' not supported when falling back to download using the GitHub REST API. To create a local Git repository instead, add Git ${gitCommandManager.MinimumGitVersion} or higher to the PATH.`);
}
yield githubApiHelper.downloadRepository(settings.authToken, settings.repositoryOwner, settings.repositoryName, settings.ref, settings.commit, settings.repositoryPath);
yield githubApiHelper.downloadRepository(settings.authToken, settings.repositoryOwner, settings.repositoryName, settings.ref, settings.commit, settings.repositoryPath, settings.githubServerUrl);
return;
}
// Save state for POST action
Expand Down Expand Up @@ -7415,7 +7472,7 @@ function getSource(settings) {
settings.ref = yield git.getDefaultBranch(repositoryUrl);
}
else {
settings.ref = yield githubApiHelper.getDefaultBranch(settings.authToken, settings.repositoryOwner, settings.repositoryName);
settings.ref = yield githubApiHelper.getDefaultBranch(settings.authToken, settings.repositoryOwner, settings.repositoryName, settings.githubServerUrl);
}
core.endGroup();
}
Expand Down Expand Up @@ -7481,7 +7538,7 @@ function getSource(settings) {
// Log commit sha
yield git.log1("--format='%H'");
// Check for incorrect pull request merge commit
yield refHelper.checkCommitInfo(settings.authToken, commitInfo, settings.repositoryOwner, settings.repositoryName, settings.ref, settings.commit);
yield refHelper.checkCommitInfo(settings.authToken, commitInfo, settings.repositoryOwner, settings.repositoryName, settings.ref, settings.commit, settings.githubServerUrl);
}
finally {
// Remove auth
Expand Down Expand Up @@ -10966,24 +11023,24 @@ exports.getDefaultBranch = exports.downloadRepository = void 0;
const assert = __importStar(__webpack_require__(357));
const core = __importStar(__webpack_require__(470));
const fs = __importStar(__webpack_require__(747));
const github = __importStar(__webpack_require__(469));
const io = __importStar(__webpack_require__(1));
const path = __importStar(__webpack_require__(622));
const retryHelper = __importStar(__webpack_require__(587));
const toolCache = __importStar(__webpack_require__(533));
const v4_1 = __importDefault(__webpack_require__(826));
const octokit_helper_1 = __webpack_require__(24);
const IS_WINDOWS = process.platform === 'win32';
function downloadRepository(authToken, owner, repo, ref, commit, repositoryPath) {
function downloadRepository(authToken, owner, repo, ref, commit, repositoryPath, baseUrl) {
return __awaiter(this, void 0, void 0, function* () {
// Determine the default branch
if (!ref && !commit) {
core.info('Determining the default branch');
ref = yield getDefaultBranch(authToken, owner, repo);
ref = yield getDefaultBranch(authToken, owner, repo, baseUrl);
}
// Download the archive
let archiveData = yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () {
core.info('Downloading the archive');
return yield downloadArchive(authToken, owner, repo, ref, commit);
return yield downloadArchive(authToken, owner, repo, ref, commit, baseUrl);
}));
// Write archive to disk
core.info('Writing archive to disk');
Expand Down Expand Up @@ -11027,12 +11084,12 @@ exports.downloadRepository = downloadRepository;
/**
* Looks up the default branch name
*/
function getDefaultBranch(authToken, owner, repo) {
function getDefaultBranch(authToken, owner, repo, baseUrl) {
return __awaiter(this, void 0, void 0, function* () {
return yield retryHelper.execute(() => __awaiter(this, void 0, void 0, function* () {
var _a;
core.info('Retrieving the default branch name');
const octokit = new github.GitHub(authToken);
const octokit = (0, octokit_helper_1.getOctokit)(authToken, { baseUrl: baseUrl });
let result;
try {
// Get the default branch from the repo info
Expand Down Expand Up @@ -11062,9 +11119,9 @@ function getDefaultBranch(authToken, owner, repo) {
});
}
exports.getDefaultBranch = getDefaultBranch;
function downloadArchive(authToken, owner, repo, ref, commit) {
function downloadArchive(authToken, owner, repo, ref, commit, baseUrl) {
return __awaiter(this, void 0, void 0, function* () {
const octokit = new github.GitHub(authToken);
const octokit = (0, octokit_helper_1.getOctokit)(authToken, { baseUrl: baseUrl });
const params = {
owner: owner,
repo: repo,
Expand Down Expand Up @@ -17330,6 +17387,9 @@ function getInputs() {
// Set safe.directory in git global config.
result.setSafeDirectory =
(core.getInput('set-safe-directory') || 'true').toUpperCase() === 'TRUE';
// Determine the GitHub URL that the repository is being hosted from
result.githubServerUrl = core.getInput('github-server-url');
core.debug(`GitHub Host URL = ${result.githubServerUrl}`);
return result;
});
}
Expand Down