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

ka94 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 d0306be8c Improve test connection for Python plugins (#4695)
d0306be8c is described below

commit d0306be8cb21a8ea099626e93794724e1ee88ba8
Author: Camille Teruel <[email protected]>
AuthorDate: Tue Mar 21 05:04:26 2023 +0100

    Improve test connection for Python plugins (#4695)
    
    * feat: Make connection name optional in test-connection since we are not 
saving the connection at that time there is no need to enforce that DB 
constraint.
    
    * feat: Add error message to test-connection response
    
    * fix: Make pydevlake dependency editable
    
    * fix: Fix azure remote-scopes
    
    * refactor: Rename pat to token
    
    * feat: Allow empty proxy in connection
    
    Allow to supply an empty str as proxy in connections.
    More constistent with other plugins.
    
    ---------
    
    Co-authored-by: Camille Teruel <[email protected]>
---
 .../python/plugins/azuredevops/azuredevops/api.py    |  4 ++--
 .../python/plugins/azuredevops/azuredevops/main.py   | 20 +++++++++++---------
 .../python/plugins/azuredevops/azuredevops/models.py |  2 +-
 backend/python/plugins/azuredevops/pyproject.toml    |  2 +-
 backend/python/pydevlake/pydevlake/ipc.py            |  2 ++
 backend/python/pydevlake/pydevlake/model.py          |  8 +++++++-
 .../server/services/remote/plugin/connection_api.go  | 11 +++++++++--
 7 files changed, 33 insertions(+), 16 deletions(-)

diff --git a/backend/python/plugins/azuredevops/azuredevops/api.py 
b/backend/python/plugins/azuredevops/azuredevops/api.py
index 352eb3955..7bf40f79a 100644
--- a/backend/python/plugins/azuredevops/azuredevops/api.py
+++ b/backend/python/plugins/azuredevops/azuredevops/api.py
@@ -38,8 +38,8 @@ class AzureDevOpsAPI(API):
 
     @request_hook
     def authenticate(self, request: Request):
-        pat_b64 = base64.b64encode((':' + 
self.connection.pat).encode()).decode()
-        request.headers['Authorization'] = 'Basic ' + pat_b64
+        token_b64 = base64.b64encode((':' + 
self.connection.token).encode()).decode()
+        request.headers['Authorization'] = 'Basic ' + token_b64
 
     @request_hook
     def set_api_version(self, request: Request):
diff --git a/backend/python/plugins/azuredevops/azuredevops/main.py 
b/backend/python/plugins/azuredevops/azuredevops/main.py
index 5b5bc3841..33595e4d9 100644
--- a/backend/python/plugins/azuredevops/azuredevops/main.py
+++ b/backend/python/plugins/azuredevops/azuredevops/main.py
@@ -50,21 +50,23 @@ class AzureDevOpsPlugin(Plugin):
             url=git_repo.url
         )
 
-    def remote_scope_groups(self, ctx) -> list[RemoteScopeGroup]:
-        api = AzureDevOpsAPI(ctx.connection)
-        member_id = api.my_profile.json['id']
+    def remote_scope_groups(self, connection) -> list[RemoteScopeGroup]:
+        api = AzureDevOpsAPI(connection)
+        member_id = api.my_profile().json['id']
         accounts = api.accounts(member_id).json
-        orgs = [acc['accountId'] for acc in accounts]
-        for org in orgs:
+        for account in accounts['value']:
+            org = account['accountName']
             for proj in api.projects(org):
+                proj_name = proj['name']
+
                 yield RemoteScopeGroup(
-                    id=f'{org}/{proj["name"]}',
-                    name=proj['name']
+                    id=f'{org}/{proj_name}',
+                    name=proj_name
                 )
 
-    def remote_scopes(self, ctx, group_id: str) -> list[GitRepository]:
+    def remote_scopes(self, connection, group_id: str) -> list[GitRepository]:
         org, proj = group_id.split('/')
-        api = AzureDevOpsAPI(ctx.connection)
+        api = AzureDevOpsAPI(connection)
         for raw_repo in api.git_repos(org, proj):
             repo = GitRepository(**raw_repo, project_id=proj, org_id=org)
             if not repo.defaultBranch:
diff --git a/backend/python/plugins/azuredevops/azuredevops/models.py 
b/backend/python/plugins/azuredevops/azuredevops/models.py
index a6f72376f..edac65071 100644
--- a/backend/python/plugins/azuredevops/azuredevops/models.py
+++ b/backend/python/plugins/azuredevops/azuredevops/models.py
@@ -26,7 +26,7 @@ default_date = datetime.datetime.fromisoformat("1970-01-01")
 
 
 class AzureDevOpsConnection(Connection):
-    pat: str
+    token: str
 
 
 class Project(ToolModel, table=True):
diff --git a/backend/python/plugins/azuredevops/pyproject.toml 
b/backend/python/plugins/azuredevops/pyproject.toml
index ad160b9b7..762acbf9e 100644
--- a/backend/python/plugins/azuredevops/pyproject.toml
+++ b/backend/python/plugins/azuredevops/pyproject.toml
@@ -22,7 +22,7 @@ readme = "README.md"
 
 [tool.poetry.dependencies]
 python = "^3.10"
-pydevlake = { path = "../../pydevlake", develop = false }
+pydevlake = { path = "../../pydevlake", develop = true }
 iso8601 = "^1.1.0"
 
 
diff --git a/backend/python/pydevlake/pydevlake/ipc.py 
b/backend/python/pydevlake/pydevlake/ipc.py
index 2e2a33598..ea5abf4e3 100644
--- a/backend/python/pydevlake/pydevlake/ipc.py
+++ b/backend/python/pydevlake/pydevlake/ipc.py
@@ -69,6 +69,8 @@ class PluginCommands:
     @plugin_method
     def test_connection(self, connection: dict):
         connection = self._parse(connection)
+        if "name" not in connection:
+            connection["name"] = "Test connection"
         connection = self._plugin.connection_type(**connection)
         self._plugin.test_connection(connection)
 
diff --git a/backend/python/pydevlake/pydevlake/model.py 
b/backend/python/pydevlake/pydevlake/model.py
index 158eaffd1..dd9b68cea 100644
--- a/backend/python/pydevlake/pydevlake/model.py
+++ b/backend/python/pydevlake/pydevlake/model.py
@@ -20,7 +20,7 @@ from inspect import getmodule
 from datetime import datetime
 
 import inflect
-from pydantic import AnyUrl
+from pydantic import AnyUrl, validator
 from sqlalchemy import Column, DateTime, func
 from sqlalchemy.orm import declared_attr
 from sqlalchemy.inspection import inspect
@@ -51,6 +51,12 @@ class Connection(ToolTable):
     name: str
     proxy: Optional[AnyUrl]
 
+    @validator('proxy', pre=True)
+    def allow_empty_proxy(cls, proxy):
+        if proxy == "":
+            return None
+        return proxy
+
 
 class TransformationRule(ToolTable):
     name: str
diff --git a/backend/server/services/remote/plugin/connection_api.go 
b/backend/server/services/remote/plugin/connection_api.go
index 7ad041302..1c324d292 100644
--- a/backend/server/services/remote/plugin/connection_api.go
+++ b/backend/server/services/remote/plugin/connection_api.go
@@ -22,15 +22,22 @@ import (
 
        "github.com/apache/incubator-devlake/core/errors"
        "github.com/apache/incubator-devlake/core/plugin"
+       "github.com/apache/incubator-devlake/server/api/shared"
        "github.com/apache/incubator-devlake/server/services/remote/bridge"
 )
 
 func (pa *pluginAPI) TestConnection(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput, errors.Error) {
        err := pa.invoker.Call("test-connection", bridge.DefaultContext, 
input.Body).Err
        if err != nil {
-               return &plugin.ApiResourceOutput{Body: false, Status: 401}, nil
+               body := shared.ApiBody{
+                       Success: false,
+                       Message: err.Error(),
+               }
+               return &plugin.ApiResourceOutput{Body: body, Status: 401}, nil
+       } else {
+               body := shared.ApiBody{Success: true}
+               return &plugin.ApiResourceOutput{Body: body, Status: 200}, nil
        }
-       return &plugin.ApiResourceOutput{Body: true, Status: 200}, nil
 }
 
 func (pa *pluginAPI) PostConnections(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput, errors.Error) {

Reply via email to