import { octokit } from '../octokit' ; import { context } from '@actions/github' ; import signatureWithPRComment from './signatureComment' ; import { commentContent } from './pullRequestCommentContent' ; import { CommitterMap , CommittersDetails } from '../interfaces' ; import { getUseDcoFlag } from '../shared/getInputs' ; export default async function prCommentSetup ( committerMap , committers ) { const signed = committerMap ?. notSigned && committerMap ?. notSigned . length === 0 ; try { const claBotComment = await getComment ( ) ; if ( ! claBotComment && ! signed ) { return createComment ( signed , committerMap ) ; } else if ( claBotComment ?. id ) { if ( signed ) { await updateComment ( signed , committerMap , claBotComment ) ; } const reactedCommitters = await signatureWithPRComment ( committerMap , committers ) ; if ( reactedCommitters ?. onlyCommitters ) { reactedCommitters . allSignedFlag = prepareAllSignedCommitters ( committerMap , reactedCommitters . onlyCommitters , committers ) ; } committerMap = prepareCommiterMap ( committerMap , reactedCommitters ) ; await updateComment ( reactedCommitters . allSignedFlag , committerMap , claBotComment ) ; return reactedCommitters ; } } catch ( error ) { throw new Error ( `Error occured when creating or editing the comments of the pull request: ${ error . message }` ) ; } } async function createComment ( signed , committerMap ) { await octokit . issues . createComment ( { owner : context . repo . owner , repo : context . repo . repo , issue_number : context . issue . number , body : commentContent ( signed , committerMap ) } ) . catch ( error => { throw new Error ( `Error occured when creating a pull request comment: ${ error . message }` ) ; } ) ; } async function updateComment ( signed , committerMap , claBotComment ) { await octokit . issues . updateComment ( { owner : context . repo . owner , repo : context . repo . repo , comment_id : claBotComment . id , body : commentContent ( signed , committerMap ) } ) . catch ( error => { throw new Error ( `Error occured when updating the pull request comment: ${ error . message }` ) ; } ) ; } async function getComment ( ) { try { const response = await octokit . issues . listComments ( { owner : context . repo . owner , repo : context . repo . repo , issue_number : context . issue . number } ) ; if ( getUseDcoFlag ( ) === 'true' ) { return response . data . find ( comment => comment . body . match ( /.*DCO Assistant Lite bot.*/ ) ) ; } else if ( getUseDcoFlag ( ) === 'false' ) { return response . data . find ( comment => comment . body . match ( /.*CLA Assistant Lite bot.*/ ) ) ; } } catch ( error ) { throw new Error ( `Error occured when getting  all the comments of the pull request: ${ error . message }` ) ; } } function prepareCommiterMap ( committerMap , reactedCommitters ) { committerMap . signed ?. push ( ... reactedCommitters . newSigned ) ; committerMap . notSigned = committerMap . notSigned ! . filter ( committer => ! reactedCommitters . newSigned . some ( reactedCommitter => committer . id === reactedCommitter . id ) ) ; return committerMap ; } function prepareAllSignedCommitters ( committerMap , signedInPrCommitters , committers ) { let allSignedCommitters = [ ] as CommittersDetails [ ] ; const ids = new Set ( signedInPrCommitters . map ( committer => committer . id ) ) ; allSignedCommitters = [ ... signedInPrCommitters , ... committerMap . signed ! . filter ( signedCommitter => ! ids . has ( signedCommitter . id ) ) ] ; let allSignedFlag = committers . every ( committer => allSignedCommitters . some ( reactedCommitter => committer . id === reactedCommitter . id ) ) ; return allSignedFlag ; }