Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref!: Allow existing bearer token to be passed to dockerhub #138

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

technicallyjosh
Copy link

@technicallyjosh technicallyjosh commented Jul 22, 2023

  • Allow for an existing access token to be used to not duplicate login/access token calls
  • Remove "all tags" function with loop to return all tags
    This is a less than ideal behavior against our APIs and would probably hit rate limits quickly depending on the image queried.

This is really just some simple improvements to the existing functionality.

@technicallyjosh technicallyjosh changed the title ref: Rework docker hub client ref!: Rework docker hub client Jul 22, 2023
.prettierrc.json Outdated Show resolved Hide resolved
src/docker-api.ts Outdated Show resolved Hide resolved
__tests__/dockerhub.test.ts Outdated Show resolved Hide resolved
@technicallyjosh technicallyjosh changed the title ref!: Rework docker hub client ref!: Allow existing bearer token to be passed to dockerhub Jul 31, 2023
Copy link
Member

@crazy-max crazy-max left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some changes looks unrelated like changes in buildx and docker dirs. Can we keep a minimal scope of changes with just the ability to reuse an existing token and do refactor in follow-up please?

Also having a commit message following our history like this would be nice:

dockerhub: ability to reuse existing token

This is because we have many things in this toolkit and it will be easier to look through the history if we want to bisect in the future.

Comment on lines +178 to +193
export function validateRepoParts(val: RepoParts) {
val = val ?? {
namespace: '',
name: ''
};
val.namespace = val.namespace ?? '';
val.name = val.name ?? '';

if (val.namespace.trim() == '') {
throw new Error('req.namespace is required');
}

private static async handleResponse(resp: httpm.HttpClientResponse): Promise<string> {
const body = await resp.readBody();
resp.message.statusCode = resp.message.statusCode || HttpCodes.InternalServerError;
if (resp.message.statusCode < 200 || resp.message.statusCode >= 300) {
if (resp.message.statusCode == HttpCodes.Unauthorized) {
throw new Error(`Docker Hub API: operation not permitted`);
}
const errResp = <Record<string, string>>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]}`);
}
if (val.name.trim() == '') {
throw new Error('req.name is required');
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really necessary? I would prefer this to be handled server side and I think this is already the case no?

Comment on lines +195 to +215
async function handleResponse<T>(response: HttpClientResponse): Promise<T> {
const body = await response.readBody();

// Default the status code to internal.
const statusCode = response.message.statusCode ?? HttpCodes.InternalServerError;

if (statusCode < 200 || statusCode >= 400) {
const errResp = JSON.parse(body) as Record<string, string>;

for (const k of ['message', 'detail', 'error']) {
// We have a couple different props that can come back. Check the known
// ones.
if (errResp[k]) {
throw new Error(`docker api request failed: ${statusCode} ${errResp[k]}`);
}
throw new Error(`Docker Hub API: bad status code ${resp.message.statusCode}`);
}
return body;

throw new Error(`docker api request failed: ${statusCode} ${body}`);
}

return JSON.parse(body) as T;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not seem there are any functional changes?

}
}

async function handleResponse<T>(response: HttpClientResponse): Promise<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to change the scope?

name: string;
}

export function validateRepoParts(val: RepoParts) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid exposing methods not intended to be consumed.

const tokenResp = <TokenResponse>JSON.parse(await DockerHub.handleResponse(resp));
core.setSecret(`${tokenResp.token}`);
return `${tokenResp.token}`;
export interface RepoParts {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants