Add token test.
This commit is contained in:
23
__tests__/Inputs.test.ts
Normal file
23
__tests__/Inputs.test.ts
Normal 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
29
src/Inputs.ts
Normal 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!")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import * as github from '@actions/github';
|
import * as github from '@actions/github';
|
||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
|
import { Inputs } from './Inputs';
|
||||||
import { Releases } from './Releases';
|
import { Releases } from './Releases';
|
||||||
|
|
||||||
async function run() {
|
async function run() {
|
||||||
@@ -8,7 +9,8 @@ async function run() {
|
|||||||
const context = github.context
|
const context = github.context
|
||||||
const git = new github.GitHub(token);
|
const git = new github.GitHub(token);
|
||||||
const releases = new Releases(context, git)
|
const releases = new Releases(context, git)
|
||||||
await releases.create()
|
const inputs = new Inputs(context)
|
||||||
|
await releases.create(inputs.tag)
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
core.warning(error)
|
core.warning(error)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -11,14 +11,14 @@ export class Releases {
|
|||||||
this.git = git;
|
this.git = git;
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(): Promise<Response<ReposCreateReleaseResponse>> {
|
async create(tag: string, commitHash?: string): Promise<Response<ReposCreateReleaseResponse>> {
|
||||||
return this.git.repos.createRelease({
|
return this.git.repos.createRelease({
|
||||||
name: "Test release",
|
name: "Test release",
|
||||||
draft: true,
|
draft: true,
|
||||||
owner: this.context.repo.owner,
|
owner: this.context.repo.owner,
|
||||||
repo: this.context.repo.repo,
|
repo: this.context.repo.repo,
|
||||||
target_commitish: "master",
|
target_commitish: commitHash,
|
||||||
tag_name: "0.0.666"
|
tag_name: tag
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user