export async function postResultsToPRComments()

in vsts/src/utils.ts [344:403]


export async function postResultsToPRComments(
  toolName: string,
  sourceDir: string,
  content: string,
  hasIssues: boolean,
  postComment: boolean
): Promise<void> {
  try {
    if (!postComment || tl.getVariable('Build.Reason') !== 'PullRequest') {
      return
    }
    const comment_tag_pattern = getCommentTag(toolName, sourceDir)
    const body = `${content}\n${comment_tag_pattern}`

    const pullRequestId = parseInt(
      getVariable('System.PullRequest.PullRequestId'),
      10
    )
    const project = getVariable('System.TeamProject')
    const repoId = getVariable('Build.Repository.Id')
    const gitApi = await getGitApi()
    const {thread, comment} = await findCommentByTag(comment_tag_pattern)
    const newComment = {
      content: body
    }

    if (comment?.id === undefined || thread?.id === undefined) {
      const thread: GitInterfaces.CommentThread = {
        comments: [newComment],
        status: hasIssues
          ? GitInterfaces.CommentThreadStatus.Active
          : GitInterfaces.CommentThreadStatus.ByDesign
      }
      await gitApi.createThread(thread, repoId, pullRequestId, project)
    } else {
      const threadStatusUpdated: GitInterfaces.CommentThread = {
        status: hasIssues
          ? GitInterfaces.CommentThreadStatus.Active
          : GitInterfaces.CommentThreadStatus.ByDesign
      }
      await gitApi.updateThread(
        threadStatusUpdated,
        repoId,
        pullRequestId,
        thread.id,
        project
      )
      await gitApi.updateComment(
        newComment,
        repoId,
        pullRequestId,
        thread.id,
        comment.id,
        project
      )
    }
  } catch (error) {
    tl.warning(`Failed to post results to comment: ${(error as Error).message}`)
  }
}