dockerhub: getRepositoryTags

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-02-14 04:30:26 +01:00
parent 01219141e4
commit c2dfc9dae8
5 changed files with 8313 additions and 2 deletions

View File

@@ -19,13 +19,15 @@ import * as fs from 'fs';
import * as path from 'path';
import {DockerHub} from '../src/dockerhub';
import {RepositoryResponse} from '../src/types/dockerhub';
import {RepositoryResponse, RepositoryTagsResponse} from '../src/types/dockerhub';
beforeEach(() => {
jest.clearAllMocks();
});
import repoInfoFixture from './fixtures/dockerhub-repoinfo.json';
import repoTagsFixture from './fixtures/dockerhub-repotags.json';
import repoAllTagsFixture from './fixtures/dockerhub-repoalltags.json';
describe('getRepository', () => {
it('returns repo info', async () => {
@@ -50,6 +52,54 @@ describe('getRepository', () => {
});
});
describe('getRepositoryTags', () => {
it('return repo tags', async () => {
jest.spyOn(DockerHub.prototype, 'getRepositoryTags').mockImplementation((): Promise<RepositoryTagsResponse> => {
return <Promise<RepositoryTagsResponse>>(repoTagsFixture as unknown);
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
jest.spyOn(DockerHub as any, 'login').mockReturnValue('jwt_token');
const dockerhub = await DockerHub.build({
credentials: {
username: 'foo',
password: '0123456-7890-0000-1111-222222222'
}
});
const resp = await dockerhub.getRepositoryTags({
namespace: 'crazymax',
name: 'diun'
});
expect(resp.count).toBeGreaterThan(0);
expect(resp.next).not.toBeNull();
expect(resp.results.length).toBeGreaterThan(0);
expect(resp.results[0].last_updater_username).toEqual('crazymax');
});
});
describe('getRepositoryAllTags', () => {
it('return repo all tags', async () => {
jest.spyOn(DockerHub.prototype, 'getRepositoryAllTags').mockImplementation((): Promise<RepositoryTagsResponse> => {
return <Promise<RepositoryTagsResponse>>(repoAllTagsFixture as unknown);
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
jest.spyOn(DockerHub as any, 'login').mockReturnValue('jwt_token');
const dockerhub = await DockerHub.build({
credentials: {
username: 'foo',
password: '0123456-7890-0000-1111-222222222'
}
});
const resp = await dockerhub.getRepositoryAllTags({
namespace: 'crazymax',
name: 'diun'
});
expect(resp.count).toBeGreaterThan(0);
expect(resp.next).toBeNull();
expect(resp.results.length).toBeGreaterThan(0);
expect(resp.results[0].last_updater_username).toEqual('crazymax');
});
});
describe('updateRepoDescription', () => {
it.skip('set repo description', async () => {
const dockerhub = await DockerHub.build({

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -18,7 +18,7 @@ import * as core from '@actions/core';
import * as httpm from '@actions/http-client';
import {HttpCodes} from '@actions/http-client';
import {RepositoryRequest, RepositoryResponse, TokenRequest, TokenResponse, UpdateRepoDescriptionRequest} from './types/dockerhub';
import {RepositoryRequest, RepositoryResponse, RepositoryTagsRequest, RepositoryTagsResponse, TokenRequest, TokenResponse, UpdateRepoDescriptionRequest} from './types/dockerhub';
export interface DockerHubOpts {
credentials: TokenRequest;
@@ -49,6 +49,36 @@ export class DockerHub {
);
}
public async getRepositoryTags(req: RepositoryTagsRequest): Promise<RepositoryTagsResponse> {
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: httpm.HttpClientResponse = await this.httpc.get(url.toString());
return <RepositoryTagsResponse>JSON.parse(await DockerHub.handleResponse(resp));
}
public async getRepositoryAllTags(req: RepositoryTagsRequest): Promise<RepositoryTagsResponse> {
const tags: RepositoryTagsResponse = 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
} as RepositoryTagsRequest);
tags.results.push(...nextTags.results);
tags.next = nextTags.next;
}
return tags;
}
public async getRepository(req: RepositoryRequest): Promise<RepositoryResponse> {
const resp: httpm.HttpClientResponse = await this.httpc.get(`${repositoriesURL}${req.namespace}/${req.name}`);
return <RepositoryResponse>JSON.parse(await DockerHub.handleResponse(resp));

View File

@@ -58,6 +58,53 @@ export interface RepositoryResponse {
content_types: Array<string>;
}
export interface RepositoryTagsRequest {
namespace: string;
name: string;
page?: number;
page_size?: number;
}
export interface RepositoryTagsResponse {
count: number;
next?: string;
previous?: string;
results: Array<RepositoryTagsResult>;
}
export interface RepositoryTagsResult {
creator: number;
id: number;
images: Array<RepositoryTagsResultImage>;
last_updated: Date;
last_updater: number;
last_updater_username: string;
name: string;
repository: number;
full_size: number;
v2: boolean;
tag_status: string;
tag_last_pulled: Date;
tag_last_pushed: Date;
media_type: string;
content_type: string;
digest: string;
}
export interface RepositoryTagsResultImage {
architecture: string;
features: string;
variant?: string;
digest: string;
os: string;
os_features: string;
os_version?: string;
size: number;
status: string;
last_pulled: Date;
last_pushed: Date;
}
export interface UpdateRepoDescriptionRequest {
name: string;
namespace: string;