Skip to content

Commit

Permalink
feat(delete): batch delection with promise all
Browse files Browse the repository at this point in the history
  • Loading branch information
andykenward committed Apr 1, 2024
1 parent 76d96fd commit 875b0dc
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 81 deletions.
2 changes: 1 addition & 1 deletion dist/delete/index.js

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

4 changes: 2 additions & 2 deletions dist/delete/index.js.map

Large diffs are not rendered by default.

165 changes: 87 additions & 78 deletions src/delete/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {info, warning} from '@unlike/github-actions-core'
import {debug, info, setFailed, warning} from '@unlike/github-actions-core'

import {DeploymentStatusState} from '@/gql/graphql.js'

Expand All @@ -15,90 +15,99 @@ import {MutationCreateGitHubDeploymentStatus} from '@/common/github/deployment/s

const PREFIX = `delete -`

export async function run() {
const deployments = await getGitHubDeployments()

if (deployments.length === 0) {
info(`${PREFIX} No deployments found to delete`)
return
}

for (const deployment of deployments) {
const payload = deployment.payload
const batchDelete = async (
deployment: Awaited<ReturnType<typeof getGitHubDeployments>>[number]
) => {
const payload = deployment.payload

try {
const {commentId, url, cloudflare} = getPayload(payload)
try {
const {commentId, url, cloudflare} = getPayload(payload)

/**
* Delete Cloudflare deployment
*/
const deletedCloudflareDeployment =
await deleteCloudflareDeployment(cloudflare)
if (!deletedCloudflareDeployment) continue
/**
* On success of Cloudflare deployment delete GitHub deployment & comment.
*/

const updateStatusGitHubDeployment = await request({
query: MutationCreateGitHubDeploymentStatus,
variables: {
environment: deployment.environment,
deploymentId: deployment.node_id,
environmentUrl: url,
logUrl: getCloudflareLogEndpoint(cloudflare),
state: DeploymentStatusState.Inactive
},
options: {
errorThrows: false
}
})
/**
* Delete Cloudflare deployment
*/
const deletedCloudflareDeployment =
await deleteCloudflareDeployment(cloudflare)
if (!deletedCloudflareDeployment) return
/**
* On success of Cloudflare deployment delete GitHub deployment & comment.
*/

if (updateStatusGitHubDeployment.errors) {
warning(
`${PREFIX} Error updating GitHub deployment status: ${JSON.stringify(
updateStatusGitHubDeployment.errors
)}`
)
continue
const updateStatusGitHubDeployment = await request({
query: MutationCreateGitHubDeploymentStatus,
variables: {
environment: deployment.environment,
deploymentId: deployment.node_id,
environmentUrl: url,
logUrl: getCloudflareLogEndpoint(cloudflare),
state: DeploymentStatusState.Inactive
},
options: {
errorThrows: false
}
})

const deletedGitHubDeployment = commentId
? await request({
query: MutationDeleteGitHubDeploymentAndComment,
variables: {
deploymentId: deployment.node_id,
commentId: commentId
},
options: {
errorThrows: false
}
})
: await request({
query: MutationDeleteGitHubDeployment,
variables: {
deploymentId: deployment.node_id
},
options: {
errorThrows: false
}
})
if (updateStatusGitHubDeployment.errors) {
warning(
`${PREFIX} Error updating GitHub deployment status: ${JSON.stringify(
updateStatusGitHubDeployment.errors
)}`
)
return
}

if (deletedGitHubDeployment.errors) {
warning(
`${PREFIX} Error deleting GitHub deployment: ${JSON.stringify(
deletedGitHubDeployment.errors
)}`
)
}
info(`${PREFIX} GitHub Deployment Deleted: ${deployment.node_id}`)
} catch (error) {
info(
`${PREFIX} Deployment payload is not valid : ${JSON.stringify(error)}`
const deletedGitHubDeployment = commentId
? await request({
query: MutationDeleteGitHubDeploymentAndComment,
variables: {
deploymentId: deployment.node_id,
commentId: commentId
},
options: {
errorThrows: false
}
})
: await request({
query: MutationDeleteGitHubDeployment,
variables: {
deploymentId: deployment.node_id
},
options: {
errorThrows: false
}
})

if (deletedGitHubDeployment.errors) {
warning(
`${PREFIX} Error deleting GitHub deployment: ${JSON.stringify(
deletedGitHubDeployment.errors
)}`
)
continue
}
/**
* Add comment with summary of deleted deployments etc?
*/
info(`${PREFIX} GitHub Deployment Deleted: ${deployment.node_id}`)
} catch (error) {
info(`${PREFIX} Deployment payload is not valid : ${JSON.stringify(error)}`)
return
}
/**
* Add comment with summary of deleted deployments etc?
*/
}

export async function run() {
const deployments = await getGitHubDeployments()

if (deployments.length === 0) {
info(`${PREFIX} No deployments found to delete`)
return
}

try {
const values = await Promise.all(
deployments.map(deployment => batchDelete(deployment))
)
debug(`${PREFIX} Deleted deployments: ${JSON.stringify(values)}`)
} catch (error) {
setFailed(`${PREFIX} Error deleting deployments: ${JSON.stringify(error)}`)
}
}

0 comments on commit 875b0dc

Please sign in to comment.