|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +# Shows a focused summary of a pull request: metadata, reviews, issue-level comments, |
| 3 | +# and inline review comments (which `gh pr view --json` does not expose). |
| 4 | +# Requires `gh` CLI authenticated against the target repo. |
| 5 | +# |
| 6 | +# Usage: |
| 7 | +# ./Show-PullRequestComments.ps1 2100 |
| 8 | +# ./Show-PullRequestComments.ps1 2100 -Repo dotnet/docker-tools |
| 9 | + |
| 10 | +[CmdletBinding()] |
| 11 | +param( |
| 12 | + [Parameter(Mandatory)][int] $PullRequest, |
| 13 | + [string] $Repo |
| 14 | +) |
| 15 | + |
| 16 | +$ErrorActionPreference = "Stop" |
| 17 | +$PSNativeCommandUseErrorActionPreference = $true |
| 18 | + |
| 19 | +function Write-BlockComment { |
| 20 | + param([string] $Text) |
| 21 | + if (-not $Text) { return } |
| 22 | + $Text.TrimEnd() -split "`n" | ForEach-Object { Write-Host "> $_" } |
| 23 | + Write-Host "" |
| 24 | +} |
| 25 | + |
| 26 | +# Fetch PR overview. |
| 27 | +$viewArgs = @( |
| 28 | + "pr", "view", $PullRequest, |
| 29 | + "--json", "number,title,state,author,baseRefName,headRefName,isDraft,url,additions,deletions,changedFiles,reviewDecision,labels,reviews,comments" |
| 30 | +) |
| 31 | +if ($Repo) { $viewArgs += @("--repo", $Repo) } |
| 32 | + |
| 33 | +$prJson = & gh @viewArgs |
| 34 | +$pr = $prJson | ConvertFrom-Json |
| 35 | + |
| 36 | +# Fetch inline review comments via REST API. `gh pr view --json` exposes review bodies |
| 37 | +# but drops the inline diff comments attached to specific files/lines. |
| 38 | +$apiPath = if ($Repo) { |
| 39 | + "repos/$Repo/pulls/$PullRequest/comments" |
| 40 | +} else { |
| 41 | + "repos/{owner}/{repo}/pulls/$PullRequest/comments" |
| 42 | +} |
| 43 | + |
| 44 | +$inlineJson = & gh api --paginate $apiPath |
| 45 | +$inline = $inlineJson | ConvertFrom-Json |
| 46 | + |
| 47 | +# Render. |
| 48 | +$title = if ($Repo) { "$Repo#$PullRequest" } else { "PR #$PullRequest" } |
| 49 | +Write-Host "## $title - $($pr.title)" |
| 50 | +Write-Host "" |
| 51 | +Write-Host "- State: $($pr.state)$(if ($pr.isDraft) { ' (draft)' })" |
| 52 | +Write-Host "- Author: $($pr.author.login)" |
| 53 | +Write-Host "- Branch: $($pr.headRefName) -> $($pr.baseRefName)" |
| 54 | +Write-Host "- Changes: +$($pr.additions)/-$($pr.deletions) ($($pr.changedFiles) files)" |
| 55 | +Write-Host "- Review decision: $($pr.reviewDecision)" |
| 56 | +if ($pr.labels) { |
| 57 | + Write-Host "- Labels: $(($pr.labels | ForEach-Object { $_.name }) -join ', ')" |
| 58 | +} |
| 59 | +Write-Host "- URL: $($pr.url)" |
| 60 | +Write-Host "" |
| 61 | + |
| 62 | +# Conversation: top-level issue comments and review submissions, merged in |
| 63 | +# chronological order. |
| 64 | +$conversation = @() |
| 65 | +foreach ($comment in $pr.comments) { |
| 66 | + $conversation += [pscustomobject]@{ |
| 67 | + Timestamp = [datetime]$comment.createdAt |
| 68 | + Header = "**$($comment.author.login)** commented at $($comment.createdAt):" |
| 69 | + Body = $comment.body |
| 70 | + } |
| 71 | +} |
| 72 | +foreach ($review in $pr.reviews) { |
| 73 | + $header = if ($review.state -eq "COMMENTED") { |
| 74 | + "**$($review.author.login)** left a comment at $($review.submittedAt):" |
| 75 | + } else { |
| 76 | + "**$($review.author.login)** reviewed ($($review.state)) at $($review.submittedAt):" |
| 77 | + } |
| 78 | + $conversation += [pscustomobject]@{ |
| 79 | + Timestamp = [datetime]$review.submittedAt |
| 80 | + Header = $header |
| 81 | + Body = $review.body |
| 82 | + } |
| 83 | +} |
| 84 | +$conversation = $conversation | Sort-Object Timestamp |
| 85 | + |
| 86 | +Write-Host "### Conversation ($($conversation.Count))" |
| 87 | +Write-Host "" |
| 88 | +if ($conversation.Count -eq 0) { |
| 89 | + Write-Host "_None_" |
| 90 | +} else { |
| 91 | + foreach ($entry in $conversation) { |
| 92 | + Write-Host $entry.Header |
| 93 | + Write-BlockComment $entry.Body |
| 94 | + } |
| 95 | +} |
| 96 | +Write-Host "" |
| 97 | + |
| 98 | +# Inline review comments grouped by file and line. |
| 99 | +Write-Host "### Code review comments ($($inline.Count))" |
| 100 | +Write-Host "" |
| 101 | +if ($inline.Count -eq 0) { |
| 102 | + Write-Host "_None_" |
| 103 | +} else { |
| 104 | + $grouped = $inline | |
| 105 | + Group-Object -Property { "$($_.path):$(if ($_.line) { $_.line } else { $_.original_line })" } | |
| 106 | + Sort-Object Name |
| 107 | + foreach ($group in $grouped) { |
| 108 | + $first = $group.Group[0] |
| 109 | + $line = if ($first.line) { $first.line } else { $first.original_line } |
| 110 | + Write-Host "#### $($first.path) (line $line)" |
| 111 | + Write-Host "" |
| 112 | + foreach ($comment in $group.Group | Sort-Object created_at) { |
| 113 | + Write-Host "**$($comment.user.login)** commented at $($comment.created_at):" |
| 114 | + Write-BlockComment $comment.body |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments