From a113a8e8dcb4c4c76cb2aba8f6c7e112e05ae7d9 Mon Sep 17 00:00:00 2001 From: joey Date: Wed, 25 Sep 2024 14:05:29 +0800 Subject: [PATCH] feat(pipeline): inject project/org default namespace for ci/cd pipeline --- internal/apps/dop/endpoints/pipeline.go | 19 +++++++++++++++++++ internal/apps/dop/endpoints/pipeline_test.go | 16 ++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/internal/apps/dop/endpoints/pipeline.go b/internal/apps/dop/endpoints/pipeline.go index 8f6255ab987..27cc0a12fb0 100644 --- a/internal/apps/dop/endpoints/pipeline.go +++ b/internal/apps/dop/endpoints/pipeline.go @@ -95,6 +95,11 @@ func (e *Endpoints) pipelineCreate(ctx context.Context, r *http.Request, vars ma if err != nil { return errorresp.ErrResp(err) } + + // also add project/org default config namespace + reqPipeline.ConfigManageNamespaces = append(reqPipeline.ConfigManageNamespaces, makeOrgDefaultLevelCmsNs(app.OrgID)...) + reqPipeline.ConfigManageNamespaces = append(reqPipeline.ConfigManageNamespaces, makeProjectDefaultLevelCmsNs(app.ProjectID)...) + rules, err := e.branchRule.Query(apistructs.ProjectScope, int64(app.ProjectID)) if err != nil { return errorresp.ErrResp(err) @@ -167,6 +172,20 @@ func (e *Endpoints) pipelineDetail(ctx context.Context, r *http.Request, vars ma return httpserver.OkResp(result) } +func makeProjectDefaultLevelCmsNs(projectID uint64) []string { + // default need be added before custom + return []string{ + fmt.Sprintf("project-%d-default", projectID), + } +} + +func makeOrgDefaultLevelCmsNs(orgID uint64) []string { + // default need be added before custom + return []string{ + fmt.Sprintf("org-%d-default", orgID), + } +} + func getPipelineDetailAndCheckPermission(svc pipelinepb.PipelineServiceServer, permission *permission.Permission, req apistructs.CICDPipelineDetailRequest, identityInfo apistructs.IdentityInfo) (*pipelinepb.PipelineDetailDTO, error) { result, err := svc.PipelineDetail(apis.WithInternalClientContext(context.Background(), discover.DOP()), &pipelinepb.PipelineDetailRequest{ PipelineID: req.PipelineID, diff --git a/internal/apps/dop/endpoints/pipeline_test.go b/internal/apps/dop/endpoints/pipeline_test.go index 9de4a7a6a61..6294e41c76d 100644 --- a/internal/apps/dop/endpoints/pipeline_test.go +++ b/internal/apps/dop/endpoints/pipeline_test.go @@ -182,3 +182,19 @@ func Test_pipelineList(t *testing.T) { _, err = e.pipelineList(context.Background(), r, nil) assert.NoError(t, err) } + +func Test_makeProjectDefaultLevelCmsNs(t *testing.T) { + projectNamespace1 := makeProjectDefaultLevelCmsNs(1) + assert.Equal(t, "project-1-default", projectNamespace1[0]) + + projectNamespace2 := makeProjectDefaultLevelCmsNs(2) + assert.Equal(t, "project-2-default", projectNamespace2[0]) +} + +func Test_makeOrgDefaultLevelCmsNs(t *testing.T) { + orgNamespace1 := makeOrgDefaultLevelCmsNs(1) + assert.Equal(t, "org-1-default", orgNamespace1[0]) + + orgNamespace2 := makeOrgDefaultLevelCmsNs(2) + assert.Equal(t, "org-2-default", orgNamespace2[0]) +}