Skip to content

feat: add support for merge_base API #2236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/cli-examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ Define the status of a commit (as would be done from a CI tool for example):
--target-url http://server/build/123 \
--description "Jenkins build succeeded"

Get the merge base for two or more branches, tags or commits:

.. code-block:: console

gitlab project repository-merge-base --id 1 --refs bd1324e2f,main,v1.0.0

Artifacts
---------

Expand Down
4 changes: 4 additions & 0 deletions docs/gl_objects/projects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ Compare two branches, tags or commits::
for file_diff in result['diffs']:
print(file_diff)

Get the merge base for two or more branches, tags or commits::

commit = project.repository_merge_base(['main', 'v1.2.3', 'bd1324e2f'])

Get a list of contributors for the repository::

contributors = project.repository_contributors()
Expand Down
28 changes: 27 additions & 1 deletion gitlab/v4/objects/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import gitlab
from gitlab import cli
from gitlab import exceptions as exc
from gitlab import utils
from gitlab import types, utils

if TYPE_CHECKING:
# When running mypy we use these as the base classes
Expand Down Expand Up @@ -246,6 +246,32 @@ def repository_archive(
result, streamed, action, chunk_size, iterator=iterator
)

@cli.register_custom_action("Project", ("refs",))
@exc.on_http_error(exc.GitlabGetError)
def repository_merge_base(
self, refs: List[str], **kwargs: Any
) -> Union[Dict[str, Any], requests.Response]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This made me think if we should start forcing these to just be a Dict?

Copy link
Member Author

@nejch nejch Aug 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @JohnVillalovos! Makes sense to me! I was looking at doing this via overloading http_get/http_request to narrow the types there already, depending on the streamed/raw arguments but it got a bit out of hand. Then I think we wouldn't need to do it in "downstream" methods, IIUC. I will look into it again :)

But it requires quite a few overloads for all the possible signatures. This is what I was looking at https://medium.com/analytics-vidhya/making-sense-of-typing-overload-437e6deecade.

"""Return a diff between two branches/commits.

Args:
refs: The refs to find the common ancestor of. Multiple refs can be passed.
**kwargs: Extra options to send to the server (e.g. sudo)

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabGetError: If the server failed to perform the request

Returns:
The common ancestor commit (*not* a RESTObject)
"""
path = f"/projects/{self.encoded_id}/repository/merge_base"
query_data, _ = utils._transform_types(
data={"refs": refs},
custom_types={"refs": types.ArrayAttribute},
transform_data=True,
)
return self.manager.gitlab.http_get(path, query_data=query_data, **kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To force it to be a Dict I think could do this:

Suggested change
return self.manager.gitlab.http_get(path, query_data=query_data, **kwargs)
result = self.manager.gitlab.http_get(path, query_data=query_data, raw=False, streamed=False, **kwargs)
if TYPE_CHECKING:
assert isinstance(result, dict)
return result


@cli.register_custom_action("Project")
@exc.on_http_error(exc.GitlabDeleteError)
def delete_merged_branches(self, **kwargs: Any) -> None:
Expand Down
10 changes: 10 additions & 0 deletions tests/functional/api/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,13 @@ def test_revert_commit(project):
with pytest.raises(gitlab.GitlabRevertError):
# Two revert attempts should raise GitlabRevertError
commit.revert(branch="main")


def test_repository_merge_base(project):
refs = [commit.id for commit in project.commits.list(all=True)]

commit = project.repository_merge_base(refs)
assert commit["id"] in refs

with pytest.raises(gitlab.GitlabGetError, match="Provide at least 2 refs"):
commit = project.repository_merge_base(refs[0])