Skip to content

Commit

Permalink
gitlab: skip source branch with equal name from forked projects
Browse files Browse the repository at this point in the history
The "source_branch" information from MR objects doesn't contain the
project from which this branch comes from and the only place in the code
where the source branch is specified on search operation is when the
user doesn't explicitly specifies what's the MR ID on CLI and let lab
"guess" the right MR ID based on the current checked-out branch which,
obviously, is related to a single project ID.

In the above case, the branch name is used as "source_branch" in the
search operation, however, if any MR has its source branch with same
name, but coming from another, forked, project, it'll be in the mix of
found MRs, possibly returning the wrong ID.

For instance, if the user has the "test" branch checked out which is
present in the "test-project" project, but the following MRs exist:

MR 1 source branch: "https://gitlab.com/test-project/test"
MR 2 source branch: "https://gitlab.com/fork-from-test-project/test"

In the current state, both MR 1 and 2 are returned. However, the
correct would be to return only MR 1.

This patch fixes this wrong behavior by checking the mr.SourceProjectID
and mr.ProjectID based on the following statement from API doc [1]:

"project_id represents the ID of the project where the MR resides.
project_id always equals target_project_id.

In the case of a merge request from the same project, source_project_id,
target_project_id and project_id are the same. In the case of a merge
request from a fork, target_project_id and project_id are the same and
source_project_id is the fork project’s ID.
"
[1] https://docs.gitlab.com/ee/api/merge_requests.html#list-project-merge-requests

Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
  • Loading branch information
bmeneg committed Jun 26, 2022
1 parent 2182439 commit 472b2e6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 0 additions & 3 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ func getBranchMR(rn, branch string) int {
}

mrs, err := lab.MRList(rn, gitlab.ListProjectMergeRequestsOptions{
ListOptions: gitlab.ListOptions{
PerPage: 10,
},
Labels: mrLabels,
State: &mrState,
OrderBy: gitlab.String("updated_at"),
Expand Down
12 changes: 12 additions & 0 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,18 @@ func MRList(projID interface{}, opts gitlab.ListProjectMergeRequestsOptions, n i
if err != nil {
return nil, err
}
// SourceBranch was specified, so we need to make sure we're listing MRs
// for the source branch for the specific projID and not a fork.
if opts.SourceBranch != nil {
var projMRs []*gitlab.MergeRequest
for _, mr := range mrs {
if mr.SourceProjectID != mr.ProjectID {
continue
}
projMRs = append(projMRs, mr)
}
mrs = projMRs
}
list = append(list, mrs...)

var ok bool
Expand Down

0 comments on commit 472b2e6

Please sign in to comment.