import issueKeyParser from "jira-issue-key-parser" ; import { getJiraId } from "../jira/util/id" ; import _ from "lodash" ; import { Octokit } from "@octokit/rest" ; import { LoggerWithTarget } from "probot/lib/wrap-logger" ; import { getJiraAuthor } from "../util/jira" ; import { GitHubAPI } from "probot" ; import { getGithubUser } from "../services/github/user" ; import { JiraAuthor } from "../interfaces/jira" ; import { booleanFlag , BooleanFlags } from "../config/feature-flags" ; function mapStatus ( status , merged_at ? ) { if ( status === "merged" ) return "MERGED" ; if ( status === "open" ) return "OPEN" ; if ( status === "closed" && merged_at ) return "MERGED" ; if ( status === "closed" && ! merged_at ) return "DECLINED" ; return "UNKNOWN" ; } interface Review extends JiraAuthor { approvalStatus : string ; } function mapReviews ( reviews = [ ] ) { const sortedReviews = _ . orderBy ( reviews , "submitted_at" , "desc" ) ; const usernames = { } ; return sortedReviews . reduce ( ( acc , review ) => { const author = review ?. user ; if ( ! usernames [ author ?. login ] ) { usernames [ author ?. login ] = { ... getJiraAuthor ( author ) , approvalStatus : review . state === "APPROVED" ? "APPROVED" : "UNAPPROVED" } ; acc . push ( usernames [ author ?. login ] ) ; } else if ( usernames [ author ?. login ] . approvalStatus !== "APPROVED" && review . state === "APPROVED" ) { usernames [ author ?. login ] . approvalStatus = "APPROVED" ; } return acc ; } , [ ] ) ; } export default async ( github , pullRequest , reviews ? , log ? ) => { const { title : prTitle , head , body } = pullRequest ; const textToSearch = await booleanFlag ( BooleanFlags . ASSOCIATE_PR_TO_ISSUES_IN_BODY , true ) ? `${ prTitle }\n${ head . ref }\n${ body }}` : `${ prTitle }\n${ pullRequest . head . ref }` ; const issueKeys = issueKeyParser ( ) . parse ( textToSearch ) ; const logPayload = { prTitle : prTitle || "none" , repoName : head ?. repo . name || "none" , prRef : pullRequest . head . ref || "none" } ; if ( _ . isEmpty ( issueKeys ) || ! head ?. repo ) { log ?. info ( logPayload , "Ignoring pullrequest hence it has no issue keys or repo" ) ; return undefined ; } const pullRequestStatus = mapStatus ( pullRequest . state , pullRequest . merged_at ) ; log ?. info ( logPayload , `Pull request status mapped to ${ pullRequestStatus }` ) ; return { id : pullRequest . base . repo . id , name : pullRequest . base . repo . full_name , url : pullRequest . base . repo . html_url , branches : pullRequestStatus === "MERGED" ? [ ] : [ { createPullRequestUrl : `${ pullRequest ?. head ?. repo ?. html_url }/pull/new/${ pullRequest ?. head ?. ref }` , lastCommit : { author : getJiraAuthor ( pullRequest . head ?. user , await getGithubUser ( github , pullRequest . head ?. user ?. login ) ) , authorTimestamp : pullRequest . updated_at , displayId : pullRequest ?. head ?. sha ?. substring ( 0 , 6 ) , fileCount : 0 , hash : pullRequest . head . sha , id : pullRequest . head . sha , issueKeys , message : "n/a" , updateSequenceId : Date . now ( ) , url : `${ pullRequest . head . repo . html_url }/commit/${ pullRequest . head . sha }` } , id : getJiraId ( pullRequest . head . ref ) , issueKeys , name : pullRequest . head . ref , url : `${ pullRequest . head . repo . html_url }/tree/${ pullRequest . head . ref }` , updateSequenceId : Date . now ( ) } ] , pullRequests : [ { author : getJiraAuthor ( pullRequest . user , await getGithubUser ( github , pullRequest . user ?. login ) ) , commentCount : pullRequest . comments , destinationBranch : `${ pullRequest . base . repo . html_url }/tree/${ pullRequest . base . ref }` , displayId : `#${ pullRequest . number }` , id : pullRequest . number , issueKeys , lastUpdate : pullRequest . updated_at , reviewers : mapReviews ( reviews ) , sourceBranch : pullRequest . head . ref , sourceBranchUrl : `${ pullRequest . head . repo . html_url }/tree/${ pullRequest . head . ref }` , status : pullRequestStatus , timestamp : pullRequest . updated_at , title : pullRequest . title , url : pullRequest . html_url , updateSequenceId : Date . now ( ) } ] , updateSequenceId : Date . now ( ) } ; } ;