You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by ab...@apache.org on 2023/06/14 15:10:27 UTC

[incubator-devlake] branch main updated: feat: add owner to the remote API (#5488)

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

abeizn 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 ae4f2e52c feat: add owner to the remote API (#5488)
ae4f2e52c is described below

commit ae4f2e52c66d602222add83ac22def3962d8ff73
Author: Liang Zhang <li...@merico.dev>
AuthorDate: Wed Jun 14 23:09:22 2023 +0800

    feat: add owner to the remote API (#5488)
---
 backend/plugins/github/api/blueprint_V200_test.go |  3 +-
 backend/plugins/github/api/blueprint_v200.go      | 10 ++---
 backend/plugins/github/api/init.go                |  7 +--
 backend/plugins/github/api/remote.go              | 52 +++++++++++++++++------
 backend/plugins/github/impl/impl.go               |  7 +--
 5 files changed, 53 insertions(+), 26 deletions(-)

diff --git a/backend/plugins/github/api/blueprint_V200_test.go b/backend/plugins/github/api/blueprint_V200_test.go
index 4113f0622..2bbfe7a8c 100644
--- a/backend/plugins/github/api/blueprint_V200_test.go
+++ b/backend/plugins/github/api/blueprint_V200_test.go
@@ -135,7 +135,8 @@ func mockBasicRes() {
 	testGithubRepo := &models.GithubRepo{
 		ConnectionId:  1,
 		GithubId:      12345,
-		Name:          "test/testRepo",
+		Name:          "testRepo",
+		FullName:      "test/testRepo",
 		CloneUrl:      "https://this_is_cloneUrl",
 		ScopeConfigId: 1,
 	}
diff --git a/backend/plugins/github/api/blueprint_v200.go b/backend/plugins/github/api/blueprint_v200.go
index c788e4b7e..19de30132 100644
--- a/backend/plugins/github/api/blueprint_v200.go
+++ b/backend/plugins/github/api/blueprint_v200.go
@@ -109,7 +109,7 @@ func makeDataSourcePipelinePlanV200(
 		op := &tasks.GithubOptions{
 			ConnectionId: githubRepo.ConnectionId,
 			GithubId:     githubRepo.GithubId,
-			Name:         githubRepo.Name,
+			Name:         githubRepo.FullName,
 		}
 		if syncPolicy.TimeAfter != nil {
 			op.TimeAfter = syncPolicy.TimeAfter.Format(time.RFC3339)
@@ -135,7 +135,7 @@ func makeDataSourcePipelinePlanV200(
 				Plugin: "gitextractor",
 				Options: map[string]interface{}{
 					"url":    cloneUrl.String(),
-					"name":   githubRepo.Name,
+					"name":   githubRepo.FullName,
 					"repoId": didgen.NewDomainIdGenerator(&models.GithubRepo{}).Generate(connection.ID, githubRepo.GithubId),
 					"proxy":  connection.Proxy,
 				},
@@ -173,7 +173,7 @@ func makeScopesV200(bpScopes []*plugin.BlueprintScopeV200, connection *models.Gi
 				DomainEntity: domainlayer.DomainEntity{
 					Id: didgen.NewDomainIdGenerator(&models.GithubRepo{}).Generate(connection.ID, githubRepo.GithubId),
 				},
-				Name: githubRepo.Name,
+				Name: githubRepo.FullName,
 			}
 			if githubRepo.ParentHTMLUrl != "" {
 				scopeRepo.ForkedFrom = githubRepo.ParentHTMLUrl
@@ -186,7 +186,7 @@ func makeScopesV200(bpScopes []*plugin.BlueprintScopeV200, connection *models.Gi
 				DomainEntity: domainlayer.DomainEntity{
 					Id: didgen.NewDomainIdGenerator(&models.GithubRepo{}).Generate(connection.ID, githubRepo.GithubId),
 				},
-				Name: githubRepo.Name,
+				Name: githubRepo.FullName,
 			}
 			scopes = append(scopes, scopeCICD)
 		}
@@ -196,7 +196,7 @@ func makeScopesV200(bpScopes []*plugin.BlueprintScopeV200, connection *models.Gi
 				DomainEntity: domainlayer.DomainEntity{
 					Id: didgen.NewDomainIdGenerator(&models.GithubRepo{}).Generate(connection.ID, githubRepo.GithubId),
 				},
-				Name: githubRepo.Name,
+				Name: githubRepo.FullName,
 			}
 			scopes = append(scopes, scopeTicket)
 		}
diff --git a/backend/plugins/github/api/init.go b/backend/plugins/github/api/init.go
index 4201779c9..0a45a3568 100644
--- a/backend/plugins/github/api/init.go
+++ b/backend/plugins/github/api/init.go
@@ -18,6 +18,7 @@ limitations under the License.
 package api
 
 import (
+	"github.com/apache/incubator-devlake/core/plugin"
 	"strconv"
 
 	"github.com/apache/incubator-devlake/core/context"
@@ -33,7 +34,7 @@ var connectionHelper *api.ConnectionApiHelper
 var scopeHelper *api.ScopeApiHelper[models.GithubConnection, models.GithubRepo, models.GithubScopeConfig]
 var basicRes context.BasicRes
 var scHelper *api.ScopeConfigHelper[models.GithubScopeConfig]
-var remoteHelper *api.RemoteApiHelper[models.GithubConnection, models.GithubRepo, repo, org]
+var remoteHelper *api.RemoteApiHelper[models.GithubConnection, models.GithubRepo, repo, plugin.ApiGroup]
 
 func Init(br context.BasicRes) {
 	basicRes = br
@@ -67,7 +68,7 @@ func Init(br context.BasicRes) {
 				if err != nil {
 					return "", err
 				}
-				return repo.Name, nil
+				return repo.FullName, nil
 			},
 		},
 	)
@@ -75,7 +76,7 @@ func Init(br context.BasicRes) {
 		basicRes,
 		vld,
 	)
-	remoteHelper = api.NewRemoteHelper[models.GithubConnection, models.GithubRepo, repo, org](
+	remoteHelper = api.NewRemoteHelper[models.GithubConnection, models.GithubRepo, repo, plugin.ApiGroup](
 		basicRes,
 		vld,
 		connectionHelper,
diff --git a/backend/plugins/github/api/remote.go b/backend/plugins/github/api/remote.go
index 5a6fbbf47..961b19d39 100644
--- a/backend/plugins/github/api/remote.go
+++ b/backend/plugins/github/api/remote.go
@@ -33,18 +33,8 @@ import (
 )
 
 type org struct {
-	Login            string `json:"login"`
-	ID               int    `json:"id"`
-	NodeID           string `json:"node_id"`
-	URL              string `json:"url"`
-	ReposURL         string `json:"repos_url"`
-	EventsURL        string `json:"events_url"`
-	HooksURL         string `json:"hooks_url"`
-	IssuesURL        string `json:"issues_url"`
-	MembersURL       string `json:"members_url"`
-	PublicMembersURL string `json:"public_members_url"`
-	AvatarURL        string `json:"avatar_url"`
-	Description      string `json:"description"`
+	Login string `json:"login"`
+	ID    int    `json:"id"`
 }
 
 func (o org) GroupId() string {
@@ -55,6 +45,19 @@ func (o org) GroupName() string {
 	return o.Login
 }
 
+type owner struct {
+	Login string `json:"login"`
+	ID    int    `json:"id"`
+}
+
+func (o owner) GroupId() string {
+	return o.Login
+}
+
+func (o owner) GroupName() string {
+	return o.Login
+}
+
 type repo struct {
 	ID       int    `json:"id"`
 	NodeID   string `json:"node_id"`
@@ -194,7 +197,7 @@ func (r repo) ConvertApiScope() plugin.ToolLayerScope {
 // @Router /plugins/github/connections/{connectionId}/remote-scopes [GET]
 func RemoteScopes(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
 	return remoteHelper.GetScopesFromRemote(input,
-		func(basicRes context2.BasicRes, gid string, queryData *api.RemoteQueryData, connection models.GithubConnection) ([]org, errors.Error) {
+		func(basicRes context2.BasicRes, gid string, queryData *api.RemoteQueryData, connection models.GithubConnection) ([]plugin.ApiGroup, errors.Error) {
 			if gid != "" {
 				return nil, nil
 			}
@@ -212,7 +215,21 @@ func RemoteScopes(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, er
 			if err != nil {
 				return nil, err
 			}
-			return resBody, err
+			res, err = apiClient.Get("user", nil, nil)
+			if err != nil {
+				return nil, err
+			}
+			var o owner
+			err = api.UnmarshalResponse(res, &o)
+			if err != nil {
+				return nil, err
+			}
+			result := make([]plugin.ApiGroup, 0, len(resBody)+1)
+			for _, v := range resBody {
+				result = append(result, v)
+			}
+			result = append(result, o)
+			return result, err
 		},
 		func(basicRes context2.BasicRes, gid string, queryData *api.RemoteQueryData, connection models.GithubConnection) ([]repo, errors.Error) {
 			if gid == "" {
@@ -228,6 +245,13 @@ func RemoteScopes(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, er
 			if err != nil {
 				return nil, err
 			}
+			// if not found, try to get user repos
+			if res.StatusCode == http.StatusNotFound {
+				res, err = apiClient.Get(fmt.Sprintf("users/%s/repos", gid), query, nil)
+				if err != nil {
+					return nil, err
+				}
+			}
 			var resBody []repo
 			err = api.UnmarshalResponse(res, &resBody)
 			if err != nil {
diff --git a/backend/plugins/github/impl/impl.go b/backend/plugins/github/impl/impl.go
index 49b99a05a..3b7dbfe2d 100644
--- a/backend/plugins/github/impl/impl.go
+++ b/backend/plugins/github/impl/impl.go
@@ -271,10 +271,10 @@ func EnrichOptions(taskCtx plugin.TaskContext,
 	logger := taskCtx.GetLogger()
 	// for advanced mode or others which we only have name, for bp v200, we have githubId
 	err = taskCtx.GetDal().First(&githubRepo, dal.Where(
-		"connection_id = ? AND( name = ? OR github_id = ?)",
+		"connection_id = ? AND( full_name = ? OR github_id = ?)",
 		op.ConnectionId, op.Name, op.GithubId))
 	if err == nil {
-		op.Name = githubRepo.Name
+		op.Name = githubRepo.FullName
 		op.GithubId = githubRepo.GithubId
 		if op.ScopeConfigId == 0 {
 			op.ScopeConfigId = githubRepo.ScopeConfigId
@@ -321,7 +321,8 @@ func convertApiRepoToScope(repo *tasks.GithubApiRepo, connectionId uint64) *mode
 	scope.Language = repo.Language
 	scope.Description = repo.Description
 	scope.HTMLUrl = repo.HTMLUrl
-	scope.Name = repo.FullName
+	scope.Name = repo.Name
+	scope.FullName = repo.FullName
 	scope.CloneUrl = repo.CloneUrl
 	return &scope
 }