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 }}
repository: ''
# Gist name with owner. For example, schacon/1
gist: ''
# 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.
# Otherwise, defaults to `master`.

View file

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

View file

@ -117,6 +117,13 @@ describe('input-helper tests', () => {
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', () => {
inputs.ref = '1111111111222222222233333333334444444444'
const settings: IGitSourceSettings = inputHelper.getInputs()

View file

@ -4,6 +4,8 @@ inputs:
repository:
description: 'Repository name with owner. For example, actions/checkout'
default: ${{ github.repository }}
gist:
description: 'Gist name with owner. For example, schacon/1'
ref:
description: >
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) {
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.isGist);
const encodedOwner = encodeURIComponent(settings.repositoryOwner);
const encodedName = encodeURIComponent(settings.repositoryName);
let encodedNwo = `${encodedOwner}/${encodedName}`;
if (settings.isGist) {
encodedNwo = encodedName;
}
if (settings.sshKey) {
return `git@${serviceUrl.hostname}:${encodedOwner}/${encodedName}.git`;
return `git@${serviceUrl.hostname}:${encodedNwo}.git`;
}
// "origin" is SCHEME://HOSTNAME[:PORT]
return `${serviceUrl.origin}/${encodedOwner}/${encodedName}`;
return `${serviceUrl.origin}/${encodedNwo}`;
}
exports.getFetchUrl = getFetchUrl;
function getServerUrl() {
function getServerUrl(isGist) {
// 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'] ||
'https://github.com');
// todo: don't assume subdomain isolation
if (isGist) {
serverUrl.hostname = `gist.${serverUrl.hostname}`;
}
return serverUrl;
}
exports.getServerUrl = getServerUrl;
@ -5418,7 +5427,7 @@ class GitAuthHelper {
this.git = gitCommandManager;
this.settings = gitSourceSettings || {};
// 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]
const basicCredential = Buffer.from(`x-access-token:${this.settings.authToken}`, 'utf8').toString('base64');
core.setSecret(basicCredential);
@ -14438,15 +14447,22 @@ function getInputs() {
githubWorkspacePath = path.resolve(githubWorkspacePath);
core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`);
fsHelper.directoryExistsSync(githubWorkspacePath, true);
// Gist repository?
result.isGist = !!core.getInput('gist') || false;
core.debug(`isGist = '${result.isGist}'`);
// Qualified repository
const qualifiedRepository = core.getInput('repository') ||
let qualifiedRepository = core.getInput('repository') ||
`${github.context.repo.owner}/${github.context.repo.repo}`;
if (result.isGist) {
qualifiedRepository = core.getInput('gist');
}
core.debug(`qualified repository = '${qualifiedRepository}'`);
const splitRepository = qualifiedRepository.split('/');
if (splitRepository.length !== 2 ||
!splitRepository[0] ||
!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.repositoryName = splitRepository[1];

View file

@ -51,7 +51,7 @@ class GitAuthHelper {
this.settings = gitSourceSettings || (({} as unknown) as IGitSourceSettings)
// 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]
const basicCredential = Buffer.from(
`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
*/
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}'`)
fsHelper.directoryExistsSync(githubWorkspacePath, true)
// Gist repository?
result.isGist = !!core.getInput('gist') || false
core.debug(`isGist = '${result.isGist}'`)
// Qualified repository
const qualifiedRepository =
let qualifiedRepository =
core.getInput('repository') ||
`${github.context.repo.owner}/${github.context.repo.repo}`
if (result.isGist) {
qualifiedRepository = core.getInput('gist')
}
core.debug(`qualified repository = '${qualifiedRepository}'`)
const splitRepository = qualifiedRepository.split('/')
if (
@ -27,8 +34,9 @@ export function getInputs(): IGitSourceSettings {
!splitRepository[0] ||
!splitRepository[1]
) {
const model = result.isGist ? 'gist' : 'repository'
throw new Error(
`Invalid repository '${qualifiedRepository}'. Expected format {owner}/{repo}.`
`Invalid ${model} '${qualifiedRepository}'. Expected format {owner}/{repo}.`
)
}
result.repositoryOwner = splitRepository[0]

View file

@ -8,22 +8,33 @@ 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.isGist)
const encodedOwner = encodeURIComponent(settings.repositoryOwner)
const encodedName = encodeURIComponent(settings.repositoryName)
let encodedNwo = `${encodedOwner}/${encodedName}`
if (settings.isGist) {
encodedNwo = encodedName
}
if (settings.sshKey) {
return `git@${serviceUrl.hostname}:${encodedOwner}/${encodedName}.git`
return `git@${serviceUrl.hostname}:${encodedNwo}.git`
}
// "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
return new URL(
let serverUrl = new URL(
process.env['GITHUB_SERVER_URL'] ||
process.env['GITHUB_URL'] ||
'https://github.com'
)
// todo: don't assume subdomain isolation
if (isGist) {
serverUrl.hostname = `gist.${serverUrl.hostname}`
}
return serverUrl
}