-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ci): move in code for posting comment, generalized to any provider
- Loading branch information
1 parent
d81b3ac
commit b61d674
Showing
3 changed files
with
113 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import fs from 'node:fs/promises'; | ||
import type { Logger, ProviderAPIClient } from './models'; | ||
|
||
export async function commentOnPR( | ||
mdPath: string, | ||
api: ProviderAPIClient, | ||
logger: Logger, | ||
): Promise<number> { | ||
const markdown = await fs.readFile(mdPath, 'utf8'); | ||
const identifier = `<!-- generated by code-pushup/github-action -->`; | ||
const body = truncateBody( | ||
`${markdown}\n\n${identifier}\n`, | ||
api.maxCommentChars, | ||
logger, | ||
); | ||
|
||
const comments = await api.listComments(); | ||
logger.debug(`Fetched ${comments.length} comments for pull request`); | ||
|
||
const prevComment = comments.find(comment => | ||
comment.body.includes(identifier), | ||
); | ||
logger.debug( | ||
prevComment | ||
? `Found previous comment ${prevComment.id} from Code PushUp` | ||
: 'Previous Code PushUp comment not found', | ||
); | ||
|
||
if (prevComment) { | ||
const updatedComment = await api.updateComment(prevComment.id, body); | ||
logger.debug(`Updated body of comment ${updatedComment.url}`); | ||
return updatedComment.id; | ||
} | ||
|
||
const createdComment = await api.createComment(body); | ||
logger.debug(`Created new comment ${createdComment.url}`); | ||
return createdComment.id; | ||
} | ||
|
||
function truncateBody(body: string, max: number, logger: Logger): string { | ||
const truncateWarning = '...*[Comment body truncated]*'; | ||
if (body.length > max) { | ||
logger.warn(`Comment body is too long. Truncating to ${max} characters.`); | ||
return body.slice(0, max - truncateWarning.length) + truncateWarning; | ||
} | ||
return body; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters