Skip to content

Commit 7b6177a

Browse files
committed
github: pass token to cdn
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent 15ebeff commit 7b6177a

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

__tests__/github.unmock.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {describe, expect, jest, it} from '@jest/globals';
17+
import {describe, expect, jest, it, test} from '@jest/globals';
1818

1919
import {GitHub} from '../src/github';
2020

@@ -35,6 +35,29 @@ describe('releases', () => {
3535
});
3636
});
3737

38+
describe('releasesRaw', () => {
39+
// prettier-ignore
40+
test.each([
41+
['.github/buildx-lab-releases.json'],
42+
['.github/buildx-releases.json'],
43+
['.github/compose-lab-releases.json'],
44+
['.github/compose-releases.json'],
45+
['.github/docker-releases.json'],
46+
['.github/regclient-releases.json'],
47+
['.github/undock-releases.json'],
48+
])('returns %p using GitHub CDN', async (path: string) => {
49+
const github = new GitHub();
50+
const releases = await github.releasesRaw('Undock', {
51+
owner: 'docker',
52+
repo: 'actions-toolkit',
53+
ref: 'main',
54+
path: path
55+
});
56+
expect(releases).toBeDefined();
57+
expect(Object.keys(releases).length).toBeGreaterThan(0);
58+
});
59+
});
60+
3861
describe('releasesAPI', () => {
3962
it('returns Undock releases JSON using GitHub API', async () => {
4063
const github = new GitHub();

src/github.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ export interface GitHubOpts {
4545
}
4646

4747
export class GitHub {
48+
private readonly githubToken?: string;
4849
public readonly octokit: InstanceType<typeof Octokit>;
4950

5051
constructor(opts?: GitHubOpts) {
51-
this.octokit = github.getOctokit(`${opts?.token || process.env.GITHUB_TOKEN}`);
52+
this.githubToken = opts?.token || process.env.GITHUB_TOKEN;
53+
this.octokit = github.getOctokit(`${this.githubToken}`);
5254
}
5355

5456
public repoData(): Promise<GitHubRepo> {
@@ -72,7 +74,10 @@ export class GitHub {
7274
public async releasesRaw(name: string, opts: GitHubContentOpts): Promise<Record<string, GitHubRelease>> {
7375
const url = `https://raw.githubusercontent.com/${opts.owner}/${opts.repo}/${opts.ref}/${opts.path}`;
7476
const http: httpm.HttpClient = new httpm.HttpClient('docker-actions-toolkit');
75-
const httpResp: httpm.HttpClientResponse = await http.get(url);
77+
// prettier-ignore
78+
const httpResp: httpm.HttpClientResponse = await http.get(url, this.githubToken ? {
79+
Authorization: `token ${this.githubToken}`
80+
} : undefined);
7681
const dt = await httpResp.readBody();
7782
const statusCode = httpResp.message.statusCode || 500;
7883
if (statusCode >= 400) {

0 commit comments

Comments
 (0)