@docker/actions-toolkit
Version:
Toolkit for Docker (GitHub) Actions
111 lines • 4.48 kB
JavaScript
/**
* Copyright 2023 actions-toolkit authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as core from '@actions/core';
import * as httpm from '@actions/http-client';
import { HttpCodes } from '@actions/http-client';
const apiBaseURL = 'https://hub.docker.com';
const loginURL = `${apiBaseURL}/v2/users/login?refresh_token=true`;
const repositoriesURL = `${apiBaseURL}/v2/repositories/`;
export class DockerHub {
opts;
httpc;
constructor(opts, httpc) {
this.opts = opts;
this.httpc = httpc;
}
static async build(opts) {
return new DockerHub(opts, new httpm.HttpClient('docker-actions-toolkit', [], {
headers: {
Authorization: `JWT ${await DockerHub.login(opts.credentials)}`,
'Content-Type': 'application/json'
}
}));
}
async getRepositoryTags(req) {
const url = new URL(`${repositoriesURL}${req.namespace}/${req.name}/tags`);
if (req.page) {
url.searchParams.append('page', req.page.toString());
}
if (req.page_size) {
url.searchParams.append('page_size', req.page_size.toString());
}
const resp = await this.httpc.get(url.toString());
return JSON.parse(await DockerHub.handleResponse(resp));
}
async getRepositoryAllTags(req) {
const tags = await this.getRepositoryTags(req);
while (tags.next) {
const nextURL = new URL(tags.next);
const pageNumber = Number(nextURL.searchParams.get('page'));
const pageSize = Number(nextURL.searchParams.get('page_size')) || undefined;
const nextTags = await this.getRepositoryTags({
namespace: req.namespace,
name: req.name,
page: pageNumber,
page_size: pageSize || req.page_size
});
tags.results.push(...nextTags.results);
tags.next = nextTags.next;
}
return tags;
}
async getRepository(req) {
const resp = await this.httpc.get(`${repositoriesURL}${req.namespace}/${req.name}`);
return JSON.parse(await DockerHub.handleResponse(resp));
}
async updateRepoDescription(req) {
const body = {
full_description: req.full_description
};
if (req.description) {
body['description'] = req.description;
}
const resp = await this.httpc.patch(`${repositoriesURL}${req.namespace}/${req.name}`, JSON.stringify(body));
return JSON.parse(await DockerHub.handleResponse(resp));
}
static async login(req) {
const http = new httpm.HttpClient('docker-actions-toolkit', [], {
headers: {
'Content-Type': 'application/json'
}
});
const resp = await http.post(loginURL, JSON.stringify(req));
const tokenResp = JSON.parse(await DockerHub.handleResponse(resp));
core.setSecret(`${tokenResp.token}`);
return `${tokenResp.token}`;
}
static async handleResponse(resp) {
const body = await resp.readBody();
resp.message.statusCode = resp.message.statusCode || HttpCodes.InternalServerError;
if (resp.message.statusCode < 200 || resp.message.statusCode >= 300) {
throw DockerHub.parseError(resp, body);
}
return body;
}
static parseError(resp, body) {
if (resp.message.statusCode == HttpCodes.Unauthorized) {
throw new Error(`Docker Hub API: operation not permitted`);
}
const errResp = JSON.parse(body);
for (const k of ['message', 'detail', 'error']) {
if (errResp[k]) {
throw new Error(`Docker Hub API: bad status code ${resp.message.statusCode}: ${errResp[k]}`);
}
}
throw new Error(`Docker Hub API: bad status code ${resp.message.statusCode}`);
}
}
//# sourceMappingURL=dockerhub.js.map