Skip to content

Commit

Permalink
Fix missing Git owner issue on bitbucket and gitlab (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverdlov93 authored Jul 30, 2023
1 parent 2b3de63 commit a27f807
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
27 changes: 21 additions & 6 deletions vcsclient/bitbucketserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
})
}
}
Expand All @@ -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},
Expand Down Expand Up @@ -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
}
10 changes: 5 additions & 5 deletions vcsclient/bitbucketserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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])
}

Expand Down
20 changes: 14 additions & 6 deletions vcsclient/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions vcsclient/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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])
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@
"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"
},
"toRef": {
"id": "refs/heads/master",
"repository": {
"slug": "my-repo",
"slug": "repo-1",
"name": null,
"project": {
"key": "PRJ"
"key": "jfrog"
}
},
"displayId": "master"
Expand Down

0 comments on commit a27f807

Please sign in to comment.