-
Notifications
You must be signed in to change notification settings - Fork 16
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
Conversation
There was a problem hiding this 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.
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'); | ||
} | ||
} |
There was a problem hiding this comment.
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?
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; |
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
I'll revisit this in the future if needed. |
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.