Skip to content

Commit 3c33510

Browse files
author
Nate Smith
authored
Merge pull request cli#748 from doi-t/add-pr-metadata-to-view
Add relevant metadata to pr view in CLI
2 parents 7f9aef6 + 6d0e5bf commit 3c33510

File tree

6 files changed

+506
-29
lines changed

6 files changed

+506
-29
lines changed

api/queries_pr.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package api
33
import (
44
"fmt"
55
"strings"
6+
"time"
67

78
"github.com/cli/cli/internal/ghrepo"
89
)
@@ -61,6 +62,51 @@ type PullRequest struct {
6162
}
6263
}
6364
}
65+
ReviewRequests struct {
66+
Nodes []struct {
67+
RequestedReviewer struct {
68+
TypeName string `json:"__typename"`
69+
Login string
70+
}
71+
}
72+
TotalCount int
73+
}
74+
Reviews struct {
75+
Nodes []struct {
76+
Author struct {
77+
Login string
78+
}
79+
State string
80+
CreatedAt time.Time
81+
PublishedAt time.Time
82+
}
83+
}
84+
Assignees struct {
85+
Nodes []struct {
86+
Login string
87+
}
88+
TotalCount int
89+
}
90+
Labels struct {
91+
Nodes []struct {
92+
Name string
93+
}
94+
TotalCount int
95+
}
96+
ProjectCards struct {
97+
Nodes []struct {
98+
Project struct {
99+
Name string
100+
}
101+
Column struct {
102+
Name string
103+
}
104+
}
105+
TotalCount int
106+
}
107+
Milestone struct {
108+
Title string
109+
}
64110
}
65111

66112
type NotFoundError struct {
@@ -323,6 +369,54 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
323369
isCrossRepository
324370
isDraft
325371
maintainerCanModify
372+
reviewRequests(first: 100) {
373+
nodes {
374+
requestedReviewer {
375+
__typename
376+
...on User {
377+
login
378+
}
379+
}
380+
}
381+
totalCount
382+
}
383+
reviews(last: 100) {
384+
nodes {
385+
author {
386+
login
387+
}
388+
state
389+
createdAt
390+
publishedAt
391+
}
392+
totalCount
393+
}
394+
assignees(first: 100) {
395+
nodes {
396+
login
397+
}
398+
totalCount
399+
}
400+
labels(first: 100) {
401+
nodes {
402+
name
403+
}
404+
totalCount
405+
}
406+
projectCards(first: 100) {
407+
nodes {
408+
project {
409+
name
410+
}
411+
column {
412+
name
413+
}
414+
}
415+
totalCount
416+
}
417+
milestone{
418+
title
419+
}
326420
}
327421
}
328422
}`
@@ -374,6 +468,54 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, hea
374468
}
375469
isCrossRepository
376470
isDraft
471+
reviewRequests(first: 100) {
472+
nodes {
473+
requestedReviewer {
474+
__typename
475+
...on User {
476+
login
477+
}
478+
}
479+
}
480+
totalCount
481+
}
482+
reviews(last: 100) {
483+
nodes {
484+
author {
485+
login
486+
}
487+
state
488+
createdAt
489+
publishedAt
490+
}
491+
totalCount
492+
}
493+
assignees(first: 100) {
494+
nodes {
495+
login
496+
}
497+
totalCount
498+
}
499+
labels(first: 100) {
500+
nodes {
501+
name
502+
}
503+
totalCount
504+
}
505+
projectCards(first: 100) {
506+
nodes {
507+
project {
508+
name
509+
}
510+
column {
511+
name
512+
}
513+
}
514+
totalCount
515+
}
516+
milestone{
517+
title
518+
}
377519
}
378520
}
379521
}

command/pr.go

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ func prView(cmd *cobra.Command, args []string) error {
327327
}
328328

329329
func printPrPreview(out io.Writer, pr *api.PullRequest) error {
330+
// Header (Title and State)
330331
fmt.Fprintln(out, utils.Bold(pr.Title))
331332
fmt.Fprintf(out, "%s", prStateTitleWithColor(*pr))
332333
fmt.Fprintln(out, utils.Gray(fmt.Sprintf(
@@ -336,20 +337,94 @@ func printPrPreview(out io.Writer, pr *api.PullRequest) error {
336337
pr.BaseRefName,
337338
pr.HeadRefName,
338339
)))
340+
341+
// Metadata
342+
// TODO: Reviewers
343+
fmt.Fprintln(out)
344+
if assignees := prAssigneeList(*pr); assignees != "" {
345+
fmt.Fprint(out, utils.Bold("Assignees: "))
346+
fmt.Fprintln(out, assignees)
347+
}
348+
if labels := prLabelList(*pr); labels != "" {
349+
fmt.Fprint(out, utils.Bold("Labels: "))
350+
fmt.Fprintln(out, labels)
351+
}
352+
if projects := prProjectList(*pr); projects != "" {
353+
fmt.Fprint(out, utils.Bold("Projects: "))
354+
fmt.Fprintln(out, projects)
355+
}
356+
if pr.Milestone.Title != "" {
357+
fmt.Fprint(out, utils.Bold("Milestone: "))
358+
fmt.Fprintln(out, pr.Milestone.Title)
359+
}
360+
361+
// Body
339362
if pr.Body != "" {
340363
fmt.Fprintln(out)
341364
md, err := utils.RenderMarkdown(pr.Body)
342365
if err != nil {
343366
return err
344367
}
345368
fmt.Fprintln(out, md)
346-
fmt.Fprintln(out)
347369
}
370+
fmt.Fprintln(out)
348371

372+
// Footer
349373
fmt.Fprintf(out, utils.Gray("View this pull request on GitHub: %s\n"), pr.URL)
350374
return nil
351375
}
352376

377+
func prAssigneeList(pr api.PullRequest) string {
378+
if len(pr.Assignees.Nodes) == 0 {
379+
return ""
380+
}
381+
382+
AssigneeNames := make([]string, 0, len(pr.Assignees.Nodes))
383+
for _, assignee := range pr.Assignees.Nodes {
384+
AssigneeNames = append(AssigneeNames, assignee.Login)
385+
}
386+
387+
list := strings.Join(AssigneeNames, ", ")
388+
if pr.Assignees.TotalCount > len(pr.Assignees.Nodes) {
389+
list += ", …"
390+
}
391+
return list
392+
}
393+
394+
func prLabelList(pr api.PullRequest) string {
395+
if len(pr.Labels.Nodes) == 0 {
396+
return ""
397+
}
398+
399+
labelNames := make([]string, 0, len(pr.Labels.Nodes))
400+
for _, label := range pr.Labels.Nodes {
401+
labelNames = append(labelNames, label.Name)
402+
}
403+
404+
list := strings.Join(labelNames, ", ")
405+
if pr.Labels.TotalCount > len(pr.Labels.Nodes) {
406+
list += ", …"
407+
}
408+
return list
409+
}
410+
411+
func prProjectList(pr api.PullRequest) string {
412+
if len(pr.ProjectCards.Nodes) == 0 {
413+
return ""
414+
}
415+
416+
projectNames := make([]string, 0, len(pr.ProjectCards.Nodes))
417+
for _, project := range pr.ProjectCards.Nodes {
418+
projectNames = append(projectNames, fmt.Sprintf("%s (%s)", project.Project.Name, project.Column.Name))
419+
}
420+
421+
list := strings.Join(projectNames, ", ")
422+
if pr.ProjectCards.TotalCount > len(pr.ProjectCards.Nodes) {
423+
list += ", …"
424+
}
425+
return list
426+
}
427+
353428
var prURLRE = regexp.MustCompile(`^https://github\.com/([^/]+)/([^/]+)/pull/(\d+)`)
354429

355430
func prFromURL(arg string) (string, ghrepo.Interface) {

0 commit comments

Comments
 (0)