This is an automated email from the ASF dual-hosted git repository. warren pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git
commit 495420cfb4a5713b9a271e038be764959167cd26 Author: Mr.An <[email protected]> AuthorDate: Sat Jun 18 02:26:19 2022 +0800 update api connection.go --- plugins/gitee/api/connection.go | 80 +++++++++++++++++--------------------- plugins/gitee/api/init.go | 42 ++++++++++++++++++++ plugins/gitee/models/connection.go | 25 ++++-------- 3 files changed, 84 insertions(+), 63 deletions(-) diff --git a/plugins/gitee/api/connection.go b/plugins/gitee/api/connection.go index 7c912eb9..b074dc7b 100644 --- a/plugins/gitee/api/connection.go +++ b/plugins/gitee/api/connection.go @@ -23,29 +23,23 @@ import ( "net/url" "time" - "github.com/apache/incubator-devlake/config" - "github.com/apache/incubator-devlake/plugins/core" "github.com/apache/incubator-devlake/plugins/gitee/models" - "github.com/apache/incubator-devlake/plugins/helper" - "github.com/go-playground/validator/v10" + "github.com/apache/incubator-devlake/plugins/helper" "github.com/mitchellh/mapstructure" -) -var vld = validator.New() + "github.com/apache/incubator-devlake/plugins/core" +) /* POST /plugins/gitee/test */ func TestConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) { - // decode - var err error var connection models.TestConnectionRequest - err = mapstructure.Decode(input.Body, &connection) + err := mapstructure.Decode(input.Body, &connection) if err != nil { return nil, err } - // validate err = vld.Struct(connection) if err != nil { return nil, err @@ -61,8 +55,9 @@ func TestConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, erro if err != nil { return nil, err } - query := make(url.Values) - query["access_token"] = []string{connection.Auth} + + query := url.Values{} + query.Set("access_token", connection.Token) res, err := apiClient.Get("user", query, nil) if err != nil { @@ -81,68 +76,63 @@ func TestConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, erro } /* -PATCH /plugins/gitee/connections/:connectionId +POST /plugins/gitee/connections */ -func PatchConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) { - v := config.GetConfig() +func PostConnections(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) { connection := &models.GiteeConnection{} - err := helper.EncodeStruct(v, connection, "env") + err := connectionHelper.Create(connection, input) if err != nil { return nil, err } - // update from request and save to .env - err = helper.DecodeStruct(v, connection, input.Body, "env") + return &core.ApiResourceOutput{Body: connection, Status: http.StatusOK}, nil +} + +/* +PATCH /plugins/gitee/connections/:connectionId +*/ +func PatchConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) { + connection := &models.GiteeConnection{} + err := connectionHelper.Patch(connection, input) if err != nil { return nil, err } - err = config.WriteConfig(v) + return &core.ApiResourceOutput{Body: connection, Status: http.StatusOK}, nil +} + +/* +DELETE /plugins/gitee/connections/:connectionId +*/ +func DeleteConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) { + connection := &models.GiteeConnection{} + err := connectionHelper.First(connection, input.Params) if err != nil { return nil, err } - response := models.GiteeResponse{ - GiteeConnection: *connection, - Name: "Gitee", - ID: 1, - } - return &core.ApiResourceOutput{Body: response, Status: http.StatusOK}, nil + err = connectionHelper.Delete(connection) + return &core.ApiResourceOutput{Body: connection}, err } /* GET /plugins/gitee/connections */ func ListConnections(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) { - // RETURN ONLY 1 SOURCE (FROM ENV) until multi-connection is developed. - v := config.GetConfig() - connection := &models.GiteeConnection{} - - err := helper.EncodeStruct(v, connection, "env") + var connections []models.GiteeConnection + err := connectionHelper.List(&connections) if err != nil { return nil, err } - response := models.GiteeResponse{ - GiteeConnection: *connection, - Name: "Gitee", - ID: 1, - } - return &core.ApiResourceOutput{Body: []models.GiteeResponse{response}}, nil + return &core.ApiResourceOutput{Body: connections}, nil } /* GET /plugins/gitee/connections/:connectionId */ func GetConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) { - // RETURN ONLY 1 SOURCE FROM ENV (Ignore ID until multi-connection is developed.) - v := config.GetConfig() connection := &models.GiteeConnection{} - err := helper.EncodeStruct(v, connection, "env") + err := connectionHelper.First(connection, input.Params) if err != nil { return nil, err } - response := &models.GiteeResponse{ - GiteeConnection: *connection, - Name: "Gitee", - ID: 1, - } - return &core.ApiResourceOutput{Body: response}, nil + return &core.ApiResourceOutput{Body: connection}, err } diff --git a/plugins/gitee/api/init.go b/plugins/gitee/api/init.go new file mode 100644 index 00000000..aef88dcb --- /dev/null +++ b/plugins/gitee/api/init.go @@ -0,0 +1,42 @@ +/* +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 ( + "github.com/apache/incubator-devlake/plugins/core" + "github.com/apache/incubator-devlake/plugins/github/models" + "github.com/apache/incubator-devlake/plugins/helper" + "github.com/go-playground/validator/v10" + "github.com/spf13/viper" + "gorm.io/gorm" +) + +type ApiUserPublicEmailResponse []models.PublicEmail + +var vld *validator.Validate +var connectionHelper *helper.ConnectionApiHelper +var basicRes core.BasicRes + +func Init(config *viper.Viper, logger core.Logger, database *gorm.DB) { + basicRes = helper.NewDefaultBasicRes(config, logger, database) + vld = validator.New() + connectionHelper = helper.NewConnectionHelper( + basicRes, + vld, + ) +} diff --git a/plugins/gitee/models/connection.go b/plugins/gitee/models/connection.go index 24ad1b6d..f161df73 100644 --- a/plugins/gitee/models/connection.go +++ b/plugins/gitee/models/connection.go @@ -17,30 +17,28 @@ limitations under the License. package models -// This object conforms to what the frontend currently sends. +import "github.com/apache/incubator-devlake/plugins/helper" + type GiteeConnection struct { - Endpoint string `mapstructure:"endpoint" validate:"required" env:"GITEE_ENDPOINT" json:"endpoint"` - Auth string `mapstructure:"auth" validate:"required" env:"GITEE_AUTH" json:"auth"` - Proxy string `mapstructure:"proxy" env:"GITEE_PROXY" json:"proxy"` + helper.RestConnection `mapstructure:",squash"` + helper.AccessToken `mapstructure:",squash"` } -// This object conforms to what the frontend currently expects. type GiteeResponse struct { Name string `json:"name"` ID int `json:"id"` GiteeConnection } -// Using User because it requires authentication. type ApiUserResponse struct { Id int Name string `json:"name"` } type TestConnectionRequest struct { - Endpoint string `json:"endpoint" validate:"required"` - Auth string `json:"auth" validate:"required"` - Proxy string `json:"proxy"` + Endpoint string `json:"endpoint" validate:"required"` + Proxy string `json:"proxy"` + helper.AccessToken `mapstructure:",squash"` } type Config struct { @@ -53,12 +51,3 @@ type Config struct { IssueTypeIncident string `mapstructure:"issueTypeIncident" env:"GITEE_ISSUE_TYPE_INCIDENT" json:"issueTypeIncident"` IssueTypeRequirement string `mapstructure:"issueTypeRequirement" env:"GITEE_ISSUE_TYPE_REQUIREMENT" json:"issueTypeRequirement"` } - -// Using Public Email because it requires authentication, and it is public information anyway. -// We're not using email information for anything here. -type PublicEmail struct { - Email string - Primary bool - Verified bool - Visibility string -}
