Skip to content

Commit

Permalink
improve pipeline listing
Browse files Browse the repository at this point in the history
  • Loading branch information
3ximus committed Feb 18, 2024
1 parent ad34708 commit 564d40b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 77 deletions.
25 changes: 6 additions & 19 deletions api/bb-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
15 changes: 11 additions & 4 deletions cmd/pipeline/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
}
Expand All @@ -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
}
72 changes: 18 additions & 54 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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 {
Expand Down

0 comments on commit 564d40b

Please sign in to comment.