Merge pull request #18 from peter-evans/typescript

refactor: convert to typescript
This commit is contained in:
Peter Evans 2020-09-25 15:43:53 +09:00 committed by GitHub
commit dd8b1b1548
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 10442 additions and 111 deletions

3
.eslintignore Normal file
View file

@ -0,0 +1,3 @@
dist/
lib/
node_modules/

19
.eslintrc.json Normal file
View file

@ -0,0 +1,19 @@
{
"env": { "node": true, "jest": true },
"parser": "@typescript-eslint/parser",
"parserOptions": { "ecmaVersion": 9, "sourceType": "module" },
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript",
"plugin:prettier/recommended",
"prettier/@typescript-eslint"
],
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/camelcase": "off"
}
}

99
.github/workflows/ci.yml vendored Normal file
View file

@ -0,0 +1,99 @@
name: CI
on:
push:
branches: [master]
paths-ignore:
- 'README.md'
- 'docs/**'
pull_request:
branches: [master]
paths-ignore:
- 'README.md'
- 'docs/**'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12.x
- run: npm ci
- run: npm run build
- run: npm run format-check
- run: npm run lint
- run: npm run test
- uses: actions/upload-artifact@v2
with:
name: dist
path: dist
- uses: actions/upload-artifact@v2
with:
name: action.yml
path: action.yml
test:
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository
needs: [build]
runs-on: ubuntu-latest
strategy:
matrix:
target: [built, committed]
steps:
- uses: actions/checkout@v2
- if: matrix.target == 'built' || github.event_name == 'pull_request'
uses: actions/download-artifact@v2
with:
name: dist
path: dist
- if: matrix.target == 'built' || github.event_name == 'pull_request'
uses: actions/download-artifact@v2
with:
name: action.yml
path: .
# Tests
- name: Prepare non-default filepath
run: |
mkdir -p some/path
cp README.md some/path/TEST.md
- name: Docker Hub Description (with)
uses: ./
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
repository: peterevans/dd-ci-fixture-${{ matrix.target }}
short-description: ${{ matrix.target }} test fixture for dockerhub-description action
readme-filepath: ./some/path/TEST.md
- name: Docker Hub Description (env)
uses: ./
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}
DOCKERHUB_REPOSITORY: peterevans/dd-ci-fixture-${{ matrix.target }}
package:
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
needs: [test]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/download-artifact@v2
with:
name: dist
path: dist
- name: Create Pull Request
uses: peter-evans/create-pull-request@v3
with:
commit-message: 'build: update distribution'
title: Update distribution
body: |
- Updates the distribution for changes on `master`
Auto-generated by [create-pull-request][1]
[1]: https://github.com/peter-evans/create-pull-request
branch: update-distribution

View file

@ -18,15 +18,9 @@ jobs:
sed -i 's/# Docker Hub Description/# [Docker Hub Description](https:\/\/github.com\/peter-evans\/dockerhub-description)/' README.md sed -i 's/# Docker Hub Description/# [Docker Hub Description](https:\/\/github.com\/peter-evans\/dockerhub-description)/' README.md
sed -i 's/(LICENSE)/(https:\/\/github.com\/peter-evans\/dockerhub-description\/blob\/master\/LICENSE)/' README.md sed -i 's/(LICENSE)/(https:\/\/github.com\/peter-evans\/dockerhub-description\/blob\/master\/LICENSE)/' README.md
- name: Test non-default filepath
run: |
mkdir -p some/path
mv README.md some/path/TEST.md
- name: Docker Hub Description - name: Docker Hub Description
uses: ./ uses: ./
env: with:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} dockerhub-username: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }} dockerhub-password: ${{ secrets.DOCKERHUB_PASSWORD }}
DOCKERHUB_REPOSITORY: peterevans/dockerhub-description dockerhub-repository: peterevans/dockerhub-description
README_FILEPATH: ./some/path/TEST.md

41
.github/workflows/publish-docker.yml vendored Normal file
View file

@ -0,0 +1,41 @@
name: Publish Docker Image
on:
push:
branches:
- master
tags:
- v*
env:
IMAGE_NAME: peterevans/dockerhub-description
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Docker Hub login
run: echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin
- name: Push image to Docker Hub
run: |
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
# Use Docker `latest` tag convention
[ "$VERSION" == "master" ] && VERSION=latest
# Build and tag image
docker build . --file Dockerfile --tag $IMAGE_NAME --label "org.opencontainers.image.version=$VERSION"
docker tag $IMAGE_NAME $IMAGE_NAME:$VERSION
# Tag with the minor version if valid
MINOR_VERSION=$(echo $VERSION | sed -n "s/^\([0-9]*.[0-9]*\).[0-9]*$/\1/p")
[[ ${#MINOR_VERSION} -gt 0 ]] && docker tag $IMAGE_NAME $IMAGE_NAME:$MINOR_VERSION
# Tag with the major version if valid
MAJOR_VERSION=$(echo $VERSION | sed -n "s/^\([0-9]*\).[0-9]*.[0-9]*$/\1/p")
[[ ${#MAJOR_VERSION} -gt 0 ]] && docker tag $IMAGE_NAME $IMAGE_NAME:$MAJOR_VERSION
docker push $IMAGE_NAME

31
.github/workflows/update-dep.yml vendored Normal file
View file

@ -0,0 +1,31 @@
name: Update Dependencies
on:
schedule:
- cron: '0 1 * * 4'
jobs:
update-dep:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '12.x'
- name: Update dependencies
run: |
npx -p npm-check-updates ncu -u
npm install
- name: Create Pull Request
uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.ACTIONS_BOT_TOKEN }}
commit-message: 'chore: update dependencies'
committer: GitHub <noreply@github.com>
author: actions-bot <actions-bot@users.noreply.github.com>
title: Update dependencies
body: |
- Dependency updates
Auto-generated by [create-pull-request][1]
[1]: https://github.com/peter-evans/create-pull-request
branch: update-dependencies

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
lib/
node_modules/

3
.prettierignore Normal file
View file

@ -0,0 +1,3 @@
dist/
lib/
node_modules/

11
.prettierrc.json Normal file
View file

@ -0,0 +1,11 @@
{
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": false,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": false,
"arrowParens": "avoid",
"parser": "typescript"
}

View file

@ -1,16 +1,18 @@
FROM peterevans/curl-jq:1.0.1 FROM node:12-alpine
LABEL maintainer="Peter Evans <mail@peterevans.dev>" LABEL \
LABEL repository="https://github.com/peter-evans/dockerhub-description" maintainer="Peter Evans <mail@peterevans.dev>" \
LABEL homepage="https://github.com/peter-evans/dockerhub-description" org.opencontainers.image.title="dockerhub-description" \
org.opencontainers.image.description="An action to update a Docker Hub repository description from README.md" \
LABEL com.github.actions.name="Docker Hub Description" org.opencontainers.image.authors="Peter Evans <mail@peterevans.dev>" \
LABEL com.github.actions.description="An action to update a Docker Hub repository description from README.md" org.opencontainers.image.url="https://github.com/peter-evans/dockerhub-description" \
LABEL com.github.actions.icon="upload" org.opencontainers.image.vendor="https://peterevans.dev" \
LABEL com.github.actions.color="blue" org.opencontainers.image.licenses="MIT"
COPY LICENSE README.md / COPY LICENSE README.md /
COPY dist/index.js /index.js
COPY entrypoint.sh /entrypoint.sh COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"] ENTRYPOINT ["/entrypoint.sh"]

View file

@ -10,36 +10,40 @@ This is useful if you `docker push` your images to Docker Hub. It provides an ea
```yml ```yml
- name: Docker Hub Description - name: Docker Hub Description
uses: peter-evans/dockerhub-description@v2 uses: peter-evans/dockerhub-description@v2
env: with:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} username: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }} password: ${{ secrets.DOCKERHUB_PASSWORD }}
DOCKERHUB_REPOSITORY: peterevans/dockerhub-description repository: peterevans/dockerhub-description
``` ```
#### Required environment variables ### Action inputs
- `DOCKERHUB_USERNAME` - Docker Hub username. If updating a Docker Hub repository belonging to an organization, this user must have `Admin` permissions for the repository. Aliases: `DOCKER_USERNAME` **Note**: Docker Hub [Personal Access Tokens](https://docs.docker.com/docker-hub/access-tokens/) cannot be used as they are not supported by the API. See [here](https://github.com/docker/hub-feedback/issues/1927) and [here](https://github.com/docker/hub-feedback/issues/1914) for further details. Unfortunately, this means that enabling 2FA on Docker Hub will prevent the action from working.
- `DOCKERHUB_PASSWORD` - Docker Hub password. Fallback to `DOCKER_PASSWORD` if set. Aliases: `DOCKER_PASSWORD`
- `DOCKERHUB_REPOSITORY` - The Docker Hub repository to update in the format `<namespace>/<name>`. May also be passed as a secret if considered sensitive. Aliases: `DOCKER_REPOSITORY`, `GITHUB_REPOSITORY`
**Note**: Docker Hub [Personal Access Tokens](https://docs.docker.com/docker-hub/access-tokens/) cannot be used as they are not supported by the API. See [here](https://github.com/docker/hub-feedback/issues/1927) and [here](https://github.com/docker/hub-feedback/issues/1914) for further details. Unfortunately, this means that enabling the new 2FA feature on Docker Hub will prevent the action from working. | Name | Description | Default |
| --- | --- | --- |
| `username` | (**required**) Docker Hub username. If updating a Docker Hub repository belonging to an organization, this user must have `Admin` permissions for the repository. | |
| `password` | (**required**) Docker Hub password. | |
| `repository` | Docker Hub repository in the format `<namespace>/<name>`. | `github.repository` |
| `short-description` | Docker Hub repository short description. Input exceeding 100 characters will be truncated. | |
| `readme-filepath` | Path to the repository readme. | `./README.md` |
#### Optionally specifying the file path #### Specifying the file path
The action assumes that there is a file called `README.md` located at the root of the repository. The action assumes that there is a file called `README.md` located at the root of the repository.
If this is not the case, the path can be overridden with an environment variable. If this is not the case the path can be specified with the `readme-filepath` input.
```yml ```yml
- name: Docker Hub Description - name: Docker Hub Description
uses: peter-evans/dockerhub-description@v2 uses: peter-evans/dockerhub-description@v2
env: with:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} username: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }} password: ${{ secrets.DOCKERHUB_PASSWORD }}
DOCKERHUB_REPOSITORY: peterevans/dockerhub-description repository: peterevans/dockerhub-description
README_FILEPATH: ./some-path/README.md readme-filepath: ./path/to/README.md
``` ```
#### Examples ### Examples
The following workflow updates the Docker Hub repository description whenever there are changes to `README.md` and the workflow file itself on the `master` branch. This workflow assumes its location to be `.github/workflows/dockerhub-description.yml`. The following workflow updates the Docker Hub repository description whenever there are changes to `README.md` and the workflow file itself on the `master` branch. This workflow assumes its location to be `.github/workflows/dockerhub-description.yml`.
@ -57,12 +61,13 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Docker Hub Description - name: Docker Hub Description
uses: peter-evans/dockerhub-description@v2 uses: peter-evans/dockerhub-description@v2
env: with:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} username: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }} password: ${{ secrets.DOCKERHUB_PASSWORD }}
DOCKERHUB_REPOSITORY: peterevans/dockerhub-description repository: peterevans/dockerhub-description
``` ```
Updates the Docker Hub repository description whenever a new release is created. Updates the Docker Hub repository description whenever a new release is created.
@ -75,12 +80,13 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Docker Hub Description - name: Docker Hub Description
uses: peter-evans/dockerhub-description@v2 uses: peter-evans/dockerhub-description@v2
env: with:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} username: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }} password: ${{ secrets.DOCKERHUB_PASSWORD }}
DOCKERHUB_REPOSITORY: peterevans/dockerhub-description repository: peterevans/dockerhub-description
``` ```
## Using the Docker image independently of GitHub Actions ## Using the Docker image independently of GitHub Actions
@ -94,7 +100,7 @@ docker run -v $PWD:/workspace \
-e DOCKERHUB_PASSWORD='xxxxx' \ -e DOCKERHUB_PASSWORD='xxxxx' \
-e DOCKERHUB_REPOSITORY='user1/my-docker-image' \ -e DOCKERHUB_REPOSITORY='user1/my-docker-image' \
-e README_FILEPATH='/workspace/README.md' \ -e README_FILEPATH='/workspace/README.md' \
peterevans/dockerhub-description:2.3 peterevans/dockerhub-description:2
``` ```
## License ## License

View file

@ -1,22 +1,28 @@
name: 'Docker Hub Description' name: 'Docker Hub Description'
author: 'Peter Evans' author: 'Peter Evans'
description: 'An action to update a Docker Hub repository description from README.md' description: 'An action to update a Docker Hub repository description from README.md'
# env: inputs:
# DOCKERHUB_USERNAME: username:
# description: Username to login to Docker Hub. Aliases: DOCKER_USERNAME description: Docker Hub username
# required: true required: true
# DOCKERHUB_PASSWORD: password:
# description: Password to login to Docker Hub. Aliases: DOCKER_PASSWORD description: Docker Hub password
# required: true required: true
# DOCKERHUB_REPOSITORY: repository:
# description: Explicit Docker Hub repository name. Aliases: DOCKER_REPOSITORY, GITHUB_REPOSITORY description: >
# required: true Docker Hub repository in the format `<namespace>/<name>`
# README_FILEPATH: Default: `github.repository`
# description: Path to the repository readme. short-description:
# default: ./README.md description: >
Docker Hub repository short description
Input exceeding 100 characters will be truncated
readme-filepath:
description: >
Path to the repository readme
Default: `./README.md`
runs: runs:
using: 'docker' using: 'node12'
image: 'docker://peterevans/dockerhub-description:2.3.1' main: 'dist/index.js'
branding: branding:
icon: 'upload' icon: 'upload'
color: 'blue' color: 'blue'

2346
dist/index.js vendored Normal file

File diff suppressed because it is too large Load diff

View file

@ -2,56 +2,12 @@
set -eo pipefail set -eo pipefail
IFS=$'\n\t' IFS=$'\n\t'
# Allow DOCKERHUB_* variables to be set from their DOCKER_* variant # Execute the action code and output to file
DOCKERHUB_USERNAME=${DOCKERHUB_USERNAME:-${DOCKER_USERNAME}} node index.js > action.log 2>&1
DOCKERHUB_PASSWORD=${DOCKERHUB_PASSWORD:-${DOCKER_PASSWORD}}
DOCKERHUB_REPOSITORY=${DOCKERHUB_REPOSITORY:-${DOCKER_REPOSITORY}}
# If the repository isn't explicitly defined, infer it from GitHub if possible # Remove lines containing sensitive information from the log
DOCKERHUB_REPOSITORY=${DOCKERHUB_REPOSITORY:-${GITHUB_REPOSITORY}} sed -i '/::debug::/d' ./action.log
sed -i '/::add-mask::/d' ./action.log
# Validate we can authenticate # Output the log
if [ -z "$DOCKERHUB_USERNAME" ] || [ -z "$DOCKERHUB_PASSWORD" ]; then cat action.log
echo 'Unable to authenticate with Docker Hub, set a valid $DOCKERHUB_USERNAME and $DOCKERHUB_PASSWORD'
exit 1
fi
# Validate we have the repository name
if [ -z "$DOCKERHUB_REPOSITORY" ]; then
echo 'Unable to determine Docker Hub repository name, set with $DOCKERHUB_REPOSITORY'
exit 1
fi
# Set the default path to README.md
README_FILEPATH=${README_FILEPATH:="./README.md"}
# Check the file exists
if [ ! -f ${README_FILEPATH} ]; then
echo "Readme file not found"
exit 1
fi
# Check the file size
if [ $(wc -c <${README_FILEPATH}) -gt 25000 ]; then
echo "File size exceeds the maximum allowed 25000 bytes"
exit 1
fi
# Acquire a token for the Docker Hub API
echo "Acquiring token"
LOGIN_PAYLOAD="{\"username\": \"${DOCKERHUB_USERNAME}\", \"password\": \"${DOCKERHUB_PASSWORD}\"}"
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d ${LOGIN_PAYLOAD} https://hub.docker.com/v2/users/login/ | jq -r .token)
# Send a PATCH request to update the description of the repository
echo "Sending PATCH request"
REPO_URL="https://hub.docker.com/v2/repositories/${DOCKERHUB_REPOSITORY}/"
RESPONSE_CODE=$(curl -s --write-out %{response_code} --output /dev/null -H "Authorization: JWT ${TOKEN}" -X PATCH --data-urlencode full_description@${README_FILEPATH} ${REPO_URL})
echo "Received response code: $RESPONSE_CODE"
if [ $RESPONSE_CODE -eq 200 ]; then
echo "Request successful"
exit 0
else
echo "Request failed"
exit 1
fi

11
jest.config.js Normal file
View file

@ -0,0 +1,11 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
testRunner: 'jest-circus/runner',
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true
}

7572
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

48
package.json Normal file
View file

@ -0,0 +1,48 @@
{
"name": "dockerhub-description",
"version": "3.0.0",
"private": true,
"description": "An action to update a Docker Hub repository description from README.md",
"main": "lib/main.js",
"scripts": {
"build": "tsc && ncc build",
"format": "prettier --write '**/*.ts'",
"format-check": "prettier --check '**/*.ts'",
"lint": "eslint src/**/*.ts",
"test": "jest --passWithNoTests"
},
"repository": {
"type": "git",
"url": "git+https://github.com/peter-evans/dockerhub-description.git"
},
"keywords": [
"actions",
"dockerhub",
"description"
],
"author": "Peter Evans",
"license": "MIT",
"bugs": {
"url": "https://github.com/peter-evans/dockerhub-description/issues"
},
"homepage": "https://github.com/peter-evans/dockerhub-description#readme",
"dependencies": {
"@actions/core": "1.2.5",
"node-fetch": "2.6.1"
},
"devDependencies": {
"@types/jest": "26.0.14",
"@types/node": "14.10.3",
"@typescript-eslint/parser": "4.1.1",
"@vercel/ncc": "0.24.1",
"eslint": "7.9.0",
"eslint-plugin-github": "4.1.1",
"eslint-plugin-jest": "24.0.1",
"jest": "26.4.2",
"jest-circus": "26.4.2",
"js-yaml": "3.14.0",
"prettier": "2.1.2",
"ts-jest": "26.3.0",
"typescript": "4.0.2"
}
}

44
src/dockerhub-helper.ts Normal file
View file

@ -0,0 +1,44 @@
import * as core from '@actions/core'
import * as fetch from 'node-fetch'
const DESCRIPTION_MAX_CHARS = 100
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,
description: string,
fullDescription: string
): Promise<void> {
const body = {
full_description: fullDescription
}
if (description) {
body['description'] = description.slice(0, DESCRIPTION_MAX_CHARS)
}
await fetch(`https://hub.docker.com/v2/repositories/${repository}`, {
method: 'patch',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
Authorization: `JWT ${token}`
}
})
}

73
src/input-helper.ts Normal file
View file

@ -0,0 +1,73 @@
import * as core from '@actions/core'
const README_FILEPATH_DEFAULT = './README.md'
interface Inputs {
username: string
password: string
repository: string
shortDescription: string
readmeFilepath: string
}
export function getInputs(): Inputs {
const inputs = {
username: core.getInput('username'),
password: core.getInput('password'),
repository: core.getInput('repository'),
shortDescription: core.getInput('short-description'),
readmeFilepath: core.getInput('readme-filepath')
}
// Environment variable input alternatives and their aliases
if (!inputs.username && process.env['DOCKERHUB_USERNAME']) {
inputs.username = process.env['DOCKERHUB_USERNAME']
}
if (!inputs.username && process.env['DOCKER_USERNAME']) {
inputs.username = process.env['DOCKER_USERNAME']
}
if (!inputs.password && process.env['DOCKERHUB_PASSWORD']) {
inputs.password = process.env['DOCKERHUB_PASSWORD']
}
if (!inputs.password && process.env['DOCKER_PASSWORD']) {
inputs.password = process.env['DOCKER_PASSWORD']
}
if (!inputs.repository && process.env['DOCKERHUB_REPOSITORY']) {
inputs.repository = process.env['DOCKERHUB_REPOSITORY']
}
if (!inputs.repository && process.env['DOCKER_REPOSITORY']) {
inputs.repository = process.env['DOCKER_REPOSITORY']
}
if (!inputs.shortDescription && process.env['SHORT_DESCRIPTION']) {
inputs.shortDescription = process.env['SHORT_DESCRIPTION']
}
if (!inputs.readmeFilepath && process.env['README_FILEPATH']) {
inputs.readmeFilepath = process.env['README_FILEPATH']
}
// Set defaults
if (!inputs.readmeFilepath) {
inputs.readmeFilepath = README_FILEPATH_DEFAULT
}
if (!inputs.repository && process.env['GITHUB_REPOSITORY']) {
inputs.repository = process.env['GITHUB_REPOSITORY']
}
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.username, 'username')
checkRequiredInput(inputs.password, 'password')
}

48
src/main.ts Normal file
View file

@ -0,0 +1,48 @@
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.username,
inputs.password
)
// Send a PATCH request to update the description of the repository
core.info('Sending PATCH request')
await dockerhubHelper.updateRepositoryDescription(
token,
inputs.repository,
inputs.shortDescription,
readmeContent
)
core.info('Request successful')
} catch (error) {
core.debug(inspect(error))
core.setFailed(error.message)
}
}
run()

16
tsconfig.json Normal file
View file

@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": [
"es6"
],
"outDir": "./lib",
"rootDir": "./src",
"declaration": true,
"strict": true,
"noImplicitAny": false,
"esModuleInterop": true
},
"exclude": ["__test__", "lib", "node_modules"]
}