Parcourir la source

Merge branch 'nico/por-515-allow-user-to-change-the-branch-of-a' of github.com:porter-dev/porter into nico/por-515-allow-user-to-change-the-branch-of-a

jnfrati il y a 3 ans
Parent
commit
8ab015f468

+ 7 - 7
api/server/handlers/release/update_action_config.go → api/server/handlers/release/update_git_action_config.go

@@ -13,26 +13,26 @@ import (
 	"gorm.io/gorm"
 )
 
-type UpdateActionConfigHandler struct {
+type UpdateGitActionConfigHandler struct {
 	handlers.PorterHandlerReadWriter
 }
 
-func NewUpdateActionConfigHandler(
+func NewUpdateGitActionConfigHandler(
 	config *config.Config,
 	decoderValidator shared.RequestDecoderValidator,
 	writer shared.ResultWriter,
-) *UpdateActionConfigHandler {
-	return &UpdateActionConfigHandler{
+) *UpdateGitActionConfigHandler {
+	return &UpdateGitActionConfigHandler{
 		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
 	}
 }
 
-func (c *UpdateActionConfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+func (c *UpdateGitActionConfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 	cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
 	name, _ := requestutils.GetURLParamString(r, types.URLParamReleaseName)
 	namespace := r.Context().Value(types.NamespaceScope).(string)
 
-	request := &types.UpdateActionConfigRequest{}
+	request := &types.UpdateGitActionConfigRequest{}
 
 	if ok := c.DecodeAndValidate(w, r, request); !ok {
 		return
@@ -49,7 +49,7 @@ func (c *UpdateActionConfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 	}
 
-	actionConfig, err := c.Repo().GitActionConfig().ReadGitActionConfig(release.ID)
+	actionConfig, err := c.Repo().GitActionConfig().ReadGitActionConfig(release.GitActionConfig.ID)
 
 	if err != nil {
 		if err == gorm.ErrRecordNotFound {

+ 1 - 1
api/server/router/release.go

@@ -833,7 +833,7 @@ func getReleaseRoutes(
 		},
 	)
 
-	updateGitActionConfigHandler := release.NewUpdateBuildConfigHandler(
+	updateGitActionConfigHandler := release.NewUpdateGitActionConfigHandler(
 		config,
 		factory.GetDecoderValidator(),
 		factory.GetResultWriter(),

+ 1 - 1
api/types/release.go

@@ -194,6 +194,6 @@ type PatchUpdateReleaseTags struct {
 	Tags []string `json:"tags"`
 }
 
-type UpdateActionConfigRequest struct {
+type UpdateGitActionConfigRequest struct {
 	GitActionConfig *GitActionConfig `json:"git_action_config"`
 }

+ 38 - 2
dashboard/src/main/home/cluster-dashboard/expanded-chart/BuildSettingsTab.tsx

@@ -20,6 +20,7 @@ import { AddCustomBuildpackForm } from "components/repo-selector/BuildpackSelect
 import { DeviconsNameList } from "assets/devicons-name-list";
 import Selector from "components/Selector";
 import BranchList from "components/repo-selector/BranchList";
+import Banner from "components/Banner";
 
 type Buildpack = {
   name: string;
@@ -78,7 +79,37 @@ const BuildSettingsTab: React.FC<Props> = ({ chart, isPreviousVersion }) => {
     () => chart?.git_action_config?.git_branch
   );
 
-  const saveNewBranch = async (newBranch: string) => {};
+  const saveNewBranch = async (newBranch: string) => {
+    if (!newBranch?.length) {
+      return;
+    }
+
+    if (newBranch === chart?.git_action_config?.git_branch) {
+      return;
+    }
+
+    const newGitActionConfig: FullActionConfigType = {
+      ...chart.git_action_config,
+      git_branch: newBranch,
+    };
+
+    try {
+      api.updateGitActionConfig(
+        "<token>",
+        {
+          git_action_config: newGitActionConfig,
+        },
+        {
+          project_id: currentProject.id,
+          cluster_id: currentCluster.id,
+          release_name: chart.name,
+          namespace: chart.namespace,
+        }
+      );
+    } catch (error) {
+      throw error;
+    }
+  };
 
   const saveBuildConfig = async (config: BuildConfig) => {
     if (config === null) {
@@ -214,6 +245,7 @@ const BuildSettingsTab: React.FC<Props> = ({ chart, isPreviousVersion }) => {
     setButtonStatus("loading");
     try {
       await saveBuildConfig(buildConfig);
+      await saveNewBranch(currentBranch);
       await saveEnvVariables(envVariables);
       setButtonStatus("successful");
     } catch (error) {
@@ -228,6 +260,7 @@ const BuildSettingsTab: React.FC<Props> = ({ chart, isPreviousVersion }) => {
     setButtonStatus("loading");
     try {
       await saveBuildConfig(buildConfig);
+      await saveNewBranch(currentBranch);
       await saveEnvVariables(envVariables);
       await triggerWorkflow();
       setButtonStatus("successful");
@@ -304,10 +337,13 @@ const BuildSettingsTab: React.FC<Props> = ({ chart, isPreviousVersion }) => {
           }}
         ></KeyValueArray>
 
-        <Heading>Select default branch</Heading>
+        <Heading>Select Default Branch</Heading>
         <Helper>
           Change the default branch the deployments will be made from.
         </Helper>
+        <Banner type="warning">
+          You must also update the deploy branch in your GitHub Action file.
+        </Banner>
         <BranchList
           actionConfig={currentActionConfig}
           setBranch={setCurrentBranch}

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

@@ -1,9 +1,8 @@
 import { PolicyDocType } from "./auth/types";
 import { PullRequest } from "main/home/cluster-dashboard/preview-environments/types";
-import { release } from "process";
 import { baseApi } from "./baseApi";
 
-import { BuildConfig, FullActionConfigType, StorageType } from "./types";
+import { BuildConfig, FullActionConfigType } from "./types";
 import { CreateStackBody } from "main/home/cluster-dashboard/stacks/types";
 
 /**
@@ -1807,6 +1806,22 @@ const updateBuildConfig = baseApi<
     `/api/projects/${project_id}/clusters/${cluster_id}/namespaces/${namespace}/releases/${release_name}/buildconfig`
 );
 
+const updateGitActionConfig = baseApi<
+  {
+    git_action_config: FullActionConfigType;
+  },
+  {
+    project_id: number;
+    cluster_id: number;
+    namespace: string;
+    release_name: string;
+  }
+>(
+  "PATCH",
+  ({ project_id, cluster_id, namespace, release_name }) =>
+    `/api/projects/${project_id}/clusters/${cluster_id}/namespaces/${namespace}/releases/${release_name}/git_action_config`
+);
+
 const reRunGHWorkflow = baseApi<
   {},
   {
@@ -2182,6 +2197,7 @@ export default {
   upgradePorterAgent,
   deletePRDeployment,
   updateBuildConfig,
+  updateGitActionConfig,
   reRunGHWorkflow,
   triggerPreviewEnvWorkflow,
   getTagsByProjectId,