1
1
# -*- coding: utf-8 -*-
2
+ """Implementation of a branch on a repository."""
2
3
from __future__ import unicode_literals
3
4
4
5
from json import dumps
5
6
6
- from ..models import GitHubCore
7
+ from . import commit
8
+ from .. import models
9
+
7
10
from .commit import RepoCommit
8
11
9
12
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:
14
19
"""
15
20
16
21
# The Accept header will likely be removable once the feature is out of
17
22
# preview mode. See: http://git.io/v4O1e
18
23
PREVIEW_HEADERS = {'Accept' : 'application/vnd.github.loki-preview+json' }
19
24
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'
30
26
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' ]
40
30
41
31
def _repr (self ):
42
- return '<Repository Branch [{0 }]>' .format (self .name )
32
+ return '<{0} [{1 }]>' .format (self . class_name , self .name )
43
33
44
34
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.
46
36
47
37
See: https://git.io/vaqIw
48
38
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
51
43
"""
52
44
# If-None-Match returns 200 instead of 304 value does not have quotes
53
45
headers = {
@@ -66,12 +58,18 @@ def protect(self, enforcement=None, status_checks=None):
66
58
67
59
See: http://git.io/v4Gvu
68
60
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
75
73
"""
76
74
previous_values = None
77
75
if self .protection :
@@ -95,3 +93,80 @@ def unprotect(self):
95
93
headers = self .PREVIEW_HEADERS ), 200 )
96
94
self ._update_attributes (json )
97
95
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 )
0 commit comments