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

add support for gist.github.com #262

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, uses the default branch.
Expand Down
1 change: 1 addition & 0 deletions __test__/git-auth-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,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
Expand Down
7 changes: 7 additions & 0 deletions __test__/input-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ describe('input-helper tests', () => {
)
})

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()
Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 27 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -14517,15 +14526,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];
Expand All @@ -14550,6 +14566,9 @@ function getInputs() {
result.ref = `refs/heads/${result.ref}`;
}
}
if (result.isGist && !result.ref && !result.commit) {
result.ref = 'refs/heads/master';
}
}
// SHA?
else if (result.ref.match(/^[0-9a-fA-F]{40}$/)) {
Expand Down
2 changes: 1 addition & 1 deletion src/git-auth-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down
5 changes: 5 additions & 0 deletions src/git-source-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
16 changes: 14 additions & 2 deletions src/input-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,27 @@ 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 (
splitRepository.length !== 2 ||
!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]
Expand Down Expand Up @@ -68,6 +76,10 @@ export function getInputs(): IGitSourceSettings {
result.ref = `refs/heads/${result.ref}`
}
}

if (result.isGist && !result.ref && !result.commit) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added this back from 00a3be8

let me know if you'd like me to change src/git-source-provider.ts instead.

result.ref = 'refs/heads/master'
}
}
// SHA?
else if (result.ref.match(/^[0-9a-fA-F]{40}$/)) {
Expand Down
21 changes: 16 additions & 5 deletions src/url-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}