mindlesscloud commented on code in PR #4632: URL: https://github.com/apache/incubator-devlake/pull/4632#discussion_r1135030790
########## backend/helpers/pluginhelper/api/scope_helper.go: ########## @@ -0,0 +1,346 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You 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 api + +import ( + "fmt" + "github.com/apache/incubator-devlake/core/context" + "github.com/apache/incubator-devlake/core/dal" + "github.com/apache/incubator-devlake/core/errors" + "github.com/apache/incubator-devlake/core/log" + "github.com/apache/incubator-devlake/core/plugin" + "github.com/go-playground/validator/v10" + "github.com/mitchellh/mapstructure" + "gorm.io/gorm" + "strconv" + "strings" + "time" + + "reflect" +) + +// ScopeApiHelper is used to write the CURD of connection +type ScopeApiHelper struct { + log log.Logger + db dal.Dal + validator *validator.Validate + connHelper *ConnectionApiHelper +} + +// NewScopeHelper creates a ScopeHelper for connection management +func NewScopeHelper( + basicRes context.BasicRes, + vld *validator.Validate, + connHelper *ConnectionApiHelper, +) *ScopeApiHelper { + if vld == nil { + vld = validator.New() + } + if connHelper == nil { + return nil + } + return &ScopeApiHelper{ + log: basicRes.GetLogger(), + db: basicRes.GetDal(), + validator: vld, + connHelper: connHelper, + } +} + +// Put saves the given scopes to the database. It expects a slice of struct pointers +// as the scopes argument. It also expects a fieldName argument, which is used to extract +// the connection ID from the input.Params map. +func (c *ScopeApiHelper) Put(input *plugin.ApiResourceInput, apiScope interface{}, connection interface{}) errors.Error { + err := errors.Convert(mapstructure.Decode(input.Body, apiScope)) + if err != nil { + return errors.BadInput.Wrap(err, "decoding Github repo error") + } + // Ensure that the scopes argument is a slice + v := reflect.ValueOf(apiScope) + scopesValue := v.Elem().FieldByName("Data") + if scopesValue.Kind() != reflect.Slice { + panic("expected a slice") + } + // Extract the connection ID from the input.Params map + connectionId, _ := ExtractParam(input.Params) + if connectionId == 0 { + return errors.BadInput.New("invalid connectionId or scopeId") + } + err = c.VerifyConnection(connection, connectionId) + if err != nil { + return err + } + // Create a map to keep track of primary key values + keeper := make(map[string]struct{}) + + // Set the CreatedDate and UpdatedDate fields to the current time for each scope + now := time.Now() + for i := 0; i < scopesValue.Len(); i++ { + // Get the reflect.Value of the i-th struct pointer in the slice + structValue := scopesValue.Index(i) + + // Ensure that the structValue is a pointer to a struct + if structValue.Kind() != reflect.Ptr || structValue.Elem().Kind() != reflect.Struct { + panic("expected a pointer to a struct") + } + + // Ensure that the primary key value is unique + primaryValueStr := ReturnPrimaryKeyValue(structValue.Elem().Interface()) + if _, ok := keeper[primaryValueStr]; ok { + return errors.BadInput.New("duplicated item") + } else { + keeper[primaryValueStr] = struct{}{} + } + + // Set the connection ID, CreatedDate, and UpdatedDate fields + SetScopeFields(structValue.Interface(), connectionId, &now, &now) + + // Verify that the primary key value is valid + err = VerifyPrimaryKeyValue(structValue.Elem().Interface()) + if err != nil { + return err + } + } + + // Save the scopes to the database + return c.save(scopesValue.Interface(), c.db.Create) +} + +func (c *ScopeApiHelper) Update(input *plugin.ApiResourceInput, fieldName string, connection interface{}, scope interface{}) errors.Error { + connectionId, scopeId := ExtractParam(input.Params) + + if connectionId == 0 || len(scopeId) == 0 || scopeId == "0" { Review Comment: Shall we check for `scopeId == ""`? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
