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

lynwee 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 a2464ce93 Lw fix webhook 1024 (#6317)
a2464ce93 is described below

commit a2464ce930cb9e04409d6de8dfa55d5f85e6de4f
Author: Lynwee <[email protected]>
AuthorDate: Tue Oct 24 17:25:00 2023 +0800

    Lw fix webhook 1024 (#6317)
    
    * fix(webhook): remove duplicated database locks
    
    * fix(webhook): user can post issues via webhook now
---
 backend/helpers/pluginhelper/api/mapstructure.go | 41 ++++++++++++++++++++----
 backend/plugins/webhook/api/deployments.go       |  2 +-
 backend/plugins/webhook/api/incident.go          |  1 -
 3 files changed, 36 insertions(+), 8 deletions(-)

diff --git a/backend/helpers/pluginhelper/api/mapstructure.go 
b/backend/helpers/pluginhelper/api/mapstructure.go
index 6f24659e6..ff8e84ee1 100644
--- a/backend/helpers/pluginhelper/api/mapstructure.go
+++ b/backend/helpers/pluginhelper/api/mapstructure.go
@@ -19,6 +19,7 @@ package api
 
 import (
        "encoding/json"
+       "github.com/spf13/cast"
        "reflect"
        "time"
 
@@ -30,7 +31,7 @@ import (
        "github.com/mitchellh/mapstructure"
 )
 
-var defaultCustomDecoderHooks = 
[]mapstructure.DecodeHookFunc{decodeHookStringFloat64, DecodeHook}
+var defaultCustomDecoderHooks = 
[]mapstructure.DecodeHookFunc{decodeHookStringFloat64, decodeHookStringToTime, 
DecodeHook}
 
 func decodeHookStringFloat64(f reflect.Type, t reflect.Type, data interface{}) 
(interface{}, error) {
        if f.Kind() == reflect.Float64 && t.Kind() == reflect.Struct && t == 
reflect.TypeOf(common.StringFloat64{}) {
@@ -42,6 +43,26 @@ func decodeHookStringFloat64(f reflect.Type, t reflect.Type, 
data interface{}) (
        return data, nil
 }
 
+func decodeHookStringToTime(f reflect.Type, t reflect.Type, data interface{}) 
(interface{}, error) {
+       if f.Kind() == reflect.String {
+               if t.Kind() == reflect.Struct && t == 
reflect.TypeOf(time.Time{}) {
+                       timeData, err := 
common.ConvertStringToTime(cast.ToString(data))
+                       if err != nil {
+                               return data, err
+                       }
+                       return timeData, nil
+               }
+               if t.Kind() == reflect.Ptr && t == reflect.TypeOf(&time.Time{}) 
{
+                       timeData, err := 
common.ConvertStringToTime(cast.ToString(data))
+                       if err != nil {
+                               return data, err
+                       }
+                       return &timeData, nil
+               }
+       }
+       return data, nil
+}
+
 func DecodeHook(f reflect.Type, t reflect.Type, data interface{}) 
(interface{}, error) {
        if data == nil {
                return nil, nil
@@ -49,14 +70,12 @@ func DecodeHook(f reflect.Type, t reflect.Type, data 
interface{}) (interface{},
        if t == reflect.TypeOf(json.RawMessage{}) {
                return json.Marshal(data)
        }
-
        if t != reflect.TypeOf(common.Iso8601Time{}) && t != 
reflect.TypeOf(time.Time{}) {
                return data, nil
        }
 
        var tt time.Time
        var err error
-
        switch f.Kind() {
        case reflect.String:
                tt, err = common.ConvertStringToTime(data.(string))
@@ -64,15 +83,25 @@ func DecodeHook(f reflect.Type, t reflect.Type, data 
interface{}) (interface{},
                tt = time.Unix(0, int64(data.(float64))*int64(time.Millisecond))
        case reflect.Int64:
                tt = time.Unix(0, data.(int64)*int64(time.Millisecond))
+       case reflect.Struct:
+               if f == reflect.TypeOf(time.Time{}) || f == 
reflect.TypeOf(common.Iso8601Time{}) {
+                       return data, nil
+               }
+       case reflect.Ptr:
+               if f == reflect.TypeOf(&time.Time{}) || f == 
reflect.TypeOf(&common.Iso8601Time{}) {
+                       return data, nil
+               }
        }
        if err != nil {
                return data, nil
        }
-
-       if t == reflect.TypeOf(common.Iso8601Time{}) {
+       switch t {
+       case reflect.TypeOf(common.Iso8601Time{}):
                return common.Iso8601Time{Time: tt}, nil
+       case reflect.TypeOf(&common.Iso8601Time{}):
+               return &common.Iso8601Time{Time: tt}, nil
        }
-       return tt, nil
+       return data, nil
 }
 
 // DecodeMapStruct with time.Time and Iso8601Time support
diff --git a/backend/plugins/webhook/api/deployments.go 
b/backend/plugins/webhook/api/deployments.go
index 1c074578e..67752c72c 100644
--- a/backend/plugins/webhook/api/deployments.go
+++ b/backend/plugins/webhook/api/deployments.go
@@ -83,8 +83,8 @@ func PostDeploymentCicdTask(input *plugin.ApiResourceInput) 
(*plugin.ApiResource
                return nil, errors.BadInput.Wrap(vld.Struct(request), `input 
json error`)
        }
        txHelper := dbhelper.NewTxHelper(basicRes, &err)
-       tx := txHelper.Begin()
        defer txHelper.End()
+       tx := txHelper.Begin()
        urlHash16 := fmt.Sprintf("%x", md5.Sum([]byte(request.RepoUrl)))[:16]
        scopeId := fmt.Sprintf("%s:%d", "webhook", connection.ID)
        deploymentCommitId := fmt.Sprintf("%s:%d:%s:%s", "webhook", 
connection.ID, urlHash16, request.CommitSha)
diff --git a/backend/plugins/webhook/api/incident.go 
b/backend/plugins/webhook/api/incident.go
index 5367c5713..dfcabe56c 100644
--- a/backend/plugins/webhook/api/incident.go
+++ b/backend/plugins/webhook/api/incident.go
@@ -89,7 +89,6 @@ func PostIssue(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput, error
        if err != nil {
                return &plugin.ApiResourceOutput{Body: err.Error(), Status: 
http.StatusBadRequest}, nil
        }
-
        db := basicRes.GetDal()
        domainIssue := &ticket.Issue{
                DomainEntity: domainlayer.DomainEntity{

Reply via email to