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

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

commit 3b200b39bd4d6e0d45e8f8313ecd4ef3cce9b417
Author: zhangliang <[email protected]>
AuthorDate: Tue Aug 15 20:39:04 2023 +0800

    fix: zentao issue url
---
 backend/plugins/zentao/tasks/bug_convertor.go   |  2 +-
 backend/plugins/zentao/tasks/shared.go          | 16 +++++-
 backend/plugins/zentao/tasks/shared_test.go     | 71 +++++++++++++++++++++++++
 backend/plugins/zentao/tasks/story_convertor.go |  2 +-
 backend/plugins/zentao/tasks/task_convertor.go  |  2 +-
 5 files changed, 88 insertions(+), 5 deletions(-)

diff --git a/backend/plugins/zentao/tasks/bug_convertor.go 
b/backend/plugins/zentao/tasks/bug_convertor.go
index 540903154..eca844a85 100644
--- a/backend/plugins/zentao/tasks/bug_convertor.go
+++ b/backend/plugins/zentao/tasks/bug_convertor.go
@@ -88,7 +88,7 @@ func ConvertBug(taskCtx plugin.SubTaskContext) errors.Error {
                                CreatorName:     toolEntity.OpenedByName,
                                AssigneeName:    toolEntity.AssignedToName,
                                Severity:        
string(rune(toolEntity.Severity)),
-                               Url:             toolEntity.Url,
+                               Url:             
convertIssueURL(toolEntity.Url, "bug", toolEntity.ID),
                                OriginalProject: getOriginalProject(data),
                                Status:          toolEntity.StdStatus,
                        }
diff --git a/backend/plugins/zentao/tasks/shared.go 
b/backend/plugins/zentao/tasks/shared.go
index 08e127d7a..c4fd10d49 100644
--- a/backend/plugins/zentao/tasks/shared.go
+++ b/backend/plugins/zentao/tasks/shared.go
@@ -19,14 +19,15 @@ package tasks
 
 import (
        "fmt"
-       "github.com/apache/incubator-devlake/core/dal"
-       "github.com/apache/incubator-devlake/core/plugin"
        "net/http"
        "net/url"
+       "path"
        "reflect"
        "strings"
 
+       "github.com/apache/incubator-devlake/core/dal"
        "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/zentao/models"
 )
@@ -293,3 +294,14 @@ func (a *AccountCache) 
getAccountNameFromApiAccount(account *models.ApiAccount)
        }
        return a.getAccountName(account.Account)
 }
+
+func convertIssueURL(apiURL, issueType string, id int64) string {
+       u, err := url.Parse(apiURL)
+       if err != nil {
+               return apiURL
+       }
+       before, _, _ := strings.Cut(u.Path, "/api.php/v1")
+       u.RawQuery = ""
+       u.Path = path.Join(before, fmt.Sprintf("/%s-view-%d.html", issueType, 
id))
+       return u.String()
+}
diff --git a/backend/plugins/zentao/tasks/shared_test.go 
b/backend/plugins/zentao/tasks/shared_test.go
new file mode 100644
index 000000000..bd9aacc85
--- /dev/null
+++ b/backend/plugins/zentao/tasks/shared_test.go
@@ -0,0 +1,71 @@
+/*
+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 tasks
+
+import "testing"
+
+func Test_convertIssueURL(t *testing.T) {
+       type args struct {
+               apiURL    string
+               issueType string
+               id        int64
+       }
+       tests := []struct {
+               name string
+               args args
+               want string
+       }{
+               {
+                       "bug",
+                       
args{"http://example.com/api.php/v1/products/16/bugs?limit=100&page=1";, "bug", 
1169},
+                       "http://example.com/bug-view-1169.html";,
+               },
+               {
+                       "bug with prefix",
+                       
args{"http://example.com/prefix1/prefix2/api.php/v1/products/16/bugs?limit=100&page=1";,
 "bug", 1169},
+                       "http://example.com/prefix1/prefix2/bug-view-1169.html";,
+               },
+               {
+                       "story",
+                       
args{"http://example.com/api.php/v1/executions/41/stories?limit=100&page=1&status=allstory";,
 "story", 4513},
+                       "http://example.com/story-view-4513.html";,
+               },
+               {
+                       "story with prefix",
+                       
args{"http://example.com/prefix1/prefix2/api.php/v1/executions/41/stories?limit=100&page=1&status=allstory";,
 "story", 4513},
+                       
"http://example.com/prefix1/prefix2/story-view-4513.html";,
+               },
+               {
+                       "task",
+                       
args{"http://example.com/api.php/v1/executions/39/tasks?limit=100&page=1";, 
"task", 2381},
+                       "http://example.com/task-view-2381.html";,
+               },
+               {
+                       "task with prefix",
+                       
args{"http://example.com/prefix1/prefix2/api.php/v1/executions/41/stories?limit=100&page=1&status=allstory";,
 "task", 2381},
+                       
"http://example.com/prefix1/prefix2/task-view-2381.html";,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       if got := convertIssueURL(tt.args.apiURL, 
tt.args.issueType, tt.args.id); got != tt.want {
+                               t.Errorf("convertIssueURL() = %v, want %v", 
got, tt.want)
+                       }
+               })
+       }
+}
diff --git a/backend/plugins/zentao/tasks/story_convertor.go 
b/backend/plugins/zentao/tasks/story_convertor.go
index cf66a49a7..c6a12bdf6 100644
--- a/backend/plugins/zentao/tasks/story_convertor.go
+++ b/backend/plugins/zentao/tasks/story_convertor.go
@@ -86,7 +86,7 @@ func ConvertStory(taskCtx plugin.SubTaskContext) errors.Error 
{
                                Priority:                
getPriority(toolEntity.Pri),
                                CreatorName:             
toolEntity.OpenedByName,
                                AssigneeName:            
toolEntity.AssignedToName,
-                               Url:                     toolEntity.Url,
+                               Url:                     
convertIssueURL(toolEntity.Url, "story", toolEntity.ID),
                                OriginalProject:         
getOriginalProject(data),
                                Status:                  toolEntity.StdStatus,
                                OriginalEstimateMinutes: 
int64(toolEntity.Estimate) * 60,
diff --git a/backend/plugins/zentao/tasks/task_convertor.go 
b/backend/plugins/zentao/tasks/task_convertor.go
index d671abdba..d49cfe153 100644
--- a/backend/plugins/zentao/tasks/task_convertor.go
+++ b/backend/plugins/zentao/tasks/task_convertor.go
@@ -85,7 +85,7 @@ func ConvertTask(taskCtx plugin.SubTaskContext) errors.Error {
                                Priority:                
getPriority(toolEntity.Pri),
                                CreatorName:             
toolEntity.OpenedByName,
                                AssigneeName:            
toolEntity.AssignedToName,
-                               Url:                     toolEntity.Url,
+                               Url:                     
convertIssueURL(toolEntity.Url, "task", toolEntity.ID),
                                OriginalProject:         
getOriginalProject(data),
                                Status:                  toolEntity.StdStatus,
                                OriginalEstimateMinutes: 
int64(toolEntity.Estimate) * 60,

Reply via email to