Skip to content

Commit

Permalink
compact listing commands
Browse files Browse the repository at this point in the history
  • Loading branch information
3ximus committed Feb 18, 2024
1 parent ebdc23a commit ad34708
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 36 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ jira_token: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX(192 characters)
# include branch name at beggining of the pull request (useful to link with jira tickets)
include_branch_name: true

# define custom status for jira tickets, to more easily filter by the preset options and colorize output
# define custom text icons or text for jira_status, jira_types, pr_status or pipeline_status. The format is as follows:
# identifier:
# values: ["State 1", "State 2"] # this is the string that matches the state being printed
# color: "1;34" # the ANSII sequence for the color used. if I want to remove icon 1;38;5;235;44
# text: "ACTUAL STATE" # string printed, takes precedence over icon
# icon: "ﲊ" # icon to display
#
# examples bellow:

jira_status:
inprogress:
values: ["In Progress", "In Progress_T"]
icon: "" # ﲊ 羽  
color: "1;34" # if I want to remove icon 1;38;5;235;44
color: "1;34"
todo:
values: ["À FAIRE"]
icon: "" #  
Expand Down
13 changes: 7 additions & 6 deletions cmd/issue/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var ListCmd = &cobra.Command{
statuses, _ := cmd.Flags().GetStringArray("status")
project, _ := cmd.Flags().GetString("project")
priority, _ := cmd.Flags().GetBool("priority")
showUsers, _ := cmd.Flags().GetBool("show-users")
showUsers, _ := cmd.Flags().GetBool("users")
showTime, _ := cmd.Flags().GetBool("time")

if !cmd.Flags().Changed("project") {
branch, err := util.GetCurrentBranch()
Expand Down Expand Up @@ -54,7 +55,6 @@ var ListCmd = &cobra.Command{
}
}

fmt.Println()
for issue := range api.GetIssueList(nResults, all, reporter, project, statusConversion, priority) {
timeSpent := "-"
if issue.Fields.TimeTracking.TimeSpent != " " {
Expand All @@ -71,9 +71,10 @@ var ListCmd = &cobra.Command{
fmt.Printf(" \033[37mReporter: \033[1m%s \033[37m(%d comments)\033[m\n", issue.Fields.Reporter.DisplayName, issue.Fields.Comment.Total)
}
}
fmt.Printf(" \033[37mTime spent: \033[1;34m%s\033[m [ %s/%s ]\n", timeSpent, issue.Fields.TimeTracking.RemainingEstimate, issue.Fields.TimeTracking.OriginalEstimate)
if showTime {
fmt.Printf(" \033[37mTime spent: \033[1;34m%s\033[m [ %s/%s ]\n", timeSpent, issue.Fields.TimeTracking.RemainingEstimate, issue.Fields.TimeTracking.OriginalEstimate)
}
}
fmt.Println()
},
}

Expand All @@ -90,8 +91,8 @@ func init() {
// TODO add way to sort by recent or the ones the user has participated on

// display
ListCmd.Flags().BoolP("show-users", "u", false, "show users")
ListCmd.Flags().BoolP("one-line", "o", false, "show one issue per line. \033[31mNot implemented\033[m")
ListCmd.Flags().BoolP("users", "u", false, "show users")
ListCmd.Flags().BoolP("time", "t", false, "show time information")
ListCmd.Flags().IntP("number-results", "n", 10, "max number of results retrieve")
// sort
ListCmd.Flags().BoolP("priority", "P", false, "sort by priority")
Expand Down
6 changes: 5 additions & 1 deletion cmd/pipeline/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var ListCmd = &cobra.Command{
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
pages, _ := cmd.Flags().GetInt("pages")
showAuthor, _ := cmd.Flags().GetBool("author")
pipelineChannel := api.GetPipelineList(viper.GetString("repo"), pages, "")
for pipeline := range pipelineChannel {
if pipeline.State.Result.Name == "" {
Expand All @@ -32,11 +33,14 @@ var ListCmd = &cobra.Command{

fmt.Printf(" \033[37m%s (%s)\033[m\n", util.TimeDuration(time.Duration(pipeline.DurationInSeconds*1000000000)), util.TimeAgo(pipeline.CreatedOn))

fmt.Printf(" \033[33m%s\033[m \033[37mTrigger: %s\033[m\n", pipeline.Author.DisplayName, pipeline.Trigger.Name) // \033[37mComments: %d\033[m",
if showAuthor {
fmt.Printf(" \033[33m%s\033[m \033[37mTrigger: %s\033[m\n", pipeline.Author.DisplayName, pipeline.Trigger.Name) // \033[37mComments: %d\033[m",
}
}
},
}

func init() {
ListCmd.Flags().Int("pages", 1, "number of pages with results to retrieve")
ListCmd.Flags().BoolP("author", "a", false, "show author information")
}
5 changes: 3 additions & 2 deletions cmd/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
)

var PipelineCmd = &cobra.Command{
Use: "pipeline",
Short: "Manage pipelines",
Use: "pipeline",
Aliases: []string{"pl"},
Short: "Manage pipelines",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
err := viper.BindPFlag("repo", cmd.Flags().Lookup("repo"))
cobra.CheckErr(err)
Expand Down
16 changes: 5 additions & 11 deletions cmd/pr/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,9 @@ var ListCmd = &cobra.Command{

prChannel := api.GetPrList(viper.GetString("repo"), states, author, search, source, destination, pages, status, participants)

// fmt.Printf("\n Pull Requests for \033[1;36m%s\033[m\n\n", viper.GetString("repo"))
count := 0
fmt.Println()
for pr := range prChannel {
// if we didn't provide filter don't show the pr status
fmt.Printf("%-19s \033[1;32m#%d\033[m %s \033[1;34m[ %s → %s]\033[m\n", util.FormatPrState(pr.State), pr.ID, pr.Title, pr.Source.Branch.Name, pr.Destination.Branch.Name)
fmt.Printf(" \033[33m%s\033[m \033[37mComments: %d\033[m", pr.Author.Nickname, pr.CommentCount)
fmt.Printf("%s \033[1;32m#%d\033[m %s \033[1;34m[ %s \033[m→\033[1;34m %s ]\033[m \033[33m%s\033[m", util.FormatPrState(pr.State), pr.ID, pr.Title, pr.Source.Branch.Name, pr.Destination.Branch.Name, pr.Author.Nickname)
if status {
fmt.Printf(" %s", util.FormatPipelineState(pr.Status.State))
}
Expand All @@ -49,23 +45,21 @@ var ListCmd = &cobra.Command{
outputStr = append(outputStr, fmt.Sprintf("\033[0;37m%s\033[m", participant.User.DisplayName))
}
}
fmt.Printf(" ( %s )", strings.Join(outputStr, ", "))
fmt.Printf("\n \033[37mComments: %d\033[m ( %s )", pr.CommentCount, strings.Join(outputStr, ", "))
}
fmt.Println()
count++
}
if count == 0 {
fmt.Printf("\n No pull requests for \033[1;36m%s\033[m\n\n", viper.GetString("repo"))
} else {
fmt.Println()
}
},
}

func init() {
// filter
ListCmd.Flags().StringP("author", "a", "", "filter by author nick name (full nickname is needed due to an API limitation from bitbucket)")
ListCmd.Flags().StringP("search", "S", "", "search pull request with query")
ListCmd.Flags().String("search", "", "search pull request with query")
ListCmd.Flags().StringArrayP("state", "s", []string{string(api.OPEN)}, `filter by state. Default: "open". Multiple of these options can be given
possible options: "open", "merged", "declined" or "superseded"`)
ListCmd.RegisterFlagCompletionFunc("state", stateCompletion)
Expand All @@ -76,8 +70,8 @@ func init() {
ListCmd.Flags().Bool("all", false, "return pull request with all possible states.")

ListCmd.Flags().Int("pages", 1, "number of pages with results to retrieve")
ListCmd.Flags().Bool("status", false, "include status of each pull request on the result. (the result will be slower)")
ListCmd.Flags().BoolP("participants", "p", false, "include participant data for each pull request on the result. (the result will be slower)")
ListCmd.Flags().BoolP("status", "S", false, "include status of each pull request on the result. (the result will be slower)")
ListCmd.Flags().BoolP("participants", "p", false, "include participant and comment data for each pull request on the result. (the result will be slower)")
}

func stateCompletion(comd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
37 changes: 23 additions & 14 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,35 @@ import (
"github.com/spf13/viper"
)

type JiraResultConfig struct {
type ResultSwitchConfig struct {
Values []string `mapstructure:"values"`
Text string `mapstructure:"text"`
Icon string `mapstructure:"icon"`
Color string `mapstructure:"color"`
}

func FormatPrState(state api.PrState) string {
stateString := ""
switch state {
case "OPEN":
stateString = "\033[1;38;5;235;44m OPEN \033[m"
case "MERGED":
stateString = "\033[1;38;5;235;45m MERGED \033[m"
case "DECLINED":
stateString = "\033[1;38;5;235;41m DECLINED \033[m"
case "SUPERSEDED":
stateString = "\033[1;38;5;235;44m SUPERSEDED \033[m"
str := fmt.Sprintf("\033[1;38;5;235;47m %s \033[m", state) // default

jiraStatusMap := make(map[string]ResultSwitchConfig)
if err := viper.UnmarshalKey("pr_status", &jiraStatusMap); err != nil {
cobra.CheckErr(err)
}
return stateString

for k, v := range jiraStatusMap {
for _, s := range v.Values {
if s == state.String() {
if v.Text != "" {
str = fmt.Sprintf("\033[%sm %s \033[m", v.Color, v.Text)
} else if v.Icon != "" {
str = fmt.Sprintf("\033[%sm%s\033[m", v.Color, v.Icon)
} else {
str = fmt.Sprintf("\033[%sm %s \033[m", v.Color, strings.ToUpper(k))
}
}
}
}
return str
}

func FormatPipelineState(state string) string {
Expand All @@ -55,7 +64,7 @@ func FormatPipelineState(state string) string {
func FormatIssueType(issueType string) string {
str := ""

jiraStatusMap := make(map[string]JiraResultConfig)
jiraStatusMap := make(map[string]ResultSwitchConfig)
if err := viper.UnmarshalKey("jira_type", &jiraStatusMap); err != nil {
cobra.CheckErr(err)
}
Expand All @@ -79,7 +88,7 @@ func FormatIssueType(issueType string) string {
func FormatIssueStatus(status string) string {
str := fmt.Sprintf("\033[1;38;5;235;47m %s \033[m", status) // default

jiraStatusMap := make(map[string]JiraResultConfig)
jiraStatusMap := make(map[string]ResultSwitchConfig)
if err := viper.UnmarshalKey("jira_status", &jiraStatusMap); err != nil {
cobra.CheckErr(err)
}
Expand Down

0 comments on commit ad34708

Please sign in to comment.