Skip to content

Commit 803fd35

Browse files
committed
fix: gist_description will override the original description if not provided
1 parent f223946 commit 803fd35

File tree

4 files changed

+47
-25
lines changed

4 files changed

+47
-25
lines changed

__tests__/index.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import dotenv from 'dotenv'
44
import { getInput } from '../src/input'
55

66
test('input', () => {
7+
process.env.INPUT_TOKEN = 'token'
78
process.env.INPUT_GIST_ID = 'gist_id'
89
process.env.INPUT_GIST_DESCRIPTION = 'gist_description'
910
process.env.INPUT_GIST_FILE_NAME = 'gist_file_name'
1011
process.env.INPUT_FILE_PATH = 'file_path'
1112

1213
const input = getInput()
1314

14-
expect(input.token).toMatch('')
15+
expect(input.token).toMatch('token')
1516
expect(input.gistId).toMatch('gist_id')
1617
expect(input.gistDescription).toMatch('gist_description')
1718
expect(input.gistFileName).toMatch('gist_file_name')
@@ -20,6 +21,7 @@ test('input', () => {
2021

2122
test('run', () => {
2223
process.env.GITHUB_WORKSPACE = process.cwd()
24+
delete process.env.INPUT_TOKEN
2325
dotenv.config()
2426
process.env.INPUT_GIST_ID = 'e885afa349a0e5d1cfb408e46d6a37bc'
2527
process.env.INPUT_GIST_DESCRIPTION = 'foo bar'

dist/index.js

Lines changed: 17 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/input.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
1-
import { getInput as getActionInput } from '@actions/core'
1+
// https://github.com/actions/toolkit/blob/27f76dfe1afb2b7e5e679cd8e97192d34d8320e6/packages/core/src/core.ts#L128
2+
function getInputFromEnv(name: string, options: { required: true }): string
3+
function getInputFromEnv(
4+
name: string,
5+
options?: Record<string, never> | { required: false }
6+
): string | undefined
7+
function getInputFromEnv(
8+
name: string,
9+
options: { required?: boolean } = {}
10+
): string | undefined {
11+
const value = process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`]
12+
const { required = false } = options
13+
if (required && value === undefined) {
14+
throw new Error(`Input required and not supplied: ${name}`)
15+
}
16+
return value?.trim()
17+
}
218

319
type Input = Readonly<{
420
token: string
521
gistId: string
6-
gistDescription: string
7-
gistFileName: string
22+
gistDescription: string | undefined
23+
gistFileName: string | undefined
824
filePath: string
925
}>
1026

1127
export const getInput = (): Input => {
1228
return {
13-
token: getActionInput('token'),
14-
gistId: getActionInput('gist_id'),
15-
gistDescription: getActionInput('gist_description'),
16-
gistFileName: getActionInput('gist_file_name'),
17-
filePath: getActionInput('file_path')
29+
token: getInputFromEnv('token', { required: true }),
30+
gistId: getInputFromEnv('gist_id', { required: true }),
31+
gistDescription: getInputFromEnv('gist_description'),
32+
gistFileName: getInputFromEnv('gist_file_name'),
33+
filePath: getInputFromEnv('file_path', { required: true })
1834
}
1935
}

src/run.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ export const run = async (): Promise<void> => {
99

1010
const workSpace = process.env.GITHUB_WORKSPACE!
1111
const filePath = path.join(workSpace, input.filePath)
12-
const fileName =
13-
input.gistFileName.length === 0
14-
? path.basename(filePath)
15-
: input.gistFileName
12+
13+
const fileName = input.gistFileName ?? path.basename(filePath)
1614

1715
startGroup('Dump inputs')
1816
info(`\
1917
[INFO] GistId: ${input.gistId}${
20-
input.gistDescription.length === 0
18+
input.gistDescription === undefined
2119
? ''
2220
: `\n[INFO] GistDescription: ${input.gistDescription}`
2321
}

0 commit comments

Comments
 (0)