Skip to content

Commit

Permalink
feat: add pipeline wait
Browse files Browse the repository at this point in the history
  • Loading branch information
craftamap committed Sep 13, 2024
1 parent 9b95f4b commit 1c9ff72
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/commands/pipelines/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/craftamap/bb/cmd/commands/pipelines/list"
"github.com/craftamap/bb/cmd/commands/pipelines/logs"
"github.com/craftamap/bb/cmd/commands/pipelines/view"
"github.com/craftamap/bb/cmd/commands/pipelines/wait"
"github.com/craftamap/bb/cmd/options"
"github.com/spf13/cobra"
)
Expand All @@ -18,6 +19,7 @@ func Add(rootCmd *cobra.Command, globalOpts *options.GlobalOptions) {
list.Add(&pipelineCommand, globalOpts)
view.Add(&pipelineCommand, globalOpts)
logs.Add(&pipelineCommand, globalOpts)
wait.Add(&pipelineCommand, globalOpts)

rootCmd.AddCommand(&pipelineCommand)
}
68 changes: 68 additions & 0 deletions cmd/commands/pipelines/wait/wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package wait

import (
"strconv"
"strings"
"time"

"github.com/craftamap/bb/cmd/options"
"github.com/craftamap/bb/util/logging"
"github.com/spf13/cobra"
)

var (
verbose bool
)

const (
SUCCESSFUL = "SUCCESSFUL"
FAILED = "FAILED"
STOPPED = "STOPPED"
RUNNING = "RUNNING"
IN_PROGRESS = "IN_PROGRESS"
)

func Add(pipelinesCmd *cobra.Command, globalOpts *options.GlobalOptions) {
waitCmd := &cobra.Command{
Use: "wait <number of pipeline>",
Short: "wait until a specific pipeline execution has finished",
Long: "wait until a specific pipeline execution has finished. On it's own, this command, this is not particulary useful, but it can be combined with other commands, like notify-send, to send you a notification when a pipeline is done.",
Annotations: map[string]string{
"RequiresClient": "true",
"RequiresRepository": "true",
},
Run: func(cmd *cobra.Command, args []string) {
c := globalOpts.Client
bbrepo := globalOpts.BitbucketRepo

if len(args) == 0 {
logging.Error("Missing argument <number of pipeline>")
return
}
pipelineID, err := strconv.Atoi(strings.Replace(args[0], "#", "", 1))
if err != nil {
logging.Error(err)
return
}

for {
pipeline, err := c.PipelineGet(bbrepo.RepoOrga, bbrepo.RepoSlug, strconv.Itoa(pipelineID))
if err != nil {
logging.Error(err)
return
}
if verbose {
logging.Note("Current pipeline state: ", pipeline.PipelineState.Name)
}

if pipeline.PipelineState.Name == "COMPLETED" {
break
}
time.Sleep(1 * time.Second)
}

},
}
waitCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "verbosity of the command; logs the current state")
pipelinesCmd.AddCommand(waitCmd)
}

0 comments on commit 1c9ff72

Please sign in to comment.