2020-09-30 01:11:33 -04:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2023-09-07 21:40:02 -04:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-09-30 01:11:33 -04:00
|
|
|
|
|
|
|
package print
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-12-16 12:16:50 -05:00
|
|
|
"strings"
|
2020-09-30 01:11:33 -04:00
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
)
|
|
|
|
|
2020-12-16 12:16:50 -05:00
|
|
|
var ciStatusSymbols = map[gitea.StatusState]string{
|
|
|
|
gitea.StatusSuccess: "✓ ",
|
|
|
|
gitea.StatusPending: "⭮ ",
|
|
|
|
gitea.StatusWarning: "⚠ ",
|
|
|
|
gitea.StatusError: "✘ ",
|
|
|
|
gitea.StatusFailure: "❌ ",
|
|
|
|
}
|
|
|
|
|
2020-09-30 01:11:33 -04:00
|
|
|
// PullDetails print an pull rendered to stdout
|
2020-12-16 12:16:50 -05:00
|
|
|
func PullDetails(pr *gitea.PullRequest, reviews []*gitea.PullReview, ciStatus *gitea.CombinedStatus) {
|
2020-12-07 23:06:05 -05:00
|
|
|
base := pr.Base.Name
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 16:36:33 -04:00
|
|
|
head := formatPRHead(pr)
|
|
|
|
state := formatPRState(pr)
|
2020-12-16 12:16:50 -05:00
|
|
|
|
2020-12-07 23:06:05 -05:00
|
|
|
out := fmt.Sprintf(
|
2020-12-16 12:16:50 -05:00
|
|
|
"# #%d %s (%s)\n@%s created %s\t**%s** <- **%s**\n\n%s\n\n",
|
2020-10-04 22:23:57 -04:00
|
|
|
pr.Index,
|
2020-09-30 01:11:33 -04:00
|
|
|
pr.Title,
|
2020-12-16 12:16:50 -05:00
|
|
|
state,
|
2020-09-30 01:11:33 -04:00
|
|
|
pr.Poster.UserName,
|
2022-03-28 18:37:13 -04:00
|
|
|
FormatTime(*pr.Created, false),
|
2020-12-07 23:06:05 -05:00
|
|
|
base,
|
|
|
|
head,
|
2020-09-30 01:11:33 -04:00
|
|
|
pr.Body,
|
2020-12-07 23:06:05 -05:00
|
|
|
)
|
|
|
|
|
2020-12-16 12:16:50 -05:00
|
|
|
if ciStatus != nil || len(reviews) != 0 || pr.State == gitea.StateOpen {
|
|
|
|
out += "---\n"
|
|
|
|
}
|
|
|
|
|
2022-09-28 22:49:24 -04:00
|
|
|
out += formatReviews(pr, reviews)
|
2020-12-16 12:16:50 -05:00
|
|
|
|
|
|
|
if ciStatus != nil {
|
|
|
|
var summary, errors string
|
|
|
|
for _, s := range ciStatus.Statuses {
|
|
|
|
summary += ciStatusSymbols[s.State]
|
|
|
|
if s.State != gitea.StatusSuccess {
|
|
|
|
errors += fmt.Sprintf(" - [**%s**:\t%s](%s)\n", s.Context, s.Description, s.TargetURL)
|
2020-12-07 23:06:05 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-16 12:16:50 -05:00
|
|
|
if len(ciStatus.Statuses) != 0 {
|
|
|
|
out += fmt.Sprintf("- CI: %s\n%s", summary, errors)
|
2020-12-07 23:06:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 12:16:50 -05:00
|
|
|
if pr.State == gitea.StateOpen {
|
|
|
|
if pr.Mergeable {
|
|
|
|
out += "- No Conflicts\n"
|
|
|
|
} else {
|
|
|
|
out += "- **Conflicting files**\n"
|
|
|
|
}
|
2020-12-07 23:06:05 -05:00
|
|
|
}
|
|
|
|
|
2022-09-27 11:36:36 -04:00
|
|
|
if pr.AllowMaintainerEdit {
|
|
|
|
out += "- Maintainers are allowed to edit\n"
|
|
|
|
}
|
|
|
|
|
2021-12-02 13:59:02 -05:00
|
|
|
outputMarkdown(out, getRepoURL(pr.HTMLURL))
|
2020-12-08 05:28:54 -05:00
|
|
|
}
|
|
|
|
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 16:36:33 -04:00
|
|
|
func formatPRHead(pr *gitea.PullRequest) string {
|
|
|
|
head := pr.Head.Name
|
|
|
|
if pr.Head.RepoID != pr.Base.RepoID {
|
|
|
|
if pr.Head.Repository != nil {
|
|
|
|
head = pr.Head.Repository.Owner.UserName + ":" + head
|
|
|
|
} else {
|
|
|
|
head = "delete:" + head
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return head
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatPRState(pr *gitea.PullRequest) string {
|
|
|
|
if pr.Merged != nil {
|
|
|
|
return "merged"
|
|
|
|
}
|
|
|
|
return string(pr.State)
|
|
|
|
}
|
|
|
|
|
2022-09-28 22:49:24 -04:00
|
|
|
func formatReviews(pr *gitea.PullRequest, reviews []*gitea.PullReview) string {
|
2020-12-16 12:16:50 -05:00
|
|
|
result := ""
|
|
|
|
if len(reviews) == 0 {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// deduplicate reviews by user (via review time & userID),
|
2022-09-28 22:49:24 -04:00
|
|
|
reviewByUserOrTeam := make(map[string]*gitea.PullReview)
|
2020-12-16 12:16:50 -05:00
|
|
|
for _, review := range reviews {
|
|
|
|
switch review.State {
|
|
|
|
case gitea.ReviewStateApproved,
|
|
|
|
gitea.ReviewStateRequestChanges,
|
|
|
|
gitea.ReviewStateRequestReview:
|
2022-09-28 22:49:24 -04:00
|
|
|
if review.Reviewer != nil {
|
|
|
|
if r, ok := reviewByUserOrTeam[fmt.Sprintf("user_%d", review.Reviewer.ID)]; !ok || review.Submitted.After(r.Submitted) {
|
|
|
|
reviewByUserOrTeam[fmt.Sprintf("user_%d", review.Reviewer.ID)] = review
|
|
|
|
}
|
|
|
|
} else if review.ReviewerTeam != nil {
|
|
|
|
if r, ok := reviewByUserOrTeam[fmt.Sprintf("team_%d", review.ReviewerTeam.ID)]; !ok || review.Submitted.After(r.Submitted) {
|
|
|
|
reviewByUserOrTeam[fmt.Sprintf("team_%d", review.ReviewerTeam.ID)] = review
|
|
|
|
}
|
2020-12-16 12:16:50 -05:00
|
|
|
}
|
2022-09-28 22:49:24 -04:00
|
|
|
|
2020-12-16 12:16:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// group reviews by type
|
2022-09-28 22:49:24 -04:00
|
|
|
reviewByState := make(map[gitea.ReviewStateType][]string)
|
|
|
|
for _, r := range reviewByUserOrTeam {
|
|
|
|
if r.Reviewer != nil {
|
|
|
|
reviewByState[r.State] = append(reviewByState[r.State],
|
|
|
|
r.Reviewer.UserName,
|
|
|
|
)
|
|
|
|
} else if r.ReviewerTeam != nil {
|
|
|
|
// only pulls to orgs can have team reviews
|
|
|
|
org := pr.Base.Repository.Owner
|
|
|
|
reviewByState[r.State] = append(reviewByState[r.State],
|
|
|
|
fmt.Sprintf("%s/%s", org.UserName, r.ReviewerTeam.Name),
|
|
|
|
)
|
|
|
|
}
|
2020-12-16 12:16:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// stringify
|
2022-09-28 22:49:24 -04:00
|
|
|
for state, user := range reviewByState {
|
2020-12-16 12:16:50 -05:00
|
|
|
result += fmt.Sprintf("- %s by @%s\n", state, strings.Join(user, ", @"))
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-12-08 05:28:54 -05:00
|
|
|
// PullsList prints a listing of pulls
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 16:36:33 -04:00
|
|
|
func PullsList(prs []*gitea.PullRequest, output string, fields []string) {
|
|
|
|
printPulls(prs, output, fields)
|
|
|
|
}
|
2020-12-08 05:28:54 -05:00
|
|
|
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 16:36:33 -04:00
|
|
|
// PullFields are all available fields to print with PullsList()
|
|
|
|
var PullFields = []string{
|
|
|
|
"index",
|
|
|
|
"state",
|
|
|
|
"author",
|
|
|
|
"author-id",
|
|
|
|
"url",
|
|
|
|
|
|
|
|
"title",
|
|
|
|
"body",
|
|
|
|
|
|
|
|
"mergeable",
|
|
|
|
"base",
|
|
|
|
"base-commit",
|
|
|
|
"head",
|
|
|
|
"diff",
|
|
|
|
"patch",
|
|
|
|
|
|
|
|
"created",
|
|
|
|
"updated",
|
|
|
|
"deadline",
|
|
|
|
|
|
|
|
"assignees",
|
|
|
|
"milestone",
|
|
|
|
"labels",
|
|
|
|
"comments",
|
|
|
|
}
|
|
|
|
|
|
|
|
func printPulls(pulls []*gitea.PullRequest, output string, fields []string) {
|
|
|
|
labelMap := map[int64]string{}
|
|
|
|
var printables = make([]printable, len(pulls))
|
|
|
|
machineReadable := isMachineReadable(output)
|
|
|
|
|
|
|
|
for i, x := range pulls {
|
|
|
|
// pre-serialize labels for performance
|
|
|
|
for _, label := range x.Labels {
|
|
|
|
if _, ok := labelMap[label.ID]; !ok {
|
|
|
|
labelMap[label.ID] = formatLabel(label, !machineReadable, "")
|
|
|
|
}
|
2020-12-08 05:28:54 -05:00
|
|
|
}
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 16:36:33 -04:00
|
|
|
// store items with printable interface
|
|
|
|
printables[i] = &printablePull{x, &labelMap}
|
2020-12-08 05:28:54 -05:00
|
|
|
}
|
|
|
|
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 16:36:33 -04:00
|
|
|
t := tableFromItems(fields, printables, machineReadable)
|
2020-12-09 17:04:36 -05:00
|
|
|
t.print(output)
|
2020-09-30 01:11:33 -04:00
|
|
|
}
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 16:36:33 -04:00
|
|
|
|
|
|
|
type printablePull struct {
|
|
|
|
*gitea.PullRequest
|
|
|
|
formattedLabels *map[int64]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x printablePull) FormatField(field string, machineReadable bool) string {
|
|
|
|
switch field {
|
|
|
|
case "index":
|
|
|
|
return fmt.Sprintf("%d", x.Index)
|
|
|
|
case "state":
|
|
|
|
return formatPRState(x.PullRequest)
|
|
|
|
case "author":
|
|
|
|
return formatUserName(x.Poster)
|
|
|
|
case "author-id":
|
|
|
|
return x.Poster.UserName
|
|
|
|
case "url":
|
|
|
|
return x.HTMLURL
|
|
|
|
case "title":
|
|
|
|
return x.Title
|
|
|
|
case "body":
|
|
|
|
return x.Body
|
|
|
|
case "created":
|
2022-03-28 18:37:13 -04:00
|
|
|
return FormatTime(*x.Created, machineReadable)
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 16:36:33 -04:00
|
|
|
case "updated":
|
2022-03-28 18:37:13 -04:00
|
|
|
return FormatTime(*x.Updated, machineReadable)
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 16:36:33 -04:00
|
|
|
case "deadline":
|
|
|
|
if x.Deadline == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2022-03-28 18:37:13 -04:00
|
|
|
return FormatTime(*x.Deadline, machineReadable)
|
PR listing: add --fields & expose additional fields (#415)
This PR adds the `--fields` flag to `tea pr ls` (#342), and exposes more fields specific to the `PullRequest` type:
```
--fields value, -f value Comma-separated list of fields to print.
Available values:
index,state,author,author-id,url,title,body,mergeable,base,base-commit,head,diff,patch,created,updated,deadline,assignees,milestone,labels,comments
(default: "index,title,state,author,milestone,updated,labels")
```
Co-authored-by: justusbunsi <61625851+justusbunsi@users.noreply.github.com>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/415
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: justusbunsi <justusbunsi@noreply.gitea.io>
Co-committed-by: justusbunsi <justusbunsi@noreply.gitea.io>
2021-09-28 16:36:33 -04:00
|
|
|
case "milestone":
|
|
|
|
if x.Milestone != nil {
|
|
|
|
return x.Milestone.Title
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
case "labels":
|
|
|
|
var labels = make([]string, len(x.Labels))
|
|
|
|
for i, l := range x.Labels {
|
|
|
|
labels[i] = (*x.formattedLabels)[l.ID]
|
|
|
|
}
|
|
|
|
return strings.Join(labels, " ")
|
|
|
|
case "assignees":
|
|
|
|
var assignees = make([]string, len(x.Assignees))
|
|
|
|
for i, a := range x.Assignees {
|
|
|
|
assignees[i] = formatUserName(a)
|
|
|
|
}
|
|
|
|
return strings.Join(assignees, " ")
|
|
|
|
case "comments":
|
|
|
|
return fmt.Sprintf("%d", x.Comments)
|
|
|
|
case "mergeable":
|
|
|
|
isMergeable := x.Mergeable && x.State == gitea.StateOpen
|
|
|
|
return formatBoolean(isMergeable, !machineReadable)
|
|
|
|
case "base":
|
|
|
|
return x.Base.Ref
|
|
|
|
case "base-commit":
|
|
|
|
return x.MergeBase
|
|
|
|
case "head":
|
|
|
|
return formatPRHead(x.PullRequest)
|
|
|
|
case "diff":
|
|
|
|
return x.DiffURL
|
|
|
|
case "patch":
|
|
|
|
return x.PatchURL
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|