Skip to content
This repository was archived by the owner on May 22, 2021. It is now read-only.

Commit a38a111

Browse files
committed
Update github3.repos.branch for consistency
1 parent c74e5c0 commit a38a111

File tree

4 files changed

+114
-37
lines changed

4 files changed

+114
-37
lines changed

github3/repos/branch.py

+109-34
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,45 @@
11
# -*- coding: utf-8 -*-
2+
"""Implementation of a branch on a repository."""
23
from __future__ import unicode_literals
34

45
from json import dumps
56

6-
from ..models import GitHubCore
7+
from . import commit
8+
from .. import models
9+
710
from .commit import RepoCommit
811

912

10-
class Branch(GitHubCore):
11-
"""The :class:`Branch <Branch>` object. It holds the information GitHub
12-
returns about a branch on a
13-
:class:`Repository <github3.repos.repo.Repository>`.
13+
class _Branch(models.GitHubCore):
14+
"""A representation of a branch on a repository.
15+
16+
See also https://developer.github.com/v3/repos/branches/
17+
18+
This object has the following attributes:
1419
"""
1520

1621
# The Accept header will likely be removable once the feature is out of
1722
# preview mode. See: http://git.io/v4O1e
1823
PREVIEW_HEADERS = {'Accept': 'application/vnd.github.loki-preview+json'}
1924

20-
def _update_attributes(self, branch):
21-
#: Name of the branch.
22-
self.name = self._get_attribute(branch, 'name')
23-
24-
#: Returns the branch's
25-
#: :class:`RepoCommit <github3.repos.commit.RepoCommit>` or ``None``.
26-
self.commit = self._class_attribute(branch, 'commit', RepoCommit, self)
27-
28-
#: Returns '_links' attribute.
29-
self.links = self._get_attribute(branch, '_links', [])
25+
class_name = 'Repository Branch'
3026

31-
#: Provides the branch's protection status.
32-
self.protection = self._get_attribute(branch, 'protection')
33-
34-
if self.links and 'self' in self.links:
35-
self._api = self.links['self']
36-
elif isinstance(self.commit, RepoCommit):
37-
# Branches obtained via `repo.branches` don't have links.
38-
base = self.commit.url.split('/commit', 1)[0]
39-
self._api = self._build_url('branches', self.name, base_url=base)
27+
def _update_attributes(self, branch):
28+
self.commit = commit.RepoCommit(branch['commit'], self)
29+
self.name = branch['name']
4030

4131
def _repr(self):
42-
return '<Repository Branch [{0}]>'.format(self.name)
32+
return '<{0} [{1}]>'.format(self.class_name, self.name)
4333

4434
def latest_sha(self, differs_from=''):
45-
"""Check if SHA-1 is the same as remote branch
35+
"""Check if SHA-1 is the same as remote branch.
4636
4737
See: https://git.io/vaqIw
4838
49-
:param str differs_from: (optional), sha to compare against
50-
:returns: string of the SHA or None
39+
:param str differs_from:
40+
(optional), sha to compare against
41+
:returns:
42+
string of the SHA or None
5143
"""
5244
# If-None-Match returns 200 instead of 304 value does not have quotes
5345
headers = {
@@ -66,12 +58,18 @@ def protect(self, enforcement=None, status_checks=None):
6658
6759
See: http://git.io/v4Gvu
6860
69-
:param str enforcement: (optional), Specifies the enforcement level of
70-
the status checks. Must be one of 'off', 'non_admins', or
71-
'everyone'. Use `None` or omit to use the already associated value.
72-
:param list status_checks: (optional), An list of strings naming
73-
status checks that must pass before merging. Use `None` or omit to
74-
use the already associated value.
61+
:param str enforcement:
62+
(optional), Specifies the enforcement level of the status checks.
63+
Must be one of 'off', 'non_admins', or 'everyone'. Use `None` or
64+
omit to use the already associated value.
65+
:param list status_checks:
66+
(optional), An list of strings naming status checks that must pass
67+
before merging. Use `None` or omit to use the already associated
68+
value.
69+
:returns:
70+
True if successful, False otherwise
71+
:rtype:
72+
bool
7573
"""
7674
previous_values = None
7775
if self.protection:
@@ -95,3 +93,80 @@ def unprotect(self):
9593
headers=self.PREVIEW_HEADERS), 200)
9694
self._update_attributes(json)
9795
return True
96+
97+
98+
class ShortBranch(_Branch):
99+
"""The representation of a branch returned in a collection.
100+
101+
GitHub's API returns different amounts of information about repositories
102+
based upon how that information is retrieved. This object exists to
103+
represent the limited amount of information returned for a specific
104+
branch in a collection. For example, you would receive this class when
105+
calling :meth:`~github3.repos.repo.Repository.branches`. To provide a
106+
clear distinction between the types of branches, github3.py uses different
107+
classes with different sets of attributes.
108+
109+
This object has the following attributes:
110+
111+
.. attribute:: commit
112+
113+
A :class:`~github3.repos.commit.RepoCommit` representation of the
114+
newest commit on this branch with the associated repository metadata.
115+
116+
.. attribute:: name
117+
118+
The name of this branch.
119+
"""
120+
121+
class_name = 'Short Repository Branch'
122+
123+
124+
class Branch(_Branch):
125+
"""The representation of a branch returned in a collection.
126+
127+
GitHub's API returns different amounts of information about repositories
128+
based upon how that information is retrieved. This object exists to
129+
represent the limited amount of information returned for a specific
130+
branch in a collection. For example, you would receive this class when
131+
calling :meth:`~github3.repos.repo.Repository.branches`. To provide a
132+
clear distinction between the types of branches, github3.py uses different
133+
classes with different sets of attributes.
134+
135+
This object has the same attributes as a
136+
:class:`~github3.repos.branch.ShortBranch` as well as the following:
137+
138+
.. attribute:: links
139+
140+
The dictionary of URLs returned by the API as ``_links``.
141+
142+
.. attribute:: protected
143+
144+
A boolean attribute that describes whether this branch is protected or
145+
not.
146+
147+
.. attribute:: protection
148+
149+
A dictionary with details about the protection configuration of this
150+
branch.
151+
152+
.. attribute:: protection_url
153+
154+
The URL to access and manage details about this branch's protection.
155+
"""
156+
157+
class_name = 'Repository Branch'
158+
159+
def _update_attributes(self, branch):
160+
super(Branch, self)._update_attributes(branch)
161+
#: Returns '_links' attribute.
162+
self.links = branch['_links']
163+
#: Provides the branch's protection status.
164+
self.protected = branch['protected']
165+
self.protection = branch['protection']
166+
self.protection_url = branch['protection_url']
167+
if self.links and 'self' in self.links:
168+
self._api = self.links['self']
169+
elif isinstance(self.commit, RepoCommit):
170+
# Branches obtained via `repo.branches` don't have links.
171+
base = self.commit.url.split('/commit', 1)[0]
172+
self._api = self._build_url('branches', self.name, base_url=base)

github3/repos/repo.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from ..projects import Project
2828
from ..pulls import ShortPullRequest, PullRequest
2929
from ..utils import stream_response_to_file, timestamp_parameter
30+
from . import branch
3031
from .branch import Branch
3132
from .comment import RepoComment
3233
from .commit import RepoCommit
@@ -220,8 +221,8 @@ def branches(self, number=-1, protected=False, etag=None):
220221
"""
221222
url = self._build_url('branches', base_url=self._api)
222223
params = {'protected': '1'} if protected else None
223-
return self._iter(int(number), url, Branch, params, etag=etag,
224-
headers=Branch.PREVIEW_HEADERS)
224+
return self._iter(int(number), url, branch.ShortBranch, params,
225+
etag=etag, headers=branch.Branch.PREVIEW_HEADERS)
225226

226227
def code_frequency(self, number=-1, etag=None):
227228
"""Iterate over the code frequency per week.

tests/integration/test_repos_repo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_branches(self):
5555
repository = self.gh.repository('sigmavirus24', 'github3.py')
5656
assert repository is not None
5757
for branch in repository.branches():
58-
assert isinstance(branch, github3.repos.branch.Branch)
58+
assert isinstance(branch, github3.repos.branch.ShortBranch)
5959

6060
def test_project(self):
6161
"""Test the ability to retrieve a single repository project."""

tests/unit/json/repos_branch_example

+1
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,6 @@
7575
"self": "https://api.github.com/repos/octocat/Hello-World/branches/master"
7676
},
7777
"protected": true,
78+
"protection": {"enabled": true, "required_status_checks": {"enforcement_level": "non_admins", "contexts": ["continuous-integration/travis-ci"]}},
7879
"protection_url": "https://api.github.com/repos/octocat/Hello-World/branches/master/protection"
7980
}

0 commit comments

Comments
 (0)