Skip to content

Commit

Permalink
web: stop build button replaces start build button when building (#5514)
Browse files Browse the repository at this point in the history
  • Loading branch information
landism authored Feb 18, 2022
1 parent e311110 commit f5e2ef5
Show file tree
Hide file tree
Showing 26 changed files with 236 additions and 115 deletions.
2 changes: 1 addition & 1 deletion internal/controllers/apis/tiltfile/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func MainTiltfile(filename string, args []string) *v1alpha1.Tiltfile {
FileWatches: []string{fwName},
},
StopOn: &v1alpha1.StopOnSpec{
UIButtons: []string{uibutton.CancelButtonName(name)},
UIButtons: []string{uibutton.StopBuildButtonName(name)},
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ import (
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
)

func CancelButtonName(resourceName string) string {
return fmt.Sprintf("%s-cancel", resourceName)
func StopBuildButtonName(resourceName string) string {
return fmt.Sprintf("%s-stopbuild", resourceName)
}

func CancelButton(resourceName string) *v1alpha1.UIButton {
func StopBuildButton(resourceName string) *v1alpha1.UIButton {
return &v1alpha1.UIButton{
ObjectMeta: metav1.ObjectMeta{
Name: CancelButtonName(resourceName),
Name: StopBuildButtonName(resourceName),
Annotations: map[string]string{
v1alpha1.AnnotationButtonType: v1alpha1.ButtonTypeCancelUpdate,
v1alpha1.AnnotationButtonType: v1alpha1.ButtonTypeStopBuild,
},
},
Spec: v1alpha1.UIButtonSpec{
Location: v1alpha1.UIComponentLocation{
ComponentID: resourceName,
ComponentType: v1alpha1.ComponentTypeResource,
},
Text: "Cancel",
Text: "Stop Build",
IconName: "cancel",
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/core/tiltfile/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func toCancelButtons(tlr *tiltfile.TiltfileLoadResult) apiset.TypedObjectSet {
}

for _, m := range tlr.Manifests {
button := uibutton.CancelButton(m.Name.String())
button := uibutton.StopBuildButton(m.Name.String())
result[button.Name] = button
}
return result
Expand Down
8 changes: 4 additions & 4 deletions internal/controllers/core/tiltfile/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,11 @@ func TestCancel(t *testing.T) {
},
Spec: v1alpha1.TiltfileSpec{
Path: p,
StopOn: &v1alpha1.StopOnSpec{UIButtons: []string{uibutton.CancelButtonName("my-tf")}},
StopOn: &v1alpha1.StopOnSpec{UIButtons: []string{uibutton.StopBuildButtonName("my-tf")}},
},
}

cancelButton := uibutton.CancelButton(tf.Name)
cancelButton := uibutton.StopBuildButton(tf.Name)
err := f.Client.Create(f.Context(), cancelButton)
require.NoError(t, err)

Expand Down Expand Up @@ -426,11 +426,11 @@ func TestCancelClickedBeforeLoad(t *testing.T) {
},
Spec: v1alpha1.TiltfileSpec{
Path: p,
StopOn: &v1alpha1.StopOnSpec{UIButtons: []string{uibutton.CancelButtonName("my-tf")}},
StopOn: &v1alpha1.StopOnSpec{UIButtons: []string{uibutton.StopBuildButtonName("my-tf")}},
},
}

cancelButton := uibutton.CancelButton(tf.Name)
cancelButton := uibutton.StopBuildButton(tf.Name)
cancelButton.Status.LastClickedAt = metav1.NowMicro()
err := f.Client.Create(f.Context(), cancelButton)
require.NoError(t, err)
Expand Down
12 changes: 6 additions & 6 deletions internal/engine/buildcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type BuildController struct {

// CancelFuncs for in-progress builds
mu sync.Mutex
cancelBuilds map[model.ManifestName]context.CancelFunc
stopBuildFns map[model.ManifestName]context.CancelFunc
}

type buildEntry struct {
Expand All @@ -48,7 +48,7 @@ func (e buildEntry) BuildReason() model.BuildReason { return e.buildReason }
func NewBuildController(b buildcontrol.BuildAndDeployer) *BuildController {
return &BuildController{
b: b,
cancelBuilds: make(map[model.ManifestName]context.CancelFunc),
stopBuildFns: make(map[model.ManifestName]context.CancelFunc),
}
}

Expand Down Expand Up @@ -164,7 +164,7 @@ func (c *BuildController) cleanUpCanceledBuilds(st store.RStore) {
}
disabled := ms.DisableState == v1alpha1.DisableStateDisabled
canceled := false
if cancelButton, ok := state.UIButtons[uibutton.CancelButtonName(ms.Name.String())]; ok {
if cancelButton, ok := state.UIButtons[uibutton.StopBuildButtonName(ms.Name.String())]; ok {
lastCancelClick := cancelButton.Status.LastClickedAt
canceled = timecmp.AfterOrEqual(lastCancelClick, ms.CurrentBuild.StartTime)
}
Expand All @@ -181,16 +181,16 @@ func (c *BuildController) buildContext(ctx context.Context, entry buildEntry, st
ctx, cancel := context.WithCancel(ctx)
c.mu.Lock()
defer c.mu.Unlock()
c.cancelBuilds[entry.name] = cancel
c.stopBuildFns[entry.name] = cancel
return ctx
}

func (c *BuildController) cleanupBuildContext(mn model.ManifestName) {
c.mu.Lock()
defer c.mu.Unlock()
if cancel, ok := c.cancelBuilds[mn]; ok {
if cancel, ok := c.stopBuildFns[mn]; ok {
cancel()
delete(c.cancelBuilds, mn)
delete(c.stopBuildFns, mn)
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/engine/buildcontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,7 @@ local_resource('local', 'sleep 10000')
f.waitUntilManifestBuilding("local")

var cancelButton v1alpha1.UIButton
err := f.ctrlClient.Get(f.ctx, types.NamespacedName{Name: uibutton.CancelButtonName("local")}, &cancelButton)
err := f.ctrlClient.Get(f.ctx, types.NamespacedName{Name: uibutton.StopBuildButtonName("local")}, &cancelButton)
require.NoError(t, err)
cancelButton.Status.LastClickedAt = metav1.NowMicro()
err = f.ctrlClient.Status().Update(f.ctx, &cancelButton)
Expand Down Expand Up @@ -1655,7 +1655,7 @@ local_resource('local', 'sleep 10000')
f.waitUntilManifestBuilding("local")

var cancelButton v1alpha1.UIButton
err := f.ctrlClient.Get(f.ctx, types.NamespacedName{Name: uibutton.CancelButtonName("local")}, &cancelButton)
err := f.ctrlClient.Get(f.ctx, types.NamespacedName{Name: uibutton.StopBuildButtonName("local")}, &cancelButton)
require.NoError(t, err)
cancelButton.Status.LastClickedAt = ts
err = f.ctrlClient.Status().Update(f.ctx, &cancelButton)
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/configs/configs_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (cc *ConfigsController) maybeCreateInitialTiltfileCancelButton(ctx context.
if !cancelEnabled {
return nil
}
err := cc.ctrlClient.Create(ctx, uibutton.CancelButton(model.MainTiltfileManifestName.String()))
err := cc.ctrlClient.Create(ctx, uibutton.StopBuildButton(model.MainTiltfileManifestName.String()))
if err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions internal/engine/configs/configs_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestCreateTiltfile(t *testing.T) {
FileWatches: []string{"configs:(Tiltfile)"},
},
StopOn: &v1alpha1.StopOnSpec{
UIButtons: []string{"(Tiltfile)-cancel"},
UIButtons: []string{uibutton.StopBuildButtonName("(Tiltfile)")},
},
})
}
Expand All @@ -64,15 +64,15 @@ func TestCreateTiltfileCancelEnabled(t *testing.T) {
FileWatches: []string{"configs:(Tiltfile)"},
},
StopOn: &v1alpha1.StopOnSpec{
UIButtons: []string{"(Tiltfile)-cancel"},
UIButtons: []string{uibutton.StopBuildButtonName("(Tiltfile)")},
},
}
assert.Equal(t, expectedTfSpec, tf.Spec)

var actualButton v1alpha1.UIButton
name := types.NamespacedName{Name: uibutton.CancelButtonName(model.MainTiltfileManifestName.String())}
name := types.NamespacedName{Name: uibutton.StopBuildButtonName(model.MainTiltfileManifestName.String())}
err := client.Get(ctx, name, &actualButton)
require.NoError(t, err)
expectedButton := uibutton.CancelButton(model.MainTiltfileManifestName.String())
expectedButton := uibutton.StopBuildButton(model.MainTiltfileManifestName.String())
assert.Equal(t, expectedButton.Spec, actualButton.Spec)
}
2 changes: 1 addition & 1 deletion pkg/apis/core/v1alpha1/uibutton_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ type UIComponentLocationResource struct {
const AnnotationButtonType = "tilt.dev/uibutton-type"

const ButtonTypeDisableToggle = "DisableToggle"
const ButtonTypeCancelUpdate = "CancelUpdate"
const ButtonTypeStopBuild = "StopBuild"

var _ resource.Object = &UIButton{}
var _ resourcestrategy.Validater = &UIButton{}
Expand Down
23 changes: 14 additions & 9 deletions web/src/ApiButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,9 @@ describe("ApiButton", () => {
describe("helper functions", () => {
describe("buttonsByComponent", () => {
it("returns an empty object if there are no buttons", () => {
expect(buttonsByComponent(undefined)).toStrictEqual({})
expect(buttonsByComponent(undefined)).toStrictEqual(
new Map<string, ButtonSet>()
)
})

it("returns a map of resources names to button sets", () => {
Expand All @@ -440,14 +442,17 @@ describe("ApiButton", () => {
makeUIButton({ componentID: "" }),
]

const expectedOutput: { [key: string]: ButtonSet } = {
frontend: {
default: [buttons[0], buttons[1]],
toggleDisable: buttons[2],
},
backend: { default: [buttons[3]], toggleDisable: buttons[4] },
["data-warehouse"]: { default: [buttons[5]] },
}
const expectedOutput = new Map<string, ButtonSet>([
[
"frontend",
{
default: [buttons[0], buttons[1]],
toggleDisable: buttons[2],
},
],
["backend", { default: [buttons[3]], toggleDisable: buttons[4] }],
["data-warehouse", { default: [buttons[5]] }],
])

expect(buttonsByComponent(buttons)).toStrictEqual(expectedOutput)
})
Expand Down
Loading

0 comments on commit f5e2ef5

Please sign in to comment.