From a27f807d04b0eb46d374013cf644aaedf9403203 Mon Sep 17 00:00:00 2001 From: Michael Sverdlov Date: Sun, 30 Jul 2023 14:30:10 +0300 Subject: [PATCH] Fix missing Git owner issue on bitbucket and gitlab (#103) --- vcsclient/bitbucketserver.go | 27 ++++++++++++++----- vcsclient/bitbucketserver_test.go | 10 +++---- vcsclient/gitlab.go | 20 +++++++++----- vcsclient/gitlab_test.go | 8 +++--- .../pull_requests_list_response.json | 8 +++--- 5 files changed, 48 insertions(+), 25 deletions(-) diff --git a/vcsclient/bitbucketserver.go b/vcsclient/bitbucketserver.go index 8a6491d6..495bb9e2 100644 --- a/vcsclient/bitbucketserver.go +++ b/vcsclient/bitbucketserver.go @@ -350,15 +350,24 @@ func (client *BitbucketServerClient) getOpenPullRequests(ctx context.Context, ow body = pullRequest.Description } if pullRequest.Open { + var sourceOwner string + sourceOwner, err = getSourceRepositoryOwner(pullRequest) + if err != nil { + return nil, err + } results = append(results, PullRequestInfo{ ID: int64(pullRequest.ID), Body: body, Source: BranchInfo{ Name: pullRequest.FromRef.DisplayID, - Repository: pullRequest.FromRef.Repository.Slug}, + Repository: pullRequest.FromRef.Repository.Slug, + Owner: sourceOwner, + }, Target: BranchInfo{ Name: pullRequest.ToRef.DisplayID, - Repository: pullRequest.ToRef.Repository.Slug}, + Repository: pullRequest.ToRef.Repository.Slug, + Owner: owner, + }, }) } } @@ -378,12 +387,10 @@ func (client *BitbucketServerClient) GetPullRequestByID(ctx context.Context, own if err != nil { return } - project := pullRequest.FromRef.Repository.Project - if project == nil { - err = fmt.Errorf("failed to get source repository owner, project is nil") + sourceOwner, err := getSourceRepositoryOwner(pullRequest) + if err != nil { return } - sourceOwner := project.Key pullRequestInfo = PullRequestInfo{ ID: int64(pullRequest.ID), Source: BranchInfo{Name: pullRequest.FromRef.ID, Repository: pullRequest.ToRef.Repository.Slug, Owner: sourceOwner}, @@ -752,3 +759,11 @@ func getBitbucketServerRepositoryVisibility(public bool) RepositoryVisibility { } return Private } + +func getSourceRepositoryOwner(pullRequest bitbucketv1.PullRequest) (string, error) { + project := pullRequest.FromRef.Repository.Project + if project == nil { + return "", fmt.Errorf("failed to get source repository owner, project is nil. (PR - %s, repository - %s)", pullRequest.FromRef.DisplayID, pullRequest.FromRef.Repository.Slug) + } + return project.Key, nil +} diff --git a/vcsclient/bitbucketserver_test.go b/vcsclient/bitbucketserver_test.go index f778d865..36ddbcf2 100644 --- a/vcsclient/bitbucketserver_test.go +++ b/vcsclient/bitbucketserver_test.go @@ -218,13 +218,13 @@ func TestBitbucketServer_ListOpenPullRequests(t *testing.T) { defer cleanUp() result, err := client.ListOpenPullRequests(ctx, owner, repo1) - + forkedOwner := "jfrogForked" assert.NoError(t, err) assert.Len(t, result, 1) assert.EqualValues(t, PullRequestInfo{ ID: 101, - Source: BranchInfo{Name: "feature-ABC-123", Repository: "my-repo"}, - Target: BranchInfo{Name: "master", Repository: "my-repo"}, + Source: BranchInfo{Name: "feature-ABC-123", Repository: repo1, Owner: forkedOwner}, + Target: BranchInfo{Name: "master", Repository: repo1, Owner: owner}, }, result[0]) // With body: @@ -235,8 +235,8 @@ func TestBitbucketServer_ListOpenPullRequests(t *testing.T) { assert.EqualValues(t, PullRequestInfo{ ID: 101, Body: "hello world", - Source: BranchInfo{Name: "feature-ABC-123", Repository: "my-repo"}, - Target: BranchInfo{Name: "master", Repository: "my-repo"}, + Source: BranchInfo{Name: "feature-ABC-123", Repository: repo1, Owner: forkedOwner}, + Target: BranchInfo{Name: "master", Repository: repo1, Owner: owner}, }, result[0]) } diff --git a/vcsclient/gitlab.go b/vcsclient/gitlab.go index 6df287ab..69fe2b25 100644 --- a/vcsclient/gitlab.go +++ b/vcsclient/gitlab.go @@ -269,7 +269,7 @@ func (client *GitLabClient) getOpenPullRequests(ctx context.Context, owner, repo if err != nil { return []PullRequestInfo{}, err } - return mapGitLabMergeRequestToPullRequestInfoList(mergeRequests, withBody), nil + return mapGitLabMergeRequestToPullRequestInfoList(mergeRequests, owner, repository, withBody), nil } // GetPullRequestInfoById on GitLab @@ -588,17 +588,25 @@ func mapGitLabNotesToCommentInfoList(notes []*gitlab.Note) (res []CommentInfo) { return } -func mapGitLabMergeRequestToPullRequestInfoList(mergeRequests []*gitlab.MergeRequest, withBody bool) (res []PullRequestInfo) { +func mapGitLabMergeRequestToPullRequestInfoList(mergeRequests []*gitlab.MergeRequest, owner, repository string, withBody bool) (res []PullRequestInfo) { for _, mergeRequest := range mergeRequests { var body string if withBody { body = mergeRequest.Description } res = append(res, PullRequestInfo{ - ID: int64(mergeRequest.IID), - Body: body, - Source: BranchInfo{Name: mergeRequest.SourceBranch}, - Target: BranchInfo{Name: mergeRequest.TargetBranch}, + ID: int64(mergeRequest.IID), + Body: body, + Source: BranchInfo{ + Name: mergeRequest.SourceBranch, + Repository: repository, + Owner: owner, + }, + Target: BranchInfo{ + Name: mergeRequest.TargetBranch, + Repository: repository, + Owner: owner, + }, }) } return diff --git a/vcsclient/gitlab_test.go b/vcsclient/gitlab_test.go index 09c923b7..10100903 100644 --- a/vcsclient/gitlab_test.go +++ b/vcsclient/gitlab_test.go @@ -222,8 +222,8 @@ func TestGitLabClient_ListOpenPullRequests(t *testing.T) { assert.Len(t, result, 1) assert.EqualValues(t, PullRequestInfo{ ID: 302, - Source: BranchInfo{Name: "test1", Repository: ""}, - Target: BranchInfo{Name: "master", Repository: ""}, + Source: BranchInfo{Name: "test1", Repository: repo1, Owner: owner}, + Target: BranchInfo{Name: "master", Repository: repo1, Owner: owner}, }, result[0]) // With body @@ -233,8 +233,8 @@ func TestGitLabClient_ListOpenPullRequests(t *testing.T) { assert.EqualValues(t, PullRequestInfo{ ID: 302, Body: "hello world", - Source: BranchInfo{Name: "test1", Repository: ""}, - Target: BranchInfo{Name: "master", Repository: ""}, + Source: BranchInfo{Name: "test1", Repository: repo1, Owner: owner}, + Target: BranchInfo{Name: "master", Repository: repo1, Owner: owner}, }, result[0]) } diff --git a/vcsclient/testdata/bitbucketserver/pull_requests_list_response.json b/vcsclient/testdata/bitbucketserver/pull_requests_list_response.json index 3dc36985..ac958aa2 100644 --- a/vcsclient/testdata/bitbucketserver/pull_requests_list_response.json +++ b/vcsclient/testdata/bitbucketserver/pull_requests_list_response.json @@ -16,10 +16,10 @@ "fromRef": { "id": "refs/heads/feature-ABC-123", "repository": { - "slug": "my-repo", + "slug": "repo-1", "name": null, "project": { - "key": "PRJ" + "key": "jfrogForked" } }, "displayId": "feature-ABC-123" @@ -27,10 +27,10 @@ "toRef": { "id": "refs/heads/master", "repository": { - "slug": "my-repo", + "slug": "repo-1", "name": null, "project": { - "key": "PRJ" + "key": "jfrog" } }, "displayId": "master"