8000 feat: add inline comment viewing support to pr diff and view commands by yplassiard · Pull Request #11793 · cli/cli · GitHub
[go: up one dir, main page]

Skip to content

Conversation

yplassiard
Copy link

Summary

This PR adds comprehensive inline comment viewing functionality to the GitHub CLI:

New Features

  • gh pr diff --comments (-c): View pull request diff with inline comments overlaid at their respective line positions
  • gh pr view --inline (-i): Structured view of inline comments organized by file with actual code context

Key Capabilities

  • Shows 2 lines of code context above/below each commented line
  • Color-coded output with proper syntax highlighting
  • Supports line ranges and multi-line comments
  • Displays comment metadata (author, timestamp, resolved status)
  • Graceful error handling when code context unavailable

Implementation

  • Extended GraphQL queries to fetch reviewThreads data
  • Added API structures for PullRequestReviewThread and PullRequestReviewComment
  • Implemented diff parsing for accurate line number mapping
  • Added file content fetching via GitHub Contents API
  • Comprehensive test coverage for new functionality

Usage Examples

# View diff with inline comments
gh pr diff --comments
gh pr diff -c

# View structured inline comments with code context
gh pr view --inline
gh pr view -i

This enhancement significantly improves the code review experience by bringing inline comment context directly to the CLI.

Test Plan

  • Unit tests for command flag parsing
  • Integration tests for comment display functionality
  • Manual testing with real PRs containing inline comments
  • Error handling for missing files and API failures

- Add --comments/-c flag to 'gh pr diff' to show inline comments overlaid on diff
- Add --inline/-i flag to 'gh pr view' to show structured inline comments with code context
- Extend GraphQL queries to fetch review threads and inline comment data
- Display code context with line numbers and syntax highlighting for commented lines
@yplassiard yplassiard requested a review from a team as a code owner September 23, 2025 11:31
@yplassiard yplassiard requested review from babakks and Copilot and removed request for babakks September 23, 2025 11:31
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Sep 23, 2025
@cliAutomation
Copy link
Collaborator

Hi! Thanks for the pull request. Please ensure that this change is linked to an issue by mentioning an issue number in the description of the pull request. If this pull request would close the issue, please put the word 'Fixes' before the issue number somewhere in the pull request body. If this is a tiny change like fixing a typo, feel free to ignore this message.

Copy link
Contributor
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds comprehensive inline comment viewing functionality to the GitHub CLI by extending both gh pr diff and gh pr view commands with new flags to display inline comments with code context.

  • Adds --comments flag to gh pr diff to overlay inline comments on diff output
  • Adds --inline flag to gh pr view to display structured inline comments with code context
  • Implements GraphQL query extensions and API structures for fetching review thread data

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
api/queries_pr.go Defines new API structures for PullRequestReviewThread and PullRequestReviewComment with Comment interface implementation
api/query_builder.go Adds GraphQL query fragment for reviewThreads and includes it in PullRequestFields
pkg/cmd/pr/diff/diff.go Implements diff parsing and inline comment overlay functionality for --comments flag
pkg/cmd/pr/diff/diff_test.go Adds test coverage for diff with inline comments functionality
pkg/cmd/pr/view/view.go Implements structured inline comment viewing with code context fetching via GitHub API
pkg/cmd/pr/view/view_test.go Adds test coverage for --inline flag parsing and reviewThreads field inclusion

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +708 to +713
func fetchFileContent(client *http.Client, repo ghrepo.Interface, path, ref string) (string, error) {
url := fmt.Sprintf("%srepos/%s/contents/%s?ref=%s",
ghinstance.RESTPrefix(repo.RepoHost()),
ghrepo.FullName(repo),
path,
ref)
Copy link
Preview
Copilot AI Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to d 8000 escribe this comment to others. Learn more.

The file path parameter is directly concatenated into the URL without proper encoding, which could lead to path traversal vulnerabilities if the path contains special characters like '../'. Use url.PathEscape() or proper URL construction to encode the path parameter.

Copilot uses AI. Check for mistakes.

Comment on lines +540 to +546
if useColor {
fmt.Fprintf(w, "\x1b[36m") // Cyan color for comment indicator
}
fmt.Fprintf(w, " 💬 %s", comment.AuthorLogin())
if useColor {
fmt.Fprintf(w, "\x1b[m") // Reset color
}
Copy link
Preview
Copilot AI Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded ANSI escape sequences should be replaced with color constants or a color scheme for better maintainability. Consider using the existing color constants (colorHeader, colorAddition, etc.) or defining new ones for consistency.

Copilot uses AI. Check for mistakes.

Comment on lines +616 to +617
markdown.WithTheme("dark"),
markdown.WithWrap(0)) // No wrap for now
Copy link
Preview
Copilot AI Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The markdown theme is hard-coded to 'dark' and wrap is hard-coded to 0. Consider using the terminal theme from opts.IO.TerminalTheme() and opts.IO.TerminalWidth() for consistency with other parts of the application.

Suggested change
markdown.WithTheme("dark"),
markdown.WithWrap(0)) // No wrap for now
markdown.WithTheme(opts.IO.TerminalTheme()),
markdown.WithWrap(opts.IO.TerminalWidth()))

Copilot uses AI. Check for mistakes.

Comment on lines +663 to +664
// Calculate context range (show 2 lines above and below)
contextLines := 2
Copy link
Preview
Copilot AI Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The number of context lines is hard-coded as a magic number. Consider defining this as a constant at the package level or making it configurable for better maintainability.

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external pull request originating outside of the CLI core team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0