Add token test.

This commit is contained in:
Nick Cipollo
2019-08-26 12:15:12 -04:00
parent 0412ec85b5
commit 488aa36d03
4 changed files with 58 additions and 4 deletions

23
__tests__/Inputs.test.ts Normal file
View File

@@ -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")
})
})

29
src/Inputs.ts Normal file
View File

@@ -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!")
}
}

View File

@@ -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)
})

View File

@@ -11,14 +11,14 @@ export class Releases {
this.git = git;
}
async create(): Promise<Response<ReposCreateReleaseResponse>> {
async create(tag: string, commitHash?: string): Promise<Response<ReposCreateReleaseResponse>> {
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
})
}
}