import { getGithubActorName, GithubPullRequest, GithubPullRequestState, GithubReviewer } from "../types"; import { PreviewDetail } from "../../../preview/PreviewDetail"; import { cleanHtml, formatElapsedTime } from "../../../utils"; import { checkRunEmoji, reviewStateEmoji } from "../markdown"; import { Notification } from "../../../notification"; import { Color, Detail } from "@raycast/api"; interface GithubPullRequestPreviewProps { notification: Notification; githubPullRequest: GithubPullRequest; } function stateTag(pr: GithubPullRequest): { text: string; color: Color } { if (pr.state === GithubPullRequestState.Merged) return { text: "Merged", color: Color.Purple }; if (pr.state === GithubPullRequestState.Closed) return { text: "Closed", color: Color.Red }; if (pr.is_draft) return { text: "Draft", color: Color.SecondaryText }; return { text: "Open", color: Color.Green }; } function reviewerName(reviewer: GithubReviewer): string { switch (reviewer.type) { case "GithubUserSummary": return reviewer.content.name || reviewer.content.login; case "GithubBotSummary": case "GithubMannequinSummary": return reviewer.content.login; case "GithubTeamSummary": return reviewer.content.name; } } export function GithubPullRequestPreview({ notification, githubPullRequest: pr }: GithubPullRequestPreviewProps) { let markdown = `# ${pr.title} #${pr.number}`; if (pr.body) { markdown += `\n\n${cleanHtml(pr.body)}`; } const checkRuns = pr.latest_commit.check_suites?.flatMap((suite) => suite.check_runs) ?? []; if (checkRuns.length > 0) { markdown += `\n\n---\n\n## Checks\n\n`; markdown += checkRuns.map((run) => `${checkRunEmoji(run.conclusion, run.status)} ${run.name}`).join("\n"); } if (pr.reviews.length > 0) { markdown += `\n\n---\n\n## Reviews\n\n`; markdown += pr.reviews .map((review) => { const head = `${reviewStateEmoji(review.state)} **${getGithubActorName(review.author)}**`; return review.body ? `${head}\n\n${cleanHtml(review.body)}` : head; }) .join("\n\n"); } if (pr.comments.length > 0) { markdown += `\n\n---\n\n## Comments\n\n`; markdown += pr.comments .map( (comment) => `**${getGithubActorName(comment.author)}** · ${formatElapsedTime(comment.created_at)}\n\n${cleanHtml(comment.body)}`, ) .join("\n\n---\n\n"); } const state = stateTag(pr); const metadata = ( <> {pr.state === GithubPullRequestState.Open ? ( ) : null} {pr.review_decision ? : null} {pr.author ? : null} {pr.assignees.length > 0 ? ( {pr.assignees.map((assignee, index) => ( ))} ) : null} {pr.review_requests.length > 0 ? ( {pr.review_requests.map((reviewer, index) => ( ))} ) : null} {pr.labels.length > 0 ? ( {pr.labels.map((label) => ( ))} ) : null} ); return ; }