From 564d40b2e7f457a6f26768335b3869ec5b7b4b20 Mon Sep 17 00:00:00 2001 From: eximus Date: Sat, 17 Feb 2024 21:31:53 -0500 Subject: [PATCH] improve pipeline listing --- api/bb-api.go | 25 ++++----------- cmd/pipeline/list.go | 15 ++++++--- util/util.go | 72 +++++++++++--------------------------------- 3 files changed, 35 insertions(+), 77 deletions(-) diff --git a/api/bb-api.go b/api/bb-api.go index 53b07a5..63fcfe5 100644 --- a/api/bb-api.go +++ b/api/bb-api.go @@ -325,7 +325,7 @@ func RequestChangesPr(repository string, id int) { bbApiPost(fmt.Sprintf("repositories/%s/pullrequests/%d/request-changes", repository, id), nil) } -func GetPipelineList(repository string, pages int, targetBranch string) <-chan Pipeline { +func GetPipelineList(repository string, nResults int, targetBranch string) <-chan Pipeline { channel := make(chan Pipeline) go func() { defer close(channel) @@ -336,24 +336,11 @@ func GetPipelineList(repository string, pages int, targetBranch string) <-chan P } var prevResponse BBPaginatedResponse[Pipeline] - for i := 0; i < pages; i++ { - var response []byte - if i == 0 { - response = bbApiGet(fmt.Sprintf("repositories/%s/pipelines?sort=-created_on&pagelen=5%s", repository, query)) - } else { - newUrl := strings.Replace(prevResponse.Next, "https://api.bitbucket.org/2.0/", "", 1) - if newUrl == "" { - break // there's no next page - } - response = bbApiGet(newUrl) - } - err := json.Unmarshal(response, &prevResponse) - cobra.CheckErr(err) - - for _, pipeline := range prevResponse.Values { - channel <- pipeline - } - + response := bbApiGet(fmt.Sprintf("repositories/%s/pipelines?sort=-created_on&pagelen=%d%s", repository, nResults, query)) + err := json.Unmarshal(response, &prevResponse) + cobra.CheckErr(err) + for _, pipeline := range prevResponse.Values { + channel <- pipeline } }() return channel diff --git a/cmd/pipeline/list.go b/cmd/pipeline/list.go index a6178f1..966b53c 100644 --- a/cmd/pipeline/list.go +++ b/cmd/pipeline/list.go @@ -15,9 +15,10 @@ var ListCmd = &cobra.Command{ Short: "List pipelines from a repository", Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { - pages, _ := cmd.Flags().GetInt("pages") + nResults, _ := cmd.Flags().GetInt("number-results") showAuthor, _ := cmd.Flags().GetBool("author") - pipelineChannel := api.GetPipelineList(viper.GetString("repo"), pages, "") + targetBranch, _ := cmd.Flags().GetString("target") + pipelineChannel := api.GetPipelineList(viper.GetString("repo"), nResults, targetBranch) for pipeline := range pipelineChannel { if pipeline.State.Result.Name == "" { fmt.Printf("%s", util.FormatPipelineState(pipeline.State.Name)) @@ -26,7 +27,7 @@ var ListCmd = &cobra.Command{ } fmt.Printf(" \033[1;32m#%d\033[m ", pipeline.BuildNumber) if pipeline.Target.Source != "" { - fmt.Printf("%s \033[1;34m[ %s → %s]\033[m", pipeline.Target.PullRequest.Title, pipeline.Target.Source, pipeline.Target.Destination) + fmt.Printf("%s \033[1;34m[ %s → %s ]\033[m", pipeline.Target.PullRequest.Title, pipeline.Target.Source, pipeline.Target.Destination) } else { fmt.Printf("\033[1;34m[ %s ]\033[m", pipeline.Target.RefName) } @@ -41,6 +42,12 @@ var ListCmd = &cobra.Command{ } func init() { - ListCmd.Flags().Int("pages", 1, "number of pages with results to retrieve") + ListCmd.Flags().IntP("number-results", "n", 10, "max number of results retrieve (max: 100)") ListCmd.Flags().BoolP("author", "a", false, "show author information") + ListCmd.Flags().String("target", "", "filter target branch of pipeline") + ListCmd.RegisterFlagCompletionFunc("target", branchCompletion) +} + +func branchCompletion(comd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return util.ListBranches(), cobra.ShellCompDirectiveDefault } diff --git a/util/util.go b/util/util.go index 18b771a..5913b22 100644 --- a/util/util.go +++ b/util/util.go @@ -22,17 +22,12 @@ type ResultSwitchConfig struct { Color string `mapstructure:"color"` } -func FormatPrState(state api.PrState) string { - 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) - } +func FormatSwitchConfig(result string, mapping map[string]ResultSwitchConfig) string { + str := fmt.Sprintf("\033[1;38;5;235;47m %s \033[m", result) // default - for k, v := range jiraStatusMap { + for k, v := range mapping { for _, s := range v.Values { - if s == state.String() { + if s == result { if v.Text != "" { str = fmt.Sprintf("\033[%sm %s \033[m", v.Color, v.Text) } else if v.Icon != "" { @@ -46,67 +41,36 @@ func FormatPrState(state api.PrState) string { return str } +func FormatPrState(state api.PrState) string { + prStatusMap := make(map[string]ResultSwitchConfig) + if err := viper.UnmarshalKey("pr_status", &prStatusMap); err != nil { + cobra.CheckErr(err) + } + return FormatSwitchConfig(state.String(), prStatusMap) +} + func FormatPipelineState(state string) string { - stateString := "" - switch state { - case "INPROGRESS", "IN_PROGRESS": - stateString = "\033[1;38;5;235;44m RUNNING \033[m" - case "STOPPED", "stopped": - stateString = "\033[1;38;5;235;43m STOPPED \033[m" - case "SUCCESSFUL", "successful": - stateString = "\033[1;38;5;235;42m PASS \033[m" - case "FAILED", "failed": - stateString = "\033[1;38;5;235;41m FAIL \033[m" + pipelineStatusMap := make(map[string]ResultSwitchConfig) + if err := viper.UnmarshalKey("pipeline_status", &pipelineStatusMap); err != nil { + cobra.CheckErr(err) } - return stateString + return FormatSwitchConfig(state, pipelineStatusMap) } func FormatIssueType(issueType string) string { - str := "" - jiraStatusMap := make(map[string]ResultSwitchConfig) if err := viper.UnmarshalKey("jira_type", &jiraStatusMap); err != nil { cobra.CheckErr(err) } - - for k, v := range jiraStatusMap { - for _, s := range v.Values { - if s == issueType { - 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 + return FormatSwitchConfig(issueType, jiraStatusMap) } func FormatIssueStatus(status string) string { - str := fmt.Sprintf("\033[1;38;5;235;47m %s \033[m", status) // default - jiraStatusMap := make(map[string]ResultSwitchConfig) if err := viper.UnmarshalKey("jira_status", &jiraStatusMap); err != nil { cobra.CheckErr(err) } - - for k, v := range jiraStatusMap { - for _, s := range v.Values { - if s == status { - 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 + return FormatSwitchConfig(status, jiraStatusMap) } func FormatIssuePriority(id string, name string) string {