From b6b803e263adf8bb47d5f269afc99350dc93ca62 Mon Sep 17 00:00:00 2001 From: davidvader Date: Wed, 14 Feb 2024 15:00:16 -0600 Subject: [PATCH] refactor: moved build buttons from build page to build layout --- src/elm/Components/Build.elm | 38 ++---- src/elm/Effect.elm | 2 +- src/elm/Layouts/Default/Build.elm | 185 +++++++++++++++++++++++++++- src/elm/Pages/Org_/Repo_/Build_.elm | 84 +------------ 4 files changed, 198 insertions(+), 111 deletions(-) diff --git a/src/elm/Components/Build.elm b/src/elm/Components/Build.elm index ea7e5c285..15cdaab51 100644 --- a/src/elm/Components/Build.elm +++ b/src/elm/Components/Build.elm @@ -593,29 +593,15 @@ viewRestartButton org repo buildNumber restartBuild = {-| viewApproveButton: takes org repo and build number and renders button to approve a build run -} -viewApproveButton : Vela.Org -> Vela.Repo -> WebData Vela.Build -> (Vela.Org -> Vela.Repo -> Vela.BuildNumber -> msg) -> Html msg -viewApproveButton org repo build approveBuild = - case build of - RemoteData.Success b -> - let - approveButton = - button - [ classList - [ ( "button", True ) - , ( "-outline", True ) - ] - , onClick <| approveBuild org repo <| String.fromInt b.number - , Util.testAttribute "approve-build" - ] - [ text "Approve Build" - ] - in - case b.status of - Vela.PendingApproval -> - approveButton - - _ -> - text "" - - _ -> - text "" +viewApproveButton : Vela.Org -> Vela.Repo -> Vela.BuildNumber -> ({ org : Vela.Org, repo : Vela.Repo, buildNumber : Vela.BuildNumber } -> msg) -> Html msg +viewApproveButton org repo buildNumber approveBuild = + button + [ classList + [ ( "button", True ) + , ( "-outline", True ) + ] + , onClick <| approveBuild { org = org, repo = repo, buildNumber = buildNumber } + , Util.testAttribute "approve-build" + ] + [ text "Approve Build" + ] diff --git a/src/elm/Effect.elm b/src/elm/Effect.elm index 262870bb0..f8307ed9d 100644 --- a/src/elm/Effect.elm +++ b/src/elm/Effect.elm @@ -488,7 +488,7 @@ approveBuild : approveBuild options = Api.try options.onResponse - (Api.Operations.restartBuild options.baseUrl options.session options) + (Api.Operations.approveBuild options.baseUrl options.session options) |> sendCmd diff --git a/src/elm/Layouts/Default/Build.elm b/src/elm/Layouts/Default/Build.elm index 111342cd8..3e7fab49f 100644 --- a/src/elm/Layouts/Default/Build.elm +++ b/src/elm/Layouts/Default/Build.elm @@ -117,6 +117,12 @@ type Msg OnUrlChanged { from : Route (), to : Route () } -- BUILD | GetBuildResponse (Result (Http.Detailed.Error String) ( Http.Metadata, Vela.Build )) + | RestartBuild { org : Vela.Org, repo : Vela.Repo, buildNumber : Vela.BuildNumber } + | RestartBuildResponse { org : Vela.Org, repo : Vela.Repo, buildNumber : Vela.BuildNumber } (Result (Http.Detailed.Error String) ( Http.Metadata, Vela.Build )) + | CancelBuild { org : Vela.Org, repo : Vela.Repo, buildNumber : Vela.BuildNumber } + | CancelBuildResponse { org : Vela.Org, repo : Vela.Repo, buildNumber : Vela.BuildNumber } (Result (Http.Detailed.Error String) ( Http.Metadata, Vela.Build )) + | ApproveBuild { org : Vela.Org, repo : Vela.Repo, buildNumber : Vela.BuildNumber } + | ApproveBuildResponse { org : Vela.Org, repo : Vela.Repo, buildNumber : Vela.BuildNumber } (Result (Http.Detailed.Error String) ( Http.Metadata, Vela.Build )) -- REFRESH | Tick { time : Time.Posix, interval : Interval.Interval } @@ -168,6 +174,127 @@ update props shared route msg model = ] ) + RestartBuild options -> + ( model + , Effect.restartBuild + { baseUrl = shared.velaAPIBaseURL + , session = shared.session + , onResponse = RestartBuildResponse options + , org = options.org + , repo = options.repo + , buildNumber = options.buildNumber + } + ) + + RestartBuildResponse options response -> + case response of + Ok ( _, build ) -> + let + restartedBuild = + "Build " ++ String.join "/" [ options.org, options.repo, options.buildNumber ] + + newBuildNumber = + String.fromInt <| build.number + + newBuild = + String.join "/" [ "", options.org, options.repo, newBuildNumber ] + + -- todo: create new build link, add to toastie, refresh builds + in + ( model + , Effect.batch + [ Effect.getRepoBuildsShared + { pageNumber = Nothing + , perPage = Nothing + , maybeEvent = Nothing + , org = props.org + , repo = props.repo + } + , Effect.addAlertSuccess { content = restartedBuild ++ " restarted.", addToastIfUnique = True } + ] + ) + + Err error -> + ( model + , Effect.handleHttpError { httpError = error } + ) + + CancelBuild options -> + ( model + , Effect.cancelBuild + { baseUrl = shared.velaAPIBaseURL + , session = shared.session + , onResponse = CancelBuildResponse options + , org = options.org + , repo = options.repo + , buildNumber = options.buildNumber + } + ) + + CancelBuildResponse options response -> + case response of + Ok ( _, build ) -> + let + canceledBuild = + "Build " ++ String.join "/" [ options.org, options.repo, options.buildNumber ] + in + ( model + , Effect.batch + [ Effect.getBuild + { baseUrl = shared.velaAPIBaseURL + , session = shared.session + , onResponse = GetBuildResponse + , org = props.org + , repo = props.repo + , buildNumber = props.buildNumber + } + , Effect.addAlertSuccess { content = canceledBuild ++ " canceled.", addToastIfUnique = True } + ] + ) + + Err error -> + ( model + , Effect.handleHttpError { httpError = error } + ) + + ApproveBuild options -> + ( model + , Effect.approveBuild + { baseUrl = shared.velaAPIBaseURL + , session = shared.session + , onResponse = ApproveBuildResponse options + , org = options.org + , repo = options.repo + , buildNumber = options.buildNumber + } + ) + + ApproveBuildResponse options response -> + case response of + Ok ( _, build ) -> + let + approvedBuild = + "Build " ++ String.join "/" [ options.org, options.repo, options.buildNumber ] + in + ( model + , Effect.batch + [ Effect.getBuild + { baseUrl = shared.velaAPIBaseURL + , session = shared.session + , onResponse = GetBuildResponse + , org = props.org + , repo = props.repo + , buildNumber = props.buildNumber + } + , Effect.addAlertSuccess { content = approvedBuild ++ " approved.", addToastIfUnique = True } + ] + ) + + Err error -> + ( model + , Effect.handleHttpError { httpError = error } + ) + -- REFRESH Tick options -> ( model @@ -202,11 +329,67 @@ subscriptions model = view : Props contentMsg -> Shared.Model -> Route () -> { toContentMsg : Msg -> contentMsg, content : View contentMsg, model : Model } -> View contentMsg view props shared route { toContentMsg, model, content } = + let + viewRestartButton = + case model.build of + RemoteData.Success build -> + case build.status of + Vela.PendingApproval -> + text "" + + _ -> + Components.Build.viewRestartButton props.org props.repo props.buildNumber RestartBuild + |> Html.map toContentMsg + + _ -> + text "" + + viewCancelButton = + case model.build of + RemoteData.Success build -> + case build.status of + Vela.Pending -> + Components.Build.viewCancelButton props.org props.repo props.buildNumber CancelBuild + |> Html.map toContentMsg + + Vela.PendingApproval -> + Components.Build.viewCancelButton props.org props.repo props.buildNumber CancelBuild + |> Html.map toContentMsg + + Vela.Running -> + Components.Build.viewCancelButton props.org props.repo props.buildNumber CancelBuild + |> Html.map toContentMsg + + _ -> + text "" + + _ -> + text "" + + viewApproveButton = + case model.build of + RemoteData.Success build -> + case build.status of + Vela.PendingApproval -> + Components.Build.viewApproveButton props.org props.repo props.buildNumber ApproveBuild + |> Html.map toContentMsg + + _ -> + text "" + + _ -> + text "" + in { title = props.org ++ "/" ++ props.repo ++ " #" ++ props.buildNumber ++ " " ++ content.title , body = [ Components.Nav.view shared route - { buttons = [ text "" ] ++ props.navButtons + { buttons = + [ viewRestartButton + , viewCancelButton + , viewApproveButton + ] + ++ props.navButtons , crumbs = Components.Crumbs.view route.path props.crumbs } , main_ [ class "content-wrap" ] diff --git a/src/elm/Pages/Org_/Repo_/Build_.elm b/src/elm/Pages/Org_/Repo_/Build_.elm index 609516994..7fa809ccf 100644 --- a/src/elm/Pages/Org_/Repo_/Build_.elm +++ b/src/elm/Pages/Org_/Repo_/Build_.elm @@ -7,7 +7,6 @@ module Pages.Org_.Repo_.Build_ exposing (..) import Auth import Browser.Dom exposing (focus) -import Components.Build import Components.Loading import Components.Logs import Components.Svgs @@ -57,10 +56,7 @@ page user shared route = toLayout : Auth.User -> Route { org : String, repo : String, buildNumber : String } -> Model -> Layouts.Layout Msg toLayout user route model = Layouts.Default_Build - { navButtons = - [ Components.Build.viewRestartButton route.params.org route.params.repo route.params.buildNumber RestartBuild - , Components.Build.viewCancelButton route.params.org route.params.repo route.params.buildNumber CancelBuild - ] + { navButtons = [] , utilButtons = [] , helpCommands = [] , crumbs = @@ -136,13 +132,6 @@ type Msg OnHashChanged { from : Maybe String, to : Maybe String } | PushUrlHash { hash : String } | FocusOn { target : String } - -- BUILD - | RestartBuild { org : Vela.Org, repo : Vela.Repo, buildNumber : Vela.BuildNumber } - | RestartBuildResponse { org : Vela.Org, repo : Vela.Repo, buildNumber : Vela.BuildNumber } (Result (Http.Detailed.Error String) ( Http.Metadata, Vela.Build )) - | CancelBuild { org : Vela.Org, repo : Vela.Repo, buildNumber : Vela.BuildNumber } - | CancelBuildResponse { org : Vela.Org, repo : Vela.Repo, buildNumber : Vela.BuildNumber } (Result (Http.Detailed.Error String) ( Http.Metadata, Vela.Build )) - | ApproveBuild { org : Vela.Org, repo : Vela.Repo, buildNumber : Vela.BuildNumber } - | ApproveBuildResponse { org : Vela.Org, repo : Vela.Repo, buildNumber : Vela.BuildNumber } (Result (Http.Detailed.Error String) ( Http.Metadata, Vela.Build )) -- STEPS | GetBuildStepsResponse { applyDomFocus : Bool } (Result (Http.Detailed.Error String) ( Http.Metadata, List Vela.Step )) | GetBuildStepsRefreshResponse (Result (Http.Detailed.Error String) ( Http.Metadata, List Vela.Step )) @@ -201,77 +190,6 @@ update shared route msg model = , Effect.focusOn options ) - -- BUILD - RestartBuild options -> - ( model - , Effect.restartBuild - { baseUrl = shared.velaAPIBaseURL - , session = shared.session - , onResponse = RestartBuildResponse options - , org = options.org - , repo = options.repo - , buildNumber = options.buildNumber - } - ) - - RestartBuildResponse options response -> - case response of - Ok ( _, build ) -> - let - restartedBuild = - "Build " ++ String.join "/" [ options.org, options.repo, options.buildNumber ] - - newBuildNumber = - String.fromInt <| build.number - - newBuild = - String.join "/" [ "", options.org, options.repo, newBuildNumber ] - - -- todo: create new build link, add to toastie, refresh builds - in - ( model - , Effect.addAlertSuccess { content = restartedBuild ++ " restarted.", addToastIfUnique = True } - ) - - Err error -> - ( model - , Effect.handleHttpError { httpError = error } - ) - - CancelBuild options -> - ( model - , Effect.cancelBuild - { baseUrl = shared.velaAPIBaseURL - , session = shared.session - , onResponse = CancelBuildResponse options - , org = options.org - , repo = options.repo - , buildNumber = options.buildNumber - } - ) - - CancelBuildResponse options response -> - case response of - Ok ( _, build ) -> - let - canceledBuild = - "Build " ++ String.join "/" [ options.org, options.repo, options.buildNumber ] - in - ( model - , Effect.addAlertSuccess { content = canceledBuild ++ " canceled.", addToastIfUnique = True } - ) - - Err error -> - ( model - , Effect.handleHttpError { httpError = error } - ) - - ApproveBuild options -> - ( model, Effect.none ) - - ApproveBuildResponse options response -> - ( model, Effect.none ) - -- STEPS GetBuildStepsResponse options response -> case response of