From 8b6d463cd92ab8c1be8982a13956b7989c7e901e Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Fri, 1 Jun 2012 19:41:50 +0100 Subject: [PATCH] Generate code for complex parameters (+ find bug in Repository.create_commit with parents !) --- .../description.human_readable.json | 3 +++ .../description.normalized.json | 8 ++++++++ .../GithubObject.MethodBody.DoRequest.py | 4 ++-- src/github/AuthenticatedUser.py | 2 +- src/github/Gist.py | 2 +- src/github/GitCommit.py | 4 ++++ src/github/InputFileContent.py | 3 ++- src/github/InputGitAuthor.py | 3 ++- src/github/InputGitTreeElement.py | 3 ++- src/github/NamedUser.py | 2 +- src/github/Repository.py | 10 +++++----- .../Repository.testCreateGitCommitWithParents.txt | 15 +++++++++++++++ test/Repository.py | 8 ++++++++ 13 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 test/ReplayData/Repository.testCreateGitCommitWithParents.txt diff --git a/codegen/JsonDescriptionOfGithubApiV3/description.human_readable.json b/codegen/JsonDescriptionOfGithubApiV3/description.human_readable.json index 14bdcc47..58ade774 100644 --- a/codegen/JsonDescriptionOfGithubApiV3/description.human_readable.json +++ b/codegen/JsonDescriptionOfGithubApiV3/description.human_readable.json @@ -555,6 +555,9 @@ { "name": "sha", "type": "string" }, { "name": "url", "type": "string" }, { "name": "tree", "type": "GitTree" } + ], + "identity": [ + { "type": "attribute", "value": [ "sha" ] } ] }, { diff --git a/codegen/JsonDescriptionOfGithubApiV3/description.normalized.json b/codegen/JsonDescriptionOfGithubApiV3/description.normalized.json index 5ea10052..179112ca 100644 --- a/codegen/JsonDescriptionOfGithubApiV3/description.normalized.json +++ b/codegen/JsonDescriptionOfGithubApiV3/description.normalized.json @@ -3214,6 +3214,14 @@ ], "isCompletable": true, "name": "GitCommit", + "identity": [ + { + "type": "attribute", + "value": [ + "sha" + ] + } + ], "methods": [] }, { diff --git a/codegen/templates/GithubObject.MethodBody.DoRequest.py b/codegen/templates/GithubObject.MethodBody.DoRequest.py index 87d543cc..693676fc 100644 --- a/codegen/templates/GithubObject.MethodBody.DoRequest.py +++ b/codegen/templates/GithubObject.MethodBody.DoRequest.py @@ -9,7 +9,7 @@ {% 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 %} @@ -17,7 +17,7 @@ {% 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 %} diff --git a/src/github/AuthenticatedUser.py b/src/github/AuthenticatedUser.py index a08afbc7..f46d7846 100644 --- a/src/github/AuthenticatedUser.py +++ b/src/github/AuthenticatedUser.py @@ -209,7 +209,7 @@ class AuthenticatedUser( GithubObject.GithubObject ): 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 diff --git a/src/github/Gist.py b/src/github/Gist.py index ca749781..677a50c3 100644 --- a/src/github/Gist.py +++ b/src/github/Gist.py @@ -127,7 +127,7 @@ class Gist( GithubObject.GithubObject ): 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 ), diff --git a/src/github/GitCommit.py b/src/github/GitCommit.py index 732fff15..8f74c7f9 100644 --- a/src/github/GitCommit.py +++ b/src/github/GitCommit.py @@ -43,6 +43,10 @@ class GitCommit( GithubObject.GithubObject ): 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 diff --git a/src/github/InputFileContent.py b/src/github/InputFileContent.py index 639e91b3..cf1dab64 100644 --- a/src/github/InputFileContent.py +++ b/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, diff --git a/src/github/InputGitAuthor.py b/src/github/InputGitAuthor.py index ea148ad0..f74c6748 100644 --- a/src/github/InputGitAuthor.py +++ b/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, diff --git a/src/github/InputGitTreeElement.py b/src/github/InputGitTreeElement.py index 2766b7d1..74328262 100644 --- a/src/github/InputGitTreeElement.py +++ b/src/github/InputGitTreeElement.py @@ -1,6 +1,6 @@ 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 @@ -8,6 +8,7 @@ class InputGitTreeElement: self.__content = content self.__sha = sha + @property def _identity( self ): identity = { "path": self.__path, diff --git a/src/github/NamedUser.py b/src/github/NamedUser.py index ff3592d4..2a8a1531 100644 --- a/src/github/NamedUser.py +++ b/src/github/NamedUser.py @@ -149,7 +149,7 @@ class NamedUser( GithubObject.GithubObject ): 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 diff --git a/src/github/Repository.py b/src/github/Repository.py index 435ad7b9..bf6bc9b4 100644 --- a/src/github/Repository.py +++ b/src/github/Repository.py @@ -253,12 +253,12 @@ class Repository( GithubObject.GithubObject ): 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", @@ -297,7 +297,7 @@ class Repository( GithubObject.GithubObject ): "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", @@ -311,7 +311,7 @@ class Repository( GithubObject.GithubObject ): 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 diff --git a/test/ReplayData/Repository.testCreateGitCommitWithParents.txt b/test/ReplayData/Repository.testCreateGitCommitWithParents.txt new file mode 100644 index 00000000..78f14a58 --- /dev/null +++ b/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"}} + diff --git a/test/Repository.py b/test/Repository.py index 210d9729..8ca217aa 100644 --- a/test/Repository.py +++ b/test/Repository.py @@ -163,6 +163,14 @@ class Repository( Framework.TestCase ): 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" )