From da75e14aa0d5c68d6fcf5c7369a5419398f50ebb Mon Sep 17 00:00:00 2001 From: Kelly Merrick Date: Tue, 6 Feb 2024 14:39:33 -0600 Subject: [PATCH] refactor(builds): get actions menu to display and begin restart build --- src/elm/Api/Operations.elm | 23 ++++++++++++++++++ src/elm/Components/Build.elm | 15 +++++++++--- src/elm/Components/Builds.elm | 5 ++-- src/elm/Effect.elm | 12 +++++++++- src/elm/Layouts/Default/Build.elm | 3 ++- src/elm/Pages/Org_/Builds.elm | 19 ++++++++------- src/elm/Pages/Org_/Repo_.elm | 39 ++++++++++++++++++++++--------- src/elm/Shared.elm | 37 +++++++++++++++++++++++++++++ src/elm/Shared/Msg.elm | 3 +++ 9 files changed, 129 insertions(+), 27 deletions(-) diff --git a/src/elm/Api/Operations.elm b/src/elm/Api/Operations.elm index 32822b3fd..6bad67b8b 100644 --- a/src/elm/Api/Operations.elm +++ b/src/elm/Api/Operations.elm @@ -46,6 +46,7 @@ module Api.Operations exposing , logout , redeliverHook , repairRepo + , restartBuild , updateCurrentUser , updateOrgSecret , updateRepo @@ -284,6 +285,28 @@ getRepoBuilds baseUrl session options = |> withAuth session +restartBuild : + String + -> Session + -> + { a + | org : String + , repo : String + , buildNumber : String + } + -> Request Vela.Build +restartBuild baseUrl session options = + post baseUrl + (Api.Endpoint.Build + options.org + options.repo + options.buildNumber + ) + Http.emptyBody + Vela.decodeBuild + |> withAuth session + + {-| getRepoDeployments : retrieves deployments for a repo -} getRepoDeployments : diff --git a/src/elm/Components/Build.elm b/src/elm/Components/Build.elm index ed4c1e43d..7b42942fa 100644 --- a/src/elm/Components/Build.elm +++ b/src/elm/Components/Build.elm @@ -26,7 +26,8 @@ import Vela type alias Props msg = { build : WebData Vela.Build , showFullTimestamps : Bool - , actionsMenu : Maybe (Html msg) + , actionsMenu : Html msg + , showActionsMenuBool : Bool } @@ -180,6 +181,7 @@ view shared props = , infoRow = [ viewError build ] , buildAnimation = [ buildAnimation build.status build.number ] , hoverTitle = title hoverTime + , viewActionsMenu = props.actionsMenu } _ -> @@ -194,6 +196,7 @@ view shared props = , infoRow = [] , buildAnimation = [ buildAnimation Vela.Pending 1 ] , hoverTitle = Html.Attributes.class "" + , viewActionsMenu = Html.div [] [] } @@ -208,6 +211,7 @@ viewBuildPreview : , infoRow : List (Html msg) , buildAnimation : List (Html msg) , hoverTitle : Html.Attribute msg + , viewActionsMenu : Html msg } -> Html msg viewBuildPreview props = @@ -240,6 +244,7 @@ viewBuildPreview props = , span [ class "delimiter" ] [ text " /" ] , div [ class "duration" ] props.duration ] + , props.viewActionsMenu ] ] , div [ class "row" ] props.infoRow @@ -253,13 +258,18 @@ viewBuildPreview props = viewActionsMenu : { msgs : { showHideActionsMenus : Maybe Int -> Maybe Bool -> msg + , restartBuild : { org : String, repo : String, buildNumber : String } -> msg } , build : Vela.Build , showActionsMenus : List Int + , showActionsMenuBool : Bool } -> Html msg viewActionsMenu props = let + isMenuOpen = + List.member props.build.id props.showActionsMenus + buildMenuBaseClassList = classList [ ( "details", True ) @@ -299,8 +309,7 @@ viewActionsMenu props = [ a [ href "#" , class "menu-item" - - -- , Util.onClickPreventDefault <| props.msgs.restartBuild org repo <| String.fromInt build.number + , Util.onClickPreventDefault <| props.msgs.restartBuild { org = "", repo = "", buildNumber = String.fromInt props.build.number } , Util.testAttribute "restart-build" ] [ text "Restart Build" diff --git a/src/elm/Components/Builds.elm b/src/elm/Components/Builds.elm index f1470570d..4eeb51825 100644 --- a/src/elm/Components/Builds.elm +++ b/src/elm/Components/Builds.elm @@ -48,7 +48,7 @@ import Vela type alias Msgs msg = { approveBuild : Vela.Org -> Vela.Repo -> Vela.BuildNumber -> msg - , restartBuild : Vela.Org -> Vela.Repo -> Vela.BuildNumber -> msg + , restartBuild : { org : Vela.Org, repo : Vela.Repo, buildNumber : Vela.BuildNumber } -> msg , cancelBuild : Vela.Org -> Vela.Repo -> Vela.BuildNumber -> msg , showHideActionsMenus : Maybe Int -> Maybe Bool -> msg } @@ -59,7 +59,7 @@ type alias Props msg = , builds : WebData (List Vela.Build) , maybeEvent : Maybe String , showFullTimestamps : Bool - , viewActionsMenu : Vela.Build -> Maybe (Html msg) + , viewActionsMenu : Vela.Build -> Html msg } @@ -123,6 +123,7 @@ view shared props = { build = RemoteData.succeed build , showFullTimestamps = props.showFullTimestamps , actionsMenu = props.viewActionsMenu build + , showActionsMenuBool = True } ) builds diff --git a/src/elm/Effect.elm b/src/elm/Effect.elm index f29b91bb3..6be887df1 100644 --- a/src/elm/Effect.elm +++ b/src/elm/Effect.elm @@ -9,7 +9,7 @@ module Effect exposing , sendCmd, sendMsg , pushRoute, replaceRoute, loadExternalUrl , map, toCmd - , addAlertError, addAlertSuccess, addDeployment, addOrgSecret, addRepoSchedule, addRepoSecret, addSharedSecret, alertsUpdate, chownRepo, clearRedirect, deleteOrgSecret, deleteRepoSchedule, deleteRepoSecret, deleteSharedSecret, disableRepo, downloadFile, enableRepo, expandPipelineConfig, finishAuthentication, focusOn, getBuild, getBuildGraph, getBuildServiceLog, getBuildServices, getBuildStepLog, getBuildSteps, getCurrentUser, getOrgBuilds, getOrgRepos, getOrgSecret, getOrgSecrets, getPipelineConfig, getPipelineTemplates, getRepo, getRepoBuilds, getRepoBuildsShared, getRepoDeployments, getRepoHooks, getRepoHooksShared, getRepoSchedule, getRepoSchedules, getRepoSecret, getRepoSecrets, getSharedSecret, getSharedSecrets, handleHttpError, logout, pushPath, redeliverHook, repairRepo, replacePath, replaceRouteRemoveTabHistorySkipDomFocus, setRedirect, setTheme, updateFavicon, updateFavorites, updateOrgSecret, updateRepo, updateRepoSchedule, updateRepoSecret, updateSharedSecret + , addAlertError, addAlertSuccess, addDeployment, addOrgSecret, addRepoSecret, alertsUpdate, clearRedirect, downloadFile, enableRepo, expandPipelineConfig, finishAuthentication, focusOn, getBuild, getBuildServiceLog, getBuildServices, getBuildStepLog, getBuildSteps, getCurrentUser, getOrgBuilds, getOrgRepos, getOrgSecret, getOrgSecrets, getPipelineConfig, getPipelineTemplates, getRepo, getRepoBuilds, getRepoBuildsShared, getRepoDeployments, getRepoHooks, getRepoHooksShared, getRepoSchedules, getRepoSecret, getRepoSecrets, getSharedSecrets, handleHttpError, logout, pushPath, redeliverHook, restartBuild, setRedirect, setTheme, updateFavorites, updateOrgSecret, updateRepoSecret ) {-| @@ -444,6 +444,16 @@ getRepoDeployments options = |> sendCmd +restartBuild : + { org : String + , repo : String + , buildNumber : String + } + -> Effect msg +restartBuild options = + SendSharedMsg <| Shared.Msg.RestartBuild options + + addDeployment : { baseUrl : String , session : Auth.Session.Session diff --git a/src/elm/Layouts/Default/Build.elm b/src/elm/Layouts/Default/Build.elm index 10b810121..86726de43 100644 --- a/src/elm/Layouts/Default/Build.elm +++ b/src/elm/Layouts/Default/Build.elm @@ -223,7 +223,8 @@ view props shared route { toContentMsg, model, content } = , Components.Build.view shared { build = model.build , showFullTimestamps = False - , actionsMenu = Nothing + , actionsMenu = Html.div [] [] + , showActionsMenuBool = True } , Components.Tabs.viewBuildTabs shared { org = props.org diff --git a/src/elm/Pages/Org_/Builds.elm b/src/elm/Pages/Org_/Builds.elm index 95e3e86bb..017a66e16 100644 --- a/src/elm/Pages/Org_/Builds.elm +++ b/src/elm/Pages/Org_/Builds.elm @@ -105,7 +105,7 @@ type Msg | GetOrgBuildsResponse (Result (Http.Detailed.Error String) ( Http.Metadata, List Vela.Build )) | GotoPage Int | ApproveBuild Vela.Org Vela.Repo Vela.BuildNumber - | RestartBuild Vela.Org Vela.Repo Vela.BuildNumber + | RestartBuild { org : Vela.Org, repo : Vela.Repo, buildNumber : Vela.BuildNumber } | CancelBuild Vela.Org Vela.Repo Vela.BuildNumber | ShowHideActionsMenus (Maybe Int) (Maybe Bool) | FilterByEvent (Maybe String) @@ -171,7 +171,7 @@ update shared route msg model = ApproveBuild _ _ _ -> ( model, Effect.none ) - RestartBuild _ _ _ -> + RestartBuild _ -> ( model, Effect.none ) CancelBuild _ _ _ -> @@ -296,14 +296,15 @@ view shared route model = , showFullTimestamps = model.showFullTimestamps , viewActionsMenu = \b -> - Just <| - Components.Build.viewActionsMenu - { msgs = - { showHideActionsMenus = ShowHideActionsMenus - } - , build = b - , showActionsMenus = model.showActionsMenus + Components.Build.viewActionsMenu + { msgs = + { showHideActionsMenus = ShowHideActionsMenus + , restartBuild = RestartBuild } + , build = b + , showActionsMenus = model.showActionsMenus + , showActionsMenuBool = True + } } , Components.Pager.viewIfNeeded model.pager Components.Pager.defaultLabels GotoPage model.builds ] diff --git a/src/elm/Pages/Org_/Repo_.elm b/src/elm/Pages/Org_/Repo_.elm index eca9fd309..2c35a627e 100644 --- a/src/elm/Pages/Org_/Repo_.elm +++ b/src/elm/Pages/Org_/Repo_.elm @@ -108,7 +108,7 @@ type Msg | GetRepoBuildsResponse (Result (Http.Detailed.Error String) ( Http.Metadata, List Vela.Build )) | GotoPage Int | ApproveBuild Vela.Org Vela.Repo Vela.BuildNumber - | RestartBuild Vela.Org Vela.Repo Vela.BuildNumber + | RestartBuild { org : Vela.Org, repo : Vela.Repo, buildNumber : Vela.BuildNumber } | CancelBuild Vela.Org Vela.Repo Vela.BuildNumber | ShowHideActionsMenus (Maybe Int) (Maybe Bool) | FilterByEvent (Maybe String) @@ -188,13 +188,29 @@ update shared route msg model = in ( model, Effect.none ) - RestartBuild _ _ _ -> + RestartBuild options -> let _ = - Debug.log "restart build clicked" "" + Debug.log "restart build clicked" + "" + + _ = + Debug.log "options" options in - ( model, Effect.none ) + ( model + , Effect.restartBuild + { org = options.org + , repo = options.repo + , buildNumber = options.buildNumber + } + ) + -- RestartBuild _ -> + -- let + -- _ = + -- Debug.log "restart build clicked" "" + -- in + -- ( model, Effect.none ) CancelBuild _ _ _ -> let _ = @@ -323,14 +339,15 @@ view shared route model = , showFullTimestamps = model.showFullTimestamps , viewActionsMenu = \b -> - Just <| - Components.Build.viewActionsMenu - { msgs = - { showHideActionsMenus = ShowHideActionsMenus - } - , build = b - , showActionsMenus = model.showActionsMenus + Components.Build.viewActionsMenu + { msgs = + { showHideActionsMenus = ShowHideActionsMenus + , restartBuild = RestartBuild } + , build = b + , showActionsMenus = model.showActionsMenus + , showActionsMenuBool = True + } } , Components.Pager.viewIfNeeded model.pager Components.Pager.defaultLabels GotoPage model.builds ] diff --git a/src/elm/Shared.elm b/src/elm/Shared.elm index 54c9d8326..54c9022d5 100644 --- a/src/elm/Shared.elm +++ b/src/elm/Shared.elm @@ -516,6 +516,43 @@ update route msg model = , Effect.handleHttpError { httpError = error } ) + -- BUILD + Shared.Msg.RestartBuild options -> + let + _ = + Debug.log "restart a build" options + in + ( model + , Api.try + (Shared.Msg.RestartBuildResponse options) + (Api.Operations.restartBuild model.velaAPI model.session options) + |> Effect.sendCmd + ) + + Shared.Msg.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 } + ) + -- BUILDS Shared.Msg.GetRepoHooks options -> ( model diff --git a/src/elm/Shared/Msg.elm b/src/elm/Shared/Msg.elm index 06e7f6a96..249bffe6d 100644 --- a/src/elm/Shared/Msg.elm +++ b/src/elm/Shared/Msg.elm @@ -49,6 +49,9 @@ type Msg -- BUILDS | GetRepoBuilds { org : String, repo : String, pageNumber : Maybe Int, perPage : Maybe Int, maybeEvent : Maybe String } | GetRepoBuildsResponse (Result (Http.Detailed.Error String) ( Http.Metadata, List Vela.Build )) + -- BUILD + | RestartBuild { org : String, repo : String, buildNumber : String } + | RestartBuildResponse { org : String, repo : String, buildNumber : String } (Result (Http.Detailed.Error String) ( Http.Metadata, Vela.Build )) -- HOOKS | GetRepoHooks { org : String, repo : String, pageNumber : Maybe Int, perPage : Maybe Int, maybeEvent : Maybe String } | GetRepoHooksResponse (Result (Http.Detailed.Error String) ( Http.Metadata, List Vela.Hook ))