Skip to content

Commit

Permalink
Merge pull request #118 from crazy-max/git-remoteSha-token
Browse files Browse the repository at this point in the history
git: use GitHub API for remoteSha if token provided
  • Loading branch information
crazy-max authored Jun 12, 2023
2 parents acd3c9d + b2a51dd commit d0ee813
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
5 changes: 4 additions & 1 deletion __tests__/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ describe('isInsideWorkTree', () => {
});

describe('remoteSha', () => {
it('returns git remote sha', async () => {
it('returns sha using git ls-remote', async () => {
expect(await Git.remoteSha('https://github.com/docker/buildx.git', 'refs/pull/648/head')).toEqual('f11797113e5a9b86bd976329c5dbb8a8bfdfadfa');
});
it('returns sha using github api', async () => {
expect(await Git.remoteSha('https://github.com/docker/buildx.git', 'refs/pull/648/head', process.env.GITHUB_TOKEN)).toEqual('f11797113e5a9b86bd976329c5dbb8a8bfdfadfa');
});
});

describe('remoteURL', () => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@actions/http-client": "^2.0.1",
"@actions/io": "^1.1.2",
"@actions/tool-cache": "^2.0.1",
"@octokit/plugin-rest-endpoint-methods": "^7.2.1",
"async-retry": "^1.3.3",
"csv-parse": "^5.4.0",
"handlebars": "^4.7.7",
Expand Down
2 changes: 1 addition & 1 deletion src/buildx/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class Install {
if (ref.match(/^[0-9a-fA-F]{40}$/)) {
vspec = ref;
} else {
vspec = await Git.remoteSha(repo, ref);
vspec = await Git.remoteSha(repo, ref, process.env.GIT_AUTH_TOKEN);
}
core.debug(`Install.build: tool version spec ${vspec}`);

Expand Down
30 changes: 29 additions & 1 deletion src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@
* limitations under the License.
*/

import * as core from '@actions/core';
import {Octokit} from '@octokit/core';
import {restEndpointMethods} from '@octokit/plugin-rest-endpoint-methods';

import {Exec} from './exec';
import {GitHub} from './github';
import {Context} from '@actions/github/lib/context';

import {Context as GitContext} from './types/git';

export class Git {
Expand All @@ -36,7 +42,29 @@ export class Git {
});
}

public static async remoteSha(repo: string, ref: string): Promise<string> {
public static async remoteSha(repo: string, ref: string, token?: string): Promise<string> {
const repoMatch = repo.match(/github.com\/([^/]+)\/([^/]+?)(?:\.git)?(\/|$)/);
// if we have a token and this is a GitHub repo we can use the GitHub API
if (token && repoMatch) {
core.setSecret(token);
const octokit = new (Octokit.plugin(restEndpointMethods).defaults({
baseUrl: GitHub.apiURL
}))({auth: token});
const [owner, repoName] = repoMatch.slice(1, 3);
try {
return (
await octokit.rest.repos.listCommits({
owner: owner,
repo: repoName,
sha: ref,
per_page: 1
})
).data[0].sha;
} catch (e) {
throw new Error(`Cannot find remote ref for ${repo}#${ref}: ${e.message}`);
}
}
// otherwise we fall back to git ls-remote
return await Git.exec(['ls-remote', repo, ref]).then(out => {
const [rsha] = out.split(/[\s\t]/);
if (rsha.length == 0) {
Expand Down
28 changes: 28 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ __metadata:
"@actions/http-client": ^2.0.1
"@actions/io": ^1.1.2
"@actions/tool-cache": ^2.0.1
"@octokit/plugin-rest-endpoint-methods": ^7.2.1
"@types/csv-parse": ^1.2.2
"@types/node": ^16.18.21
"@types/semver": ^7.5.0
Expand Down Expand Up @@ -1348,6 +1349,13 @@ __metadata:
languageName: node
linkType: hard

"@octokit/openapi-types@npm:^18.0.0":
version: 18.0.0
resolution: "@octokit/openapi-types@npm:18.0.0"
checksum: d487d6c6c1965e583eee417d567e4fe3357a98953fc49bce1a88487e7908e9b5dbb3e98f60dfa340e23b1792725fbc006295aea071c5667a813b9c098185b56f
languageName: node
linkType: hard

"@octokit/openapi-types@npm:^7.0.0":
version: 7.0.0
resolution: "@octokit/openapi-types@npm:7.0.0"
Expand Down Expand Up @@ -1378,6 +1386,17 @@ __metadata:
languageName: node
linkType: hard

"@octokit/plugin-rest-endpoint-methods@npm:^7.2.1":
version: 7.2.1
resolution: "@octokit/plugin-rest-endpoint-methods@npm:7.2.1"
dependencies:
"@octokit/types": ^9.3.1
peerDependencies:
"@octokit/core": ">=3"
checksum: 069e52305f9d2e85fb83819a80860e526b9da2e0936640975f749a2c63020674053e6f4b5af771651a93320172579b0779bf3d663c8adcd090f152aac29f4ad4
languageName: node
linkType: hard

"@octokit/request-error@npm:^2.0.0":
version: 2.0.2
resolution: "@octokit/request-error@npm:2.0.2"
Expand Down Expand Up @@ -1468,6 +1487,15 @@ __metadata:
languageName: node
linkType: hard

"@octokit/types@npm:^9.3.1":
version: 9.3.1
resolution: "@octokit/types@npm:9.3.1"
dependencies:
"@octokit/openapi-types": ^18.0.0
checksum: 56fce104114730553c79175261f288a263055af4a6de848130fa964940460ee4fe8fa610f33dd0862c2c178d7d97f703e44a799898f3b52583e7ce5ae595f8ff
languageName: node
linkType: hard

"@sinclair/typebox@npm:^0.25.16":
version: 0.25.24
resolution: "@sinclair/typebox@npm:0.25.24"
Expand Down

0 comments on commit d0ee813

Please sign in to comment.