8000 Merge branch 'pr/552' into develop · pythonthings/github3.py@35932cf · GitHub
[go: up one dir, main page]

Skip to content

Commit 35932cf

Browse files
committed
Merge branch 'pr/552' into develop
2 parents bcb8173 + 5c61661 commit 35932cf

File tree

6 files changed

+126
-25
lines changed

6 files changed

+126
-25
lines changed

github3/repos/repo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ def create_file(self, path, message, content, branch=None,
743743
'author': validate_commmitter(author)}
744744
self._remove_none(data)
745745
json = self._json(self._put(url, data=dumps(data)), 201)
746-
if 'content' in json and 'commit' in json:
746+
if json and 'content' in json and 'commit' in json:
747747
json['content'] = Contents(json['content'], self)
748748
json['commit'] = Commit(json['commit'], self)
749749
return json
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"http_interactions": [{"request": {"body": {"string": "", "encoding": "utf-8"}, "headers": {"Accept-Encoding": "gzip, deflate", "Accept": "application/vnd.github.v3.full+json", "User-Agent": "github3.py/1.0.0a2", "Accept-Charset": "utf-8", "Connection": "keep-alive", "Content-Type": "application/json", "Authorization": "token <AUTH_TOKEN>"}, "method": "GET", "uri": "https://api.github.com/repos/itsmemattchung/github3.py"}, "response": {"body": {"string": "", "base64_string": "H4sIAAAAAAAAA+2bS4/qNhSA/wrKtgwhBOYRqbrtqu2ui+mmG2SCIdbNS7bDiBvNf+85doAkd0xCnO6yGTFgfzlx/DofpnTY3gnW/pvne6vnuZOShDqBc2QyKnb+Ij87c+dQxPG2+oBJkdCESBlGRXp0G+Wyj5RyJyidODuyFCjNwkDCa734m1dvuZo75EQk4duCx1A0kjIXgevqN8VCgwtBeZilkqZyEWaJW7hV7W+nX33gHXkFQbADb7RgOatAujbQhPtTUJFM4lYU+uqq0k/FD1kcZx9AakfefTH3WhdbVXFYehzMgbqlm8mIQiPCrX1igzAhhwSm6pXwOIXcsj2SBDwbTvcDgqtqQmjYHz5Ll9M8U8hiJ0LOcsm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l0sY51klt1KYGcBW5DStrlYLS2eXGHPt88Y9qH/29ECKWG71xh7uYQ9ZSZzl2JUoT+Am0Kmge6tSe509Yze7ZNI4P+nXmPHD+oRtVSol5794yxfP67Z/kP8k5MR4IVbrDvdXLwpR4mVW6+WrDzczSPzpyhberxXQfcfRKvyQ82s00mDj9wXFxve1npyF7WuQxnN9TWzdFELvedj0NWiPer5GZbV4Qgh9TAKm88JtVB/H8bUiajhCiG1sw9fqLLWvAkb0e5Vr7CH4zOH01nsGxMNyz8SxVnsm8Fhiz8R/WOuZQEOlnok3gtIzoYcJPRPNUueZsDYyz8R8VOWZOGgDhou8e9TBGu8edJjEu0ccrvBMVDuBZ6IO13cmopW8M0HrEhC394+pOxO1hrlBe4u7DqriAPYBu2Yitr0aJn4jYHGQttEXDdZHn9wLF4WXlbAzwUfSdSb8RfkNk3VmqvJ9VqrOxB4i6kyscTSdiT5M0ploForOhLQUdCbsSHrOhP8/5JzpWjZqzsS0FHMm7H0tt3pa+qjcUMu9BhvfqOVWm/flS7DZgJm7p+Veg/VbsH7t0HKGWLuk3P1qHUrufmVxT8gZqsKiNkzHPS/XX+m49TOIpbaQU292KTntsFC+PWTkdLWGkPOeX+B7kz5Kbg1KsXJyqlLDyuGHlzuBQ3N4D0YxB5JNZAUPQVROkq12wK9mhOsdEHrdJNlge6aOhLQc0iTZfj7F2Zi8JslmOkQ3STb9jZn+CtSw4jWOy0FeM/j0nIk/Sba+p+ZMLThJNlggUQz0PS1naki0ApNkG3RKztSkk2SDA3ywaZkkmz7UOkk2mxNxplE2xnk4E3uSbJfz8r3PwZmacpJsifq9UyuF01kv/p7OnSSb4ezbJNn0jy7vnnmbJBscaLuefgPJllL5Ab/QuhypU9Ku/gVidSTP+/wP9OpJWuo6AAA=", "encoding": "utf-8"}, "headers": {"vary": "Accept, Authorization, Cookie, X-GitHub-OTP", "x-github-media-type": "github.v3; param=full; format=json", "x-oauth-scopes": "admin:public_key, gist, repo, user", "x-xss-protection": "1; mode=block", "x-content-type-options": "nosniff", "x-accepted-oauth-scopes": "repo", "etag": "W/\"4d1be7e433e53edabdf4507474802fe5\"", "cache-control": "private, max-age=60, s-maxage=60", "status": "200 OK", "x-ratelimit-remaining": "4998", "x-served-by": "c6c65e5196703428e7641f7d1e9bc353", "access-control-expose-headers": "ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval", "transfer-encoding": "chunked", "x-github-request-id": "97E45D32:135D3:47891EA:56A5D86C", "access-control-allow-credentials": "true", "last-modified": "Wed, 13 Jan 2016 04:32:42 GMT", "date": "Mon, 25 Jan 2016 08:10:20 GMT", "access-control-allow-origin": "*", "content-security-policy": "default-src 'none'", "content-encoding": "gzip", "strict-transport-security": "max-age=31536000; includeSubdomains; preload", "server": "GitHub.com", "x-ratelimit-limit": "5000", "x-frame-options": "deny", "content-type": "application/json; charset=utf-8", "x-ratelimit-reset": "1453710145"}, "status": {"message": "OK", "code": 200}, "url": "https://api.github.com/repos/itsmemattchung/github3.py"}, "recorded_at": "2016-01-25T08:10:20"}, {"request": {"body": {"string": "{\"content\": \"YlhrZ2JtVjNJR1pwYkdVZ1kyOXVkR1Z1ZEhNPQ==\", \"committer\": {\"name\": \"Matt Chung\", \"email\": \"hello@itsmemattchung.com\"}, \"message\": \"my commit message\", \"branch\": \"develop\"}", "encoding": "utf-8"}, "headers": {"Content-Length": "182", "Accept-Encoding": "gzip, deflate", "Accept": "application/vnd.github.v3.full+json", "User-Agent": "github3.py/1.0.0a2", "Accept-Charset": "utf-8", "Connection": "keep-alive", "Content-Type": "application/json", "Authorization": "token <AUTH_TOKEN>"}, "method": "PUT", "uri": "https://api.github.com/repos/itsmemattchung/github3.py/contents/hello.txt"}, "response": {"body": {"string": "{\"content\":{\"name\":\"hello.txt\",\"path\":\"hello.txt\",\"sha\":\"9bd2e7613aee7ad02fd5dff8d2dc4fb24113d8be\",\"size\":28,\"url\":\"https://api.github.com/repos/itsmemattchung/github3.py/contents/hello.txt?ref=develop\",\"html_url\":\"https://github.com/itsmemattchung/github3.py/blob/develop/hello.txt\",\"git_url\":\"https://api.github.com/repos/itsmemattchung/github3.py/git/blobs/9bd2e7613aee7ad02fd5dff8d2dc4fb24113d8be\",\"download_url\":\"https://raw.githubusercontent.com/itsmemattchung/github3.py/develop/hello.txt\",\"type\":\"file\",\"_links\":{\"self\":\"https://api.github.com/repos/itsmemattchung/github3.py/contents/hello.txt?ref=develop\",\"git\":\"https://api.github.com/repos/itsmemattchung/github3.py/git/blobs/9bd2e7613aee7ad02fd5dff8d2dc4fb24113d8be\",\"html\":\"https://github.com/itsmemattchung/github3.py/blob/develop/hello.txt\"}},\"commit\":{\"sha\":\"ba371d450dce54345ce707fc9156955fee43e583\",\"url\":\"https://api.github.com/repos/itsmemattchung/github3.py/git/commits/ba371d450dce54345ce707fc9156955fee43e583\",\"html_url\":\"https://github.com/itsmemattchung/github3.py/commit/ba371d450dce54345ce707fc9156955fee43e583\",\"author\":{\"name\":\"Matt Chung\",\"email\":\"hello@itsmemattchung.com\",\"date\":\"2016-01-25T08:10:20Z\"},\"committer\":{\"name\":\"Matt Chung\",\"email\":\"hello@itsmemattchung.com\",\"date\":\"2016-01-25T08:10:20Z\"},\"tree\":{\"sha\":\"eb6943ffd8513bd092851abfeaf9fa175aea83b2\",\"url\":\"https://api.github.com/repos/itsmemattchung/github3.py/git/trees/eb6943ffd8513bd092851abfeaf9fa175aea83b2\"},\"message\":\"my commit message\",\"parents\":[{\"sha\":\"24893ec07db2a12073703258f0089f105906d2e4\",\"url\":\"https://api.github.com/repos/itsmemattchung/github3.py/git/commits/24893ec07db2a12073703258f0089f105906d2e4\",\"html_url\":\"https://github.com/itsmemattchung/github3.py/commit/24893ec07db2a12073703258f0089f105906d2e4\"}]}}", "encoding": "utf-8"}, "headers": {"content-length": "1773", "vary": "Accept, Authorization, Cookie, X-GitHub-OTP", "x-github-media-type": "github.v3; param=full; format=json", "x-oauth-scopes": "admin:public_key, gist, repo, user", "x-xss-protection": "1; mode=block", "x-content-type-options": "nosniff", "x-accepted-oauth-scopes": "", "etag": "\"e55ec03878f29fdd62c34df72ab6a40e\"", "cache-control": "private, max-age=60, s-maxage=60", "status": "201 Created", "x-ratelimit-remaining": "4997", "x-served-by": "a474937f3b2fa272558fa6dc951018ad", "access-control-expose-headers": "ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval", "x-github-request-id": "97E45D32:135D3 10000 :478922E:56A5D86C", "access-control-allow-credentials": "true", "date": "Mon, 25 Jan 2016 08:10:20 GMT", "access-control-allow-origin": "*", "content-security-policy": "default-src 'none'", "strict-transport-security": "max-age=31536000; includeSubdomains; preload", "server": "GitHub.com", "x-ratelimit-limit": "5000", "x-frame-options": "deny", "content-type": "application/json; charset=utf-8", "x-ratelimit-reset": "1453710145"}, "status": {"message": "Created", "code": 201}, "url": "https://api.github.com/repos/itsmemattchung/github3.py/contents/hello.txt"}, "recorded_at": "2016-01-25T08:10:21"}], "recorded_with": "betamax/0.5.0"}

tests/integration/test_repos_repo.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,34 @@ def test_create_deployment(self):
236236

237237
assert isinstance(deployment, github3.repos.deployment.Deployment)
238238

239+
def test_create_file(self):
240+
"""Test the ability to create a file on a repository."""
241+
self.token_login()
242+
cassette_name = self.cassette_name('create_file')
243+
with self.recorder.use_cassette(cassette_name):
244+
repository = self.gh.repository('itsmemattchung', 'github3.py')
245+
data = {
246+
'path': 'hello.txt',
247+
'message': 'my commit message',
248+
'content': b'bXkgbmV3IGZpbGUgY29udGVudHM=',
249+
'branch': 'develop',
250+
'committer': {
251+
'name': 'Matt Chung',
252+
'email': 'hello@itsmemattchung.com'
253+
}
254+
}
255+
256+
created_file = repository.create_file(**data)
257+
258+
assert isinstance(
259+
created_file['content'],
260+
github3.repos.contents.Contents
261+
)
262+
assert isinstance(
263+
created_file['commit'],
264+
github3.git.Commit
265+
)
266+
239267
def test_create_fork(self):
240268
"""Test the ability to fork a repository."""
241269
self.token_login()

tests/test_repos.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,6 @@ def test_repr(self):
2222
def test_equality(self):
2323
assert self.repo == repos.Repository(load('repo'))
2424

25-
def test_create_file(self):
26-
self.response('create_content', 201)
27-
self.put(self.api + 'contents/setup.py')
28-
self.conf = {'data': {'message': 'Foo bar',
29-
'content': 'Zm9vIGJhciBib2d1cw==',
30-
'branch': 'develop',
31-
'author': {'name': 'Ian', 'email': 'foo'},
32-
'committer': {'name': 'Ian', 'email': 'foo'}}}
33-
34-
self.assertRaises(github3.GitHubError, self.repo.create_file,
35-
None, None, None)
36-
37-
self.not_called()
38-
self.login()
39-
40-
ret = self.repo.create_file('setup.py', 'Foo bar', b'foo bar bogus',
41-
'develop',
42-
{'name': 'Ian', 'email': 'foo'},
43-
{'name': 'Ian', 'email': 'foo'})
44-
assert isinstance(ret, dict)
45-
assert isinstance(ret['commit'], github3.git.Commit)
46-
assert isinstance(ret['content'], repos.contents.Contents)
47-
self.mock_assertions()
48-
4925
def test_weekly_commit_count(self):
5026
self.response('weekly_commit_count', ETag='"foobarbogus"')
5127
self.request.return_value.headers['Last-Modified'] = 'foo'
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"content": {
3+
"name": "hello.txt",
4+
"path": "notes/hello.txt",
5+
"sha": "95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
6+
"size": 9,
7+
"url": "https://api.github.com/repos/octocat/Hello-World/contents/notes/hello.txt",
8+
"html_url": "https://github.com/octocat/Hello-World/blob/master/notes/hello.txt",
9+
"git_url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
10+
"download_url": "https://raw.githubusercontent.com/octocat/HelloWorld/master/notes/hello.txt",
11+
"type": "file",
12+
"_links": {
13+
"self": "https://api.github.com/repos/octocat/Hello-World/contents/notes/hello.txt",
14+
"git": "https://api.github.com/repos/octocat/Hello-World/git/blobs/95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
15+
"html": "https://github.com/octocat/Hello-World/blob/master/notes/hello.txt"
16+
}
17+
},
18+
"commit": {
19+
"sha": "7638417db6d59f3c431d3e1f261cc637155684cd",
20+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/7638417db6d59f3c431d3e1f261cc637155684cd",
21+
"html_url": "https://github.com/octocat/Hello-World/git/commit/7638417db6d59f3c431d3e1f261cc637155684cd",
22+
"author": {
23+
"date": "2014-11-07T22:01:45Z",
24+
"name": "Scott Chacon",
25+
"email": "schacon@gmail.com"
26+
},
27+
"committer": {
28+
"date": "2014-11-07T22:01:45Z",
29+
"name": "Scott Chacon",
30+
"email": "schacon@gmail.com"
31+
},
32+
"message": "my commit message",
33+
"tree": {
34+
"url": "https://api.github.com/repos/octocat/Hello-World/git/trees/691272480426f78a0138979dd3ce63b77f706feb",
35+
"sha": "691272480426f78a0138979dd3ce63b77f706feb"
36+
},
37+
"parents": [
38+
{
39+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/1acc419d4d6a9ce985db7be48c6349a0475975b5",
40+
"html_url": "https://github.com/octocat/Hello-World/git/commit/1acc419d4d6a9ce985db7be48c6349a0475975b5",
41+
"sha": "1acc419d4d6a9ce985db7be48c6349a0475975b5"
42+
}
43+
]
44+
}
45+
}

tests/unit/test_repos_repo.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import mock
44
import pytest
55

6+
from base64 import b64encode
67
from github3 import GitHubError
78
from github3.null import NullObject
89
from github3.repos.repo import Repository
@@ -17,6 +18,10 @@
1718
get_repo_example_data = helper.create_example_data_helper(
1819
'repos_repo_example'
1920
)
21+
create_file_contents_example_data = helper.create_example_data_helper(
22+
'create_file_contents_example'
23+
)
24+
create_file_contents_example_data = create_file_contents_example_data()
2025
repo_example_data = get_repo_example_data()
2126

2227

@@ -61,6 +66,46 @@ def test_asset_requires_a_positive_id(self):
6166

6267
assert self.session.get.called is False
6368

69+
def test_create_file(self):
70+
"""Verify the request for creating a file on a repository."""
71+
data = {
72+
'path': 'hello.txt',
73+
'message': 'my commit message',
74+
'content': b'bXkgbmV3IGZpbGUgY29udGVudHM=',
75+
'committer': {
76+
'name': 'Scott Chacon',
77+
'email': 'schacon@gmail.com'
78+
}
79+
}
80+
81+
self.instance.create_file(**data)
82+
83+
b64_encoded_content = b64encode(data['content']).decode('utf-8')
84+
data.update({
85+
'content': b64_encoded_content
86+
})
87+
del(data['path'])
88+
89+
self.put_called_with(
90+
url_for('contents/hello.txt'),
91+
data=data
92+
)
93+
94+
def test_create_file_required_content(self):
95+
"""Verify the request for creating a file on a repository."""
96+
data = {
97+
'path': 'hello.txt',
98+
'message': 'my commit message',
99+
'content': 123,
100+
'committer': {
101+
'name': 'Scott Chacon',
102+
'email': 'schacon@gmail.com'
103+
}
104+
}
105+
106+
with pytest.raises(ValueError):
107+
self.instance.create_file(**data)
108+
64109
def test_create_fork(self):
65110
"""Verify the request to fork a repository."""
66111
self.instance.create_fork()
@@ -1305,6 +1350,12 @@ def test_create_ref(self):
13051350
with pytest.raises(GitHubError):
13061351
self.instance.create_ref('some ref', 'some sha')
13071352

1353+
def test_create_file(self):
1354+
"""
1355+
Verify that creating a file on a repository requires authentication.
1356+
"""
1357+
self.assert_requires_auth(self.instance.create_file)
1358+
13081359
def test_create_fork(self):
13091360
"""Verify that creating a fork requires authentication."""
13101361
with pytest.raises(GitHubError):

0 commit comments

Comments
 (0)
0