|
24 | 24 | from github3.repos.comment import RepoComment
|
25 | 25 | from github3.repos.commit import RepoCommit
|
26 | 26 | from github3.repos.comparison import Comparison
|
27 |
| -from github3.repos.contents import Contents |
| 27 | +from github3.repos.contents import Contents, validate_commmitter |
28 | 28 | from github3.repos.download import Download
|
29 | 29 | from github3.repos.hook import Hook
|
30 | 30 | from github3.repos.status import Status
|
@@ -304,9 +304,9 @@ def contents(self, path, ref=None):
|
304 | 304 | url = self._build_url('contents', path, base_url=self._api)
|
305 | 305 | json = self._json(self._get(url, params={'ref': ref}), 200)
|
306 | 306 | if isinstance(json, dict):
|
307 |
| - return Contents(json) |
| 307 | + return Contents(json, self) |
308 | 308 | elif isinstance(json, list):
|
309 |
| - return dict((j.get('name'), Contents(j)) for j in json) |
| 309 | + return dict((j.get('name'), Contents(j, self)) for j in json) |
310 | 310 | return None
|
311 | 311 |
|
312 | 312 | @requires_auth
|
@@ -409,18 +409,14 @@ def create_file(self, path, message, content, branch=None,
|
409 | 409 | if path and message and content:
|
410 | 410 | url = self._build_url('contents', path, base_url=self._api)
|
411 | 411 | content = b64encode(content).decode('utf-8')
|
412 |
| - |
413 |
| - data = {'message': message, 'content': content, |
414 |
| - 'branch': branch} |
415 |
| - if committer and committer.get('name') and committer.get('email'): |
416 |
| - data.update(committer=committer) |
417 |
| - if author and author.get('name') and author.get('email'): |
418 |
| - data.update(author=author) |
419 |
| - |
| 412 | + data = {'message': message, 'content': content, 'branch': branch, |
| 413 | + 'committer': validate_commmitter(committer), |
| 414 | + 'author': validate_commmitter(author)} |
420 | 415 | self._remove_none(data)
|
421 | 416 | json = self._json(self._put(url, data=dumps(data)), 201)
|
422 |
| - json['content'] = Contents(json['content']) |
423 |
| - json['commit'] = Commit(json['commit'], self) |
| 417 | + if 'content' in json and 'commit' in json: |
| 418 | + json['content'] = Contents(json['content'], self) |
| 419 | + json['commit'] = Commit(json['commit'], self) |
424 | 420 | return json
|
425 | 421 |
|
426 | 422 | @requires_auth
|
@@ -674,6 +670,41 @@ def delete(self):
|
674 | 670 | """
|
675 | 671 | return self._boolean(self._delete(self._api), 204, 404)
|
676 | 672 |
|
| 673 | + @requires_auth |
| 674 | + def delete_file(self, path, message, sha, branch=None, committer=None, |
| 675 | + author=None): |
| 676 | + """Delete the file located at ``path``. |
| 677 | +
|
| 678 | + This is part of the Contents CrUD (Create Update Delete) API. See |
| 679 | + http://developer.github.com/v3/repos/contents/#delete-a-file for more |
| 680 | + information. |
| 681 | +
|
| 682 | + :param str path: (required), path to the file being removed |
| 683 | + :param str message: (required), commit message for the deletion |
| 684 | + :param str sha: (required), blob sha of the file being removed |
| 685 | + :param str branch: (optional), if not provided, uses the repository's |
| 686 | + default branch |
| 687 | + :param dict committer: (optional), if no information is given the |
| 688 | + authenticated user's information will be used. You must specify |
| 689 | + both a name and email. |
| 690 | + :param dict author: (optional), if omitted this will be filled in with |
| 691 | + committer information. If passed, you must specify both a name and |
| 692 | + email. |
| 693 | + :returns: :class:`Commit <github3.git.Commit>` if successful |
| 694 | +
|
| 695 | + """ |
| 696 | + json = None |
| 697 | + if path and message and sha: |
| 698 | + url = self._build_url('contents', path, base_url=self._api) |
| 699 | + data = {'message': message, 'sha': sha, 'branch': branch, |
| 700 | + 'committer': validate_commmitter(committer), |
| 701 | + 'author': validate_commmitter(author)} |
| 702 | + self._remove_none(data) |
| 703 | + json = self._json(self._delete(url, data=dumps(data)), 200) |
| 704 | + if json and 'commit' in json: |
| 705 | + json = Commit(json['commit']) |
| 706 | + return json |
| 707 | + |
677 | 708 | @requires_auth
|
678 | 709 | def delete_key(self, key_id):
|
679 | 710 | """Delete the key with the specified id from your deploy keys list.
|
@@ -1382,7 +1413,7 @@ def readme(self):
|
1382 | 1413 | """
|
1383 | 1414 | url = self._build_url('readme', base_url=self._api)
|
1384 | 1415 | json = self._json(self._get(url), 200)
|
1385 |
| - return Contents(json) if json else None |
| 1416 | + return Contents(json, self) if json else None |
1386 | 1417 |
|
1387 | 1418 | def ref(self, ref):
|
1388 | 1419 | """Get a reference pointed to by ``ref``.
|
@@ -1466,6 +1497,46 @@ def tree(self, sha):
|
1466 | 1497 | json = self._json(self._get(url), 200)
|
1467 | 1498 | return Tree(json, self) if json else None
|
1468 | 1499 |
|
| 1500 | + @requires_auth |
| 1501 | + def update_file(self, path, message, content, sha, branch=None, |
| 1502 | + author=None, committer=None): |
| 1503 | + """Update the file ``path`` with ``content``. |
| 1504 | +
|
| 1505 | + This is part of the Contents CrUD (Create Update Delete) API. See |
| 1506 | + http://developer.github.com/v3/repos/contents/#update-a-file for more |
| 1507 | + information. |
| 1508 | +
|
| 1509 | + :param str path: (required), path to the file being updated |
| 1510 | + :param str message: (required), commit message |
| 1511 | + :param str content: (required), updated contents of the file |
| 1512 | + :param str sha: (required), blob sha of the file being updated |
| 1513 | + :param str branch: (optional), uses the default branch on the |
| 1514 | + repository if not provided. |
| 1515 | + :param dict author: (optional), if omitted this will be filled in with |
| 1516 | + committer information. If passed, you must specify both a name and |
| 1517 | + email. |
| 1518 | + :returns: {'commit': :class:`Commit <github3.git.Commit>`, |
| 1519 | + 'content': :class:`Contents <github3.repos.contents.Contents>`} |
| 1520 | +
|
| 1521 | + """ |
| 1522 | + if content and not isinstance(content, bytes): |
| 1523 | + raise ValueError( # (No coverage) |
| 1524 | + 'content must be a bytes object') # (No coverage) |
| 1525 | + |
| 1526 | + json = None |
| 1527 | + if path and message and content and sha: |
| 1528 | + url = self._build_url('contents', path, base_url=self._api) |
| 1529 | + content = b64encode(content).decode('utf-8') |
| 1530 | + data = {'message': message, 'content': content, 'sha': sha, |
| 1531 | + 'committer': validate_commmitter(committer), |
| 1532 | + 'author': validate_commmitter(author)} |
| 1533 | + self._remove_none(data) |
| 1534 | + json = self._json(self._put(url, data=dumps(data)), 200) |
| 1535 | + if 'content' in json and 'commit' in json: |
| 1536 | + json['content'] = Contents(json['content'], self) |
| 1537 | + json['commit'] = Commit(json['commit'], self) |
| 1538 | + return json |
| 1539 | + |
1469 | 1540 | @requires_auth
|
1470 | 1541 | def update_label(self, name, color, new_name=''):
|
1471 | 1542 | """Update the label ``name``.
|
|
0 commit comments