refactor: convert to typescript
This commit is contained in:
38
src/dockerhub-helper.ts
Normal file
38
src/dockerhub-helper.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import * as core from '@actions/core'
|
||||
import * as fetch from 'node-fetch'
|
||||
|
||||
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'}
|
||||
})
|
||||
const json = await response.json()
|
||||
core.setSecret(json['token'])
|
||||
return json['token']
|
||||
}
|
||||
|
||||
export async function updateRepositoryDescription(
|
||||
token: string,
|
||||
repository: string,
|
||||
fullDescription: string
|
||||
): Promise<void> {
|
||||
const body = {
|
||||
full_description: fullDescription
|
||||
}
|
||||
await fetch(`https://hub.docker.com/v2/repositories/${repository}`, {
|
||||
method: 'patch',
|
||||
body: JSON.stringify(body),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `JWT ${token}`
|
||||
}
|
||||
})
|
||||
}
|
||||
68
src/input-helper.ts
Normal file
68
src/input-helper.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import * as core from '@actions/core'
|
||||
|
||||
const README_FILEPATH_DEFAULT = './README.md'
|
||||
|
||||
interface Inputs {
|
||||
dockerhubUsername: string
|
||||
dockerhubPassword: string
|
||||
dockerhubRepository: string
|
||||
readmeFilepath: string
|
||||
}
|
||||
|
||||
export function getInputs(): Inputs {
|
||||
const inputs = {
|
||||
dockerhubUsername: core.getInput('dockerhub-username'),
|
||||
dockerhubPassword: core.getInput('dockerhub-password'),
|
||||
dockerhubRepository: core.getInput('dockerhub-repository'),
|
||||
readmeFilepath: core.getInput('readme-filepath')
|
||||
}
|
||||
|
||||
// Environment variable input alternatives and their aliases
|
||||
|
||||
if (!inputs.dockerhubUsername && process.env['DOCKERHUB_USERNAME']) {
|
||||
inputs.dockerhubUsername = process.env['DOCKERHUB_USERNAME']
|
||||
}
|
||||
if (!inputs.dockerhubUsername && process.env['DOCKER_USERNAME']) {
|
||||
inputs.dockerhubUsername = process.env['DOCKER_USERNAME']
|
||||
}
|
||||
|
||||
if (!inputs.dockerhubPassword && process.env['DOCKERHUB_PASSWORD']) {
|
||||
inputs.dockerhubPassword = process.env['DOCKERHUB_PASSWORD']
|
||||
}
|
||||
if (!inputs.dockerhubPassword && process.env['DOCKER_PASSWORD']) {
|
||||
inputs.dockerhubPassword = process.env['DOCKER_PASSWORD']
|
||||
}
|
||||
|
||||
if (!inputs.dockerhubRepository && process.env['DOCKERHUB_REPOSITORY']) {
|
||||
inputs.dockerhubRepository = process.env['DOCKERHUB_REPOSITORY']
|
||||
}
|
||||
if (!inputs.dockerhubRepository && process.env['DOCKER_REPOSITORY']) {
|
||||
inputs.dockerhubRepository = process.env['DOCKER_REPOSITORY']
|
||||
}
|
||||
if (!inputs.dockerhubRepository && process.env['GITHUB_REPOSITORY']) {
|
||||
inputs.dockerhubRepository = process.env['GITHUB_REPOSITORY']
|
||||
}
|
||||
|
||||
if (!inputs.readmeFilepath && process.env['README_FILEPATH']) {
|
||||
inputs.readmeFilepath = process.env['README_FILEPATH']
|
||||
}
|
||||
|
||||
// Set defaults
|
||||
if (!inputs.readmeFilepath) {
|
||||
inputs.readmeFilepath = README_FILEPATH_DEFAULT
|
||||
}
|
||||
|
||||
return inputs
|
||||
}
|
||||
|
||||
function checkRequiredInput(input: string, name: string): void {
|
||||
if (!input) {
|
||||
throw new Error(`Required input '${name}' is missing.`)
|
||||
}
|
||||
}
|
||||
|
||||
export function validateInputs(inputs: Inputs): void {
|
||||
checkRequiredInput(inputs.dockerhubUsername, 'dockerhub-username')
|
||||
checkRequiredInput(inputs.dockerhubPassword, 'dockerhub-password')
|
||||
checkRequiredInput(inputs.dockerhubRepository, 'dockerhub-repository')
|
||||
}
|
||||
47
src/main.ts
Normal file
47
src/main.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import * as core from '@actions/core'
|
||||
import * as inputHelper from './input-helper'
|
||||
import * as dockerhubHelper from './dockerhub-helper'
|
||||
import * as fs from 'fs'
|
||||
import {inspect, TextEncoder} from 'util'
|
||||
|
||||
const MAX_BYTES = 25000
|
||||
|
||||
async function run(): Promise<void> {
|
||||
try {
|
||||
const inputs = inputHelper.getInputs()
|
||||
core.debug(`Inputs: ${inspect(inputs)}`)
|
||||
|
||||
inputHelper.validateInputs(inputs)
|
||||
|
||||
// Fetch the readme content
|
||||
const readmeContent = await fs.promises.readFile(inputs.readmeFilepath, {
|
||||
encoding: 'utf8'
|
||||
})
|
||||
const byteLength = new TextEncoder().encode(readmeContent).length
|
||||
if (byteLength > MAX_BYTES) {
|
||||
throw new Error(
|
||||
`File size exceeds the maximum allowed ${MAX_BYTES} bytes`
|
||||
)
|
||||
}
|
||||
|
||||
// Acquire a token for the Docker Hub API
|
||||
core.info('Acquiring token')
|
||||
const token = await dockerhubHelper.getToken(
|
||||
inputs.dockerhubUsername,
|
||||
inputs.dockerhubPassword
|
||||
)
|
||||
// Send a PATCH request to update the description of the repository
|
||||
core.info('Sending PATCH request')
|
||||
await dockerhubHelper.updateRepositoryDescription(
|
||||
token,
|
||||
inputs.dockerhubRepository,
|
||||
readmeContent
|
||||
)
|
||||
core.info('Request successful')
|
||||
} catch (error) {
|
||||
core.debug(inspect(error))
|
||||
core.setFailed(error.message)
|
||||
}
|
||||
}
|
||||
|
||||
run()
|
||||
Reference in New Issue
Block a user