( mdPath: string, api: ProviderAPIClient, settings: Pick<Settings, 'jobId'>, )
| 3 | import type { ProviderAPIClient, Settings } from './models.js'; |
| 4 | |
| 5 | export async function commentOnPR( |
| 6 | mdPath: string, |
| 7 | api: ProviderAPIClient, |
| 8 | settings: Pick<Settings, 'jobId'>, |
| 9 | ): Promise<number> { |
| 10 | const markdown = await readFile(mdPath, 'utf8'); |
| 11 | const identifier = settings.jobId |
| 12 | ? `<!-- generated by @code-pushup/ci [jobId=${settings.jobId}] -->` |
| 13 | : '<!-- generated by @code-pushup/ci -->'; |
| 14 | const body = truncateBody( |
| 15 | `${markdown}\n\n${identifier}\n`, |
| 16 | api.maxCommentChars, |
| 17 | ); |
| 18 | |
| 19 | const comments = await api.listComments(); |
| 20 | logDebug(`Fetched ${comments.length} comments for pull request`); |
| 21 | |
| 22 | const prevComment = comments.find(comment => |
| 23 | comment.body.includes(identifier), |
| 24 | ); |
| 25 | logDebug( |
| 26 | prevComment |
| 27 | ? `Found previous comment ${prevComment.id} from Code PushUp` |
| 28 | : 'Previous Code PushUp comment not found', |
| 29 | ); |
| 30 | |
| 31 | if (prevComment) { |
| 32 | const updatedComment = await api.updateComment(prevComment.id, body); |
| 33 | logInfo(`Updated body of comment ${updatedComment.url}`); |
| 34 | return updatedComment.id; |
| 35 | } |
| 36 | |
| 37 | const createdComment = await api.createComment(body); |
| 38 | logInfo(`Created new comment ${createdComment.url}`); |
| 39 | return createdComment.id; |
| 40 | } |
| 41 | |
| 42 | function truncateBody(body: string, max: number): string { |
| 43 | const truncateWarning = '...*[Comment body truncated]*'; |
no test coverage detected