diff --git a/__tests__/Inputs.test.ts b/__tests__/Inputs.test.ts new file mode 100644 index 0000000..e7bc3a3 --- /dev/null +++ b/__tests__/Inputs.test.ts @@ -0,0 +1,23 @@ +const mockGetInput = jest.fn(); + +import { Inputs } from "../src/Inputs"; +import { Context } from "@actions/github/lib/context"; + + +jest.mock('@actions/core', () => { + return {getInput: mockGetInput}; +}); + +describe('Inputs', () => { + let context: Context + let inputs: Inputs + beforeEach(() => { + context = new Context() + inputs = new Inputs(context) + mockGetInput.mockReturnValue("") + }) + it('returns token', () => { + mockGetInput.mockReturnValue("42") + expect(inputs.token).toBe("42") + }) +}) \ No newline at end of file diff --git a/src/Inputs.ts b/src/Inputs.ts new file mode 100644 index 0000000..1bbb202 --- /dev/null +++ b/src/Inputs.ts @@ -0,0 +1,29 @@ +import * as core from '@actions/core'; +import { Context } from "@actions/github/lib/context"; + +export class Inputs { + private context: Context + + constructor(context: Context) { + this.context = context; + } + + get token(): string { + return core.getInput('token', {required: true}); + } + + get tag() : string { + const tag = core.getInput('tag'); + if(tag != null) { + return tag; + } + + const ref = this.context.ref; + const tagPath = "refs/tags/"; + if(ref.startsWith(tagPath)) { + return ref.substr(0, tagPath.length); + } + + throw Error("No tag found in ref or input!") + } +} \ No newline at end of file diff --git a/src/Main.ts b/src/Main.ts index 2e2c4b3..1dba5b4 100644 --- a/src/Main.ts +++ b/src/Main.ts @@ -1,5 +1,6 @@ import * as github from '@actions/github'; import * as core from '@actions/core'; +import { Inputs } from './Inputs'; import { Releases } from './Releases'; async function run() { @@ -8,7 +9,8 @@ async function run() { const context = github.context const git = new github.GitHub(token); const releases = new Releases(context, git) - await releases.create() + const inputs = new Inputs(context) + await releases.create(inputs.tag) .catch(error => { core.warning(error) }) diff --git a/src/Releases.ts b/src/Releases.ts index 1f205c7..a453b73 100644 --- a/src/Releases.ts +++ b/src/Releases.ts @@ -11,14 +11,14 @@ export class Releases { this.git = git; } - async create(): Promise> { + async create(tag: string, commitHash?: string): Promise> { return this.git.repos.createRelease({ name: "Test release", draft: true, owner: this.context.repo.owner, repo: this.context.repo.repo, - target_commitish: "master", - tag_name: "0.0.666" + target_commitish: commitHash, + tag_name: tag }) } }