From 8c6f8b9b2d095ebf00c6629395d5cdbb473d5d2d Mon Sep 17 00:00:00 2001
From: Easton Crupper <65553218+ecrupper@users.noreply.github.com>
Date: Wed, 6 Sep 2023 11:47:45 -0400
Subject: [PATCH] patch(v0.21.1/secrets): allow schedules for secret form if on
 allowlist (#719)

---
 cypress/integration/secrets.spec.js | 28 ++++++++++++++++++++++++++++
 src/elm/Main.elm                    |  1 +
 src/elm/Pages/Secrets/Form.elm      | 22 +++++++++++++++++++---
 src/elm/Pages/Secrets/Model.elm     |  1 +
 src/elm/Pages/Secrets/View.elm      | 29 ++++++++++++++---------------
 5 files changed, 63 insertions(+), 18 deletions(-)

diff --git a/cypress/integration/secrets.spec.js b/cypress/integration/secrets.spec.js
index ddcd90b0d..baffe856a 100644
--- a/cypress/integration/secrets.spec.js
+++ b/cypress/integration/secrets.spec.js
@@ -31,6 +31,34 @@ context('Secrets', () => {
         .contains('Remove');
     });
 
+    context(
+      'allowlist contains *',
+      {
+        env: {
+          VELA_SCHEDULE_ALLOWLIST: '*',
+        },
+      },
+      () => {
+        it('add button should show', () => {
+          cy.get('[data-test=repo-checkbox-schedule]').should('exist');
+        });
+      },
+    );
+
+    context(
+      'allowlist is empty',
+      {
+        env: {
+          VELA_SCHEDULE_ALLOWLIST: ' ',
+        },
+      },
+      () => {
+        it('add button should not show', () => {
+          cy.get('[data-test=repo-checkbox-schedule]').should('not.exist');
+        });
+      },
+    );
+
     context('click Remove', () => {
       beforeEach(() => {
         cy.get('[data-test=secret-delete-button]').click();
diff --git a/src/elm/Main.elm b/src/elm/Main.elm
index 76d83e039..2c4dbaa92 100644
--- a/src/elm/Main.elm
+++ b/src/elm/Main.elm
@@ -3733,6 +3733,7 @@ loadOrgSecretsPage model maybePage maybePerPage engine org =
             { secretsModel
                 | orgSecrets = Loading
                 , org = org
+                , repo = ""
                 , engine = engine
                 , type_ = Vela.OrgSecret
             }
diff --git a/src/elm/Pages/Secrets/Form.elm b/src/elm/Pages/Secrets/Form.elm
index 8c693fa7e..c61a9b2ea 100644
--- a/src/elm/Pages/Secrets/Form.elm
+++ b/src/elm/Pages/Secrets/Form.elm
@@ -49,7 +49,7 @@ import Html.Attributes
         )
 import Html.Events exposing (onClick, onInput)
 import Pages.RepoSettings exposing (checkbox)
-import Pages.Secrets.Model exposing (DeleteSecretState(..), Model, Msg(..), SecretForm)
+import Pages.Secrets.Model exposing (DeleteSecretState(..), Model, Msg(..), PartialModel, SecretForm)
 import Util
 import Vela exposing (Field)
 
@@ -166,8 +166,23 @@ viewValueInput val placeholder_ =
 
 {-| viewEventsSelect : renders events input selection
 -}
-viewEventsSelect : SecretForm -> Html Msg
-viewEventsSelect secretUpdate =
+viewEventsSelect : SecretForm -> PartialModel a msg -> Html Msg
+viewEventsSelect secretUpdate model =
+    let
+        schedulesAllowed =
+            Util.checkScheduleAllowlist model.secretsModel.org model.secretsModel.repo model.velaScheduleAllowlist
+
+        scheduleOption =
+            if schedulesAllowed then
+                checkbox "Schedule"
+                    "schedule"
+                    (eventEnabled "schedule" secretUpdate.events)
+                <|
+                    OnChangeEvent "schedule"
+
+            else
+                text ""
+    in
     section []
         [ div [ for "events-select" ]
             [ strong [] [ text "Limit to Events" ]
@@ -207,6 +222,7 @@ viewEventsSelect secretUpdate =
                 (eventEnabled "deployment" secretUpdate.events)
               <|
                 OnChangeEvent "deployment"
+            , scheduleOption
             ]
         ]
 
diff --git a/src/elm/Pages/Secrets/Model.elm b/src/elm/Pages/Secrets/Model.elm
index 7fb2173f1..cd7aec763 100644
--- a/src/elm/Pages/Secrets/Model.elm
+++ b/src/elm/Pages/Secrets/Model.elm
@@ -36,6 +36,7 @@ import Vela exposing (Copy, Engine, Key, Org, Repo, Secret, SecretType, Secrets,
 type alias PartialModel a msg =
     { a
         | velaAPI : String
+        , velaScheduleAllowlist : List ( Org, Repo )
         , session : Session
         , page : Page
         , secretsModel : Model msg
diff --git a/src/elm/Pages/Secrets/View.elm b/src/elm/Pages/Secrets/View.elm
index 63c517582..ca6ed327b 100644
--- a/src/elm/Pages/Secrets/View.elm
+++ b/src/elm/Pages/Secrets/View.elm
@@ -15,8 +15,7 @@ import Http
 import Pages.Secrets.Form exposing (viewAllowCommandCheckbox, viewEventsSelect, viewHelp, viewImagesInput, viewInput, viewNameInput, viewSubmitButtons, viewValueInput)
 import Pages.Secrets.Model
     exposing
-        ( Model
-        , Msg
+        ( Msg
         , PartialModel
         )
 import RemoteData exposing (RemoteData(..))
@@ -540,7 +539,7 @@ addSecret model =
     div [ class "manage-secret", Util.testAttribute "manage-secret" ]
         [ div []
             [ h2 [] [ addLabel model.secretsModel.type_ ]
-            , addForm model.secretsModel
+            , addForm model
             ]
         ]
 
@@ -562,14 +561,14 @@ addLabel type_ =
 
 {-| addForm : renders secret update form for adding a new secret
 -}
-addForm : Model msg -> Html Msg
-addForm secretsModel =
+addForm : PartialModel a msg -> Html Msg
+addForm partialModel =
     let
         secretUpdate =
-            secretsModel.form
+            partialModel.secretsModel.form
 
         teamForm =
-            if secretsModel.team == "*" && secretsModel.type_ == SharedSecret then
+            if partialModel.secretsModel.team == "*" && partialModel.secretsModel.type_ == SharedSecret then
                 viewInput "Team" secretUpdate.team "Team Name"
 
             else
@@ -579,12 +578,12 @@ addForm secretsModel =
         [ teamForm
         , viewNameInput secretUpdate.name False
         , viewValueInput secretUpdate.value "Secret Value"
-        , viewEventsSelect secretUpdate
+        , viewEventsSelect secretUpdate partialModel
         , viewImagesInput secretUpdate secretUpdate.imageInput
         , viewAllowCommandCheckbox secretUpdate
         , viewHelp
         , div [ class "form-action" ]
-            [ button [ class "button", class "-outline", onClick <| Pages.Secrets.Model.AddSecret secretsModel.engine ] [ text "Add" ]
+            [ button [ class "button", class "-outline", onClick <| Pages.Secrets.Model.AddSecret partialModel.secretsModel.engine ] [ text "Add" ]
             ]
         ]
 
@@ -617,7 +616,7 @@ editSecret model =
             div [ class "manage-secret", Util.testAttribute "manage-secret" ]
                 [ div []
                     [ h2 [] [ editHeader model.secretsModel.type_ ]
-                    , editForm model.secretsModel
+                    , editForm model
                     ]
                 ]
 
@@ -645,18 +644,18 @@ editHeader type_ =
 
 {-| editForm : renders secret update form for updating a preexisting secret
 -}
-editForm : Model msg -> Html Msg
-editForm secretsModel =
+editForm : PartialModel a msg -> Html Msg
+editForm partialModel =
     let
         secretUpdate =
-            secretsModel.form
+            partialModel.secretsModel.form
     in
     div [ class "secret-form", class "edit-form" ]
         [ viewNameInput secretUpdate.name True
         , viewValueInput secretUpdate.value "Secret Value (leave blank to make no change)"
-        , viewEventsSelect secretUpdate
+        , viewEventsSelect secretUpdate partialModel
         , viewImagesInput secretUpdate secretUpdate.imageInput
         , viewAllowCommandCheckbox secretUpdate
         , viewHelp
-        , viewSubmitButtons secretsModel
+        , viewSubmitButtons partialModel.secretsModel
         ]