Skip to content

Commit

Permalink
Add a trailing slash to URL when updating or deleting a file (tests f…
Browse files Browse the repository at this point in the history
…ixed) (#931)

* Add a trailing slash to URL when updating or deleting a file.

* Update test files to match new create/update/delete_file Repo methods

* Add trailing slash to get_contents() method

* update tests

* add missing slash in get_dir_contents

* add example usage

* remove dup slashes in tests

* clarify example comments
  • Loading branch information
JeffreyLMelvin authored and sfdye committed Oct 17, 2018
1 parent 9d2129c commit ee9f098
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 23 deletions.
103 changes: 96 additions & 7 deletions doc/examples/Repository.rst
Expand Up @@ -27,13 +27,13 @@ Get list of open issues
>>> repo = g.get_repo("PyGithub/PyGithub")
>>> open_issues = repo.get_issues(state='open')
>>> for issue in open_issues:
... print(issue)
... print(issue)
...
Issue(title="How to get public events?", number=913)
Issue(title="Prevent .netrc from overwriting Auth header", number=910)
Issue(title="Cache fetch responses", number=901)
Issue(title="Is suspended_users for github enterprise implemented in NamedUser?", number=900)
Issue(title="Adding migration api wrapper", number=899)
Issue(title="How to get public events?", number=913)
Issue(title="Prevent .netrc from overwriting Auth header", number=910)
Issue(title="Cache fetch responses", number=901)
Issue(title="Is suspended_users for github enterprise implemented in NamedUser?", number=900)
Issue(title="Adding migration api wrapper", number=899)
Get all the labels of the repository
------------------------------------
Expand All @@ -43,9 +43,98 @@ Get all the labels of the repository
>>> repo = g.get_repo("PyGithub/PyGithub")
>>> labels = repo.get_labels()
>>> for label in labels:
... print(label)
... print(label)
...
Label(name="Hacktoberfest")
Label(name="WIP")
Label(name="bug")
Label(name="documentation")
Get all of the contents of the root directory of the repository
---------------------------------------------------------------

.. code-block:: python
>>> repo = g.get_repo("PyGithub/PyGithub")
>>> contents = repo.get_contents("")
>>> for content_file in contents:
... print(content_file)
...
ContentFile(path=".github")
ContentFile(path=".gitignore")
ContentFile(path=".travis.yml")
ContentFile(path="CONTRIBUTING.md")
ContentFile(path="COPYING")
ContentFile(path="COPYING.LESSER")
ContentFile(path="MAINTAINERS")
ContentFile(path="MANIFEST.in")
ContentFile(path="README.md")
ContentFile(path="doc")
ContentFile(path="github")
ContentFile(path="manage.sh")
ContentFile(path="requirements.txt")
ContentFile(path="scripts")
ContentFile(path="setup.py")
Get all of the contents of the repository recursively
-----------------------------------------------------

.. code-block:: python
>>> repo = g.get_repo("PyGithub/PyGithub")
>>> contents = repo.get_contents("")
>>> while len(contents) > 1:
... file_content = contents.pop(0)
... if file_content.type == "dir":
... contents.extend(repo.get_contents(file_content.path))
... else:
... print(file_content)
...
ContentFile(path=".gitignore")
ContentFile(path=".travis.yml")
ContentFile(path="CONTRIBUTING.md")
...
ContentFile(path="github/tests/ReplayData/Team.testRepoPermission.txt")
ContentFile(path="github/tests/ReplayData/Team.testRepos.txt")
ContentFile(path="github/tests/ReplayData/UserKey.setUp.txt")
Get a specific content file
---------------------------

.. code-block:: python
>>> repo = g.get_repo("PyGithub/PyGithub")
>>> contents = repo.get_contents("README.md")
>>> print(contents)
...
ContentFile(path="README.md")
Create a new file in the repository
-----------------------------------

.. code-block:: python
>>> repo = g.get_repo("PyGithub/PyGithub")
>>> repo.create_file("test.txt", "test", "test", branch="test")
{'content': ContentFile(path="test.txt"), 'commit': Commit(sha="5b584cf6d32d960bb7bee8ce94f161d939aec377")}
Update a file in the repository
-------------------------------

.. code-block:: python
>>> repo = g.get_repo("PyGithub/PyGithub")
>>> contents = repo.get_contents("test.txt", ref="test")
>>> repo.update_file(contents.path, "more tests", "more tests", contents.sha, branch="test")
{'commit': Commit(sha="b06e05400afd6baee13fff74e38553d135dca7dc"), 'content': ContentFile(path="test.txt")}
Delete a file in the repository
-------------------------------

.. code-block:: python
>>> repo = g.get_repo("PyGithub/PyGithub")
>>> contents = repo.get_contents("test.txt", ref="test")
>>> repo.delete_file(contents.path, "remove test", contents.sha, branch="test")
{'commit': Commit(sha="0f40b0b4f31f62454f1758d7e6b384795e48fd96"), 'content': NotSet}
10 changes: 5 additions & 5 deletions github/Repository.py
Expand Up @@ -1449,7 +1449,7 @@ def get_file_contents(self, path, ref=github.GithubObject.NotSet):
url_parameters["ref"] = ref
headers, data = self._requester.requestJsonAndCheck(
"GET",
self.url + "/contents" + urllib.quote(path),
self.url + "/contents/" + urllib.quote(path),
parameters=url_parameters
)
if isinstance(data, list):
Expand Down Expand Up @@ -1530,7 +1530,7 @@ def create_file(self, path, message, content,

headers, data = self._requester.requestJsonAndCheck(
"PUT",
self.url + "/contents" + urllib.quote(path),
self.url + "/contents/" + urllib.quote(path),
input=put_parameters
)

Expand Down Expand Up @@ -1594,7 +1594,7 @@ def update_file(self, path, message, content, sha,

headers, data = self._requester.requestJsonAndCheck(
"PUT",
self.url + "/contents" + urllib.quote(path),
self.url + "/contents/" + urllib.quote(path),
input=put_parameters
)

Expand Down Expand Up @@ -1644,7 +1644,7 @@ def delete_file(self, path, message, sha,

headers, data = self._requester.requestJsonAndCheck(
"DELETE",
self.url + "/contents" + urllib.quote(path),
self.url + "/contents/" + urllib.quote(path),
input=url_parameters
)

Expand All @@ -1665,7 +1665,7 @@ def get_dir_contents(self, path, ref=github.GithubObject.NotSet):
url_parameters["ref"] = ref
headers, data = self._requester.requestJsonAndCheck(
"GET",
self.url + "/contents" + urllib.quote(path),
self.url + "/contents/" + urllib.quote(path),
parameters=url_parameters
)

Expand Down
6 changes: 3 additions & 3 deletions github/tests/Issue140.py
Expand Up @@ -35,7 +35,7 @@ def setUp(self):
self.repo = self.g.get_repo("twitter/bootstrap")

def testGetDirContentsThenLazyCompletionOfFile(self):
contents = self.repo.get_dir_contents("/js")
contents = self.repo.get_dir_contents("js")
self.assertEqual(len(contents), 15)
n = 0
for content in contents:
Expand All @@ -48,10 +48,10 @@ def testGetDirContentsThenLazyCompletionOfFile(self):
self.assertEqual(n, 2)

def testGetFileContents(self):
contents = self.repo.get_file_contents("/js/bootstrap-affix.js")
contents = self.repo.get_file_contents("js/bootstrap-affix.js")
self.assertEqual(contents.encoding, "base64")
self.assertEqual(contents.url, "https://api.github.com/repos/twitter/bootstrap/contents/js/bootstrap-affix.js")
self.assertEqual(len(contents.content), 4722)

def testGetDirContentsWithRef(self):
self.assertEqual(len(self.repo.get_dir_contents("/js", "8c7f9c66a7d12f47f50618ef420868fe836d0c33")), 15)
self.assertEqual(len(self.repo.get_dir_contents("js", "8c7f9c66a7d12f47f50618ef420868fe836d0c33")), 15)
2 changes: 1 addition & 1 deletion github/tests/Issue174.py
Expand Up @@ -35,5 +35,5 @@ def setUp(self):
self.repo = self.g.get_repo("twitter/bootstrap")

def testGetDirContentsWhithHttpRedirect(self):
contents = self.repo.get_dir_contents("/js/")
contents = self.repo.get_dir_contents("js/")
self.assertEqual(len(contents), 15)
Expand Up @@ -332,7 +332,7 @@ https
GET
api.github.com
None
/repos/jacquev6/PyGithub/contentsREADME.rst
/repos/jacquev6/PyGithub/contents/README.rst
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
None
200
Expand Down
12 changes: 6 additions & 6 deletions github/tests/Repository.py
Expand Up @@ -513,28 +513,28 @@ def testAssignees(self):

def testGetContents(self):
self.assertEqual(len(self.repo.get_readme().content), 10212)
self.assertEqual(len(self.repo.get_contents("/doc/ReferenceOfClasses.md").content), 38121)
self.assertEqual(len(self.repo.get_contents("doc/ReferenceOfClasses.md").content), 38121)

def testGetContentDir(self):

contents = self.repo.get_contents("/")
contents = self.repo.get_contents("")
self.assertTrue(isinstance(contents, list))
self.assertEquals(len(contents), 14)

def testGetContentsWithRef(self):
self.assertEqual(len(self.repo.get_readme(ref="refs/heads/topic/ExperimentOnDocumentation").content), 6747)
self.assertEqual(len(self.repo.get_contents("/doc/ReferenceOfClasses.md", ref="refs/heads/topic/ExperimentOnDocumentation").content), 43929)
self.assertEqual(len(self.repo.get_contents("doc/ReferenceOfClasses.md", ref="refs/heads/topic/ExperimentOnDocumentation").content), 43929)

def testCreateFile(self):
newFile = '/doc/testCreateUpdateDeleteFile.md'
newFile = 'doc/testCreateUpdateDeleteFile.md'
content = 'Hello world'
self.repo.create_file(
path=newFile, message='Create file for testCreateFile', content=content,
branch="master", committer=github.InputGitAuthor("Enix Yu", "enix223@163.com", "2016-01-15T16:13:30+12:00"),
author=github.InputGitAuthor("Enix Yu", "enix223@163.com", "2016-01-15T16:13:30+12:00"))

def testUpdateFile(self):
updateFile = '/doc/testCreateUpdateDeleteFile.md'
updateFile = 'doc/testCreateUpdateDeleteFile.md'
content = 'Hello World'
sha = self.repo.get_contents(updateFile).sha
self.repo.update_file(
Expand All @@ -543,7 +543,7 @@ def testUpdateFile(self):
author=github.InputGitAuthor("Enix Yu", "enix223@163.com", "2016-01-15T16:13:30+12:00"))

def testDeleteFile(self):
deleteFile = '/doc/testCreateUpdateDeleteFile.md'
deleteFile = 'doc/testCreateUpdateDeleteFile.md'
sha = self.repo.get_contents(deleteFile).sha
self.repo.delete_file(path=deleteFile, message='Delete file for testDeleteFile', sha=sha, branch="master")

Expand Down

0 comments on commit ee9f098

Please sign in to comment.