Skip to content

Commit

Permalink
Added first working draft of pipelines list
Browse files Browse the repository at this point in the history
  • Loading branch information
craftamap committed Dec 4, 2020
1 parent 21feb8d commit aadd5c6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
59 changes: 57 additions & 2 deletions cmd/commands/pipelines/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ package list

import (
"fmt"
"strings"

"github.com/craftamap/bb/cmd/options"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
)

const (
SUCCESSFUL = "SUCCESSFUL"
FAILED = "FAILED"
STOPPED = "STOPPED"
INPROGRESS = "INPROGRESS"
)

func Add(pipelinesCmd *cobra.Command, globalOpts *options.GlobalOptions) {
listCmd := &cobra.Command{
Use: "list",
Expand All @@ -20,9 +29,55 @@ func Add(pipelinesCmd *cobra.Command, globalOpts *options.GlobalOptions) {
c := globalOpts.Client
bbrepo := globalOpts.BitbucketRepo

pipelines, _ := c.PipelineList(bbrepo.RepoOrga, bbrepo.RepoSlug)
pipelines, err := c.PipelineList(bbrepo.RepoOrga, bbrepo.RepoSlug)
if err != nil {
fmt.Printf("%s%s%s\n", aurora.Red(":: "), aurora.Bold("An error occurred: "), err)
return
}
if len(*pipelines) == 0 {
fmt.Println("No builds/statuses found for this pull request")
return
}
hashToDescription := map[string]string{}

for _, pipeline := range *pipelines {
fmt.Println(pipeline.UUID)
revHash := pipeline.Target.(map[string]interface{})["commit"].(map[string]interface{})["hash"].(string)
commitDescription := ""
commit, err := c.GetCommit(bbrepo.RepoOrga, bbrepo.RepoSlug, revHash)
if err == nil {
commitDescription = strings.Split(commit.Message, "\n")[0]
}
hashToDescription[revHash] = commitDescription
}
for _, pipeline := range *pipelines {
var statusIcon string
switch pipeline.PipelineState.Result.Name {
case SUCCESSFUL:
statusIcon = aurora.Green("✓").String()
case FAILED, STOPPED:
statusIcon = aurora.Red("X").String()
case INPROGRESS:
statusIcon = aurora.Yellow("⏱️").String()
}
revHash := pipeline.Target.(map[string]interface{})["commit"].(map[string]interface{})["hash"].(string)
description := hashToDescription[revHash]
revHashShort := revHash[:7]

branchName := pipeline.Target.(map[string]interface{})["ref_name"].(string)

fmt.Printf(
"%s #%d %s (%ds) %s\n",
statusIcon,
aurora.Index(242, pipeline.BuildNumber),
description,
pipeline.BuildSecondsUsed,
aurora.Index(242,
fmt.Sprintf("(%s, %s, %s)",
pipeline.Creator.DisplayName,
revHashShort,
branchName,
)),
)
}
},
}
Expand Down
13 changes: 7 additions & 6 deletions internal/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ type PipelineStateResult struct {
}

type PipelineState struct {
Name string
Type string
state PipelineStateResult
Name string
Type string
Result PipelineStateResult
}

type PipelineTrigger struct {
Expand All @@ -23,9 +23,9 @@ type PipelineTrigger struct {

type Pipeline struct {
Type string
UUID string
PipelineState PipelineState
BuildNumber int `mapstructure:"build_number"`
UUID string `mapstructure:"uuid"`
PipelineState PipelineState `mapstructure:"state"`
BuildNumber int `mapstructure:"build_number"`
Creator Account
CreatedOn string `mapstructure:"created_on"`
CompletedOn string `mapstructure:"completed_on"`
Expand All @@ -44,6 +44,7 @@ func (c Client) PipelineList(repoOrga string, repoSlug string) (*[]Pipeline, err
response, err := client.Repositories.Pipelines.List(&bitbucket.PipelinesOptions{
Owner: repoOrga,
RepoSlug: repoSlug,
Sort: "-created_on",
})
if err != nil {
return nil, err
Expand Down

0 comments on commit aadd5c6

Please # to comment.