Skip to content

Commit 9c88fe4

Browse files
committed
add support for gist.github.com
1 parent aabbfeb commit 9c88fe4

File tree

10 files changed

+79
-22
lines changed

10 files changed

+79
-22
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Refer [here](https://github.com/actions/checkout/blob/v1/README.md) for previous
4040
# Default: ${{ github.repository }}
4141
repository: ''
4242

43+
# Gist name with owner. For example, schacon/1
44+
gist: ''
45+
4346
# The branch, tag or SHA to checkout. When checking out the repository that
4447
# triggered a workflow, this defaults to the reference or SHA for that event.
4548
# Otherwise, defaults to `master`.

__test__/git-auth-helper.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,7 @@ async function setup(testName: string): Promise<void> {
767767
repositoryName: 'my-repo',
768768
repositoryOwner: 'my-org',
769769
repositoryPath: '',
770+
isGist: false,
770771
sshKey: sshPath ? 'some ssh private key' : '',
771772
sshKnownHosts: '',
772773
sshStrict: true

__test__/input-helper.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ describe('input-helper tests', () => {
117117
expect(settings.commit).toBeFalsy()
118118
})
119119

120+
it('sets correct default ref/sha for gist', () => {
121+
inputs.gist = 'some-owner/some-gist'
122+
const settings: IGitSourceSettings = inputHelper.getInputs()
123+
expect(settings.ref).toBe('refs/heads/master')
124+
expect(settings.commit).toBeFalsy()
125+
})
126+
120127
it('sets ref to empty when explicit sha', () => {
121128
inputs.ref = '1111111111222222222233333333334444444444'
122129
const settings: IGitSourceSettings = inputHelper.getInputs()

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ inputs:
44
repository:
55
description: 'Repository name with owner. For example, actions/checkout'
66
default: ${{ github.repository }}
7+
gist:
8+
description: 'Gist name with owner. For example, schacon/1'
79
ref:
810
description: >
911
The branch, tag or SHA to checkout. When checking out the repository that

dist/index.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,21 +1389,30 @@ const url_1 = __webpack_require__(835);
13891389
function getFetchUrl(settings) {
13901390
assert.ok(settings.repositoryOwner, 'settings.repositoryOwner must be defined');
13911391
assert.ok(settings.repositoryName, 'settings.repositoryName must be defined');
1392-
const serviceUrl = getServerUrl();
1392+
const serviceUrl = getServerUrl(settings.isGist);
13931393
const encodedOwner = encodeURIComponent(settings.repositoryOwner);
13941394
const encodedName = encodeURIComponent(settings.repositoryName);
1395+
let encodedNwo = `${encodedOwner}/${encodedName}`;
1396+
if (settings.isGist) {
1397+
encodedNwo = encodedName;
1398+
}
13951399
if (settings.sshKey) {
1396-
return `git@${serviceUrl.hostname}:${encodedOwner}/${encodedName}.git`;
1400+
return `git@${serviceUrl.hostname}:${encodedNwo}.git`;
13971401
}
13981402
// "origin" is SCHEME://HOSTNAME[:PORT]
1399-
return `${serviceUrl.origin}/${encodedOwner}/${encodedName}`;
1403+
return `${serviceUrl.origin}/${encodedNwo}`;
14001404
}
14011405
exports.getFetchUrl = getFetchUrl;
1402-
function getServerUrl() {
1406+
function getServerUrl(isGist) {
14031407
// todo: remove GITHUB_URL after support for GHES Alpha is no longer needed
1404-
return new url_1.URL(process.env['GITHUB_SERVER_URL'] ||
1408+
let serverUrl = new url_1.URL(process.env['GITHUB_SERVER_URL'] ||
14051409
process.env['GITHUB_URL'] ||
14061410
'https://github.com');
1411+
// todo: don't assume subdomain isolation
1412+
if (isGist) {
1413+
serverUrl.hostname = `gist.${serverUrl.hostname}`;
1414+
}
1415+
return serverUrl;
14071416
}
14081417
exports.getServerUrl = getServerUrl;
14091418

@@ -5418,7 +5427,7 @@ class GitAuthHelper {
54185427
this.git = gitCommandManager;
54195428
this.settings = gitSourceSettings || {};
54205429
// Token auth header
5421-
const serverUrl = urlHelper.getServerUrl();
5430+
const serverUrl = urlHelper.getServerUrl(this.settings.isGist);
54225431
this.tokenConfigKey = `http.${serverUrl.origin}/.extraheader`; // "origin" is SCHEME://HOSTNAME[:PORT]
54235432
const basicCredential = Buffer.from(`x-access-token:${this.settings.authToken}`, 'utf8').toString('base64');
54245433
core.setSecret(basicCredential);
@@ -6163,9 +6172,11 @@ function getSource(settings) {
61636172
const authHelper = gitAuthHelper.createAuthHelper(git, settings);
61646173
try {
61656174
// Configure auth
6166-
core.startGroup('Setting up auth');
6167-
yield authHelper.configureAuth();
6168-
core.endGroup();
6175+
if (!settings.isGist) {
6176+
core.startGroup('Setting up auth');
6177+
yield authHelper.configureAuth();
6178+
core.endGroup();
6179+
}
61696180
// LFS install
61706181
if (settings.lfs) {
61716182
yield git.lfsInstall();
@@ -14438,15 +14449,22 @@ function getInputs() {
1443814449
githubWorkspacePath = path.resolve(githubWorkspacePath);
1443914450
core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`);
1444014451
fsHelper.directoryExistsSync(githubWorkspacePath, true);
14452+
// Gist repository?
14453+
result.isGist = !!core.getInput('gist') || false;
14454+
core.debug(`isGist = '${result.isGist}'`);
1444114455
// Qualified repository
14442-
const qualifiedRepository = core.getInput('repository') ||
14456+
let qualifiedRepository = core.getInput('repository') ||
1444314457
`${github.context.repo.owner}/${github.context.repo.repo}`;
14458+
if (result.isGist) {
14459+
qualifiedRepository = core.getInput('gist');
14460+
}
1444414461
core.debug(`qualified repository = '${qualifiedRepository}'`);
1444514462
const splitRepository = qualifiedRepository.split('/');
1444614463
if (splitRepository.length !== 2 ||
1444714464
!splitRepository[0] ||
1444814465
!splitRepository[1]) {
14449-
throw new Error(`Invalid repository '${qualifiedRepository}'. Expected format {owner}/{repo}.`);
14466+
const model = result.isGist ? 'gist' : 'repository';
14467+
throw new Error(`Invalid ${model} '${qualifiedRepository}'. Expected format {owner}/{repo}.`);
1445014468
}
1445114469
result.repositoryOwner = splitRepository[0];
1445214470
result.repositoryName = splitRepository[1];

src/git-auth-helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class GitAuthHelper {
5151
this.settings = gitSourceSettings || (({} as unknown) as IGitSourceSettings)
5252

5353
// Token auth header
54-
const serverUrl = urlHelper.getServerUrl()
54+
const serverUrl = urlHelper.getServerUrl(this.settings.isGist)
5555
this.tokenConfigKey = `http.${serverUrl.origin}/.extraheader` // "origin" is SCHEME://HOSTNAME[:PORT]
5656
const basicCredential = Buffer.from(
5757
`x-access-token:${this.settings.authToken}`,

src/git-source-provider.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
9999
const authHelper = gitAuthHelper.createAuthHelper(git, settings)
100100
try {
101101
// Configure auth
102-
core.startGroup('Setting up auth')
103-
await authHelper.configureAuth()
104-
core.endGroup()
102+
if (!settings.isGist) {
103+
core.startGroup('Setting up auth')
104+
await authHelper.configureAuth()
105+
core.endGroup()
106+
}
105107

106108
// LFS install
107109
if (settings.lfs) {

src/git-source-settings.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,9 @@ export interface IGitSourceSettings {
7373
* Indicates whether to persist the credentials on disk to enable scripting authenticated git commands
7474
*/
7575
persistCredentials: boolean
76+
77+
/**
78+
* Indicates whether this repository is a gist
79+
*/
80+
isGist: boolean
7681
}

src/input-helper.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,27 @@ export function getInputs(): IGitSourceSettings {
1616
core.debug(`GITHUB_WORKSPACE = '${githubWorkspacePath}'`)
1717
fsHelper.directoryExistsSync(githubWorkspacePath, true)
1818

19+
// Gist repository?
20+
result.isGist = !!core.getInput('gist') || false
21+
core.debug(`isGist = '${result.isGist}'`)
22+
1923
// Qualified repository
20-
const qualifiedRepository =
24+
let qualifiedRepository =
2125
core.getInput('repository') ||
2226
`${github.context.repo.owner}/${github.context.repo.repo}`
27+
if (result.isGist) {
28+
qualifiedRepository = core.getInput('gist')
29+
}
2330
core.debug(`qualified repository = '${qualifiedRepository}'`)
2431
const splitRepository = qualifiedRepository.split('/')
2532
if (
2633
splitRepository.length !== 2 ||
2734
!splitRepository[0] ||
2835
!splitRepository[1]
2936
) {
37+
const model = result.isGist ? 'gist' : 'repository'
3038
throw new Error(
31-
`Invalid repository '${qualifiedRepository}'. Expected format {owner}/{repo}.`
39+
`Invalid ${model} '${qualifiedRepository}'. Expected format {owner}/{repo}.`
3240
)
3341
}
3442
result.repositoryOwner = splitRepository[0]

src/url-helper.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,33 @@ export function getFetchUrl(settings: IGitSourceSettings): string {
88
'settings.repositoryOwner must be defined'
99
)
1010
assert.ok(settings.repositoryName, 'settings.repositoryName must be defined')
11-
const serviceUrl = getServerUrl()
11+
const serviceUrl = getServerUrl(settings.isGist)
1212
const encodedOwner = encodeURIComponent(settings.repositoryOwner)
1313
const encodedName = encodeURIComponent(settings.repositoryName)
14+
let encodedNwo = `${encodedOwner}/${encodedName}`
15+
if (settings.isGist) {
16+
encodedNwo = encodedName
17+
}
1418
if (settings.sshKey) {
15-
return `git@${serviceUrl.hostname}:${encodedOwner}/${encodedName}.git`
19+
return `git@${serviceUrl.hostname}:${encodedNwo}.git`
1620
}
1721

1822
// "origin" is SCHEME://HOSTNAME[:PORT]
19-
return `${serviceUrl.origin}/${encodedOwner}/${encodedName}`
23+
return `${serviceUrl.origin}/${encodedNwo}`
2024
}
2125

22-
export function getServerUrl(): URL {
26+
export function getServerUrl(isGist: boolean): URL {
2327
// todo: remove GITHUB_URL after support for GHES Alpha is no longer needed
24-
return new URL(
28+
let serverUrl = new URL(
2529
process.env['GITHUB_SERVER_URL'] ||
2630
process.env['GITHUB_URL'] ||
2731
'https://github.com'
2832
)
33+
34+
// todo: don't assume subdomain isolation
35+
if (isGist) {
36+
serverUrl.hostname = `gist.${serverUrl.hostname}`
37+
}
38+
39+
return serverUrl
2940
}

0 commit comments

Comments
 (0)