diff --git a/src/elm/Components/Form.elm b/src/elm/Components/Form.elm index 66fba903e..dd47eaeb9 100644 --- a/src/elm/Components/Form.elm +++ b/src/elm/Components/Form.elm @@ -5,11 +5,12 @@ SPDX-License-Identifier: Apache-2.0 module Components.Form exposing (..) -import Html exposing (Html, button, div, input, label, section, span, strong, text, textarea) +import Html exposing (Html, br, button, div, em, h2, h3, input, label, p, section, span, strong, text, textarea) import Html.Attributes exposing (checked, class, classList, disabled, for, id, placeholder, rows, type_, value, wrap) import Html.Events exposing (onCheck, onClick, onInput) import Maybe.Extra import Utils.Helpers as Util +import Vela @@ -158,3 +159,102 @@ viewButton { msg, text_, classList_, disabled_ } = , classList classList_ ] [ text text_ ] + + +{-| viewAllowEvents : takes model and repo and renders the settings category for updating repo webhook events +-} +viewAllowEvents : + ({ a | allow_events : Maybe Vela.AllowEvents } -> String -> Bool -> msg) + -> Maybe Vela.AllowEvents + -> { a | allow_events : Maybe Vela.AllowEvents } + -> List (Html msg) +viewAllowEvents msg allowEvents repo = + case allowEvents of + Just events -> + [ div [ class "form-controls", class "-two-col" ] + [ viewCheckbox + { title = "Push" + , subtitle = Nothing + , field = "allow_push_branch" + , state = events.push.branch + , msg = msg repo "allow_push_branch" + , disabled_ = False + } + , viewCheckbox + { title = "Tag" + , subtitle = Nothing + , field = "allow_push_tag" + , state = events.push.tag + , msg = msg repo "allow_push_tag" + , disabled_ = False + } + ] + , h3 [ class "settings-subtitle" ] [ text "Pull Request" ] + , div [ class "form-controls", class "-two-col" ] + [ viewCheckbox + { title = "Opened" + , subtitle = Nothing + , field = "allow_pull_opened" + , state = events.pull.opened + , msg = msg repo "allow_pull_opened" + , disabled_ = False + } + , viewCheckbox + { title = "Synchronize" + , subtitle = Nothing + , field = "allow_pull_synchronize" + , state = events.pull.synchronize + , msg = msg repo "allow_pull_synchronize" + , disabled_ = False + } + , viewCheckbox + { title = "Edited" + , subtitle = Nothing + , field = "allow_pull_edited" + , state = events.pull.edited + , msg = msg repo "allow_pull_edited" + , disabled_ = False + } + , viewCheckbox + { title = "Reopened" + , subtitle = Nothing + , field = "allow_pull_reopened" + , state = events.pull.reopened + , msg = msg repo "allow_pull_reopened" + , disabled_ = False + } + ] + , h3 [ class "settings-subtitle" ] [ text "Deployments" ] + , div [ class "form-controls", class "-two-col" ] + [ viewCheckbox + { title = "Created" + , subtitle = Nothing + , field = "allow_deploy_created" + , state = events.deploy.created + , msg = msg repo "allow_deploy_created" + , disabled_ = False + } + ] + , h3 [ class "settings-subtitle" ] [ text "Comment" ] + , div [ class "form-controls", class "-two-col" ] + [ viewCheckbox + { title = "Created" + , subtitle = Nothing + , field = "allow_comment_created" + , state = events.comment.created + , msg = msg repo "allow_comment_created" + , disabled_ = False + } + , viewCheckbox + { title = "Edited" + , subtitle = Nothing + , field = "allow_comment_edited" + , state = events.comment.edited + , msg = msg repo "allow_comment_edited" + , disabled_ = False + } + ] + ] + + Nothing -> + [] diff --git a/src/elm/Pages/Org_/Repo_/Settings.elm b/src/elm/Pages/Org_/Repo_/Settings.elm index 603ac611e..bf8c55645 100644 --- a/src/elm/Pages/Org_/Repo_/Settings.elm +++ b/src/elm/Pages/Org_/Repo_/Settings.elm @@ -389,8 +389,10 @@ update shared route msg model = AllowEventsUpdate repo event val -> let payload = - defaultRepoPayload - |> Vela.setAllowEvents repo event val + { defaultRepoPayload + | allow_events = + (Vela.setAllowEvents repo event val).allow_events + } body = Http.jsonBody <| Vela.encodeRepoPayload payload @@ -618,7 +620,16 @@ view shared route model = [ case model.repo of RemoteData.Success repo -> div [ class "repo-settings", Util.testAttribute "repo-settings" ] - [ viewEvents repo + [ section [ class "settings", Util.testAttribute "repo-settings-events" ] + ([ h2 [ class "settings-title" ] [ text "Webhook Events" ] + , p [ class "settings-description" ] + [ text "Control which events on Git will trigger Vela pipelines." + , br [] [] + , em [] [ text "Active repositories must have at least one event enabled." ] + ] + ] + ++ Components.Form.viewAllowEvents AllowEventsUpdate repo.allow_events repo + ) , viewAccess repo AccessUpdate , viewForkPolicy repo ForkPolicyUpdate , viewLimit shared repo model.inLimit BuildLimitUpdate BuildLimitOnInput @@ -635,10 +646,10 @@ view shared route model = } -{-| viewEvents : takes model and repo and renders the settings category for updating repo webhook events +{-| viewAllowEvents : takes repo and renders the settings category for updating repo webhook events -} -viewEvents : Vela.Repository -> Html Msg -viewEvents repo = +viewAllowEvents : Vela.Repository -> Html Msg +viewAllowEvents repo = case repo.allow_events of Just allowEvents -> section [ class "settings", Util.testAttribute "repo-settings-events" ] diff --git a/src/elm/Pages/Secrets/Engine_/Repo/Org_/Repo_/Add.elm b/src/elm/Pages/Secrets/Engine_/Repo/Org_/Repo_/Add.elm index 5c3667aa7..d490a6397 100644 --- a/src/elm/Pages/Secrets/Engine_/Repo/Org_/Repo_/Add.elm +++ b/src/elm/Pages/Secrets/Engine_/Repo/Org_/Repo_/Add.elm @@ -64,7 +64,7 @@ toLayout user route model = type alias Model = { name : String , value : String - , events : List String + , allow_events : Maybe Vela.AllowEvents , images : List String , image : String , allowCommand : Bool @@ -75,7 +75,7 @@ init : Shared.Model -> () -> ( Model, Effect Msg ) init shared () = ( { name = "" , value = "" - , events = [ "push" ] + , allow_events = Just Vela.defaultAllowEventsPayload , images = [] , image = "" , allowCommand = True @@ -94,7 +94,7 @@ type Msg | NameOnInput String | ValueOnInput String | ImageOnInput String - | EventOnCheck String Bool + | AllowEventsUpdate { allow_events : Maybe Vela.AllowEvents } String Bool | AddImage String | RemoveImage String | AllowCommandsOnClick String @@ -135,19 +135,9 @@ update shared route msg model = , Effect.none ) - EventOnCheck event val -> - let - updatedEvents = - if val then - model.events - |> List.append [ event ] - |> List.Extra.unique - - else - model.events - |> List.filter ((/=) event) - in - ( { model | events = updatedEvents } + + AllowEventsUpdate allowedEvents event val -> + ( (Vela.setAllowEvents model event val) , Effect.none ) @@ -187,9 +177,10 @@ update shared route msg model = , team = Nothing , name = Util.stringToMaybe model.name , value = Util.stringToMaybe model.value - , events = Just model.events + , events = Just [] , images = Just model.images , allowCommand = Just model.allowCommand + , allow_events = model.allow_events } body = @@ -253,11 +244,17 @@ view shared route model = , msg = ValueOnInput , disabled_ = False } - , Components.SecretForm.viewEventsSelect shared - { msg = EventOnCheck - , events = model.events - , disabled_ = False - } + , Html.span [] <| + Components.Form.viewAllowEvents + AllowEventsUpdate + model.allow_events + { allow_events = model.allow_events } + + -- , Components.SecretForm.viewEventsSelect shared + -- { msg = EventOnCheck + -- , events = model.events + -- , disabled_ = False + -- } , Components.SecretForm.viewImagesInput { onInput_ = ImageOnInput , addImage = AddImage diff --git a/src/elm/Pages/Secrets/Engine_/Repo/Org_/Repo_/Edit_.elm b/src/elm/Pages/Secrets/Engine_/Repo/Org_/Repo_/Edit_.elm index 314af284c..5d84f69e7 100644 --- a/src/elm/Pages/Secrets/Engine_/Repo/Org_/Repo_/Edit_.elm +++ b/src/elm/Pages/Secrets/Engine_/Repo/Org_/Repo_/Edit_.elm @@ -67,7 +67,7 @@ type alias Model = { secret : WebData Vela.Secret , name : String , value : String - , events : List String + , allow_events : Maybe Vela.AllowEvents , images : List String , image : String , allowCommand : Bool @@ -80,7 +80,7 @@ init shared route () = ( { secret = RemoteData.Loading , name = "" , value = "" - , events = [ "push" ] + , allow_events = Just Vela.defaultAllowEventsPayload , images = [] , image = "" , allowCommand = True @@ -110,7 +110,7 @@ type Msg | DeleteSecretResponse (Result (Http.Detailed.Error String) ( Http.Metadata, String )) | ValueOnInput String | ImageOnInput String - | EventOnCheck String Bool + -- | EventOnCheck String Bool | AddImage String | RemoveImage String | AllowCommandsOnClick String @@ -133,9 +133,11 @@ update shared route msg model = ( { model | secret = RemoteData.succeed secret , name = secret.name - , events = secret.events , images = secret.images , allowCommand = secret.allowCommand + + -- todo: fill allowed_events? + , allow_events = secret.allow_events } , Effect.none ) @@ -193,21 +195,21 @@ update shared route msg model = , Effect.none ) - EventOnCheck event val -> - let - updatedEvents = - if val then - model.events - |> List.append [ event ] - |> List.Extra.unique - - else - model.events - |> List.filter ((/=) event) - in - ( { model | events = updatedEvents } - , Effect.none - ) + -- EventOnCheck event val -> + -- let + -- updatedEvents = + -- if val then + -- model.events + -- |> List.append [ event ] + -- |> List.Extra.unique + + -- else + -- model.events + -- |> List.filter ((/=) event) + -- in + -- ( { model | events = updatedEvents } + -- , Effect.none + -- ) AddImage image -> ( { model @@ -245,7 +247,8 @@ update shared route msg model = , team = Nothing , name = Util.stringToMaybe model.name , value = Util.stringToMaybe model.value - , events = Just model.events + -- , events = Just model.events + , allow_events = model.allow_events , images = Just model.images , allowCommand = Just model.allowCommand } @@ -335,11 +338,12 @@ view shared route model = , msg = ValueOnInput , disabled_ = not <| RemoteData.isSuccess model.secret } - , Components.SecretForm.viewEventsSelect shared - { msg = EventOnCheck - , events = model.events - , disabled_ = not <| RemoteData.isSuccess model.secret - } + + -- , Components.SecretForm.viewEventsSelect shared + -- { msg = EventOnCheck + -- , events = model.events + -- , disabled_ = not <| RemoteData.isSuccess model.secret + -- } , Components.SecretForm.viewImagesInput { onInput_ = ImageOnInput , addImage = AddImage diff --git a/src/elm/Vela.elm b/src/elm/Vela.elm index 72b144e70..4e762386d 100644 --- a/src/elm/Vela.elm +++ b/src/elm/Vela.elm @@ -4,7 +4,8 @@ SPDX-License-Identifier: Apache-2.0 module Vela exposing - ( Build + ( AllowEvents + , Build , BuildGraph , BuildGraphEdge , BuildGraphInteraction @@ -66,6 +67,7 @@ module Vela exposing , decodeServices , decodeSourceRepositories , decodeSteps + , defaultAllowEventsPayload , defaultDeploymentPayload , defaultRepoPayload , defaultSchedulePayload @@ -83,6 +85,7 @@ module Vela exposing , secretTypeToString , setAllowEvents , statusToString + , toAllowEventsPayload ) import Bytes.Encode @@ -442,11 +445,15 @@ defaultRepoPayload = } -setAllowEvents : Repository -> String -> Bool -> RepoPayload -> RepoPayload -setAllowEvents repo field val payload = +setAllowEvents : + { a | allow_events : Maybe AllowEvents } + -> String + -> Bool + -> { a | allow_events : Maybe AllowEventsPayload } +setAllowEvents payload field val = let events = - defaultAllowEventsPayload repo + toAllowEventsPayload payload { push, pull, deploy, comment } = events @@ -542,15 +549,33 @@ type alias AllowEvents = } -defaultAllowEventsPayload : Repository -> AllowEventsPayload -defaultAllowEventsPayload repository = - case repository.allow_events of +defaultAllowEventsPayload : AllowEventsPayload +defaultAllowEventsPayload = + { push = + { branch = False + , tag = False + } + , pull = + { opened = False + , synchronize = False + , edited = False + , reopened = False + } + , deploy = + { created = False + } + , comment = + { created = False + , edited = False + } + } + + +toAllowEventsPayload : { a | allow_events : Maybe AllowEvents } -> AllowEventsPayload +toAllowEventsPayload resource = + case resource.allow_events of Nothing -> - AllowEventsPayload - (defaultPushActions Nothing) - (defaultPullActions Nothing) - (defaultDeployActions Nothing) - (defaultCommentActions Nothing) + defaultAllowEventsPayload Just events -> AllowEventsPayload @@ -1304,6 +1329,7 @@ type alias Secret = , images : List String , events : List String , allowCommand : Bool + , allow_events : Maybe AllowEvents } @@ -1389,6 +1415,7 @@ decodeSecret = |> optional "images" (Json.Decode.list string) [] |> optional "events" (Json.Decode.list string) [] |> optional "allow_command" bool False + |> optional "allow_events" (Json.Decode.maybe decodeAllowEvents) Nothing decodeSecrets : Decoder (List Secret) @@ -1406,6 +1433,7 @@ type alias SecretPayload = , events : Maybe (List String) , images : Maybe (List String) , allowCommand : Maybe Bool + , allow_events : Maybe AllowEventsPayload } @@ -1420,6 +1448,7 @@ defaultSecretPayload = , events = Nothing , images = Nothing , allowCommand = Nothing + , allow_events = Nothing } @@ -1435,6 +1464,7 @@ encodeSecretPayload secret = , ( "events", encodeOptionalList Json.Encode.string secret.events ) , ( "images", encodeOptionalList Json.Encode.string secret.images ) , ( "allow_command", encodeOptional Json.Encode.bool secret.allowCommand ) + , ( "allow_events", encodeOptional encodeAllowEvents secret.allow_events ) ]