Skip to content

Commit 4953034

Browse files
committed
fix: eslint errors
1 parent 16dd779 commit 4953034

File tree

2 files changed

+77
-71
lines changed

2 files changed

+77
-71
lines changed

src/main.ts

Lines changed: 62 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as core from '@actions/core'
22
import github from '@actions/github'
33
import { INSTAWP_ACTIONS } from './constants'
4-
import type { CreateSiteTemplateGitInput, InstaWPAction } from './types'
4+
import type { CreateSiteTemplateGitInput } from './types'
55
import {
66
createSiteGit,
77
getWorkflowInput,
@@ -34,91 +34,85 @@ export async function run(): Promise<void> {
3434
const pullRequestsNo = github.context.payload.pull_request?.number ?? 0
3535
const repo = github.context.repo
3636

37-
switch (INSTAWP_ACTION) {
38-
case 'create-site-template-git':
39-
core.info(`Creating site template with slug: ${INSTAWP_TEMPLATE_SLUG}`)
37+
if (INSTAWP_ACTION === 'create-site-template-git') {
38+
core.info(`Creating site template with slug: ${INSTAWP_TEMPLATE_SLUG}`)
39+
40+
const config = {
41+
template_slug: INSTAWP_TEMPLATE_SLUG,
42+
site_name: github.context.repo.repo,
43+
pr_num: pullRequestsNo,
44+
repo_id: REPO_ID,
45+
override_url: ARTIFACT_URL ?? undefined
46+
} satisfies CreateSiteTemplateGitInput
47+
48+
try {
49+
const response = await createSiteGit(config, {
50+
token: INSTAWP_TOKEN
51+
})
52+
53+
if (!response?.data) {
54+
const msg = response?.message ?? 'No data returned from InstaWP API'
55+
core.setFailed(`Failed to create site template: ${msg}`)
56+
}
57+
58+
core.info(`Site template created at: ${response.data.wp_url}`)
4059

41-
const config = {
42-
template_slug: INSTAWP_TEMPLATE_SLUG,
43-
site_name: github.context.repo.repo,
44-
pr_num: pullRequestsNo,
45-
repo_id: REPO_ID,
46-
override_url: ARTIFACT_URL ?? undefined
47-
} satisfies CreateSiteTemplateGitInput
60+
const taskId = response.data.task_id
4861

49-
try {
50-
const response = await createSiteGit(config, {
62+
let taskInProgress = true
63+
while (taskInProgress) {
64+
const taskStatus = await getTaskStatus(taskId, {
5165
token: INSTAWP_TOKEN
5266
})
5367

54-
if (!response?.data) {
55-
const msg = response?.message ?? 'No data returned from InstaWP API'
56-
core.setFailed(`Failed to create site template: ${msg}`)
68+
if (taskStatus.data.status !== 'progress') {
69+
core.info(`Site created at: ${response.data.wp_url}`)
70+
break
5771
}
5872

59-
core.info(`Site template created at: ${response.data.wp_url}`)
73+
taskInProgress = true
6074

61-
const taskId = response.data.task_id
62-
63-
while (true) {
64-
const taskStatus = await getTaskStatus(taskId, {
65-
token: INSTAWP_TOKEN
66-
})
75+
core.info(`Waiting for site creation...`)
76+
await new Promise(resolve => setTimeout(resolve, 2000))
77+
}
6778

68-
if (taskStatus.data.status !== 'progress') {
69-
core.info(`Site created at: ${response.data.wp_url}`)
70-
break
71-
}
79+
const wpUrl = response.data.wp_url
80+
const wpMagicLoginLink = getMagicLink(response.data.s_hash)
7281

73-
core.info(`Waiting for site creation...`)
74-
await new Promise(resolve => setTimeout(resolve, 2000))
75-
}
82+
core.setOutput('instawp_url', wpUrl)
83+
core.setOutput('instawp_magic_login_url', wpMagicLoginLink)
7684

77-
const wpUrl = response.data.wp_url
78-
const wpMagicLoginLink = getMagicLink(response.data.s_hash)
85+
if (pullRequestsNo > 0) {
86+
const comments = await octokit.rest.issues.listComments({
87+
...repo,
88+
issue_number: pullRequestsNo,
89+
per_page: 100
90+
})
7991

80-
core.setOutput('instawp_url', wpUrl)
81-
core.setOutput('instawp_magic_login_url', wpMagicLoginLink)
92+
const comment = comments.data.find(c =>
93+
c?.body?.includes('<!-- INSTAWP-COMMENT -->')
94+
)
8295

83-
if (pullRequestsNo > 0) {
84-
const comments = await octokit.rest.issues.listComments({
96+
if (undefined === comment) {
97+
await octokit.rest.issues.createComment({
8598
...repo,
8699
issue_number: pullRequestsNo,
87-
per_page: 100
100+
body: `<!-- INSTAWP-COMMENT -->\nWordPress Instance Deployed.\n\nURL: [${wpUrl}](${wpUrl})\nMagic Login: [${wpMagicLoginLink}](${wpMagicLoginLink})`
101+
})
102+
} else {
103+
await octokit.rest.issues.updateComment({
104+
...repo,
105+
issue_number: pullRequestsNo,
106+
comment_id: comment.id,
107+
body: `<!-- INSTAWP-COMMENT -->\nWordPress Instance Deployed.\n\nURL: [${wpUrl}](${wpUrl})\nMagic Login: [${wpMagicLoginLink}](${wpMagicLoginLink})`
88108
})
89-
90-
const comment = comments.data.find(comment =>
91-
comment?.body?.includes('<!-- INSTAWP-COMMENT -->')
92-
)
93-
94-
if (undefined === comment) {
95-
await octokit.rest.issues.createComment({
96-
...repo,
97-
issue_number: pullRequestsNo,
98-
body: `<!-- INSTAWP-COMMENT -->\nWordPress Instance Deployed.\n\nURL: [${wpUrl}](${wpUrl})\nMagic Login: [${wpMagicLoginLink}](${wpMagicLoginLink})`
99-
})
100-
} else {
101-
await octokit.rest.issues.updateComment({
102-
...repo,
103-
issue_number: pullRequestsNo,
104-
comment_id: comment.id,
105-
body: `<!-- INSTAWP-COMMENT -->\nWordPress Instance Deployed.\n\nURL: [${wpUrl}](${wpUrl})\nMagic Login: [${wpMagicLoginLink}](${wpMagicLoginLink})`
106-
})
107-
}
108109
}
109-
} catch (error) {
110-
const err = error as Error
111-
core.setFailed(err.message)
112110
}
113-
114-
break
115-
case 'destroy-site':
116-
core.info('Destroying site is not yet implemented')
117-
118-
break
119-
default:
120-
core.setFailed(`Invalid action: ${INSTAWP_ACTION}`)
121-
}
111+
} catch (error) {
112+
const err = error as Error
113+
core.setFailed(err.message)
114+
}
115+
} else [core.setFailed(`${INSTAWP_ACTION} has not been implemented yet.`)]
122116
} catch (error) {
123117
// Fail the workflow run if an error occurs
124118
if (error instanceof Error) core.setFailed(error.message)

src/utils.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@ import type {
99
} from './types'
1010
import { INSTAWP_API_BASE } from './constants'
1111

12-
export const getMagicLink = (hash: string) => {
12+
export const getMagicLink = (hash: string): string => {
1313
const url = new URL('https://app.instawp.io/wordpress-auto-login')
1414
url.searchParams.set('site', hash)
1515

1616
return url.toString()
1717
}
1818

19-
export const getWorkflowInput = () => {
19+
export const getWorkflowInput = (): {
20+
GITHUB_TOKEN: string
21+
INSTAWP_TOKEN: string
22+
INSTAWP_ACTION: InstaWPAction
23+
INSTAWP_TEMPLATE_SLUG: string
24+
ARTIFACT_URL: string | null
25+
REPO_ID: string
26+
} => {
2027
const GITHUB_TOKEN = getInput('github-token')
2128
const REPO_ID = getInput('repo-id')
2229
const INSTAWP_TOKEN = getInput('instawp-token')
@@ -38,7 +45,12 @@ export const getWorkflowInput = () => {
3845
}
3946
}
4047

41-
export const commonHeaders = (token: string) => ({
48+
export const commonHeaders = (
49+
token: string
50+
): {
51+
authorization: string
52+
'content-type': string
53+
} => ({
4254
authorization: `Bearer ${token}`,
4355
'content-type': 'application/json'
4456
})

0 commit comments

Comments
 (0)