mirror of
https://github.com/marocchino/sticky-pull-request-comment.git
synced 2026-04-21 13:35:44 +00:00
refactor: several improvements (#262)
- more tests - file read in config - reduce if depth - reduce unnecessary requests
This commit is contained in:
parent
cc76ed82aa
commit
322a2451da
8 changed files with 234 additions and 109 deletions
|
|
@ -1,13 +1,12 @@
|
|||
import * as core from '@actions/core'
|
||||
import {context} from '@actions/github'
|
||||
import {readFileSync} from 'fs'
|
||||
|
||||
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'
|
||||
|
|
@ -15,9 +14,25 @@ export const deleteOldComment =
|
|||
core.getInput('delete', {required: true}) === 'true'
|
||||
export const githubToken = core.getInput('GITHUB_TOKEN', {required: true})
|
||||
|
||||
export const body = buildBody()
|
||||
|
||||
function buildRepo(): {repo: string; owner: string} {
|
||||
return {
|
||||
owner: context.repo.owner,
|
||||
repo: core.getInput('repo', {required: false}) || context.repo.repo
|
||||
}
|
||||
}
|
||||
|
||||
function buildBody(): string {
|
||||
const path = core.getInput('path', {required: false})
|
||||
if (path) {
|
||||
try {
|
||||
return readFileSync(path, 'utf-8')
|
||||
} catch (error) {
|
||||
core.setFailed(error.message)
|
||||
return ''
|
||||
}
|
||||
} else {
|
||||
return core.getInput('message', {required: false})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
76
src/main.ts
76
src/main.ts
|
|
@ -9,15 +9,13 @@ import {
|
|||
import {
|
||||
pullRequestNumber,
|
||||
repo,
|
||||
message,
|
||||
path,
|
||||
body,
|
||||
header,
|
||||
append,
|
||||
recreate,
|
||||
deleteOldComment,
|
||||
githubToken
|
||||
} from './config'
|
||||
import {readFileSync} from 'fs'
|
||||
|
||||
async function run(): Promise<undefined> {
|
||||
if (isNaN(pullRequestNumber) || pullRequestNumber < 1) {
|
||||
|
|
@ -26,6 +24,14 @@ async function run(): Promise<undefined> {
|
|||
}
|
||||
|
||||
try {
|
||||
if (!deleteOldComment && !body) {
|
||||
throw new Error('Either message or path input is required')
|
||||
}
|
||||
|
||||
if (deleteOldComment && recreate) {
|
||||
throw new Error('delete and recreate cannot be both set to true')
|
||||
}
|
||||
|
||||
const octokit = github.getOctokit(githubToken)
|
||||
const previous = await findPreviousComment(
|
||||
octokit,
|
||||
|
|
@ -34,49 +40,31 @@ async function run(): Promise<undefined> {
|
|||
header
|
||||
)
|
||||
|
||||
if (!deleteOldComment && !message && !path) {
|
||||
throw new Error('Either message or path input is required')
|
||||
}
|
||||
|
||||
if (deleteOldComment && recreate) {
|
||||
throw new Error('delete and recreate cannot be both set to true')
|
||||
}
|
||||
|
||||
let body
|
||||
|
||||
if (path) {
|
||||
body = readFileSync(path, 'utf-8')
|
||||
} else {
|
||||
body = message
|
||||
}
|
||||
|
||||
if (previous) {
|
||||
const previousBody = append ? previous.body : undefined
|
||||
if (deleteOldComment) {
|
||||
await deleteComment(octokit, repo, previous.id)
|
||||
} else if (recreate) {
|
||||
await deleteComment(octokit, repo, previous.id)
|
||||
await createComment(
|
||||
octokit,
|
||||
repo,
|
||||
pullRequestNumber,
|
||||
body,
|
||||
header,
|
||||
previousBody
|
||||
)
|
||||
} else {
|
||||
await updateComment(
|
||||
octokit,
|
||||
repo,
|
||||
previous.id,
|
||||
body,
|
||||
header,
|
||||
previousBody
|
||||
)
|
||||
}
|
||||
} else {
|
||||
if (!previous) {
|
||||
await createComment(octokit, repo, pullRequestNumber, body, header)
|
||||
return
|
||||
}
|
||||
|
||||
if (deleteOldComment) {
|
||||
await deleteComment(octokit, repo, previous.id)
|
||||
return
|
||||
}
|
||||
|
||||
const previousBody = append ? previous.body : undefined
|
||||
if (recreate) {
|
||||
await deleteComment(octokit, repo, previous.id)
|
||||
await createComment(
|
||||
octokit,
|
||||
repo,
|
||||
pullRequestNumber,
|
||||
body,
|
||||
header,
|
||||
previousBody
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
await updateComment(octokit, repo, previous.id, body, header, previousBody)
|
||||
} catch (error) {
|
||||
core.setFailed(error.message)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue