refactor: extract config (#257)

* refactor: extract config

* test: mock dynamic value
This commit is contained in:
marocchino 2021-03-22 09:39:18 +09:00 committed by GitHub
parent 9945cf63fa
commit 56ac27318d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 201 additions and 76 deletions

23
src/config.ts Normal file
View file

@ -0,0 +1,23 @@
import * as core from '@actions/core'
import {context} from '@actions/github'
export const pullRequestNumber =
context?.payload?.pull_request?.number ||
+core.getInput('number', {required: false})
export const repo = buildRepo()
export const message = core.getInput('message', {required: false})
export const path = core.getInput('path', {required: false})
export const header = core.getInput('header', {required: false})
export const append = core.getInput('append', {required: true}) === 'true'
export const recreate = core.getInput('recreate', {required: true}) === 'true'
export const deleteOldComment =
core.getInput('delete', {required: true}) === 'true'
export const githubToken = core.getInput('GITHUB_TOKEN', {required: true})
function buildRepo(): {repo: string; owner: string} {
return {
owner: context.repo.owner,
repo: core.getInput('repo', {required: false}) || context.repo.repo
}
}

View file

@ -6,32 +6,33 @@ import {
updateComment,
deleteComment
} from './comment'
import {
pullRequestNumber,
repo,
message,
path,
header,
append,
recreate,
deleteOldComment,
githubToken
} from './config'
import {readFileSync} from 'fs'
async function run(): Promise<undefined> {
const number =
github.context?.payload?.pull_request?.number ||
+core.getInput('number', {required: false})
if (isNaN(number) || number < 1) {
if (isNaN(pullRequestNumber) || pullRequestNumber < 1) {
core.info('no pull request numbers given: skip step')
return
}
try {
const repo = github.context.repo
repo.repo = core.getInput('repo', {required: false}) || repo.repo
const message = core.getInput('message', {required: false})
const path = core.getInput('path', {required: false})
const header = core.getInput('header', {required: false}) || ''
const append =
(core.getInput('append', {required: false}) || 'false') === 'true'
const recreate =
(core.getInput('recreate', {required: false}) || 'false') === 'true'
const deleteOldComment =
(core.getInput('delete', {required: false}) || 'false') === 'true'
const githubToken = core.getInput('GITHUB_TOKEN', {required: true})
const octokit = github.getOctokit(githubToken)
const previous = await findPreviousComment(octokit, repo, number, header)
const previous = await findPreviousComment(
octokit,
repo,
pullRequestNumber,
header
)
if (!deleteOldComment && !message && !path) {
throw new Error('Either message or path input is required')
@ -55,7 +56,14 @@ async function run(): Promise<undefined> {
await deleteComment(octokit, repo, previous.id)
} else if (recreate) {
await deleteComment(octokit, repo, previous.id)
await createComment(octokit, repo, number, body, header, previousBody)
await createComment(
octokit,
repo,
pullRequestNumber,
body,
header,
previousBody
)
} else {
await updateComment(
octokit,
@ -67,7 +75,7 @@ async function run(): Promise<undefined> {
)
}
} else {
await createComment(octokit, repo, number, body, header)
await createComment(octokit, repo, pullRequestNumber, body, header)
}
} catch (error) {
core.setFailed(error.message)