export async function commentOnPullRequest()

in src/pr-comment.ts [65:106]


export async function commentOnPullRequest(
	pullRequestNumber: number,
	config: PullRequestCommentConfig,
) {
	const comment = getCommentMessage(config);
	const octokit = getOctokit(config.githubToken);

	const comments = await octokit.rest.issues.listComments({
		...context.repo,
		issue_number: pullRequestNumber,
	});

	debug(`Total comments: ${comments.data.length}`);

	const previousComments = comments.data.filter((comment) => {
		const fromBot = comment.user?.login === 'github-actions[bot]';
		const fromMe = comment.body?.includes(marker(config.projectName)) ?? false;
		return fromBot && fromMe;
	});

	if (previousComments.length > 0) {
		debug(`Found ${previousComments.length} comments by github-actions[bot]`);

		await Promise.all(
			previousComments.map(async (previousComment) => {
				debug(`Updating comment with id: ${previousComment.id}.`);
				await octokit.rest.issues.updateComment({
					...context.repo,
					comment_id: previousComment.id,
					body: comment,
				});
			}),
		);
	} else {
		debug(`No previous comment found. Creating one.`);
		await octokit.rest.issues.createComment({
			...context.repo,
			issue_number: pullRequestNumber,
			body: comment,
		});
	}
}