releases: mutualize releases handling logic and move it to github class

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2025-10-28 09:09:16 +01:00
parent 5568d95611
commit 8e64b4303b
18 changed files with 214 additions and 105 deletions

View File

@@ -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<boolean> {
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<GitHubRelease> {
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 = <Record<string, GitHubRelease>>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];
}

View File

@@ -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<boolean> {
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<GitHubRelease> {
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 = <Record<string, GitHubRelease>>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];
}

View File

@@ -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<GitHubRelease> {
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 = <Record<string, GitHubRelease>>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];
}

View File

@@ -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<Record<string, GitHubRelease>> {
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 <Record<string, GitHubRelease>>JSON.parse(dt);
}
static get context(): Context {
return github.context;
}

View File

@@ -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<GitHubRelease> {
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 = <Record<string, GitHubRelease>>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];
}

View File

@@ -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 {

View File

@@ -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;
}

View File

@@ -28,6 +28,13 @@ export interface GitHubRelease {
assets: Array<string>;
}
export interface GitHubContentOpts {
owner: string;
repo: string;
ref?: string;
path: string;
}
export type GitHubRepo = OctoOpenApiTypes['schemas']['repository'];
export interface GitHubActionsRuntimeToken extends JwtPayload {

View File

@@ -14,8 +14,10 @@
* limitations under the License.
*/
import {GitHubContentOpts} from '../github';
export interface DownloadVersion {
version: string;
downloadURL: string;
releasesURL: string;
contentOpts: GitHubContentOpts;
}

View File

@@ -14,8 +14,10 @@
* limitations under the License.
*/
import {GitHubContentOpts} from '../github';
export interface DownloadVersion {
version: string;
downloadURL: string;
releasesURL: string;
contentOpts: GitHubContentOpts;
}

View File

@@ -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<GitHubRelease> {
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 = <Record<string, GitHubRelease>>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];
}