dockerhub-description/src/dockerhub-helper.ts

54 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-09-23 08:58:52 +02:00
import * as core from '@actions/core'
import * as fetch from 'node-fetch'
2020-09-25 08:22:36 +02:00
const DESCRIPTION_MAX_CHARS = 100
2020-09-23 08:58:52 +02:00
export async function getToken(
username: string,
password: string
): Promise<string> {
const body = {
username: username,
password: password
}
const response = await fetch('https://hub.docker.com/v2/users/login', {
method: 'post',
body: JSON.stringify(body),
headers: {'Content-Type': 'application/json'}
})
if (!response.ok) {
throw new Error(
`Unexpected response: ${response.status} ${response.statusText}`
)
}
2020-09-23 08:58:52 +02:00
const json = await response.json()
core.setSecret(json['token'])
return json['token']
}
export async function updateRepositoryDescription(
token: string,
repository: string,
2020-09-25 08:22:36 +02:00
description: string,
2020-09-23 08:58:52 +02:00
fullDescription: string
): Promise<void> {
const body = {
full_description: fullDescription
}
2020-09-25 08:22:36 +02:00
if (description) {
body['description'] = description.slice(0, DESCRIPTION_MAX_CHARS)
}
2020-09-23 08:58:52 +02:00
await fetch(`https://hub.docker.com/v2/repositories/${repository}`, {
method: 'patch',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
Authorization: `JWT ${token}`
}
2020-10-27 02:41:21 +01:00
}).then(res => {
if (!res.ok) {
throw new Error(res.statusText)
}
2020-09-23 08:58:52 +02:00
})
}