Skip to content

Commit

Permalink
feat: update comment option
Browse files Browse the repository at this point in the history
  • Loading branch information
multiplehats committed Apr 25, 2024
1 parent 8812c29 commit d404659
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ inputs:
'timeout-seconds':
description: 'Timeout in seconds'
required: false
'update-comment':
description: 'Whether or not to update any comments or to create a new one'
required: false

outputs:
instawp_url:
Expand Down
20 changes: 12 additions & 8 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

22 changes: 14 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export async function run(): Promise<void> {
INSTAWP_TEMPLATE_SLUG,
REPO_ID,
ARTIFACT_URL,
TIMEOUT_SECONDS
TIMEOUT_SECONDS,
UPDATE_COMMENT
} = getWorkflowInput()

if (!INSTAWP_ACTIONS.includes(INSTAWP_ACTION)) {
Expand Down Expand Up @@ -126,7 +127,8 @@ export async function run(): Promise<void> {
repo,
pullRequestsNo,
wpURL,
wpMagicLoginLink
wpMagicLoginLink,
updateComment: UPDATE_COMMENT
})
} else {
core.setFailed(`${INSTAWP_ACTION} has not been implemented yet.`)
Expand All @@ -141,13 +143,15 @@ async function updateOrCreateComment({
repo,
pullRequestsNo,
wpURL,
wpMagicLoginLink
wpMagicLoginLink,
updateComment
}: {
octokit: ReturnType<typeof getOctokit>
repo: { owner: string; repo: string }
pullRequestsNo: number
wpURL: string
wpMagicLoginLink: string
updateComment: boolean
}) {
if (pullRequestsNo > 0) {
const comments = await octokit.rest.issues.listComments({
Expand All @@ -159,17 +163,19 @@ async function updateOrCreateComment({
c?.body?.includes('<!-- INSTAWP-COMMENT -->')
)
const body = `<!-- INSTAWP-COMMENT -->\nWordPress Instance Deployed.\n\nURL: [${wpURL}](${wpURL})\nMagic Login: [${wpMagicLoginLink}](${wpMagicLoginLink})`
if (undefined === comment) {
await octokit.rest.issues.createComment({

if (comment && updateComment) {
// Update existing comment if found and updateComment is true
await octokit.rest.issues.updateComment({
...repo,
issue_number: pullRequestsNo,
comment_id: comment.id,
body
})
} else {
await octokit.rest.issues.updateComment({
// Create a new comment if no existing comment is found or updateComment is false
await octokit.rest.issues.createComment({
...repo,
issue_number: pullRequestsNo,
comment_id: comment.id,
body
})
}
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ export const getWorkflowInput = (): {
ARTIFACT_URL: string | null
REPO_ID: string
TIMEOUT_SECONDS: number
UPDATE_COMMENT: boolean
} => {
const GITHUB_TOKEN = getInput('github-token')
const REPO_ID = getInput('repo-id')
const INSTAWP_TOKEN = getInput('instawp-token')
const INSTAWP_ACTION = getInput('instawp-action') as InstaWPAction
const INSTAWP_TEMPLATE_SLUG = getInput('instawp-template-slug')
const TIMEOUT_SECONDS = getInput('timeout-seconds', { required: false })
const UPDATE_COMMENT =
getInput('update-comment', { required: false }) ?? 'false'

const ARTIFACT_URL =
(getInput('instawp-artifact-zip-url', {
Expand All @@ -44,6 +47,7 @@ export const getWorkflowInput = (): {
INSTAWP_TEMPLATE_SLUG,
ARTIFACT_URL,
REPO_ID,
UPDATE_COMMENT: UPDATE_COMMENT === 'true' ? true : false,
TIMEOUT_SECONDS: TIMEOUT_SECONDS ? parseInt(TIMEOUT_SECONDS) : 120
}
}
Expand Down

0 comments on commit d404659

Please sign in to comment.