From ce5f096e714a0fbe8a1a05e2bdb31999d827505c Mon Sep 17 00:00:00 2001 From: Nick Cipollo Date: Mon, 26 Aug 2019 16:31:05 -0400 Subject: [PATCH] Add inputs --- __tests__/Inputs.test.ts | 57 ++++++++++++++++++++++++++++++++++++---- src/Inputs.ts | 47 +++++++++++++++++++++++++++++---- 2 files changed, 94 insertions(+), 10 deletions(-) diff --git a/__tests__/Inputs.test.ts b/__tests__/Inputs.test.ts index 9a942e4..b76223e 100644 --- a/__tests__/Inputs.test.ts +++ b/__tests__/Inputs.test.ts @@ -1,4 +1,5 @@ const mockGetInput = jest.fn(); +const mockReadFileSync = jest.fn(); import { Inputs } from "../src/Inputs"; import { Context } from "@actions/github/lib/context"; @@ -8,35 +9,81 @@ jest.mock('@actions/core', () => { return { getInput: mockGetInput }; }); +jest.mock('fs', () => { + return { readFileSync: mockReadFileSync }; +}); + describe('Inputs', () => { let context: Context; let inputs: Inputs; beforeEach(() => { + mockGetInput.mockReturnValue(null) context = new Context() inputs = new Inputs(context) }) - it('token method returns input token', () => { + it('returns artifact', () => { + mockGetInput.mockReturnValue("a/path") + expect(inputs.artifact).toBe("a/path") + }) + + it('returns targetCommit', () => { + mockGetInput.mockReturnValue("42") + expect(inputs.commit).toBe("42") + }) + + it('returns token', () => { mockGetInput.mockReturnValue("42") expect(inputs.token).toBe("42") }) + describe('description', () => { + it('returns input description', () => { + mockGetInput.mockReturnValue("description") + expect(inputs.description).toBe("description") + }) + + it('returns description file contents', () => { + mockGetInput.mockReturnValueOnce("").mockReturnValueOnce("a/path") + mockReadFileSync.mockReturnValue("file") + + expect(inputs.description).toBe("file") + }) + + it('returns empty', () => { + expect(inputs.description).toBe("") + }) + }) + + describe('name', () => { + it('returns input name', () => { + mockGetInput.mockReturnValue("name") + expect(inputs.name).toBe("name") + }) + + it('returns tag', () => { + mockGetInput.mockReturnValue("") + context.ref = "refs/tags/sha-tag" + expect(inputs.name).toBe("sha-tag") + }) + }) + describe('tag', () => { - it('tag property returns input tag', () => { + it('returns input tag', () => { mockGetInput.mockReturnValue("tag") expect(inputs.tag).toBe("tag") }) - it('tag property returns context sha when input is empty', () => { + it('returns context sha when input is empty', () => { mockGetInput.mockReturnValue("") context.ref = "refs/tags/sha-tag" expect(inputs.tag).toBe("sha-tag") }) - it('tag property returns context sha when input is null', () => { + it('returns context sha when input is null', () => { mockGetInput.mockReturnValue(null) context.ref = "refs/tags/sha-tag" expect(inputs.tag).toBe("sha-tag") }) - it('tag property throws if no tag', () => { + it('throws if no tag', () => { expect(() => inputs.tag).toThrow() }) }) diff --git a/src/Inputs.ts b/src/Inputs.ts index c3534b4..81edaf0 100644 --- a/src/Inputs.ts +++ b/src/Inputs.ts @@ -1,5 +1,7 @@ import * as core from '@actions/core'; import { Context } from "@actions/github/lib/context"; +import { isObject } from 'util'; +import { fstat, readFileSync } from 'fs'; export class Inputs { private context: Context @@ -8,22 +10,57 @@ export class Inputs { this.context = context } - get token(): string { - return core.getInput('token', {required: true}) + get artifact(): string { + return core.getInput('artifact') } - get tag() : string { + get commit(): string { + return core.getInput('commit') + } + + get description(): string { + const description = core.getInput('description') + if(description) { + return description + } + + const descriptionFile = core.getInput('descriptionFile') + if(descriptionFile) { + return this.stringFromFile(descriptionFile) + } + + return '' + } + + get name(): string { + const name = core.getInput('tag') + if (name) { + return name + } + + return this.tag + } + + get token(): string { + return core.getInput('token', { required: true }) + } + + get tag(): string { const tag = core.getInput('tag') - if(tag) { + if (tag) { return tag; } const ref = this.context.ref const tagPath = "refs/tags/" - if(ref && ref.startsWith(tagPath)) { + if (ref && ref.startsWith(tagPath)) { return ref.substr(tagPath.length, ref.length) } throw Error("No tag found in ref or input!") } + + private stringFromFile(path: string): string { + return readFileSync(path, 'utf-8') + } } \ No newline at end of file