diff --git a/__tests__/buildx/install.test.ts b/__tests__/buildx/install.test.ts index 2119b50..2043026 100644 --- a/__tests__/buildx/install.test.ts +++ b/__tests__/buildx/install.test.ts @@ -119,7 +119,12 @@ describe('getDownloadVersion', () => { expect(version.key).toEqual('official'); expect(version.version).toEqual('latest'); expect(version.downloadURL).toEqual('https://github.com/docker/buildx/releases/download/v%s/%s'); - expect(version.releasesURL).toEqual('https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/buildx-releases.json'); + expect(version.contentOpts).toEqual({ + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/buildx-releases.json' + }); }); it('returns official v0.10.1 download version', async () => { @@ -127,7 +132,12 @@ describe('getDownloadVersion', () => { expect(version.key).toEqual('official'); expect(version.version).toEqual('v0.10.1'); expect(version.downloadURL).toEqual('https://github.com/docker/buildx/releases/download/v%s/%s'); - expect(version.releasesURL).toEqual('https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/buildx-releases.json'); + expect(version.contentOpts).toEqual({ + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/buildx-releases.json' + }); }); it('returns cloud latest download version', async () => { @@ -135,7 +145,12 @@ describe('getDownloadVersion', () => { expect(version.key).toEqual('cloud'); expect(version.version).toEqual('latest'); expect(version.downloadURL).toEqual('https://github.com/docker/buildx-desktop/releases/download/v%s/%s'); - expect(version.releasesURL).toEqual('https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/buildx-lab-releases.json'); + expect(version.contentOpts).toEqual({ + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/buildx-lab-releases.json' + }); }); it('returns cloud v0.11.2-desktop.2 download version', async () => { @@ -143,7 +158,12 @@ describe('getDownloadVersion', () => { expect(version.key).toEqual('cloud'); expect(version.version).toEqual('v0.11.2-desktop.2'); expect(version.downloadURL).toEqual('https://github.com/docker/buildx-desktop/releases/download/v%s/%s'); - expect(version.releasesURL).toEqual('https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/buildx-lab-releases.json'); + expect(version.contentOpts).toEqual({ + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/buildx-lab-releases.json' + }); }); it('returns cloud for lab version', async () => { @@ -151,7 +171,12 @@ describe('getDownloadVersion', () => { expect(version.key).toEqual('cloud'); expect(version.version).toEqual('latest'); expect(version.downloadURL).toEqual('https://github.com/docker/buildx-desktop/releases/download/v%s/%s'); - expect(version.releasesURL).toEqual('https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/buildx-lab-releases.json'); + expect(version.contentOpts).toEqual({ + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/buildx-lab-releases.json' + }); }); it('unknown repo', async () => { @@ -187,6 +212,6 @@ describe('getRelease', () => { it('unknown release', async () => { const version = await Install.getDownloadVersion('foo'); - await expect(Install.getRelease(version)).rejects.toThrow(new Error('Cannot find Buildx release foo in https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/buildx-releases.json')); + await expect(Install.getRelease(version)).rejects.toThrow(new Error('Cannot find Buildx release foo in releases JSON')); }); }); diff --git a/__tests__/compose/install.test.ts b/__tests__/compose/install.test.ts index d8fbe66..c87ea0c 100644 --- a/__tests__/compose/install.test.ts +++ b/__tests__/compose/install.test.ts @@ -99,28 +99,48 @@ describe('getDownloadVersion', () => { expect(version.key).toEqual('official'); expect(version.version).toEqual('latest'); expect(version.downloadURL).toEqual('https://github.com/docker/compose/releases/download/v%s/%s'); - expect(version.releasesURL).toEqual('https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/compose-releases.json'); + expect(version.contentOpts).toEqual({ + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/compose-releases.json' + }); }); it('returns official v2.24.3 download version', async () => { const version = await Install.getDownloadVersion('v2.24.3'); expect(version.key).toEqual('official'); expect(version.version).toEqual('v2.24.3'); expect(version.downloadURL).toEqual('https://github.com/docker/compose/releases/download/v%s/%s'); - expect(version.releasesURL).toEqual('https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/compose-releases.json'); + expect(version.contentOpts).toEqual({ + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/compose-releases.json' + }); }); it('returns cloud latest download version', async () => { const version = await Install.getDownloadVersion('cloud:latest'); expect(version.key).toEqual('cloud'); expect(version.version).toEqual('latest'); expect(version.downloadURL).toEqual('https://github.com/docker/compose-desktop/releases/download/v%s/%s'); - expect(version.releasesURL).toEqual('https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/compose-lab-releases.json'); + expect(version.contentOpts).toEqual({ + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/compose-lab-releases.json' + }); }); it('returns cloud v2.27.1-desktop.1 download version', async () => { const version = await Install.getDownloadVersion('cloud:v2.27.1-desktop.1'); expect(version.key).toEqual('cloud'); expect(version.version).toEqual('v2.27.1-desktop.1'); expect(version.downloadURL).toEqual('https://github.com/docker/compose-desktop/releases/download/v%s/%s'); - expect(version.releasesURL).toEqual('https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/compose-lab-releases.json'); + expect(version.contentOpts).toEqual({ + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/compose-lab-releases.json' + }); }); it('unknown repo', async () => { await expect(Install.getDownloadVersion('foo:bar')).rejects.toThrow(new Error('Cannot find compose version for foo:bar')); @@ -152,6 +172,6 @@ describe('getRelease', () => { }); it('unknown release', async () => { const version = await Install.getDownloadVersion('foo'); - await expect(Install.getRelease(version)).rejects.toThrow(new Error('Cannot find Compose release foo in https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/compose-releases.json')); + await expect(Install.getRelease(version)).rejects.toThrow(new Error('Cannot find Compose release foo in releases JSON')); }); }); diff --git a/__tests__/docker/install.test.itg.ts b/__tests__/docker/install.test.itg.ts index f724926..e376689 100644 --- a/__tests__/docker/install.test.itg.ts +++ b/__tests__/docker/install.test.itg.ts @@ -21,9 +21,7 @@ import path from 'path'; import {Install, InstallSource, InstallSourceArchive, InstallSourceImage} from '../../src/docker/install'; import {Docker} from '../../src/docker/docker'; -import {Regctl} from '../../src/regclient/regctl'; import {Install as RegclientInstall} from '../../src/regclient/install'; -import {Undock} from '../../src/undock/undock'; import {Install as UndockInstall} from '../../src/undock/install'; import {Exec} from '../../src/exec'; @@ -48,9 +46,7 @@ describe('root', () => { source: source, runDir: tmpDir(), contextName: 'foo', - daemonConfig: `{"debug":true,"features":{"containerd-snapshotter":true}}`, - regctl: new Regctl(), - undock: new Undock() + daemonConfig: `{"debug":true,"features":{"containerd-snapshotter":true}}` }); await expect(tryInstall(install)).resolves.not.toThrow(); }, 30 * 60 * 1000); @@ -70,9 +66,7 @@ describe('rootless', () => { runDir: tmpDir(), contextName: 'foo', daemonConfig: `{"debug":true}`, - rootless: true, - regctl: new Regctl(), - undock: new Undock() + rootless: true }); await expect( tryInstall(install, async () => { @@ -97,9 +91,7 @@ describe('tcp', () => { runDir: tmpDir(), contextName: 'foo', daemonConfig: `{"debug":true}`, - localTCPPort: 2378, - regctl: new Regctl(), - undock: new Undock() + localTCPPort: 2378 }); await expect( tryInstall(install, async () => { diff --git a/__tests__/docker/install.test.ts b/__tests__/docker/install.test.ts index 1f4e7de..7b1d1ce 100644 --- a/__tests__/docker/install.test.ts +++ b/__tests__/docker/install.test.ts @@ -22,8 +22,6 @@ import * as rimraf from 'rimraf'; import osm = require('os'); import {Install, InstallSourceArchive, InstallSourceImage} from '../../src/docker/install'; -import {Regctl} from '../../src/regclient/regctl'; -import {Undock} from '../../src/undock/undock'; const tmpDir = fs.mkdtempSync(path.join(process.env.TEMP || os.tmpdir(), 'docker-install-')); @@ -66,9 +64,7 @@ describe('download', () => { jest.spyOn(osm, 'arch').mockImplementation(() => 'x64'); const install = new Install({ source: source, - runDir: tmpDir, - regctl: new Regctl(), - undock: new Undock() + runDir: tmpDir }); const toolPath = await install.download(); expect(fs.existsSync(toolPath)).toBe(true); @@ -99,7 +95,7 @@ describe('getRelease', () => { }); it('unknown release', async () => { - await expect(Install.getRelease('foo')).rejects.toThrow(new Error('Cannot find Docker release foo in https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/docker-releases.json')); + await expect(Install.getRelease('foo')).rejects.toThrow(new Error('Cannot find Docker release foo in releases JSON')); }); }); diff --git a/__tests__/github.test.ts b/__tests__/github.test.ts index 4f78cd9..7ec7748 100644 --- a/__tests__/github.test.ts +++ b/__tests__/github.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import {describe, expect, jest, it, beforeEach, afterEach} from '@jest/globals'; +import {describe, expect, jest, it, beforeEach, afterEach, test} from '@jest/globals'; import * as fs from 'fs'; import * as path from 'path'; import * as core from '@actions/core'; @@ -43,6 +43,29 @@ describe('context', () => { }); }); +describe('releases', () => { + // prettier-ignore + test.each([ + ['.github/buildx-lab-releases.json'], + ['.github/buildx-releases.json'], + ['.github/compose-lab-releases.json'], + ['.github/compose-releases.json'], + ['.github/docker-releases.json'], + ['.github/regclient-releases.json'], + ['.github/undock-releases.json'], + ])('returns %p', async (path: string) => { + const github = new GitHub(); + const releases = await github.releases('App', { + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: path + }); + expect(releases).toBeDefined(); + expect(Object.keys(releases).length).toBeGreaterThan(0); + }); +}); + describe('serverURL', () => { const originalEnv = process.env; beforeEach(() => { diff --git a/__tests__/regclient/install.test.ts b/__tests__/regclient/install.test.ts index a110ce6..132fd7a 100644 --- a/__tests__/regclient/install.test.ts +++ b/__tests__/regclient/install.test.ts @@ -88,13 +88,23 @@ describe('getDownloadVersion', () => { const version = await Install.getDownloadVersion('latest'); expect(version.version).toEqual('latest'); expect(version.downloadURL).toEqual('https://github.com/regclient/regclient/releases/download/v%s/%s'); - expect(version.releasesURL).toEqual('https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/regclient-releases.json'); + expect(version.contentOpts).toEqual({ + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/regclient-releases.json' + }); }); it('returns v0.8.1 download version', async () => { const version = await Install.getDownloadVersion('v0.8.1'); expect(version.version).toEqual('v0.8.1'); expect(version.downloadURL).toEqual('https://github.com/regclient/regclient/releases/download/v%s/%s'); - expect(version.releasesURL).toEqual('https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/regclient-releases.json'); + expect(version.contentOpts).toEqual({ + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/regclient-releases.json' + }); }); }); @@ -115,6 +125,6 @@ describe('getRelease', () => { }); it('unknown release', async () => { const version = await Install.getDownloadVersion('foo'); - await expect(Install.getRelease(version)).rejects.toThrow(new Error('Cannot find regclient release foo in https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/regclient-releases.json')); + await expect(Install.getRelease(version)).rejects.toThrow(new Error('Cannot find regclient release foo in releases JSON')); }); }); diff --git a/__tests__/undock/install.test.ts b/__tests__/undock/install.test.ts index 34d363f..fe604cf 100644 --- a/__tests__/undock/install.test.ts +++ b/__tests__/undock/install.test.ts @@ -93,13 +93,23 @@ describe('getDownloadVersion', () => { const version = await Install.getDownloadVersion('latest'); expect(version.version).toEqual('latest'); expect(version.downloadURL).toEqual('https://github.com/crazy-max/undock/releases/download/v%s/%s'); - expect(version.releasesURL).toEqual('https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/undock-releases.json'); + expect(version.contentOpts).toEqual({ + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/undock-releases.json' + }); }); it('returns v0.6.0 download version', async () => { const version = await Install.getDownloadVersion('v0.6.0'); expect(version.version).toEqual('v0.6.0'); expect(version.downloadURL).toEqual('https://github.com/crazy-max/undock/releases/download/v%s/%s'); - expect(version.releasesURL).toEqual('https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/undock-releases.json'); + expect(version.contentOpts).toEqual({ + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/undock-releases.json' + }); }); }); @@ -120,6 +130,6 @@ describe('getRelease', () => { }); it('unknown release', async () => { const version = await Install.getDownloadVersion('foo'); - await expect(Install.getRelease(version)).rejects.toThrow(new Error('Cannot find Undock release foo in https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/undock-releases.json')); + await expect(Install.getRelease(version)).rejects.toThrow(new Error('Cannot find Undock release foo in releases JSON')); }); }); diff --git a/src/buildx/install.ts b/src/buildx/install.ts index f072f14..87701b1 100644 --- a/src/buildx/install.ts +++ b/src/buildx/install.ts @@ -18,7 +18,6 @@ import fs from 'fs'; import os from 'os'; import path from 'path'; import * as core from '@actions/core'; -import * as httpm from '@actions/http-client'; import * as tc from '@actions/tool-cache'; import * as semver from 'semver'; import * as util from 'util'; @@ -29,6 +28,7 @@ import {Context} from '../context'; import {Exec} from '../exec'; import {Docker} from '../docker/docker'; import {Git} from '../git'; +import {GitHub} from '../github'; import {Util} from '../util'; import {DownloadVersion} from '../types/buildx/buildx'; @@ -39,10 +39,10 @@ export interface InstallOpts { } export class Install { - private readonly _standalone: boolean | undefined; + private readonly standalone: boolean | undefined; constructor(opts?: InstallOpts) { - this._standalone = opts?.standalone; + this.standalone = opts?.standalone; } /* @@ -205,7 +205,7 @@ export class Install { } private async isStandalone(): Promise { - const standalone = this._standalone ?? !(await Docker.isAvailable()); + const standalone = this.standalone ?? !(await Docker.isAvailable()); core.debug(`Install.isStandalone: ${standalone}`); return standalone; } @@ -285,7 +285,12 @@ export class Install { key: repoKey, version: version, downloadURL: 'https://github.com/docker/buildx/releases/download/v%s/%s', - releasesURL: 'https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/buildx-releases.json' + contentOpts: { + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/buildx-releases.json' + } }; } case 'cloud': { @@ -293,7 +298,12 @@ export class Install { key: repoKey, version: version, downloadURL: 'https://github.com/docker/buildx-desktop/releases/download/v%s/%s', - releasesURL: 'https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/buildx-lab-releases.json' + contentOpts: { + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/buildx-lab-releases.json' + } }; } default: { @@ -303,16 +313,10 @@ export class Install { } public static async getRelease(version: DownloadVersion): Promise { - const http: httpm.HttpClient = new httpm.HttpClient('docker-actions-toolkit'); - const resp: httpm.HttpClientResponse = await http.get(version.releasesURL); - const body = await resp.readBody(); - const statusCode = resp.message.statusCode || 500; - if (statusCode >= 400) { - throw new Error(`Failed to get Buildx releases from ${version.releasesURL} with status code ${statusCode}: ${body}`); - } - const releases = >JSON.parse(body); + const github = new GitHub(); + const releases = await github.releases('Buildx', version.contentOpts); if (!releases[version.version]) { - throw new Error(`Cannot find Buildx release ${version.version} in ${version.releasesURL}`); + throw new Error(`Cannot find Buildx release ${version.version} in releases JSON`); } return releases[version.version]; } diff --git a/src/compose/install.ts b/src/compose/install.ts index 106817e..b3f5419 100644 --- a/src/compose/install.ts +++ b/src/compose/install.ts @@ -18,27 +18,27 @@ import fs from 'fs'; import os from 'os'; import path from 'path'; import * as core from '@actions/core'; -import * as httpm from '@actions/http-client'; import * as tc from '@actions/tool-cache'; import * as semver from 'semver'; import * as util from 'util'; import {Cache} from '../cache'; import {Context} from '../context'; +import {Docker} from '../docker/docker'; +import {GitHub} from '../github'; import {DownloadVersion} from '../types/compose/compose'; import {GitHubRelease} from '../types/github'; -import {Docker} from '../docker/docker'; export interface InstallOpts { standalone?: boolean; } export class Install { - private readonly _standalone: boolean | undefined; + private readonly standalone: boolean | undefined; constructor(opts?: InstallOpts) { - this._standalone = opts?.standalone; + this.standalone = opts?.standalone; } /* @@ -129,7 +129,7 @@ export class Install { } private async isStandalone(): Promise { - const standalone = this._standalone ?? !(await Docker.isAvailable()); + const standalone = this.standalone ?? !(await Docker.isAvailable()); core.debug(`Install.isStandalone: ${standalone}`); return standalone; } @@ -183,7 +183,12 @@ export class Install { key: repoKey, version: version, downloadURL: 'https://github.com/docker/compose/releases/download/v%s/%s', - releasesURL: 'https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/compose-releases.json' + contentOpts: { + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/compose-releases.json' + } }; } case 'cloud': { @@ -191,7 +196,12 @@ export class Install { key: repoKey, version: version, downloadURL: 'https://github.com/docker/compose-desktop/releases/download/v%s/%s', - releasesURL: 'https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/compose-lab-releases.json' + contentOpts: { + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/compose-lab-releases.json' + } }; } default: { @@ -201,16 +211,10 @@ export class Install { } public static async getRelease(version: DownloadVersion): Promise { - const http: httpm.HttpClient = new httpm.HttpClient('docker-actions-toolkit'); - const resp: httpm.HttpClientResponse = await http.get(version.releasesURL); - const body = await resp.readBody(); - const statusCode = resp.message.statusCode || 500; - if (statusCode >= 400) { - throw new Error(`Failed to get Compose releases from ${version.releasesURL} with status code ${statusCode}: ${body}`); - } - const releases = >JSON.parse(body); + const github = new GitHub(); + const releases = await github.releases('Compose', version.contentOpts); if (!releases[version.version]) { - throw new Error(`Cannot find Compose release ${version.version} in ${version.releasesURL}`); + throw new Error(`Cannot find Compose release ${version.version} in releases JSON`); } return releases[version.version]; } diff --git a/src/docker/install.ts b/src/docker/install.ts index 9d7bfce..bb94145 100644 --- a/src/docker/install.ts +++ b/src/docker/install.ts @@ -22,16 +22,17 @@ import path from 'path'; import retry from 'async-retry'; import * as handlebars from 'handlebars'; import * as core from '@actions/core'; -import * as httpm from '@actions/http-client'; import * as io from '@actions/io'; import * as tc from '@actions/tool-cache'; import {Context} from '../context'; import {Docker} from './docker'; +import {Exec} from '../exec'; +import {GitHub} from '../github'; import {Regctl} from '../regclient/regctl'; import {Undock} from '../undock/undock'; -import {Exec} from '../exec'; import {Util} from '../util'; + import {limaYamlData, dockerServiceLogsPs1, setupDockerWinPs1} from './assets'; import {GitHubRelease} from '../types/github'; @@ -694,18 +695,16 @@ EOF`, } public static async getRelease(version: string): Promise { - const url = `https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/docker-releases.json`; - const http: httpm.HttpClient = new httpm.HttpClient('docker-actions-toolkit'); - const resp: httpm.HttpClientResponse = await http.get(url); - const body = await resp.readBody(); - const statusCode = resp.message.statusCode || 500; - if (statusCode >= 400) { - throw new Error(`Failed to get Docker release ${version} from ${url} with status code ${statusCode}: ${body}`); - } - const releases = >JSON.parse(body); + const github = new GitHub(); + const releases = await github.releases('Docker', { + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/docker-releases.json' + }); if (!releases[version]) { if (!releases['v' + version]) { - throw new Error(`Cannot find Docker release ${version} in ${url}`); + throw new Error(`Cannot find Docker release ${version} in releases JSON`); } return releases['v' + version]; } diff --git a/src/github.ts b/src/github.ts index e6eb0eb..c99d295 100644 --- a/src/github.ts +++ b/src/github.ts @@ -31,13 +31,14 @@ import {SummaryTableCell} from '@actions/core/lib/summary'; import * as github from '@actions/github'; import {GitHub as Octokit} from '@actions/github/lib/utils'; import {Context} from '@actions/github/lib/context'; +import * as httpm from '@actions/http-client'; import {TransferProgressEvent} from '@azure/core-http'; import {BlobClient, BlobHTTPHeaders} from '@azure/storage-blob'; import {jwtDecode, JwtPayload} from 'jwt-decode'; import {Util} from './util'; -import {BuildSummaryOpts, GitHubActionsRuntimeToken, GitHubActionsRuntimeTokenAC, GitHubRepo, UploadArtifactOpts, UploadArtifactResponse} from './types/github'; +import {BuildSummaryOpts, GitHubActionsRuntimeToken, GitHubActionsRuntimeTokenAC, GitHubContentOpts, GitHubRelease, GitHubRepo, UploadArtifactOpts, UploadArtifactResponse} from './types/github'; export interface GitHubOpts { token?: string; @@ -54,6 +55,18 @@ export class GitHub { return this.octokit.rest.repos.get({...github.context.repo}).then(response => response.data as GitHubRepo); } + public async releases(name: string, opts: GitHubContentOpts): Promise> { + const url = `https://raw.githubusercontent.com/${opts.owner}/${opts.repo}/${opts.ref}/${opts.path}`; + const http: httpm.HttpClient = new httpm.HttpClient('docker-actions-toolkit'); + const httpResp: httpm.HttpClientResponse = await http.get(url); + const dt = await httpResp.readBody(); + const statusCode = httpResp.message.statusCode || 500; + if (statusCode >= 400) { + throw new Error(`Failed to get ${name} releases from ${url} with status code ${statusCode}: ${dt}`); + } + return >JSON.parse(dt); + } + static get context(): Context { return github.context; } diff --git a/src/regclient/install.ts b/src/regclient/install.ts index 92b8b10..49cef53 100644 --- a/src/regclient/install.ts +++ b/src/regclient/install.ts @@ -18,13 +18,13 @@ import fs from 'fs'; import os from 'os'; import path from 'path'; import * as core from '@actions/core'; -import * as httpm from '@actions/http-client'; import * as tc from '@actions/tool-cache'; import * as semver from 'semver'; import * as util from 'util'; import {Cache} from '../cache'; import {Context} from '../context'; +import {GitHub} from '../github'; import {GitHubRelease} from '../types/github'; import {DownloadVersion} from '../types/regclient/regclient'; @@ -134,21 +134,20 @@ export class Install { return { version: v, downloadURL: 'https://github.com/regclient/regclient/releases/download/v%s/%s', - releasesURL: 'https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/regclient-releases.json' + contentOpts: { + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/regclient-releases.json' + } }; } public static async getRelease(version: DownloadVersion): Promise { - const http: httpm.HttpClient = new httpm.HttpClient('docker-actions-toolkit'); - const resp: httpm.HttpClientResponse = await http.get(version.releasesURL); - const body = await resp.readBody(); - const statusCode = resp.message.statusCode || 500; - if (statusCode >= 400) { - throw new Error(`Failed to get regclient releases from ${version.releasesURL} with status code ${statusCode}: ${body}`); - } - const releases = >JSON.parse(body); + const github = new GitHub(); + const releases = await github.releases('regclient', version.contentOpts); if (!releases[version.version]) { - throw new Error(`Cannot find regclient release ${version.version} in ${version.releasesURL}`); + throw new Error(`Cannot find regclient release ${version.version} in releases JSON`); } return releases[version.version]; } diff --git a/src/types/buildx/buildx.ts b/src/types/buildx/buildx.ts index 6dc5e6f..10ede92 100644 --- a/src/types/buildx/buildx.ts +++ b/src/types/buildx/buildx.ts @@ -14,6 +14,8 @@ * limitations under the License. */ +import {GitHubContentOpts} from '../github'; + export interface Cert { cacert?: string; cert?: string; @@ -24,7 +26,7 @@ export interface DownloadVersion { key: string; version: string; downloadURL: string; - releasesURL: string; + contentOpts: GitHubContentOpts; } export interface LocalRefsOpts { diff --git a/src/types/compose/compose.ts b/src/types/compose/compose.ts index 41d9cc9..4e69ea9 100644 --- a/src/types/compose/compose.ts +++ b/src/types/compose/compose.ts @@ -14,9 +14,11 @@ * limitations under the License. */ +import {GitHubContentOpts} from '../github'; + export interface DownloadVersion { key: string; version: string; downloadURL: string; - releasesURL: string; + contentOpts: GitHubContentOpts; } diff --git a/src/types/github.ts b/src/types/github.ts index c8e3ce6..8fc4bbe 100644 --- a/src/types/github.ts +++ b/src/types/github.ts @@ -28,6 +28,13 @@ export interface GitHubRelease { assets: Array; } +export interface GitHubContentOpts { + owner: string; + repo: string; + ref?: string; + path: string; +} + export type GitHubRepo = OctoOpenApiTypes['schemas']['repository']; export interface GitHubActionsRuntimeToken extends JwtPayload { diff --git a/src/types/regclient/regclient.ts b/src/types/regclient/regclient.ts index f06edeb..4aca14b 100644 --- a/src/types/regclient/regclient.ts +++ b/src/types/regclient/regclient.ts @@ -14,8 +14,10 @@ * limitations under the License. */ +import {GitHubContentOpts} from '../github'; + export interface DownloadVersion { version: string; downloadURL: string; - releasesURL: string; + contentOpts: GitHubContentOpts; } diff --git a/src/types/undock/undock.ts b/src/types/undock/undock.ts index c201b25..21374ca 100644 --- a/src/types/undock/undock.ts +++ b/src/types/undock/undock.ts @@ -14,8 +14,10 @@ * limitations under the License. */ +import {GitHubContentOpts} from '../github'; + export interface DownloadVersion { version: string; downloadURL: string; - releasesURL: string; + contentOpts: GitHubContentOpts; } diff --git a/src/undock/install.ts b/src/undock/install.ts index e27f673..4e381d8 100644 --- a/src/undock/install.ts +++ b/src/undock/install.ts @@ -18,13 +18,13 @@ import fs from 'fs'; import os from 'os'; import path from 'path'; import * as core from '@actions/core'; -import * as httpm from '@actions/http-client'; import * as tc from '@actions/tool-cache'; import * as semver from 'semver'; import * as util from 'util'; import {Cache} from '../cache'; import {Context} from '../context'; +import {GitHub} from '../github'; import {GitHubRelease} from '../types/github'; import {DownloadVersion} from '../types/undock/undock'; @@ -145,21 +145,20 @@ export class Install { return { version: v, downloadURL: 'https://github.com/crazy-max/undock/releases/download/v%s/%s', - releasesURL: 'https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/undock-releases.json' + contentOpts: { + owner: 'docker', + repo: 'actions-toolkit', + ref: 'main', + path: '.github/undock-releases.json' + } }; } public static async getRelease(version: DownloadVersion): Promise { - const http: httpm.HttpClient = new httpm.HttpClient('docker-actions-toolkit'); - const resp: httpm.HttpClientResponse = await http.get(version.releasesURL); - const body = await resp.readBody(); - const statusCode = resp.message.statusCode || 500; - if (statusCode >= 400) { - throw new Error(`Failed to get Undock releases from ${version.releasesURL} with status code ${statusCode}: ${body}`); - } - const releases = >JSON.parse(body); + const github = new GitHub(); + const releases = await github.releases('Undock', version.contentOpts); if (!releases[version.version]) { - throw new Error(`Cannot find Undock release ${version.version} in ${version.releasesURL}`); + throw new Error(`Cannot find Undock release ${version.version} in releases JSON`); } return releases[version.version]; }