import { URLSearchParams } from "url" ; import axios , { AxiosResponse } from "axios" ; import template from "lodash.template" ; import { ParsedCommits , ParsedCommit } from "../src/parseCommits" ; const fixId = "3" ; const closeId = "4" ; const updateIssueApiUrlTemplate = template ( "https://<%=apiHost%>/api/v2/issues/<%=issueKey%>?apiKey=<%=apiKey%>" ) ; const commentTemplate = template ( "<%=commits[0].author.name%>さんがプッシュしました\n" + "<% commits.forEach(commit=>{%>" + "\n+ <%=commit.message%> ([<%=commit.id_short%>](<%=commit.url%>))" + "<% }); %>" ) ; export type Response = { response : AxiosResponse < Record < string , unknown > > ; commits : ParsedCommit [ ] ; issueKey : string ; isFix : boolean ; isClose : boolean ; } ; param parsedCommits param apiHost param apiKey returns export const postComments = ( parsedCommits , apiHost , apiKey ) : Promise < Response [ ] > => { const promiseArray = [ ] ; for ( const [ key , value ] of Object . entries ( parsedCommits ) ) { promiseArray . push ( createPatchCommentRequest ( value , key , apiHost , apiKey ) ) ; } return Promise . all ( promiseArray ) ; } ; param commits param issueKey param apiHost param apiKey returns see https const createPatchCommentRequest = ( commits , issueKey , apiHost , apiKey ) : Promise < Response > => { const endpoint = updateIssueApiUrlTemplate ( { apiHost : apiHost , apiKey : apiKey , issueKey , } ) ; const comment = commentTemplate ( { commits } ) ; const isFix = commits . map ( ( commit ) => commit . is_fix ) . includes ( true ) ; const isClose = commits . map ( ( commit ) => commit . is_close ) . includes ( true ) ; const status = ( ( ) => { if ( isFix ) return { statusId : fixId } ; if ( isClose ) return { statusId : closeId } ; else return undefined ; } ) ( ) ; const body = { comment , ... status } ; return axios . patch ( endpoint , new URLSearchParams ( body ) . toString ( ) ) . then ( ( response ) => { return { response , commits , issueKey , isFix , isClose } ; } ) ; } ;