Skip to content

Commit ee666fd

Browse files
author
Gauvain Pocentek
committed
Add support for commit creation
Fixes #206
1 parent 04435e1 commit ee666fd

File tree

4 files changed

+55
-17
lines changed

4 files changed

+55
-17
lines changed

docs/gl_objects/commits.py

+20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@
99
commits = project.commits.list(since='2016-01-01T00:00:00Z')
1010
# end filter list
1111

12+
# create
13+
# See https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions
14+
# for actions detail
15+
data = {
16+
'branch_name': 'master',
17+
'commit_message': 'blah blah blah',
18+
'actions': [
19+
{
20+
'action': 'create',
21+
'file_path': 'blah',
22+
'content': 'blah'
23+
}
24+
]
25+
}
26+
27+
commit = gl.project_commits.create(data, project_id=1)
28+
# or
29+
commit = project.commits.create(data)
30+
# end commit
31+
1232
# get
1333
commit = gl.project_commits.get('e3d5a71b', project_id=1)
1434
# or

docs/gl_objects/commits.rst

+17-14
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ Commits
55
Commits
66
=======
77

8-
Use :class:`~gitlab.objects.ProjectCommit` objects to manipulate commits. The
9-
:attr:`gitlab.Gitlab.project_commits` and
10-
:attr:`gitlab.objects.Project.commits` manager objects provide helper
11-
functions.
8+
* Object class: :class:`~gitlab.objects.ProjectCommit`
9+
* Manager objects: :attr:`gitlab.Gitlab.project_commits`,
10+
:attr:`gitlab.objects.Project.commits`
1211

1312
Examples
1413
--------
@@ -26,6 +25,12 @@ results:
2625
:start-after: # filter list
2726
:end-before: # end filter list
2827

28+
Create a commit:
29+
30+
.. literalinclude:: commits.py
31+
:start-after: # create
32+
:end-before: # end create
33+
2934
Get a commit detail:
3035

3136
.. literalinclude:: commits.py
@@ -41,11 +46,10 @@ Get the diff for a commit:
4146
Commit comments
4247
===============
4348

44-
Use :class:`~gitlab.objects.ProjectCommitStatus` objects to manipulate commits. The
45-
:attr:`gitlab.Gitlab.project_commit_comments` and
46-
:attr:`gitlab.objects.Project.commit_comments` and
47-
:attr:`gitlab.objects.ProjectCommit.comments` manager objects provide helper
48-
functions.
49+
* Object class: :class:`~gitlab.objects.ProjectCommiComment`
50+
* Manager objects: :attr:`gitlab.Gitlab.project_commit_comments`,
51+
:attr:`gitlab.objects.Project.commit_comments`,
52+
:attr:`gitlab.objects.ProjectCommit.comments`
4953

5054
Examples
5155
--------
@@ -65,11 +69,10 @@ Add a comment on a commit:
6569
Commit status
6670
=============
6771

68-
Use :class:`~gitlab.objects.ProjectCommitStatus` objects to manipulate commits.
69-
The :attr:`gitlab.Gitlab.project_commit_statuses`,
70-
:attr:`gitlab.objects.Project.commit_statuses` and
71-
:attr:`gitlab.objects.ProjectCommit.statuses` manager objects provide helper
72-
functions.
72+
* Object class: :class:`~gitlab.objects.ProjectCommitStatus`
73+
* Manager objects: :attr:`gitlab.Gitlab.project_commit_statuses`,
74+
:attr:`gitlab.objects.Project.commit_statuses`,
75+
:attr:`gitlab.objects.ProjectCommit.statuses`
7376

7477
Examples
7578
--------

gitlab/objects.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ def _data_for_gitlab(self, extra_parameters={}, update=False,
223223
if hasattr(self, attribute):
224224
value = getattr(self, attribute)
225225
if isinstance(value, list):
226-
value = ",".join(value)
226+
if value and isinstance(value[0], six.string_types):
227+
value = ",".join(value)
227228
if attribute == 'sudo':
228229
value = str(value)
229230
data[attribute] = value
@@ -1278,8 +1279,9 @@ class ProjectCommit(GitlabObject):
12781279
_url = '/projects/%(project_id)s/repository/commits'
12791280
canDelete = False
12801281
canUpdate = False
1281-
canCreate = False
12821282
requiredUrlAttrs = ['project_id']
1283+
requiredCreateAttrs = ['branch_name', 'commit_message', 'actions']
1284+
optionalCreateAttrs = ['author_email', 'author_name']
12831285
shortPrintAttr = 'title'
12841286
managers = (
12851287
('comments', ProjectCommitCommentManager,

tools/python_test.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,21 @@
161161
readme = admin_project.files.get(file_path='README.rst', ref='master')
162162
assert(readme.decode() == 'Initial content')
163163

164+
data = {
165+
'branch_name': 'master',
166+
'commit_message': 'blah blah blah',
167+
'actions': [
168+
{
169+
'action': 'create',
170+
'file_path': 'blah',
171+
'content': 'blah'
172+
}
173+
]
174+
}
175+
admin_project.commits.create(data)
176+
164177
tree = admin_project.repository_tree()
165-
assert(len(tree) == 1)
178+
assert(len(tree) == 2)
166179
assert(tree[0]['name'] == 'README.rst')
167180
blob = admin_project.repository_blob('master', 'README.rst')
168181
assert(blob == 'Initial content')

0 commit comments

Comments
 (0)