Skip to content

Commit

Permalink
adjust comment format and encapsulate the language-ctx method
Browse files Browse the repository at this point in the history
  • Loading branch information
Malyue committed Dec 20, 2023
1 parent c68aa75 commit d30b3e1
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 33 deletions.
2 changes: 1 addition & 1 deletion bundle/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (b *Bundle) ListAuditEvent(orgID string, userID string, params url.Values)
return nil, apierrors.ErrInvoke.InternalError(
fmt.Errorf("failed to list Audit, status code: %d, body: %v",
resp.StatusCode(),
resp.Body(),
string(resp.Body()),
))
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/erda-server/conf/i18n/i18n.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ zh:
ErrAddMemberOwner: "添加所有者失败,数量超过上限"

# audit error
ErrInvalidOrg: "OrgID 非法,可能是不存在orgID或者多个orgID"
ErrInvalidOrg: "OrgID 非法,可能是不存在 OrgID 或者多个 OrgID"
ErrInvalidProjectInOrg: "所选项目错误,用户所选择的项目不属于其组织"
ErrInvalidAppInOrg: "所选应用错误,用户所选择的应用不属于其组织"
ErrInvalidAppInProject: "所选应用错误,用户所选择的应用不属于其选择的项目"
Expand Down Expand Up @@ -51,7 +51,7 @@ en:
ErrAddMemberOwner: "failed to add project owner, quantity exceeds limit"

# audit error
ErrInvalidOrgID: "Invalid OrgID,it may not exist or multiple OrgIDs"
ErrInvalidOrgID: "Invalid OrgID, it may not exist or multiple OrgIDs"
ErrInvalidProjectInOrg: "Invalid selected project. The project does not belong to the user's organization"
ErrInvalidAppInOrg: "Invalid selected application. The application does not belong to user's organization."
ErrInvalidAppInProject: "Invalid selected application. The application does not belong to the selected project"
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ require (
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/mysql v1.3.2
gorm.io/driver/sqlite v1.3.1
gorm.io/gorm v1.23.5
gorm.io/driver/sqlite v1.5.3
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55
gorm.io/plugin/soft_delete v1.1.0
gotest.tools v2.2.0+incompatible
helm.sh/helm/v3 v3.6.2
Expand Down
9 changes: 4 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,6 @@ github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71
github.com/mattn/go-sqlite3 v1.14.3/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw=
github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand Down Expand Up @@ -2815,13 +2814,13 @@ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.3.2 h1:QJryWiqQ91EvZ0jZL48NOpdlPdMjdip1hQ8bTgo4H7I=
gorm.io/driver/mysql v1.3.2/go.mod h1:ChK6AHbHgDCFZyJp0F+BmVGb06PSIoh9uVYKAlRbb2U=
gorm.io/driver/sqlite v1.1.3/go.mod h1:AKDgRWk8lcSQSw+9kxCJnX/yySj8G3rdwYlU57cB45c=
gorm.io/driver/sqlite v1.3.1 h1:bwfE+zTEWklBYoEodIOIBwuWHpnx52Z9zJFW5F33WLk=
gorm.io/driver/sqlite v1.3.1/go.mod h1:wJx0hJspfycZ6myN38x1O/AqLtNS6c5o9TndewFbELg=
gorm.io/driver/sqlite v1.5.3 h1:7/0dUgX28KAcopdfbRWWl68Rflh6osa4rDh+m51KL2g=
gorm.io/driver/sqlite v1.5.3/go.mod h1:qxAuCol+2r6PannQDpOP1FP6ag3mKi4esLnB/jHed+4=
gorm.io/gorm v1.20.1/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.23.0/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.23.1/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.23.5 h1:TnlF26wScKSvknUC/Rn8t0NLLM22fypYBlvj1+aH6dM=
gorm.io/gorm v1.23.5/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55 h1:sC1Xj4TYrLqg1n3AN10w871An7wJM0gzgcm8jkIkECQ=
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/plugin/soft_delete v1.1.0 h1:LcE4L+GD29RkkMLxMYHpT4wQCJ/9945FsdU/mHGaDuE=
gorm.io/plugin/soft_delete v1.1.0/go.mod h1:Zv7vQctOJTGOsJ/bWgrN1n3od0GBAZgnLjEx+cApLGk=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
Expand Down
45 changes: 45 additions & 0 deletions internal/core/legacy/common/ctxhelper/audit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ctxhelper

import (
"context"

"github.com/erda-project/erda-infra/providers/i18n"
)

const languageKey = "language"
const zh = "zh-CN"
const en = "en-US"

func GetAuditLanguage(ctx context.Context) (i18n.LanguageCodes, bool) {
langCodes, ok := ctx.Value(languageKey).(i18n.LanguageCodes)
if !ok {
return nil, false
}
if langCodes == nil {
return i18n.LanguageCodes{
&i18n.LanguageCode{
Code: zh,
Quality: 1,
},
}, true

Check warning on line 38 in internal/core/legacy/common/ctxhelper/audit.go

View check run for this annotation

Codecov / codecov/patch

internal/core/legacy/common/ctxhelper/audit.go#L33-L38

Added lines #L33 - L38 were not covered by tests
}
return langCodes, true
}

func PutAuditLanguage(ctx context.Context, language i18n.LanguageCodes) context.Context {
return context.WithValue(ctx, languageKey, language)
}
91 changes: 91 additions & 0 deletions internal/core/legacy/common/ctxhelper/audit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ctxhelper

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"github.com/erda-project/erda-infra/providers/i18n"
)

func TestAuditContext(t *testing.T) {

type resp struct {
language i18n.LanguageCodes
flag bool
}
testCase := []struct {
name string
lang any
want resp
}{
{
name: "invalid language",
lang: 1,
want: resp{
language: nil,
flag: false,
},
},
{
name: "language is nil",
lang: []*i18n.LanguageCode{},
want: resp{
language: nil,
flag: false,
},
},
{
name: "success get language",
lang: i18n.LanguageCodes{
&i18n.LanguageCode{
Code: en,
Quality: 1,
},
},
want: resp{
language: i18n.LanguageCodes{
&i18n.LanguageCode{
Code: en,
Quality: 1,
},
},
flag: true,
},
},
}

for _, tt := range testCase {
t.Run(tt.name, func(t *testing.T) {
var lang i18n.LanguageCodes
var flag bool
if tt.lang == 1 {
ctx := context.WithValue(context.Background(), languageKey, tt.lang)
lang, flag = GetAuditLanguage(ctx)

} else if tt.name == "language is nil" {
lang, flag = GetAuditLanguage(context.Background())
} else {
ctx := PutAuditLanguage(context.Background(), tt.lang.(i18n.LanguageCodes))
lang, flag = GetAuditLanguage(ctx)
}
assert.Equal(t, tt.want.language, lang)
assert.Equal(t, tt.want.flag, flag)
})
}
}
6 changes: 4 additions & 2 deletions internal/core/legacy/endpoints/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/erda-project/erda-infra/providers/legacy/httpendpoints/i18n"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/internal/core/legacy/common/ctxhelper"
"github.com/erda-project/erda/internal/core/legacy/conf"
"github.com/erda-project/erda/internal/core/legacy/services/apierrors"
"github.com/erda-project/erda/internal/pkg/user"
Expand Down Expand Up @@ -109,7 +110,8 @@ func (e *Endpoints) ListAudits(ctx context.Context, r *http.Request, vars map[st
return apierrors.ErrListAudit.InvalidParameter(err).ToResp(), nil
}

ctx = context.WithValue(ctx, "lang_codes", i18n.Language(r))
// set `i18n.LanguageCodes` in ctx
ctx = ctxhelper.PutAuditLanguage(ctx, i18n.Language(r))

Check warning on line 114 in internal/core/legacy/endpoints/audit.go

View check run for this annotation

Codecov / codecov/patch

internal/core/legacy/endpoints/audit.go#L114

Added line #L114 was not covered by tests

// 权限检查
identityInfo, err := user.GetIdentityInfo(r)
Expand Down Expand Up @@ -179,7 +181,7 @@ func (e *Endpoints) ExportExcelAudit(ctx context.Context, w http.ResponseWriter,
return apierrors.ErrExportExcelAudit.InvalidParameter(err)
}

ctx = context.WithValue(ctx, "lang_codes", i18n.Language(r))
ctx = ctxhelper.PutAuditLanguage(ctx, i18n.Language(r))

Check warning on line 184 in internal/core/legacy/endpoints/audit.go

View check run for this annotation

Codecov / codecov/patch

internal/core/legacy/endpoints/audit.go#L184

Added line #L184 was not covered by tests
// 权限检查
identityInfo, err := user.GetIdentityInfo(r)
if err != nil {
Expand Down
25 changes: 9 additions & 16 deletions internal/core/legacy/services/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/erda-project/erda-infra/providers/i18n"
userpb "github.com/erda-project/erda-proto-go/core/user/pb"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/internal/core/legacy/common/ctxhelper"
"github.com/erda-project/erda/internal/core/legacy/conf"
"github.com/erda-project/erda/internal/core/legacy/dao"
"github.com/erda-project/erda/internal/core/legacy/model"
Expand Down Expand Up @@ -144,7 +145,7 @@ func (a *Audit) List(ctx context.Context, param *apistructs.AuditsListRequest) (
return a.db.GetAuditsByParam(filterParam)

Check warning on line 145 in internal/core/legacy/services/audit/audit.go

View check run for this annotation

Codecov / codecov/patch

internal/core/legacy/services/audit/audit.go#L145

Added line #L145 was not covered by tests
}

// if it is not the sys level,valid the param and construct the filterParam
// if it is not the sys level, valid the param and construct the filterParam
var err error
filterParam, err = a.constructFilterParamByReq(ctx, param)
if err != nil {
Expand All @@ -156,17 +157,9 @@ func (a *Audit) List(ctx context.Context, param *apistructs.AuditsListRequest) (

// constructFilterParamByReq valid the param and construct the filterParam to query db by `apistruct.AuditsListRequest`
func (a *Audit) constructFilterParamByReq(ctx context.Context, param *apistructs.AuditsListRequest) (*model.ListAuditParam, error) {
langCodes, ok := ctx.Value("lang_codes").(i18n.LanguageCodes)
langCodes, ok := ctxhelper.GetAuditLanguage(ctx)
if !ok {
return nil, errors.New("Invalid Language")
}
if langCodes == nil {
langCodes = i18n.LanguageCodes{
&i18n.LanguageCode{
Code: "zh-CN",
Quality: 1,
},
}
return nil, errors.New("missing language")

Check warning on line 162 in internal/core/legacy/services/audit/audit.go

View check run for this annotation

Codecov / codecov/patch

internal/core/legacy/services/audit/audit.go#L162

Added line #L162 was not covered by tests
}

filterParam := &model.ListAuditParam{
Expand All @@ -180,7 +173,7 @@ func (a *Audit) constructFilterParamByReq(ctx context.Context, param *apistructs
ClientIP: param.ClientIP,
ScopeType: param.ScopeType,
}
// Valid OrgID,in org level,the len(param.OrgID) must equals 1
// Valid OrgID, in org level, the len(param.OrgID) must equals 1
if param.OrgID == nil || len(param.OrgID) > 1 {
return nil, errors.New(a.trans.Text(langCodes, ErrInvalidOrg))
}
Expand All @@ -201,7 +194,7 @@ func (a *Audit) constructFilterParamByReq(ctx context.Context, param *apistructs
var appIds []uint64
var err error
if len(param.ProjectID) > 0 {
// if projectId is not nil,the app must own to the projectId
// if projectId is not nil, the app must own to the projectId
appIds, err = a.GetAllAppIdByProjectIds(param.ProjectID)
if err != nil {
return nil, err

Check warning on line 200 in internal/core/legacy/services/audit/audit.go

View check run for this annotation

Codecov / codecov/patch

internal/core/legacy/services/audit/audit.go#L200

Added line #L200 was not covered by tests
Expand All @@ -210,7 +203,7 @@ func (a *Audit) constructFilterParamByReq(ctx context.Context, param *apistructs
return nil, errors.New(a.trans.Text(langCodes, ErrInvalidAppInProject))
}
} else {
// projectId is nil,the app must own to the orgId
// projectId is nil, the app must own to the orgId
appIds, err = a.GetAllAppIdByOrgId(param.OrgID[0])
if err != nil {
return nil, err

Check warning on line 209 in internal/core/legacy/services/audit/audit.go

View check run for this annotation

Codecov / codecov/patch

internal/core/legacy/services/audit/audit.go#L209

Added line #L209 was not covered by tests
Expand All @@ -231,7 +224,7 @@ func (a *Audit) GetAllProjectIdInOrg(orgId uint64) ([]uint64, error) {
return nil, err

Check warning on line 224 in internal/core/legacy/services/audit/audit.go

View check run for this annotation

Codecov / codecov/patch

internal/core/legacy/services/audit/audit.go#L224

Added line #L224 was not covered by tests
}

// Get the id field from projectList
// get the id field from projectList
projectIds := make([]uint64, len(projectList))
for index, project := range projectList {
projectIds[index] = uint64(project.ID)
Expand Down Expand Up @@ -325,7 +318,7 @@ func (a *Audit) convertAuditsToExcelList(ctx context.Context, audits []model.Aud
}

func (a *Audit) getContent(ctx context.Context, audit model.Audit) string {
langCodes, _ := ctx.Value("lang_codes").(i18n.LanguageCodes)
langCodes, _ := ctxhelper.GetAuditLanguage(ctx)

Check warning on line 321 in internal/core/legacy/services/audit/audit.go

View check run for this annotation

Codecov / codecov/patch

internal/core/legacy/services/audit/audit.go#L321

Added line #L321 was not covered by tests
var locale string
if len(langCodes) != 0 {
locale = langCodes[0].Code
Expand Down
8 changes: 5 additions & 3 deletions internal/core/legacy/services/audit/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/erda-project/erda-infra/providers/i18n"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/internal/core/legacy/common/ctxhelper"
"github.com/erda-project/erda/internal/core/legacy/dao"
"github.com/erda-project/erda/internal/core/legacy/model"
)
Expand Down Expand Up @@ -249,13 +250,14 @@ func Test_constructFilterParamByReq(t *testing.T) {

for _, tt := range testcase {
t.Run(tt.Name, func(t *testing.T) {
ctx := context.WithValue(context.Background(), "lang_codes", i18n.LanguageCodes{})
//ctx := context.WithValue(context.Background(), "lang_codes", i18n.LanguageCodes{})
ctx := ctxhelper.PutAuditLanguage(context.Background(), i18n.LanguageCodes{})
filterParam, err := audit.constructFilterParamByReq(ctx, tt.param)
assert.Equal(t, filterParam, tt.want.filterParam)
if err != nil {
assert.Equal(t, err.Error(), tt.want.err.Error())
assert.Equal(t, tt.want.err.Error(), err.Error())
} else {
assert.Equal(t, err, tt.want.err)
assert.Equal(t, tt.want.err, err)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions internal/core/legacy/services/permission/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ var checkAdaptor security.PermissionAdaptor
func (p *Permission) CheckPermission(req *apistructs.PermissionCheckRequest) (bool, error) {
logrus.Debugf("invoke permission, time: %s, req: %+v", time.Now().Format(time.RFC3339), req)

// if the param is invalid,it will set the req as nil
// if the param is invalid, it will set the req as nil
if req == nil {
return false, errors.New("the user operation is invalid,maybe the param is invalid")
return false, errors.New("the user operation is invalid, maybe the param is invalid")

Check warning on line 178 in internal/core/legacy/services/permission/permission.go

View check run for this annotation

Codecov / codecov/patch

internal/core/legacy/services/permission/permission.go#L177-L178

Added lines #L177 - L178 were not covered by tests
}

checkAdaptor.Once.Do(func() {
Expand Down
1 change: 1 addition & 0 deletions internal/tools/pipeline/dbclient/op_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func (client *Client) PageListPipelines(req *pipelinepb.PipelinePagingRequest, o
if req.PageNum <= 0 {
req.PageNum = 1
}

// default pageSize = 20 (Range: 0 < pageSize <= 100)
if req.PageSize <= 0 || (req.PageSize > 100 && !req.LargePageSize) {
req.PageSize = 20
Expand Down

0 comments on commit d30b3e1

Please sign in to comment.