This is an automated email from the ASF dual-hosted git repository.

zhangliang2022 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new 8bcacd8ee refactor: simplify pagerduty test onnection (#4321)
8bcacd8ee is described below

commit 8bcacd8eeefe4c88d0a0e5135bd026feac94a5b3
Author: Klesh Wong <[email protected]>
AuthorDate: Tue Feb 7 11:55:19 2023 +0800

    refactor: simplify pagerduty test onnection (#4321)
---
 backend/plugins/pagerduty/api/connection.go        | 22 +++++----------
 backend/plugins/pagerduty/models/connection.go     | 29 ++++++++++++++------
 ...h.go => 20230203_add_endpoint_proxy_to_conn.go} | 31 +++++++++++++---------
 .../pagerduty/models/migrationscripts/register.go  |  2 +-
 4 files changed, 46 insertions(+), 38 deletions(-)

diff --git a/backend/plugins/pagerduty/api/connection.go 
b/backend/plugins/pagerduty/api/connection.go
index 5a35a8f31..569596127 100644
--- a/backend/plugins/pagerduty/api/connection.go
+++ b/backend/plugins/pagerduty/api/connection.go
@@ -19,39 +19,29 @@ package api
 
 import (
        "context"
-       "fmt"
+       "net/http"
+
        "github.com/apache/incubator-devlake/core/errors"
        "github.com/apache/incubator-devlake/core/plugin"
        "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
        "github.com/apache/incubator-devlake/plugins/pagerduty/models"
-       "net/http"
-       "time"
 )
 
 // @Summary test pagerduty connection
 // @Description Test Pagerduty Connection
 // @Tags plugins/pagerduty
-// @Param body body models.TestConnectionRequest true "json body"
+// @Param body body models.PagerDutyConn true "json body"
 // @Success 200  {object} shared.ApiBody "Success"
 // @Failure 400  {string} errcode.Error "Bad Request"
 // @Failure 500  {string} errcode.Error "Internal Error"
 // @Router /plugins/pagerduty/test [POST]
 func TestConnection(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput, errors.Error) {
-       var params models.TestConnectionRequest
-       err := api.Decode(input.Body, &params, vld)
+       var connection models.PagerDutyConn
+       err := api.Decode(input.Body, &connection, vld)
        if err != nil {
                return nil, err
        }
-       apiClient, err := api.NewApiClient(
-               context.TODO(),
-               params.Endpoint,
-               map[string]string{
-                       "Authorization": fmt.Sprintf("Token token=%s", 
params.Token),
-               },
-               3*time.Second,
-               params.Proxy,
-               basicRes,
-       )
+       apiClient, err := api.NewApiClientFromConnection(context.TODO(), 
basicRes, &connection)
        if err != nil {
                return nil, err
        }
diff --git a/backend/plugins/pagerduty/models/connection.go 
b/backend/plugins/pagerduty/models/connection.go
index 9b3b8ec94..3d18839f1 100644
--- a/backend/plugins/pagerduty/models/connection.go
+++ b/backend/plugins/pagerduty/models/connection.go
@@ -18,19 +18,32 @@ limitations under the License.
 package models
 
 import (
+       "fmt"
+       "net/http"
+
+       "github.com/apache/incubator-devlake/core/errors"
        helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
 )
 
-// This object conforms to what the frontend currently sends.
-type PagerDutyConnection struct {
-       helper.BaseConnection `mapstructure:",squash"`
-       helper.AccessToken    `mapstructure:",squash"`
+// AccessToken implements HTTP Token Authentication with Access Token
+type PagerDutyAccessToken helper.AccessToken
+
+// SetupAuthentication sets up the request headers for authentication
+func (at *PagerDutyAccessToken) SetupAuthentication(request *http.Request) 
errors.Error {
+       request.Header.Set("Authorization", fmt.Sprintf("Token token=%s", 
at.Token))
+       return nil
 }
 
-type TestConnectionRequest struct {
-       Endpoint string `json:"endpoint" validate:"required,url"`
-       Token    string `json:"token" validate:"required"`
-       Proxy    string `json:"proxy"`
+// PagerDutyConn holds the essential information to connect to the PagerDuty 
API
+type PagerDutyConn struct {
+       helper.RestConnection `mapstructure:",squash"`
+       PagerDutyAccessToken  `mapstructure:",squash"`
+}
+
+// PagerDutyConnection holds GitlabConn plus ID/Name for database storage
+type PagerDutyConnection struct {
+       helper.BaseConnection `mapstructure:",squash"`
+       PagerDutyConn         `mapstructure:",squash"`
 }
 
 // This object conforms to what the frontend currently expects.
diff --git 
a/backend/plugins/pagerduty/models/migrationscripts/20230103_increase_field_length.go
 
b/backend/plugins/pagerduty/models/migrationscripts/20230203_add_endpoint_proxy_to_conn.go
similarity index 59%
rename from 
backend/plugins/pagerduty/models/migrationscripts/20230103_increase_field_length.go
rename to 
backend/plugins/pagerduty/models/migrationscripts/20230203_add_endpoint_proxy_to_conn.go
index 48c0df94a..262b0bf09 100644
--- 
a/backend/plugins/pagerduty/models/migrationscripts/20230103_increase_field_length.go
+++ 
b/backend/plugins/pagerduty/models/migrationscripts/20230203_add_endpoint_proxy_to_conn.go
@@ -21,25 +21,30 @@ import (
        "github.com/apache/incubator-devlake/core/context"
        "github.com/apache/incubator-devlake/core/errors"
        "github.com/apache/incubator-devlake/helpers/migrationhelper"
-       
"github.com/apache/incubator-devlake/plugins/tapd/models/migrationscripts/archived"
 )
 
-type increaseFieldLength struct{}
+type connection20230203 struct {
+       Endpoint         string `mapstructure:"endpoint" validate:"required" 
json:"endpoint"`
+       Proxy            string `mapstructure:"proxy" json:"proxy"`
+       RateLimitPerHour int    `comment:"api request rate limit per hour" 
json:"rateLimit"`
+}
+
+func (*connection20230203) TableName() string {
+       return "_tool_pagerduty_connections"
+}
+
+type addEndpointAndProxyToConnection struct{}
 
-func (*increaseFieldLength) Up(basicRes context.BasicRes) errors.Error {
+func (*addEndpointAndProxyToConnection) Up(basicRes context.BasicRes) 
errors.Error {
        return migrationhelper.AutoMigrateTables(basicRes,
-               &archived.TapdBug{},
-               &archived.TapdBugCustomFields{},
-               &archived.TapdStory{},
-               &archived.TapdStoryCustomFields{},
-               &archived.TapdTask{},
-               &archived.TapdTaskCustomFields{})
+               &connection20230203{},
+       )
 }
 
-func (*increaseFieldLength) Version() uint64 {
-       return 20230103201138
+func (*addEndpointAndProxyToConnection) Version() uint64 {
+       return 20230203201814
 }
 
-func (*increaseFieldLength) Name() string {
-       return "Increase field length"
+func (*addEndpointAndProxyToConnection) Name() string {
+       return "add endpoint/proxy to pagerduty connection"
 }
diff --git a/backend/plugins/pagerduty/models/migrationscripts/register.go 
b/backend/plugins/pagerduty/models/migrationscripts/register.go
index 79d2b2175..1a0e72d14 100644
--- a/backend/plugins/pagerduty/models/migrationscripts/register.go
+++ b/backend/plugins/pagerduty/models/migrationscripts/register.go
@@ -25,6 +25,6 @@ import (
 func All() []plugin.MigrationScript {
        return []plugin.MigrationScript{
                new(addInitTables),
-               new(increaseFieldLength),
+               new(addEndpointAndProxyToConnection),
        }
 }

Reply via email to