Skip to content

Commit

Permalink
pipeline view - include pipeline steps and commands
Browse files Browse the repository at this point in the history
  • Loading branch information
3ximus committed Feb 18, 2024
1 parent 68e91bf commit 2d7c31a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
15 changes: 15 additions & 0 deletions api/bb-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,21 @@ func GetPipeline(repository string, id string) <-chan Pipeline {
return channel
}

func GetPipelineSteps(repository string, id string) <-chan PipelineStep {
channel := make(chan PipelineStep)
go func() {
defer close(channel)
var steps BBPaginatedResponse[PipelineStep]
response := bbApiGet(fmt.Sprintf("repositories/%s/pipelines/%s/steps", repository, id))
err := json.Unmarshal(response, &steps)
cobra.CheckErr(err)
for _, step := range steps.Values {
channel <- step
}
}()
return channel
}

func GetPipelineVariables(repository string) <-chan EnvironmentVariable {
channel := make(chan EnvironmentVariable)
go func() {
Expand Down
30 changes: 30 additions & 0 deletions api/bb-types.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,36 @@ type Pipeline struct {
CreatedOn time.Time `json:"created_on"`
}

type PipelineStep struct {
UUID string
Name string
DurationInSeconds int `json:"duration_in_seconds"`
State struct {
Name string
Result struct {
Name string
}
Stage struct {
Name string
}
}
SetupCommands []StepCommand `json:"setup_commands"`
ScriptCommands []StepCommand `json:"script_commands"`
TeardownCommands []StepCommand `json:"teardown_commands"`
Image struct {
Name string
}
Pipeline struct {
UUID string
}
}

type StepCommand struct {
Name string
Command string
CommandType string
}

type PrComment struct {
Id int
Content struct {
Expand Down
22 changes: 21 additions & 1 deletion cmd/pipeline/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bb/util"
"fmt"
"strconv"
"time"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -16,6 +17,7 @@ var ViewCmd = &cobra.Command{
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
repo := viper.GetString("repo")
showCommands, _ := cmd.Flags().GetBool("commands")

var id int
var err error
Expand All @@ -33,6 +35,8 @@ var ViewCmd = &cobra.Command{
cobra.CheckErr(err)
}

// make the steps request so that it's ready to print later on
stepsChannel := api.GetPipelineSteps(repo, fmt.Sprintf("%d", id))
pipeline := <-api.GetPipeline(repo, fmt.Sprintf("%d", id))

if pipeline.State.Result.Name == "" {
Expand All @@ -49,6 +53,21 @@ var ViewCmd = &cobra.Command{

fmt.Printf(" \033[33m%s\033[m \033[37mTrigger: %s\033[m\n", pipeline.Author.DisplayName, pipeline.Trigger.Name)

fmt.Println()
for step := range stepsChannel {
if step.State.Result.Name != "" {
fmt.Printf("%s %s \033[37m%s\033[m", step.Name, util.FormatPipelineState(step.State.Result.Name), util.TimeDuration(time.Duration(step.DurationInSeconds*1000000000)))
} else {
fmt.Printf("%s %s \033[37m%s\033[m", step.Name, util.FormatPipelineState(step.State.Stage.Name), util.TimeDuration(time.Duration(step.DurationInSeconds*1000000000)))
}
fmt.Println()
if showCommands {
for _, command := range step.ScriptCommands {
fmt.Printf("\t%s\n", command.Name)
}
}
}

web, _ := cmd.Flags().GetBool("web")
if web {
util.OpenInBrowser(api.BBBrowsePipelines(repo, id))
Expand All @@ -59,5 +78,6 @@ var ViewCmd = &cobra.Command{
}

func init() {
ViewCmd.Flags().Bool("web", false, "Open in the browser.")
ViewCmd.Flags().Bool("web", false, "open in the browser")
ViewCmd.Flags().BoolP("commands", "c", false, "show step commands")
}

0 comments on commit 2d7c31a

Please sign in to comment.