async function updateStatusComment()

in index.js [40:119]


async function updateStatusComment(overridden, workflowStatus, workflowUrl) {
    const level = overridden ? 'warning' : 'failure'
    const { data: comments } = await octokit.rest.issues.listComments({
        owner,
        repo,
        issue_number: prNumber,
    })

    let statusComment = comments.find(comment => comment.body.includes(uniqueIdentifier))

    if (!statusComment && workflowStatus === "success") {
        console.log("No existing comment found, and job was successful, nothing to do")
        return
    }

    let commentBody = ""
    let lines = []
    if (statusComment) {
        [commentBody, _] = statusComment.body.split(uniqueIdentifier)
        lines = commentBody.trim().split(/\n+/)
    } else {
        lines = [commentHeader, commentIntro]
    }
    const workflowNameLink = `[${workflowName}](${workflowUrl})`
    const workflowMarkerPattern = `- ${workflowNameLink}: `
    const workflowLine = `${workflowMarkerPattern}${level}`
    const workflowMatch = `[${workflowName}]`
    const workflowIndex = lines.findIndex(line => line.includes(workflowMatch))

    if (workflowStatus === "failure") {
        if (workflowIndex !== -1) {
            // update existing workflow line with new level
            console.log("Updating workflow in comment")
            lines[workflowIndex] = workflowLine
        } else {
            console.log("Adding new workflow to comment")
            lines.push(workflowLine)
        }
    } else {
        // If workflow succeeded remove from list
        if (workflowIndex !== -1) {
            console.log("Removing successful workflow from comment")
            lines.splice(workflowIndex, 1)
        }
    }

    // construct final comment
    // Header should have additional newline
    lines[0] += "\n"
    // Intro too
    lines[1] += "\n"
    commentBody = lines.join('\n').trim() + `\n\n${uniqueIdentifier}\n${commentTailer}\n`

    // Update or remove the comment as necessary
    if (!statusComment) {
        console.log("Creating comment")
        await octokit.rest.issues.createComment({
            owner,
            repo,
            issue_number: prNumber,
            body: commentBody,
        })
    } else if (!commentBody.match(/^- .+/gm)) {
        console.log("Removing comment, last workflow passing or no workflows listed")
        await octokit.rest.issues.deleteComment({
            owner,
            repo,
            comment_id: statusComment.id,
        })
    } else {
        console.log("Updating comment with modified workflow list")
        // Update the comment with the new body
        await octokit.rest.issues.updateComment({
            owner,
            repo,
            comment_id: statusComment.id,
            body: commentBody,
        })
    }
}