|
1 | 1 | import * as core from '@actions/core' |
2 | 2 | import github from '@actions/github' |
3 | 3 | import { INSTAWP_ACTIONS } from './constants' |
4 | | -import type { CreateSiteTemplateGitInput, InstaWPAction } from './types' |
| 4 | +import type { CreateSiteTemplateGitInput } from './types' |
5 | 5 | import { |
6 | 6 | createSiteGit, |
7 | 7 | getWorkflowInput, |
@@ -34,91 +34,85 @@ export async function run(): Promise<void> { |
34 | 34 | const pullRequestsNo = github.context.payload.pull_request?.number ?? 0 |
35 | 35 | const repo = github.context.repo |
36 | 36 |
|
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}`) |
40 | 59 |
|
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 |
48 | 61 |
|
49 | | - try { |
50 | | - const response = await createSiteGit(config, { |
| 62 | + let taskInProgress = true |
| 63 | + while (taskInProgress) { |
| 64 | + const taskStatus = await getTaskStatus(taskId, { |
51 | 65 | token: INSTAWP_TOKEN |
52 | 66 | }) |
53 | 67 |
|
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 |
57 | 71 | } |
58 | 72 |
|
59 | | - core.info(`Site template created at: ${response.data.wp_url}`) |
| 73 | + taskInProgress = true |
60 | 74 |
|
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 | + } |
67 | 78 |
|
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) |
72 | 81 |
|
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) |
76 | 84 |
|
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 | + }) |
79 | 91 |
|
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 | + ) |
82 | 95 |
|
83 | | - if (pullRequestsNo > 0) { |
84 | | - const comments = await octokit.rest.issues.listComments({ |
| 96 | + if (undefined === comment) { |
| 97 | + await octokit.rest.issues.createComment({ |
85 | 98 | ...repo, |
86 | 99 | 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})` |
88 | 108 | }) |
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 | | - } |
108 | 109 | } |
109 | | - } catch (error) { |
110 | | - const err = error as Error |
111 | | - core.setFailed(err.message) |
112 | 110 | } |
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.`)] |
122 | 116 | } catch (error) { |
123 | 117 | // Fail the workflow run if an error occurs |
124 | 118 | if (error instanceof Error) core.setFailed(error.message) |
|
0 commit comments