All files index.ts

89.79% Statements 44/49
54.54% Branches 6/11
100% Functions 2/2
93.33% Lines 42/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 851x 1x 1x 1x 1x   1x 4x 4x 4x   4x 4x   4x 4x   4x 4x   4x 6x 9x         6x         6x 6x   6x 6x 6x     4x 1x 1x     3x     3x 3x 3x         3x 3x 1x 1x 1x       3x 1x 1x           2x 2x                     1x  
import * as core from '@actions/core'
import * as github from '@actions/github'
import getConfig from './config'
import CloudflareClient from './cloudflare'
import Comment, { COMMENT_FOOTER } from './comment'
 
export async function run(): Promise<void> {
  try {
    core.debug('Retrieving action config...')
    const config = getConfig()
 
    let deployCount = 0
    const comment = new Comment()
 
    const labels: Array<{ name: string }> = config.github.pullRequest.labels
    core.debug(`PR Labels: ${JSON.stringify(labels)}`)
 
    const branch = config.github.pullRequest.head.ref
    core.info(`Deploying branch: ${branch}`)
 
    for (let i = 0; i < config.projectMap.length; i++) {
      const map = config.projectMap[i]
      Iif (!labels.some(l => l.name === map.label)) {
        core.debug(`Label ${map.label} not found. Skipping...`)
        continue
      }
 
      const cloudflare = new CloudflareClient(
        config.cloudflare.accountId,
        config.cloudflare.cloudflareApiToken
      )
 
      const result = await cloudflare.deploy(map.project, branch)
      Iif (!result) throw new Error(`Failed to deploy ${map.project}`)
 
      deployCount += 1
      core.info(`Deployed ${map.name || map.project} to ${result.url}`)
      comment.appendLine({ name: map.name || map.project, url: result.url })
    }
 
    if (deployCount === 0) {
      core.info('No projects deployed. Skipping...')
      return
    }
 
    const commentBody = comment.addTimestamp().getBody()
 
    /** Creates or updates existing comment */
    core.debug('Searching for existing comment...')
    const githubClient = github.getOctokit(config.github.token)
    const comments = await githubClient.rest.issues.listComments({
      ...github.context.repo,
      issue_number: config.github.pullRequest.number
    })
 
    let commentId = null
    for (const comment of comments.data) {
      if (comment.body?.includes(COMMENT_FOOTER)) {
        commentId = comment.id
        break
      }
    }
 
    if (commentId) {
      core.info('Updating existing PR comment with ID ' + commentId + '...')
      await githubClient.rest.issues.updateComment({
        ...github.context.repo,
        comment_id: commentId,
        body: commentBody
      })
    } else {
      core.info('Creating new PR comment...')
      await githubClient.rest.issues.createComment({
        ...github.context.repo,
        issue_number: config.github.pullRequest.number,
        body: commentBody
      })
    }
  } catch (error) {
    Iif (error instanceof Error) core.setFailed(error.message)
  }
}
 
run()