Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SUP 1748 Implement bk build cancel and code refactoring #203

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions internal/build/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ func renderBuildState(state string, blocked bool) string {
stateIcon = "⏰"
case "failed", "failing":
stateIcon = "✖"
case "canceled", "canceling":
case "canceled":
stateIcon = "🚫"
case "item":
stateIcon = "∴∘"

case "canceling":
stateIcon = "🚫(cancelling...)"
default:
stateIcon = "❔"
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/cmd/build/build.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package build

import (
"fmt"
"net/url"
"strings"

"github.com/MakeNowJust/heredoc"
"github.com/buildkite/cli/v3/internal/config"
"github.com/buildkite/cli/v3/pkg/cmd/factory"
"github.com/buildkite/cli/v3/pkg/cmd/validation"
"github.com/charmbracelet/lipgloss"
"github.com/pkg/browser"
"github.com/spf13/cobra"
)

Expand All @@ -34,6 +37,7 @@ func NewCmdBuild(f *factory.Factory) *cobra.Command {
cmd.AddCommand(NewCmdBuildNew(f))
cmd.AddCommand(NewCmdBuildView(f))
cmd.AddCommand(NewCmdBuildRebuild(f))
cmd.AddCommand(NewCmdBuildCancel(f))

return &cmd
}
Expand All @@ -60,3 +64,21 @@ func parsePipelineArg(arg string, conf *config.Config) (string, string) {
}
return org, pipeline
}

func openBuildInBrowser(openInWeb bool, webUrl string) error {

if openInWeb {
fmt.Printf("Opening %s in your browser\n", webUrl)
err := browser.OpenURL(webUrl)
if err != nil {
fmt.Println("Error opening browser: ", err)
return err
}
}
return nil
}

func renderResult(result string) string {
return lipgloss.JoinVertical(lipgloss.Top,
lipgloss.NewStyle().Padding(1, 1).Render(result))
}
58 changes: 58 additions & 0 deletions pkg/cmd/build/cancel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package build

import (
"fmt"

"github.com/MakeNowJust/heredoc"
"github.com/buildkite/cli/v3/internal/io"
"github.com/buildkite/cli/v3/pkg/cmd/factory"
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
)

func NewCmdBuildCancel(f *factory.Factory) *cobra.Command {
var web bool

cmd := cobra.Command{
DisableFlagsInUseLine: true,
Use: "cancel <number> <pipeline> [flags]",
Short: "Cancels a build.",
Args: cobra.ExactArgs(2),
Long: heredoc.Doc(`
Cancels the specified build.

It accepts a build number and a pipeline slug as an argument.
The pipeline can be a {pipeline_slug}, {org_slug}/{pipeline_slug} or a full URL to the pipeline.
`),
RunE: func(cmd *cobra.Command, args []string) error {
buildId := args[0]
org, pipeline := parsePipelineArg(args[1], f.Config)
return cancelBuild(org, pipeline, buildId, web, f)
},
}

cmd.Flags().BoolVarP(&web, "web", "w", false, "Open the build in a web browser after it has been created.")
cmd.Flags().SortFlags = false
return &cmd
}

func cancelBuild(org string, pipeline string, buildId string, web bool, f *factory.Factory) error {
l := io.NewPendingCommand(func() tea.Msg {
build, err := f.RestAPIClient.Builds.Cancel(org, pipeline, buildId)
if err != nil {
return err
}

if err = openBuildInBrowser(web, *build.WebURL); err != nil {
return err
}

return io.PendingOutput(renderResult(fmt.Sprintf("Cancelling build: %s\n", *build.WebURL)))

}, fmt.Sprintf("Cancelling build #%s from pipeline %s", buildId, pipeline))

p := tea.NewProgram(l)
_, err := p.Run()

return err
}
16 changes: 6 additions & 10 deletions pkg/cmd/build/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/buildkite/cli/v3/pkg/cmd/factory"
"github.com/buildkite/go-buildkite/v3/buildkite"
tea "github.com/charmbracelet/bubbletea"
"github.com/pkg/browser"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -43,7 +43,6 @@ func NewCmdBuildNew(f *factory.Factory) *cobra.Command {
}

func newBuild(org string, pipeline string, f *factory.Factory, message string, commit string, branch string, web bool) error {
buildUrl := fmt.Sprintf("https://buildkite.com/%s/%s/builds", org, pipeline)
l := io.NewPendingCommand(func() tea.Msg {

if len(branch) == 0 {
Expand All @@ -64,17 +63,14 @@ func newBuild(org string, pipeline string, f *factory.Factory, message string, c
if err != nil {
return err
}
buildUrl = fmt.Sprintf("%s/%d", buildUrl, *build.Number)

if web {
fmt.Printf("Opening %s in your browser\n", buildUrl)
err = browser.OpenURL(buildUrl)
if err != nil {
fmt.Println("Error opening browser: ", err)
}
if err = openBuildInBrowser(web, *build.WebURL); err != nil {
return err
}

return io.PendingOutput(fmt.Sprintf("Build created: %s", buildUrl))
return io.PendingOutput(lipgloss.JoinVertical(lipgloss.Top,
lipgloss.NewStyle().Padding(1, 1).Render(fmt.Sprintf("Build created: %s\n", *build.WebURL))))

}, fmt.Sprintf("Starting new build for %s", pipeline))
p := tea.NewProgram(l)
_, err := p.Run()
Expand Down
18 changes: 5 additions & 13 deletions pkg/cmd/build/rebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"github.com/buildkite/cli/v3/internal/io"
"github.com/buildkite/cli/v3/pkg/cmd/factory"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/pkg/browser"
"github.com/spf13/cobra"
)

Expand All @@ -23,7 +21,8 @@ func NewCmdBuildRebuild(f *factory.Factory) *cobra.Command {
Long: heredoc.Doc(`
Runs a new build from the specified build number and pipeline and outputs the URL to the new build.

It accepts {pipeline_slug}, {org_slug}/{pipeline_slug} or a full URL to the pipeline as an argument.
It accepts a build number and a pipeline slug as an argument.
The pipeline can be a {pipeline_slug}, {org_slug}/{pipeline_slug} or a full URL to the pipeline.
`),
RunE: func(cmd *cobra.Command, args []string) error {
buildId := args[0]
Expand All @@ -44,18 +43,11 @@ func rebuild(org string, pipeline string, buildId string, web bool, f *factory.F
return err
}

buildUrl := fmt.Sprintf("https://buildkite.com/%s/%s/builds/%d", org, pipeline, *build.Number)

if web {
fmt.Printf("Opening %s in your browser\n", buildUrl)
err = browser.OpenURL(buildUrl)
if err != nil {
fmt.Println("Error opening browser: ", err)
}
if err = openBuildInBrowser(web, *build.WebURL); err != nil {
return err
}

return io.PendingOutput(lipgloss.JoinVertical(lipgloss.Top,
lipgloss.NewStyle().Padding(1, 1).Render(fmt.Sprintf("Build created: %s\n", buildUrl))))
return io.PendingOutput(renderResult(fmt.Sprintf("Build created: %s\n", *build.WebURL)))

}, fmt.Sprintf("Rerunning build #%s for pipeline %s", buildId, pipeline))

Expand Down