Skip to content

Commit

Permalink
pipeline logs
Browse files Browse the repository at this point in the history
  • Loading branch information
3ximus committed Feb 18, 2024
1 parent 2d7c31a commit 2a21cf2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
18 changes: 13 additions & 5 deletions api/bb-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,17 +361,25 @@ func GetPipeline(repository string, id string) <-chan Pipeline {
return channel
}

func GetPipelineSteps(repository string, id string) <-chan PipelineStep {
channel := make(chan PipelineStep)
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
}
channel <- steps.Values
}()
return channel
}

func GetPipelineStepLogs(repository string, id string, stepUUID string) <-chan string {
channel := make(chan string)
go func() {
defer close(channel)
response := bbApiGet(fmt.Sprintf("repositories/%s/pipelines/%s/steps/%s/log", repository, id, stepUUID))
channel <- string(response)
}()
return channel
}
Expand Down
46 changes: 44 additions & 2 deletions cmd/pipeline/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package pipeline

import (
"bb/api"
"bb/util"
"fmt"
"strconv"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -11,11 +14,50 @@ import (
var LogsCmd = &cobra.Command{
Use: "logs",
Short: "Show logs of a pipeline",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
api.GetPipelineList(viper.GetString("repo"), 0, "")
fmt.Println("Not implemented")
repo := viper.GetString("repo")

var id int
var err error
if len(args) == 0 {
branch, err := util.GetCurrentBranch()
cobra.CheckErr(err)
// retrieve id of pr for current branch
pipeline := <-api.GetPipelineList(repo, 1, branch)
if pipeline.BuildNumber == 0 {
cobra.CheckErr("No pipelines found for this branch")
}
id = pipeline.BuildNumber
} else {
id, err = strconv.Atoi(args[0])
cobra.CheckErr(err)
}

var stepUUID = ""
steps := <-api.GetPipelineSteps(repo, fmt.Sprintf("%d", id))
selectedStep, _ := cmd.Flags().GetString("step")
if selectedStep == "" {
optIndex := util.SelectFZF(steps, fmt.Sprintf("Step to Log > "), func(i int) string {
return fmt.Sprintf("%s", steps[i].Name)
})
if len(optIndex) > 0 {
stepUUID = steps[optIndex[0]].UUID
}
} else {
for _, step := range steps {
if step.Name == selectedStep || strings.ToLower(step.Name) == selectedStep {
stepUUID = step.UUID
}
}
if stepUUID == "" {
cobra.CheckErr("Step not found")
}
}
fmt.Print(<-api.GetPipelineStepLogs(repo, fmt.Sprintf("%d", id), stepUUID))
},
}

func init() {
LogsCmd.Flags().StringP("step", "s", "", "select step. Without this option the step is prompet interactively")
}
2 changes: 1 addition & 1 deletion cmd/pipeline/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ 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 {
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 {
Expand Down

0 comments on commit 2a21cf2

Please sign in to comment.