Skip to content

Commit

Permalink
Generate code for complex parameters
Browse files Browse the repository at this point in the history
(+ find bug in Repository.create_commit with parents !)
  • Loading branch information
jacquev6 committed Jun 1, 2012
1 parent b3fce39 commit 8b6d463
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 13 deletions.
Expand Up @@ -555,6 +555,9 @@
{ "name": "sha", "type": "string" },
{ "name": "url", "type": "string" },
{ "name": "tree", "type": "GitTree" }
],
"identity": [
{ "type": "attribute", "value": [ "sha" ] }
]
},
{
Expand Down
Expand Up @@ -3214,6 +3214,14 @@
],
"isCompletable": true,
"name": "GitCommit",
"identity": [
{
"type": "attribute",
"value": [
"sha"
]
}
],
"methods": []
},
{
Expand Down
4 changes: 2 additions & 2 deletions codegen/templates/GithubObject.MethodBody.DoRequest.py
Expand Up @@ -9,15 +9,15 @@
{% if method.mandatoryParameters %}
post_parameters = {
{% for parameter in method.mandatoryParameters %}
"{{ parameter.name }}": {{ parameter.name }},
"{{ parameter.name }}": {% if parameter.type.simple %}{{ parameter.name }}{% else %}{% if parameter.type.cardinality == "list" %}[ element._identity for element in {{ parameter.name }} ]{% else %}dict( ( key, value._identity ) for key, value in {{ parameter.name }}.iteritems() ){% endif %}{% endif %},
{% endfor %}
}
{% else %}
post_parameters = dict()
{% endif %}
{% for parameter in method.optionalParameters %}
if {{ parameter.name }} is not GithubObject.NotSet:
post_parameters[ "{{ parameter.name }}" ] = {{ parameter.name }}
post_parameters[ "{{ parameter.name }}" ] = {% if parameter.type.simple %}{{ parameter.name }}{% else %}{% if parameter.type.cardinality == "dict" %}dict( ( key, value._identity ) for key, value in {{ parameter.name }}.iteritems() ){% else %}{{ parameter.name }}._identity{% endif %}{% endif %}
{% endfor %}
{% endif %}
{% else %}
Expand Down
2 changes: 1 addition & 1 deletion src/github/AuthenticatedUser.py
Expand Up @@ -209,7 +209,7 @@ def create_gist( self, public, files, description = GithubObject.NotSet ):
assert description is GithubObject.NotSet or isinstance( description, ( str, unicode ) ), description
post_parameters = {
"public": public,
"files": dict( ( key, value._identity() ) for key, value in files.iteritems() ),
"files": dict( ( key, value._identity ) for key, value in files.iteritems() ),
}
if description is not GithubObject.NotSet:
post_parameters[ "description" ] = description
Expand Down
2 changes: 1 addition & 1 deletion src/github/Gist.py
Expand Up @@ -127,7 +127,7 @@ def edit( self, description = GithubObject.NotSet, files = GithubObject.NotSet )
if description is not GithubObject.NotSet:
post_parameters[ "description" ] = description
if files is not GithubObject.NotSet:
post_parameters[ "files" ] = dict( ( key, value._identity() ) for key, value in files.iteritems() )
post_parameters[ "files" ] = dict( ( key, value._identity ) for key, value in files.iteritems() )
status, headers, data = self._request(
"PATCH",
str( self.url ),
Expand Down
4 changes: 4 additions & 0 deletions src/github/GitCommit.py
Expand Up @@ -43,6 +43,10 @@ def url( self ):
self._completeIfNotSet( self._url )
return self._NoneIfNotSet( self._url )

@property
def _identity( self ):
return str( self.sha )

def _initAttributes( self ):
self._author = GithubObject.NotSet
self._committer = GithubObject.NotSet
Expand Down
3 changes: 2 additions & 1 deletion src/github/InputFileContent.py
@@ -1,7 +1,8 @@
class InputFileContent:
class InputFileContent( object ):
def __init__( self, content ):
self.__content = content

@property
def _identity( self ):
return {
"content": self.__content,
Expand Down
3 changes: 2 additions & 1 deletion src/github/InputGitAuthor.py
@@ -1,9 +1,10 @@
class InputGitAuthor:
class InputGitAuthor( object ):
def __init__( self, name, email, date ):
self.__name = name
self.__email = email
self.__date = date

@property
def _identity( self ):
return {
"name": self.__name,
Expand Down
3 changes: 2 additions & 1 deletion src/github/InputGitTreeElement.py
@@ -1,13 +1,14 @@
import GithubObject

class InputGitTreeElement:
class InputGitTreeElement( object ):
def __init__( self, path, mode, type, content = GithubObject.NotSet, sha = GithubObject.NotSet ):
self.__path = path
self.__mode = mode
self.__type = type
self.__content = content
self.__sha = sha

@property
def _identity( self ):
identity = {
"path": self.__path,
Expand Down
2 changes: 1 addition & 1 deletion src/github/NamedUser.py
Expand Up @@ -149,7 +149,7 @@ def create_gist( self, public, files, description = GithubObject.NotSet ):
assert description is GithubObject.NotSet or isinstance( description, ( str, unicode ) ), description
post_parameters = {
"public": public,
"files": dict( ( key, value._identity() ) for key, value in files.iteritems() ),
"files": dict( ( key, value._identity ) for key, value in files.iteritems() ),
}
if description is not GithubObject.NotSet:
post_parameters[ "description" ] = description
Expand Down
10 changes: 5 additions & 5 deletions src/github/Repository.py
Expand Up @@ -253,12 +253,12 @@ def create_git_commit( self, message, tree, parents, author = GithubObject.NotSe
post_parameters = {
"message": message,
"tree": tree,
"parents": parents,
"parents": [ element._identity for element in parents ],
}
if author is not GithubObject.NotSet:
post_parameters[ "author" ] = author._identity()
post_parameters[ "author" ] = author._identity
if committer is not GithubObject.NotSet:
post_parameters[ "committer" ] = committer._identity()
post_parameters[ "committer" ] = committer._identity
status, headers, data = self._request(
"POST",
str( self.url ) + "/git/commits",
Expand Down Expand Up @@ -297,7 +297,7 @@ def create_git_tag( self, tag, message, object, type, tagger = GithubObject.NotS
"type": type,
}
if tagger is not GithubObject.NotSet:
post_parameters[ "tagger" ] = tagger._identity()
post_parameters[ "tagger" ] = tagger._identity
status, headers, data = self._request(
"POST",
str( self.url ) + "/git/tags",
Expand All @@ -311,7 +311,7 @@ def create_git_tree( self, tree, base_tree = GithubObject.NotSet ):
assert all( isinstance( element, InputGitTreeElement.InputGitTreeElement ) for element in tree ), tree
assert base_tree is GithubObject.NotSet or isinstance( base_tree, ( str, unicode ) ), base_tree
post_parameters = {
"tree": [ element._identity() for element in tree ],
"tree": [ element._identity for element in tree ],
}
if base_tree is not GithubObject.NotSet:
post_parameters[ "base_tree" ] = base_tree
Expand Down
15 changes: 15 additions & 0 deletions test/ReplayData/Repository.testCreateGitCommitWithParents.txt
@@ -0,0 +1,15 @@
GET /repos/jacquev6/PyGithub/git/commits/7248e66831d4ffe09ef1f30a1df59ec0a9331ece {'Authorization': 'Basic login_and_password_removed'} null
200
[('status', '200 OK'), ('x-ratelimit-remaining', '4967'), ('content-length', '762'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"0cbf75a2a511c74f3df22dfd8d2ee42a"'), ('date', 'Fri, 01 Jun 2012 18:39:29 GMT'), ('content-type', 'application/json; charset=utf-8')]
{"committer":{"email":"vincent@vincent-jacques.net","date":"2012-05-30T09:58:18-07:00","name":"Vincent Jacques"},"message":"Check HTTP status on all requests\n","url":"https://api.github.com/repos/jacquev6/PyGithub/git/commits/7248e66831d4ffe09ef1f30a1df59ec0a9331ece","sha":"7248e66831d4ffe09ef1f30a1df59ec0a9331ece","parents":[{"url":"https://api.github.com/repos/jacquev6/PyGithub/git/commits/81ca19a009b54e64226e3f9e51210fba989d5497","sha":"81ca19a009b54e64226e3f9e51210fba989d5497"}],"tree":{"url":"https://api.github.com/repos/jacquev6/PyGithub/git/trees/91655d55b309f520fd4b3fd9e5303cfc13855a21","sha":"91655d55b309f520fd4b3fd9e5303cfc13855a21"},"author":{"email":"vincent@vincent-jacques.net","date":"2012-05-30T09:58:18-07:00","name":"Vincent Jacques"}}

GET /repos/jacquev6/PyGithub/git/commits/12d427464f8d91c8e981043a86ba8a2a9e7319ea {'Authorization': 'Basic login_and_password_removed'} null
200
[('status', '200 OK'), ('x-ratelimit-remaining', '4966'), ('content-length', '769'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"87d790f22e47dbaa3148ad7872e32dde"'), ('date', 'Fri, 01 Jun 2012 18:39:30 GMT'), ('content-type', 'application/json; charset=utf-8')]
{"tree":{"sha":"143dd39e465e5de953d944c9309b961af392da73","url":"https://api.github.com/repos/jacquev6/PyGithub/git/trees/143dd39e465e5de953d944c9309b961af392da73"},"sha":"12d427464f8d91c8e981043a86ba8a2a9e7319ea","message":"Remove the notion of ImmediateCompletion\n","parents":[{"sha":"7a622975d6a3f0ab80f573f577aa0e3ffb69e2f5","url":"https://api.github.com/repos/jacquev6/PyGithub/git/commits/7a622975d6a3f0ab80f573f577aa0e3ffb69e2f5"}],"author":{"email":"vincent@vincent-jacques.net","date":"2012-05-30T09:51:36-07:00","name":"Vincent Jacques"},"url":"https://api.github.com/repos/jacquev6/PyGithub/git/commits/12d427464f8d91c8e981043a86ba8a2a9e7319ea","committer":{"email":"vincent@vincent-jacques.net","date":"2012-05-30T10:00:37-07:00","name":"Vincent Jacques"}}

POST /repos/jacquev6/PyGithub/git/commits {'Authorization': 'Basic login_and_password_removed'} {"parents": ["7248e66831d4ffe09ef1f30a1df59ec0a9331ece", "12d427464f8d91c8e981043a86ba8a2a9e7319ea"], "message": "Commit created by PyGithub", "tree": "fae707821159639589bf94f3fb0a7154ec5d441b"}
201
[('status', '201 Created'), ('x-ratelimit-remaining', '4965'), ('content-length', '918'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"1ada1e7861f74fa4fefa922bf03e891e"'), ('date', 'Fri, 01 Jun 2012 18:39:31 GMT'), ('content-type', 'application/json; charset=utf-8'), ('location', 'https://api.github.com/repos/jacquev6/PyGithub/git/commits/6adf9ea25ff8a8f2a42bcb1c09e42526339037cd')]
{"committer":{"email":"github.com@vincent-jacques.net","date":"2012-06-01T11:39:31-07:00","name":"Vincent Jacques"},"message":"Commit created by PyGithub","url":"https://api.github.com/repos/jacquev6/PyGithub/git/commits/6adf9ea25ff8a8f2a42bcb1c09e42526339037cd","sha":"6adf9ea25ff8a8f2a42bcb1c09e42526339037cd","parents":[{"url":"https://api.github.com/repos/jacquev6/PyGithub/git/commits/7248e66831d4ffe09ef1f30a1df59ec0a9331ece","sha":"7248e66831d4ffe09ef1f30a1df59ec0a9331ece"},{"url":"https://api.github.com/repos/jacquev6/PyGithub/git/commits/12d427464f8d91c8e981043a86ba8a2a9e7319ea","sha":"12d427464f8d91c8e981043a86ba8a2a9e7319ea"}],"tree":{"url":"https://api.github.com/repos/jacquev6/PyGithub/git/trees/fae707821159639589bf94f3fb0a7154ec5d441b","sha":"fae707821159639589bf94f3fb0a7154ec5d441b"},"author":{"email":"github.com@vincent-jacques.net","date":"2012-06-01T11:39:31-07:00","name":"Vincent Jacques"}}

8 changes: 8 additions & 0 deletions test/Repository.py
Expand Up @@ -163,6 +163,14 @@ def testCreateGitCommit( self ):
commit = self.repo.create_git_commit( "Commit created by PyGithub", "107139a922f33bab6fbeb9f9eb8787e7f19e0528", [] )
self.assertEqual( commit.sha, "0b820628236ab8bab3890860fc414fa757ca15f4" )

def testCreateGitCommitWithParents( self ):
parents = [
self.repo.get_git_commit( "7248e66831d4ffe09ef1f30a1df59ec0a9331ece" ),
self.repo.get_git_commit( "12d427464f8d91c8e981043a86ba8a2a9e7319ea" ),
]
commit = self.repo.create_git_commit( "Commit created by PyGithub", "fae707821159639589bf94f3fb0a7154ec5d441b", parents )
self.assertEqual( commit.sha, "6adf9ea25ff8a8f2a42bcb1c09e42526339037cd" )

def testCreateGitCommitWithAllArguments( self ):
commit = self.repo.create_git_commit( "Commit created by PyGithub", "107139a922f33bab6fbeb9f9eb8787e7f19e0528", [], github.InputGitAuthor( "John Doe", "j.doe@vincent-jacques.net", "2008-07-09T16:13:30+12:00" ), github.InputGitAuthor( "John Doe", "j.doe@vincent-jacques.net", "2008-07-09T16:13:30+12:00" ) )
self.assertEqual( commit.sha, "526946197ae9da59c6507cacd13ad6f1cfb686ea" )
Expand Down

0 comments on commit 8b6d463

Please sign in to comment.