Skip to content

Commit 4d1e377

Browse files
Sebastian Kratzertmax-wittig
Sebastian Kratzert
authored andcommitted
feat(project): implement update_submodule
1 parent fcea41c commit 4d1e377

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

gitlab/tests/test_gitlab.py

+50
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,56 @@ def resp_mark_all_as_done(url, request):
717717
with HTTMock(resp_mark_all_as_done):
718718
self.gl.todos.mark_all_as_done()
719719

720+
def test_update_submodule(self):
721+
@urlmatch(
722+
scheme="http", netloc="localhost", path="/api/v4/projects/1$", method="get"
723+
)
724+
def resp_get_project(url, request):
725+
headers = {"content-type": "application/json"}
726+
content = '{"name": "name", "id": 1}'.encode("utf-8")
727+
return response(200, content, headers, None, 5, request)
728+
729+
@urlmatch(
730+
scheme="http",
731+
netloc="localhost",
732+
path="/api/v4/projects/$1/repository/submodules/foo%2Fbar",
733+
method="post",
734+
)
735+
def resp_update_submodule(url, request):
736+
headers = {"content-type": "application/json"}
737+
content = """{
738+
"id": "ed899a2f4b50b4370feeea94676502b42383c746",
739+
"short_id": "ed899a2f4b5",
740+
"title": "Message",
741+
"author_name": "Author",
742+
"author_email": "author@example.com",
743+
"committer_name": "Author",
744+
"committer_email": "author@example.com",
745+
"created_at": "2018-09-20T09:26:24.000-07:00",
746+
"message": "Message",
747+
"parent_ids": [ "ae1d9fb46aa2b07ee9836d49862ec4e2c46fbbba" ],
748+
"committed_date": "2018-09-20T09:26:24.000-07:00",
749+
"authored_date": "2018-09-20T09:26:24.000-07:00",
750+
"status": null}"""
751+
content = content.encode("utf-8")
752+
return response(200, content, headers, None, 5, request)
753+
754+
with HTTMock(resp_update_submodule):
755+
project = self.gl.projects.get(1)
756+
self.assertIsInstance(project, Project)
757+
self.assertEqual(project.name, "name")
758+
self.assertEqual(project.id, 1)
759+
760+
ret = project.update_submodule(
761+
submodule="foo/bar",
762+
branch="master",
763+
commit_sha="4c3674f66071e30b3311dac9b9ccc90502a72664",
764+
commit_message="Message",
765+
)
766+
self.assertIsInstance(ret, dict)
767+
self.assertEqual(ret["message"], "Message")
768+
self.assertEqual(ret["id"], "ed899a2f4b50b4370feeea94676502b42383c746")
769+
720770
def _default_config(self):
721771
fd, temp_path = tempfile.mkstemp()
722772
os.write(fd, valid_config)

gitlab/v4/objects.py

+23
Original file line numberDiff line numberDiff line change
@@ -3885,6 +3885,29 @@ class Project(SaveMixin, ObjectDeleteMixin, RESTObject):
38853885
("wikis", "ProjectWikiManager"),
38863886
)
38873887

3888+
@cli.register_custom_action("Project", ("submodule", "branch", "commit_sha"))
3889+
@exc.on_http_error(exc.GitlabUpdateError)
3890+
def update_submodule(self, submodule, branch, commit_sha, **kwargs):
3891+
"""Transfer a project to the given namespace ID
3892+
3893+
Args:
3894+
submodule (str): Full path to the submodule
3895+
branch (str): Name of the branch to commit into
3896+
commit_sha (str): Full commit SHA to update the submodule to
3897+
commit_message (str): Commit message. If no message is provided, a default one will be set (optional)
3898+
3899+
Raises:
3900+
GitlabAuthenticationError: If authentication is not correct
3901+
GitlabPutError: If the submodule could not be updated
3902+
"""
3903+
3904+
submodule = submodule.replace("/", "%2F") # .replace('.', '%2E')
3905+
path = "/projects/%s/repository/submodules/%s" % (self.get_id(), submodule)
3906+
data = {"branch": branch, "commit_sha": commit_sha}
3907+
if "commit_message" in kwargs:
3908+
data["commit_message"] = kwargs["commit_message"]
3909+
return self.manager.gitlab.http_put(path, post_data=data)
3910+
38883911
@cli.register_custom_action("Project", tuple(), ("path", "ref", "recursive"))
38893912
@exc.on_http_error(exc.GitlabGetError)
38903913
def repository_tree(self, path="", ref="", recursive=False, **kwargs):

0 commit comments

Comments
 (0)