Pārlūkot izejas kodu

add preview env deploy branches option

Mohammed Nafees 3 gadi atpakaļ
vecāks
revīzija
dd04fc4b7e

+ 1 - 0
api/server/handlers/environment/create.go

@@ -74,6 +74,7 @@ func (c *CreateEnvironmentHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 		Mode:                request.Mode,
 		WebhookID:           string(webhookUID),
 		NewCommentsDisabled: request.DisableNewComments,
+		GitDeployBranches:   strings.Join(request.GitDeployBranches, ","),
 	}
 
 	if len(request.NamespaceAnnotations) > 0 {

+ 16 - 0
api/server/handlers/environment/update_environment_settings.go

@@ -79,6 +79,22 @@ func (c *UpdateEnvironmentSettingsHandler) ServeHTTP(w http.ResponseWriter, r *h
 		env.GitRepoBranches = strings.Join(request.GitRepoBranches, ",")
 	}
 
+	newBranches = []string{}
+
+	for _, br := range request.GitDeployBranches {
+		name := strings.TrimSpace(br)
+
+		if len(name) > 0 {
+			newBranches = append(newBranches, name)
+		}
+	}
+
+	changed = !reflect.DeepEqual(env.ToEnvironmentType().GitDeployBranches, newBranches)
+
+	if changed {
+		env.GitDeployBranches = strings.Join(request.GitDeployBranches, ",")
+	}
+
 	if request.DisableNewComments != env.NewCommentsDisabled {
 		env.NewCommentsDisabled = request.DisableNewComments
 		changed = true

+ 3 - 0
api/types/environment.go

@@ -17,6 +17,7 @@ type Environment struct {
 	LastDeploymentStatus string            `json:"last_deployment_status"`
 	NewCommentsDisabled  bool              `json:"new_comments_disabled"`
 	NamespaceAnnotations map[string]string `json:"namespace_annotations,omitempty"`
+	GitDeployBranches    []string          `json:"git_deploy_branches"`
 }
 
 type CreateEnvironmentRequest struct {
@@ -25,6 +26,7 @@ type CreateEnvironmentRequest struct {
 	DisableNewComments   bool              `json:"disable_new_comments"`
 	GitRepoBranches      []string          `json:"git_repo_branches"`
 	NamespaceAnnotations map[string]string `json:"namespace_annotations"`
+	GitDeployBranches    []string          `json:"git_deploy_branches"`
 }
 
 type GitHubMetadata struct {
@@ -164,4 +166,5 @@ type UpdateEnvironmentSettingsRequest struct {
 	DisableNewComments   bool              `json:"disable_new_comments"`
 	GitRepoBranches      []string          `json:"git_repo_branches"`
 	NamespaceAnnotations map[string]string `json:"namespace_annotations"`
+	GitDeployBranches    []string          `json:"git_deploy_branches"`
 }

+ 17 - 4
dashboard/src/main/home/cluster-dashboard/preview-environments/ConnectNewRepo.tsx

@@ -42,7 +42,8 @@ const ConnectNewRepo: React.FC = () => {
   });
 
   // Branch selector data
-  const [selectedBranches, setSelectedBranches] = useState<string[]>([]);
+  const [baseBranches, setBaseBranches] = useState<string[]>([]);
+  const [deployBranches, setDeployBranches] = useState<string[]>([]);
   const [availableBranches, setAvailableBranches] = useState<string[]>([]);
   const [isLoadingBranches, setIsLoadingBranches] = useState(false);
 
@@ -150,8 +151,9 @@ const ConnectNewRepo: React.FC = () => {
           name: `preview`,
           mode: enableAutomaticDeployments ? "auto" : "manual",
           disable_new_comments: isNewCommentsDisabled,
-          git_repo_branches: selectedBranches,
+          git_repo_branches: baseBranches,
           namespace_annotations: annotations,
+          git_deploy_branches: deployBranches,
         },
         {
           project_id: currentProject.id,
@@ -258,6 +260,17 @@ const ConnectNewRepo: React.FC = () => {
         />
       </CheckboxWrapper>
 
+      <Heading>Deploy from branches</Heading>
+      <Helper>
+        Choose the list of branches that you want to deploy changes from.
+      </Helper>
+      <BranchFilterSelector
+        onChange={setDeployBranches}
+        options={availableBranches}
+        value={deployBranches}
+        showLoading={isLoadingBranches}
+      />
+
       <Heading>Select allowed branches</Heading>
       <Helper>
         If the pull request has a base branch included in this list, it will be
@@ -266,9 +279,9 @@ const ConnectNewRepo: React.FC = () => {
         (Leave empty to allow all branches)
       </Helper>
       <BranchFilterSelector
-        onChange={setSelectedBranches}
+        onChange={setBaseBranches}
         options={availableBranches}
-        value={selectedBranches}
+        value={baseBranches}
         showLoading={isLoadingBranches}
       />
 

+ 18 - 5
dashboard/src/main/home/cluster-dashboard/preview-environments/environments/EnvironmentSettings.tsx

@@ -32,7 +32,8 @@ const EnvironmentSettings = () => {
   const { currentProject, currentCluster, setCurrentError } = useContext(
     Context
   );
-  const [selectedBranches, setSelectedBranches] = useState([]);
+  const [baseBranches, setBaseBranches] = useState([]);
+  const [deployBranches, setDeployBranches] = useState([]);
   const [environment, setEnvironment] = useState<Environment>();
   const [saveStatus, setSaveStatus] = useState("");
   const [newCommentsDisabled, setNewCommentsDisabled] = useState(false);
@@ -68,7 +69,7 @@ const EnvironmentSettings = () => {
       );
 
       setEnvironment(environment);
-      setSelectedBranches(environment.git_repo_branches);
+      setBaseBranches(environment.git_repo_branches);
       setNewCommentsDisabled(environment.new_comments_disabled);
       setDeploymentMode(environment.mode);
 
@@ -158,8 +159,9 @@ const EnvironmentSettings = () => {
         {
           mode: deploymentMode,
           disable_new_comments: newCommentsDisabled,
-          git_repo_branches: selectedBranches,
+          git_repo_branches: baseBranches,
           namespace_annotations: annotations,
+          git_deploy_branches: deployBranches,
         },
         {
           project_id: currentProject.id,
@@ -277,6 +279,17 @@ const EnvironmentSettings = () => {
           }
         />
         <Br />
+        <Heading>Deploy from branches</Heading>
+        <Helper>
+          Choose the list of branches that you want to deploy changes from.
+        </Helper>
+        <BranchFilterSelector
+          onChange={setDeployBranches}
+          options={availableBranches}
+          value={deployBranches}
+          showLoading={isLoadingBranches}
+        />
+        <Br />
         <Heading>Select allowed branches</Heading>
         <Helper>
           If the pull request has a base branch included in this list, it will
@@ -285,9 +298,9 @@ const EnvironmentSettings = () => {
           (Leave empty to allow all branches)
         </Helper>
         <BranchFilterSelector
-          onChange={setSelectedBranches}
+          onChange={setBaseBranches}
           options={availableBranches}
-          value={selectedBranches}
+          value={baseBranches}
           showLoading={isLoadingBranches}
         />
         <Br />

+ 2 - 0
dashboard/src/shared/api.tsx

@@ -146,6 +146,7 @@ const createEnvironment = baseApi<
     disable_new_comments: boolean;
     git_repo_branches: string[];
     namespace_annotations: Record<string, string>;
+    git_deploy_branches: string[];
   },
   {
     project_id: number;
@@ -171,6 +172,7 @@ const updateEnvironment = baseApi<
     disable_new_comments: boolean;
     git_repo_branches: string[]; // Array with branch names
     namespace_annotations: Record<string, string>;
+    git_deploy_branches: string[];
   },
   {
     project_id: number;

+ 9 - 0
internal/models/environment.go

@@ -24,6 +24,7 @@ type Environment struct {
 
 	NewCommentsDisabled  bool
 	NamespaceAnnotations []byte
+	GitDeployBranches    string
 
 	// WebhookID uniquely identifies the environment when other fields (project, cluster)
 	// aren't present
@@ -74,6 +75,14 @@ func (e *Environment) ToEnvironmentType() *types.Environment {
 		env.GitRepoBranches = []string{}
 	}
 
+	branches = getGitRepoBranches(e.GitDeployBranches)
+
+	if len(branches) > 0 {
+		env.GitDeployBranches = branches
+	} else {
+		env.GitDeployBranches = []string{}
+	}
+
 	if len(e.NamespaceAnnotations) > 0 {
 		env.NamespaceAnnotations = make(map[string]string)
 		annotations := string(e.NamespaceAnnotations)