add support for gist.github.com

This commit is contained in:
Daniel Hwang 2020-05-29 15:57:30 -07:00
parent aabbfeb2ce
commit ec00d65c65
No known key found for this signature in database
GPG Key ID: 678563C9BB0E60C0
9 changed files with 69 additions and 16 deletions

View File

@ -40,6 +40,9 @@ Refer [here](https://github.com/actions/checkout/blob/v1/README.md) for previous
# Default: ${{ github.repository }} # Default: ${{ github.repository }}
repository: '' repository: ''
# Gist name with owner. For example, schacon/1
gist: ''
# The branch, tag or SHA to checkout. When checking out the repository that # The branch, tag or SHA to checkout. When checking out the repository that
# triggered a workflow, this defaults to the reference or SHA for that event. # triggered a workflow, this defaults to the reference or SHA for that event.
# Otherwise, defaults to `master`. # Otherwise, defaults to `master`.

View File

@ -767,6 +767,7 @@ async function setup(testName: string): Promise<void> {
repositoryName: 'my-repo', repositoryName: 'my-repo',
repositoryOwner: 'my-org', repositoryOwner: 'my-org',
repositoryPath: '', repositoryPath: '',
isGist: false,
sshKey: sshPath ? 'some ssh private key' : '', sshKey: sshPath ? 'some ssh private key' : '',
sshKnownHosts: '', sshKnownHosts: '',
sshStrict: true sshStrict: true

View File

@ -117,6 +117,13 @@ describe('input-helper tests', () => {
expect(settings.commit).toBeFalsy() expect(settings.commit).toBeFalsy()
}) })
it('sets correct default ref/sha for gist', () => {
inputs.gist = 'some-owner/some-gist'
const settings: IGitSourceSettings = inputHelper.getInputs()
expect(settings.ref).toBe('refs/heads/master')
expect(settings.commit).toBeFalsy()
})
it('sets ref to empty when explicit sha', () => { it('sets ref to empty when explicit sha', () => {
inputs.ref = '1111111111222222222233333333334444444444' inputs.ref = '1111111111222222222233333333334444444444'
const settings: IGitSourceSettings = inputHelper.getInputs() const settings: IGitSourceSettings = inputHelper.getInputs()

View File

@ -4,6 +4,8 @@ inputs:
repository: repository:
description: 'Repository name with owner. For example, actions/checkout' description: 'Repository name with owner. For example, actions/checkout'
default: ${{ github.repository }} default: ${{ github.repository }}
gist:
description: 'Gist name with owner. For example, schacon/1'
ref: ref:
description: > description: >
The branch, tag or SHA to checkout. When checking out the repository that The branch, tag or SHA to checkout. When checking out the repository that

32
dist/index.js vendored
View File

@ -1389,21 +1389,30 @@ const url_1 = __webpack_require__(835);
function getFetchUrl(settings) { function getFetchUrl(settings) {
assert.ok(settings.repositoryOwner, 'settings.repositoryOwner must be defined'); assert.ok(settings.repositoryOwner, 'settings.repositoryOwner must be defined');
assert.ok(settings.repositoryName, 'settings.repositoryName must be defined'); assert.ok(settings.repositoryName, 'settings.repositoryName must be defined');
const serviceUrl = getServerUrl(); const serviceUrl = getServerUrl(settings.isGist);
const encodedOwner = encodeURIComponent(settings.repositoryOwner); const encodedOwner = encodeURIComponent(settings.repositoryOwner);
const encodedName = encodeURIComponent(settings.repositoryName); const encodedName = encodeURIComponent(settings.repositoryName);
let encodedNwo = `${encodedOwner}/${encodedName}`;
if (settings.isGist) {
encodedNwo = encodedName;
}
if (settings.sshKey) { if (settings.sshKey) {
return `git@${serviceUrl.hostname}:${encodedOwner}/${encodedName}.git`; return `git@${serviceUrl.hostname}:${encodedNwo}.git`;
} }
// "origin" is SCHEME://HOSTNAME[:PORT] // "origin" is SCHEME://HOSTNAME[:PORT]
return `${serviceUrl.origin}/${encodedOwner}/${encodedName}`; return `${serviceUrl.origin}/${encodedNwo}`;
} }
exports.getFetchUrl = getFetchUrl; exports.getFetchUrl = getFetchUrl;
function getServerUrl() { function getServerUrl(isGist) {
// todo: remove GITHUB_URL after support for GHES Alpha is no longer needed // todo: remove GITHUB_URL after support for GHES Alpha is no longer needed
return new url_1.URL(process.env['GITHUB_SERVER_URL'] || let serverUrl = new url_1.URL(process.env['GITHUB_SERVER_URL'] ||
process.env['GITHUB_URL'] || process.env['GITHUB_URL'] ||
'https://github.com'); 'https://github.com');
// todo: don't assume subdomain isolation
if (isGist) {
serverUrl.hostname = `gist.${serverUrl.hostname}`;
}
return serverUrl;
} }
exports.getServerUrl = getServerUrl; exports.getServerUrl = getServerUrl;
@ -5418,7 +5427,7 @@ class GitAuthHelper {
this.git = gitCommandManager; this.git = gitCommandManager;
this.settings = gitSourceSettings || {}; this.settings = gitSourceSettings || {};
// Token auth header // Token auth header
const serverUrl = urlHelper.getServerUrl(); const serverUrl = urlHelper.getServerUrl(this.settings.isGist);
this.tokenConfigKey = `http.${serverUrl.origin}/.extraheader`; // "origin" is SCHEME://HOSTNAME[:PORT] this.tokenConfigKey = `http.${serverUrl.origin}/.extraheader`; // "origin" is SCHEME://HOSTNAME[:PORT]
const basicCredential = Buffer.from(`x-access-token:${this.settings.authToken}`, 'utf8').toString('base64'); const basicCredential = Buffer.from(`x-access-token:${this.settings.authToken}`, 'utf8').toString('base64');
core.setSecret(basicCredential); core.setSecret(basicCredential);
@ -14438,15 +14447,22 @@ function getInputs() {
githubWorkspacePath = path.resolve(githubWorkspacePath); githubWorkspacePath = path.resolve(githubWorkspacePath);
core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`); core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`);
fsHelper.directoryExistsSync(githubWorkspacePath, true); fsHelper.directoryExistsSync(githubWorkspacePath, true);
// Gist repository?
result.isGist = !!core.getInput('gist') || false;
core.debug(`isGist = '${result.isGist}'`);
// Qualified repository // Qualified repository
const qualifiedRepository = core.getInput('repository') || let qualifiedRepository = core.getInput('repository') ||
`${github.context.repo.owner}/${github.context.repo.repo}`; `${github.context.repo.owner}/${github.context.repo.repo}`;
if (result.isGist) {
qualifiedRepository = core.getInput('gist');
}
core.debug(`qualified repository = '${qualifiedRepository}'`); core.debug(`qualified repository = '${qualifiedRepository}'`);
const splitRepository = qualifiedRepository.split('/'); const splitRepository = qualifiedRepository.split('/');
if (splitRepository.length !== 2 || if (splitRepository.length !== 2 ||
!splitRepository[0] || !splitRepository[0] ||
!splitRepository[1]) { !splitRepository[1]) {
throw new Error(`Invalid repository '${qualifiedRepository}'. Expected format {owner}/{repo}.`); const model = result.isGist ? 'gist' : 'repository';
throw new Error(`Invalid ${model} '${qualifiedRepository}'. Expected format {owner}/{repo}.`);
} }
result.repositoryOwner = splitRepository[0]; result.repositoryOwner = splitRepository[0];
result.repositoryName = splitRepository[1]; result.repositoryName = splitRepository[1];

View File

@ -51,7 +51,7 @@ class GitAuthHelper {
this.settings = gitSourceSettings || (({} as unknown) as IGitSourceSettings) this.settings = gitSourceSettings || (({} as unknown) as IGitSourceSettings)
// Token auth header // Token auth header
const serverUrl = urlHelper.getServerUrl() const serverUrl = urlHelper.getServerUrl(this.settings.isGist)
this.tokenConfigKey = `http.${serverUrl.origin}/.extraheader` // "origin" is SCHEME://HOSTNAME[:PORT] this.tokenConfigKey = `http.${serverUrl.origin}/.extraheader` // "origin" is SCHEME://HOSTNAME[:PORT]
const basicCredential = Buffer.from( const basicCredential = Buffer.from(
`x-access-token:${this.settings.authToken}`, `x-access-token:${this.settings.authToken}`,

View File

@ -73,4 +73,9 @@ export interface IGitSourceSettings {
* Indicates whether to persist the credentials on disk to enable scripting authenticated git commands * Indicates whether to persist the credentials on disk to enable scripting authenticated git commands
*/ */
persistCredentials: boolean persistCredentials: boolean
/**
* Indicates whether this repository is a gist
*/
isGist: boolean
} }

View File

@ -16,10 +16,17 @@ export function getInputs(): IGitSourceSettings {
core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`) core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`)
fsHelper.directoryExistsSync(githubWorkspacePath, true) fsHelper.directoryExistsSync(githubWorkspacePath, true)
// Gist repository?
result.isGist = !!core.getInput('gist') || false
core.debug(`isGist = '${result.isGist}'`)
// Qualified repository // Qualified repository
const qualifiedRepository = let qualifiedRepository =
core.getInput('repository') || core.getInput('repository') ||
`${github.context.repo.owner}/${github.context.repo.repo}` `${github.context.repo.owner}/${github.context.repo.repo}`
if (result.isGist) {
qualifiedRepository = core.getInput('gist')
}
core.debug(`qualified repository = '${qualifiedRepository}'`) core.debug(`qualified repository = '${qualifiedRepository}'`)
const splitRepository = qualifiedRepository.split('/') const splitRepository = qualifiedRepository.split('/')
if ( if (
@ -27,8 +34,9 @@ export function getInputs(): IGitSourceSettings {
!splitRepository[0] || !splitRepository[0] ||
!splitRepository[1] !splitRepository[1]
) { ) {
const model = result.isGist ? 'gist' : 'repository'
throw new Error( throw new Error(
`Invalid repository '${qualifiedRepository}'. Expected format {owner}/{repo}.` `Invalid ${model} '${qualifiedRepository}'. Expected format {owner}/{repo}.`
) )
} }
result.repositoryOwner = splitRepository[0] result.repositoryOwner = splitRepository[0]

View File

@ -8,22 +8,33 @@ export function getFetchUrl(settings: IGitSourceSettings): string {
'settings.repositoryOwner must be defined' 'settings.repositoryOwner must be defined'
) )
assert.ok(settings.repositoryName, 'settings.repositoryName must be defined') assert.ok(settings.repositoryName, 'settings.repositoryName must be defined')
const serviceUrl = getServerUrl() const serviceUrl = getServerUrl(settings.isGist)
const encodedOwner = encodeURIComponent(settings.repositoryOwner) const encodedOwner = encodeURIComponent(settings.repositoryOwner)
const encodedName = encodeURIComponent(settings.repositoryName) const encodedName = encodeURIComponent(settings.repositoryName)
let encodedNwo = `${encodedOwner}/${encodedName}`
if (settings.isGist) {
encodedNwo = encodedName
}
if (settings.sshKey) { if (settings.sshKey) {
return `git@${serviceUrl.hostname}:${encodedOwner}/${encodedName}.git` return `git@${serviceUrl.hostname}:${encodedNwo}.git`
} }
// "origin" is SCHEME://HOSTNAME[:PORT] // "origin" is SCHEME://HOSTNAME[:PORT]
return `${serviceUrl.origin}/${encodedOwner}/${encodedName}` return `${serviceUrl.origin}/${encodedNwo}`
} }
export function getServerUrl(): URL { export function getServerUrl(isGist: boolean): URL {
// todo: remove GITHUB_URL after support for GHES Alpha is no longer needed // todo: remove GITHUB_URL after support for GHES Alpha is no longer needed
return new URL( let serverUrl = new URL(
process.env['GITHUB_SERVER_URL'] || process.env['GITHUB_SERVER_URL'] ||
process.env['GITHUB_URL'] || process.env['GITHUB_URL'] ||
'https://github.com' 'https://github.com'
) )
// todo: don't assume subdomain isolation
if (isGist) {
serverUrl.hostname = `gist.${serverUrl.hostname}`
}
return serverUrl
} }