Commit de295032 authored by Matthias Schoettle's avatar Matthias Schoettle Committed by Nejc Habjan
Browse files

fix(api): return the new commit when calling cherry_pick

parent 939505b9
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -48,7 +48,9 @@ class ProjectCommit(RESTObject):

    @cli.register_custom_action(cls_names="ProjectCommit", required=("branch",))
    @exc.on_http_error(exc.GitlabCherryPickError)
    def cherry_pick(self, branch: str, **kwargs: Any) -> None:
    def cherry_pick(
        self, branch: str, **kwargs: Any
    ) -> Union[Dict[str, Any], requests.Response]:
        """Cherry-pick a commit into a branch.

        Args:
@@ -58,10 +60,13 @@ class ProjectCommit(RESTObject):
        Raises:
            GitlabAuthenticationError: If authentication is not correct
            GitlabCherryPickError: If the cherry-pick could not be performed

        Returns:
            The new commit data (*not* a RESTObject)
        """
        path = f"{self.manager.path}/{self.encoded_id}/cherry_pick"
        post_data = {"branch": branch}
        self.manager.gitlab.http_post(path, post_data=post_data, **kwargs)
        return self.manager.gitlab.http_post(path, post_data=post_data, **kwargs)

    @cli.register_custom_action(cls_names="ProjectCommit", optional=("type",))
    @exc.on_http_error(exc.GitlabGetError)
+22 −0
Original line number Diff line number Diff line
@@ -165,6 +165,28 @@ def test_commit_discussion(project):
    note_from_get.delete()


def test_cherry_pick_commit(project):
    commits = project.commits.list()
    commit = commits[1]
    parent_commit = commit.parent_ids[0]

    # create a branch to cherry pick onto
    project.branches.create(
        {
            "branch": "test",
            "ref": parent_commit,
        }
    )
    cherry_pick_commit = commit.cherry_pick(branch="test")

    expected_message = f"{commit.message}\n\n(cherry picked from commit {commit.id})"
    assert cherry_pick_commit["message"].startswith(expected_message)

    with pytest.raises(gitlab.GitlabCherryPickError):
        # Two cherry pick attempts should raise GitlabCherryPickError
        commit.cherry_pick(branch="test")


def test_revert_commit(project):
    commit = project.commits.list()[0]
    revert_commit = commit.revert(branch="main")
+25 −0
Original line number Diff line number Diff line
@@ -37,6 +37,12 @@ def resp_commit():
        "short_id": "8b090c1b",
        "title": 'Revert "Initial commit"',
    }
    cherry_pick_content = {
        "id": "8b090c1b79a14f2bd9e8a738f717824ff53aebad",
        "short_id": "8b090c1b",
        "title": "Initial commit",
        "message": "Initial commit\n\n\n(cherry picked from commit 6b2257eabcec3db1f59dafbd84935e3caea04235)",
    }

    with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
        rsps.add(
@@ -53,6 +59,13 @@ def resp_commit():
            content_type="application/json",
            status=200,
        )
        rsps.add(
            method=responses.POST,
            url="http://localhost/api/v4/projects/1/repository/commits/6b2257ea/cherry_pick",
            json=cherry_pick_content,
            content_type="application/json",
            status=200,
        )
        yield rsps


@@ -118,6 +131,18 @@ def test_create_commit(project, resp_create_commit):
    assert commit.title == data["commit_message"]


def test_cherry_pick_commit(project, resp_commit):
    commit = project.commits.get("6b2257ea", lazy=True)
    cherry_pick_commit = commit.cherry_pick(branch="main")

    assert cherry_pick_commit["short_id"] == "8b090c1b"
    assert cherry_pick_commit["title"] == "Initial commit"
    assert (
        cherry_pick_commit["message"]
        == "Initial commit\n\n\n(cherry picked from commit 6b2257eabcec3db1f59dafbd84935e3caea04235)"
    )


def test_revert_commit(project, resp_commit):
    commit = project.commits.get("6b2257ea", lazy=True)
    revert_commit = commit.revert(branch="main")