2020-09-30 01:11:33 -04: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 10:41:07 -05:00
|
|
|
"strings"
|
2020-09-30 01:11:33 -04:00
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IssueDetails print an issue rendered to stdout
|
|
|
|
func IssueDetails(issue *gitea.Issue) {
|
2020-12-08 05:28:54 -05:00
|
|
|
outputMarkdown(fmt.Sprintf(
|
2020-12-07 23:06:05 -05:00
|
|
|
"# #%d %s (%s)\n@%s created %s\n\n%s\n",
|
2020-10-04 22:23:57 -04:00
|
|
|
issue.Index,
|
2020-09-30 01:11:33 -04:00
|
|
|
issue.Title,
|
|
|
|
issue.State,
|
|
|
|
issue.Poster.UserName,
|
2020-10-05 08:23:32 -04:00
|
|
|
FormatTime(issue.Created),
|
2020-09-30 01:11:33 -04:00
|
|
|
issue.Body,
|
2020-10-04 22:23:57 -04:00
|
|
|
))
|
2020-09-30 01:11:33 -04:00
|
|
|
}
|
2020-12-08 05:28:54 -05:00
|
|
|
|
2020-12-21 10:41:07 -05:00
|
|
|
// IssuesPullsList prints a listing of issues & pulls
|
|
|
|
func IssuesPullsList(issues []*gitea.Issue, output string, fields []string) {
|
|
|
|
printIssues(issues, output, fields)
|
|
|
|
}
|
2020-12-08 05:28:54 -05:00
|
|
|
|
2020-12-21 10:41:07 -05: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))
|
|
|
|
|
|
|
|
for i, x := range issues {
|
|
|
|
// pre-serialize labels for performance
|
|
|
|
for _, label := range x.Labels {
|
|
|
|
if _, ok := labelMap[label.ID]; !ok {
|
|
|
|
labelMap[label.ID] = formatLabel(label, !isMachineReadable(output), "")
|
|
|
|
}
|
2020-12-08 05:28:54 -05:00
|
|
|
}
|
2020-12-21 10:41:07 -05:00
|
|
|
// store items with printable interface
|
|
|
|
printables[i] = &printableIssue{x, &labelMap}
|
2020-12-08 05:28:54 -05:00
|
|
|
}
|
2020-12-21 10:41:07 -05:00
|
|
|
|
|
|
|
t := tableFromItems(fields, printables)
|
2020-12-09 17:04:36 -05:00
|
|
|
t.print(output)
|
2020-12-08 05:28:54 -05:00
|
|
|
}
|
|
|
|
|
2020-12-21 10:41:07 -05:00
|
|
|
type printableIssue struct {
|
|
|
|
*gitea.Issue
|
|
|
|
formattedLabels *map[int64]string
|
|
|
|
}
|
2020-12-08 05:28:54 -05:00
|
|
|
|
2020-12-21 10:41:07 -05:00
|
|
|
func (x printableIssue) FormatField(field string) string {
|
|
|
|
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 05:28:54 -05:00
|
|
|
}
|
2020-12-21 10:41:07 -05: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":
|
|
|
|
return FormatTime(x.Created)
|
|
|
|
case "updated":
|
|
|
|
return FormatTime(x.Updated)
|
|
|
|
case "deadline":
|
|
|
|
return FormatTime(*x.Deadline)
|
|
|
|
case "milestone":
|
|
|
|
if x.Milestone != nil {
|
|
|
|
return x.Milestone.Title
|
2020-12-08 05:28:54 -05:00
|
|
|
}
|
2020-12-21 10:41:07 -05: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 05:28:54 -05:00
|
|
|
}
|
2020-12-21 10:41:07 -05:00
|
|
|
return ""
|
2020-12-08 05:28:54 -05:00
|
|
|
}
|