2020-09-30 05:11:33 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package print
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-12-21 15:41:07 +00:00
|
|
|
"strings"
|
2020-09-30 05:11:33 +00:00
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
2021-10-01 08:13:32 +00:00
|
|
|
"github.com/enescakir/emoji"
|
2020-09-30 05:11:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// IssueDetails print an issue rendered to stdout
|
2021-10-01 08:13:32 +00:00
|
|
|
func IssueDetails(issue *gitea.Issue, reactions []*gitea.Reaction) {
|
|
|
|
out := fmt.Sprintf(
|
2020-12-08 04:06:05 +00:00
|
|
|
"# #%d %s (%s)\n@%s created %s\n\n%s\n",
|
2020-10-05 02:23:57 +00:00
|
|
|
issue.Index,
|
2020-09-30 05:11:33 +00:00
|
|
|
issue.Title,
|
|
|
|
issue.State,
|
|
|
|
issue.Poster.UserName,
|
2022-03-28 22:37:13 +00:00
|
|
|
FormatTime(issue.Created, false),
|
2020-09-30 05:11:33 +00:00
|
|
|
issue.Body,
|
2021-10-01 08:13:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if len(reactions) > 0 {
|
|
|
|
out += fmt.Sprintf("\n---\n\n%s\n", formatReactions(reactions))
|
|
|
|
}
|
|
|
|
|
2021-12-02 18:59:02 +00:00
|
|
|
outputMarkdown(out, getRepoURL(issue.HTMLURL))
|
2021-10-01 08:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func formatReactions(reactions []*gitea.Reaction) string {
|
|
|
|
reactionCounts := make(map[string]uint16)
|
|
|
|
for _, r := range reactions {
|
2021-10-04 17:43:06 +00:00
|
|
|
reactionCounts[r.Reaction]++
|
2021-10-01 08:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reactionStrings := make([]string, 0, len(reactionCounts))
|
|
|
|
for reaction, count := range reactionCounts {
|
|
|
|
reactionStrings = append(reactionStrings, fmt.Sprintf("%dx :%s:", count, reaction))
|
|
|
|
}
|
|
|
|
|
|
|
|
return emoji.Parse(strings.Join(reactionStrings, " | "))
|
2020-09-30 05:11:33 +00:00
|
|
|
}
|
2020-12-08 10:28:54 +00:00
|
|
|
|
2020-12-21 15:41:07 +00:00
|
|
|
// IssuesPullsList prints a listing of issues & pulls
|
|
|
|
func IssuesPullsList(issues []*gitea.Issue, output string, fields []string) {
|
|
|
|
printIssues(issues, output, fields)
|
|
|
|
}
|
2020-12-08 10:28:54 +00:00
|
|
|
|
2020-12-21 15:41:07 +00:00
|
|
|
// IssueFields are all available fields to print with IssuesList()
|
|
|
|
var IssueFields = []string{
|
|
|
|
"index",
|
|
|
|
"state",
|
|
|
|
"kind",
|
|
|
|
"author",
|
|
|
|
"author-id",
|
|
|
|
"url",
|
|
|
|
|
|
|
|
"title",
|
|
|
|
"body",
|
|
|
|
|
|
|
|
"created",
|
|
|
|
"updated",
|
|
|
|
"deadline",
|
|
|
|
|
|
|
|
"assignees",
|
|
|
|
"milestone",
|
|
|
|
"labels",
|
|
|
|
"comments",
|
|
|
|
}
|
|
|
|
|
|
|
|
func printIssues(issues []*gitea.Issue, output string, fields []string) {
|
|
|
|
labelMap := map[int64]string{}
|
|
|
|
var printables = make([]printable, len(issues))
|
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 20:36:33 +00:00
|
|
|
machineReadable := isMachineReadable(output)
|
2020-12-21 15:41:07 +00:00
|
|
|
|
|
|
|
for i, x := range issues {
|
|
|
|
// pre-serialize labels for performance
|
|
|
|
for _, label := range x.Labels {
|
|
|
|
if _, ok := labelMap[label.ID]; !ok {
|
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 20:36:33 +00:00
|
|
|
labelMap[label.ID] = formatLabel(label, !machineReadable, "")
|
2020-12-21 15:41:07 +00:00
|
|
|
}
|
2020-12-08 10:28:54 +00:00
|
|
|
}
|
2020-12-21 15:41:07 +00:00
|
|
|
// store items with printable interface
|
|
|
|
printables[i] = &printableIssue{x, &labelMap}
|
2020-12-08 10:28:54 +00:00
|
|
|
}
|
2020-12-21 15:41:07 +00: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 20:36:33 +00:00
|
|
|
t := tableFromItems(fields, printables, machineReadable)
|
2020-12-09 22:04:36 +00:00
|
|
|
t.print(output)
|
2020-12-08 10:28:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 15:41:07 +00:00
|
|
|
type printableIssue struct {
|
|
|
|
*gitea.Issue
|
|
|
|
formattedLabels *map[int64]string
|
|
|
|
}
|
2020-12-08 10:28:54 +00: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 20:36:33 +00:00
|
|
|
func (x printableIssue) FormatField(field string, machineReadable bool) string {
|
2020-12-21 15:41:07 +00:00
|
|
|
switch field {
|
|
|
|
case "index":
|
|
|
|
return fmt.Sprintf("%d", x.Index)
|
|
|
|
case "state":
|
|
|
|
return string(x.State)
|
|
|
|
case "kind":
|
|
|
|
if x.PullRequest != nil {
|
|
|
|
return "Pull"
|
2020-12-08 10:28:54 +00:00
|
|
|
}
|
2020-12-21 15:41:07 +00:00
|
|
|
return "Issue"
|
|
|
|
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 22:37:13 +00:00
|
|
|
return FormatTime(x.Created, machineReadable)
|
2020-12-21 15:41:07 +00:00
|
|
|
case "updated":
|
2022-03-28 22:37:13 +00:00
|
|
|
return FormatTime(x.Updated, machineReadable)
|
2020-12-21 15:41:07 +00:00
|
|
|
case "deadline":
|
2021-09-06 10:45:24 +00:00
|
|
|
if x.Deadline == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2022-03-28 22:37:13 +00:00
|
|
|
return FormatTime(*x.Deadline, machineReadable)
|
2020-12-21 15:41:07 +00:00
|
|
|
case "milestone":
|
|
|
|
if x.Milestone != nil {
|
|
|
|
return x.Milestone.Title
|
2020-12-08 10:28:54 +00:00
|
|
|
}
|
2020-12-21 15:41:07 +00:00
|
|
|
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)
|
2020-12-08 10:28:54 +00:00
|
|
|
}
|
2020-12-21 15:41:07 +00:00
|
|
|
return ""
|
2020-12-08 10:28:54 +00:00
|
|
|
}
|