Skip to content

Commit

Permalink
refactor: moved build buttons from build page to build layout
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Feb 14, 2024
1 parent f06a289 commit b6b803e
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 111 deletions.
38 changes: 12 additions & 26 deletions src/elm/Components/Build.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
2 changes: 1 addition & 1 deletion src/elm/Effect.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
185 changes: 184 additions & 1 deletion src/elm/Layouts/Default/Build.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 "<move build buttons here>" ] ++ props.navButtons
{ buttons =
[ viewRestartButton
, viewCancelButton
, viewApproveButton
]
++ props.navButtons
, crumbs = Components.Crumbs.view route.path props.crumbs
}
, main_ [ class "content-wrap" ]
Expand Down
84 changes: 1 addition & 83 deletions src/elm/Pages/Org_/Repo_/Build_.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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 ))
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b6b803e

Please sign in to comment.