Skip to content

Commit 8bb91a1

Browse files
committed
branch_exists() method, closes #4
Refs #3
1 parent f0cd0b7 commit 8bb91a1

4 files changed

+395
-1
lines changed
+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
{
2+
"http_interactions": [
3+
{
4+
"request": {
5+
"body": {
6+
"encoding": "utf-8",
7+
"string": ""
8+
},
9+
"headers": {
10+
"User-Agent": [
11+
"python-requests/2.23.0"
12+
],
13+
"Accept-Encoding": [
14+
"gzip, deflate"
15+
],
16+
"Accept": [
17+
"*/*"
18+
],
19+
"Connection": [
20+
"keep-alive"
21+
],
22+
"Authorization": [
23+
"token XXX"
24+
]
25+
},
26+
"method": "GET",
27+
"uri": "https://api.github.com/repos/simonw/github-contents-demo/git/refs/heads/not-a-branch"
28+
},
29+
"response": {
30+
"body": {
31+
"encoding": "utf-8",
32+
"base64_string": "H4sIAAAAAAAAAw3JMQ6AIBAF0auYtVW3sPMAll7BIHzRRFgDi43x7tLNy7wUkLPxoIkW0WaWEh115MSWgKhGT4lrSVf9h+qdJ2aHB5fcSIM/9SjbYCXwM3IVJ+yZWw/tTV8bCdGCvh8u9SX+ZwAAAA=="
33+
},
34+
"headers": {
35+
"Date": [
36+
"Fri, 13 Mar 2020 21:42:17 GMT"
37+
],
38+
"Content-Type": [
39+
"application/json; charset=utf-8"
40+
],
41+
"Transfer-Encoding": [
42+
"chunked"
43+
],
44+
"Server": [
45+
"github.com"
46+
],
47+
"Status": [
48+
"404 Not Found"
49+
],
50+
"X-RateLimit-Limit": [
51+
"5000"
52+
],
53+
"X-RateLimit-Remaining": [
54+
"4185"
55+
],
56+
"X-RateLimit-Reset": [
57+
"1584137530"
58+
],
59+
"X-Poll-Interval": [
60+
"300"
61+
],
62+
"X-OAuth-Scopes": [
63+
"repo"
64+
],
65+
"X-Accepted-OAuth-Scopes": [
66+
"repo"
67+
],
68+
"X-GitHub-Media-Type": [
69+
"github.v3; format=json"
70+
],
71+
"Access-Control-Expose-Headers": [
72+
"ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset"
73+
],
74+
"Access-Control-Allow-Origin": [
75+
"*"
76+
],
77+
"Strict-Transport-Security": [
78+
"max-age=31536000; includeSubdomains; preload"
79+
],
80+
"X-Frame-Options": [
81+
"deny"
82+
],
83+
"X-Content-Type-Options": [
84+
"nosniff"
85+
],
86+
"X-XSS-Protection": [
87+
"1; mode=block"
88+
],
89+
"Referrer-Policy": [
90+
"origin-when-cross-origin, strict-origin-when-cross-origin"
91+
],
92+
"Content-Security-Policy": [
93+
"default-src 'none'"
94+
],
95+
"Vary": [
96+
"Accept-Encoding, Accept, X-Requested-With"
97+
],
98+
"Content-Encoding": [
99+
"gzip"
100+
],
101+
"X-GitHub-Request-Id": [
102+
"CD77:44AC:940E44:AF16A1:5E6BFE38"
103+
]
104+
},
105+
"status": {
106+
"code": 404,
107+
"message": "Not Found"
108+
},
109+
"url": "https://api.github.com/repos/simonw/github-contents-demo/git/refs/heads/not-a-branch"
110+
},
111+
"recorded_at": "2020-03-13T21:42:17"
112+
}
113+
],
114+
"recorded_with": "betamax/0.8.1"
115+
}
+245
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
{
2+
"http_interactions": [
3+
{
4+
"request": {
5+
"body": {
6+
"encoding": "utf-8",
7+
"string": ""
8+
},
9+
"headers": {
10+
"User-Agent": [
11+
"python-requests/2.23.0"
12+
],
13+
"Accept-Encoding": [
14+
"gzip, deflate"
15+
],
16+
"Accept": [
17+
"*/*"
18+
],
19+
"Connection": [
20+
"keep-alive"
21+
],
22+
"Authorization": [
23+
"token XXX"
24+
]
25+
},
26+
"method": "GET",
27+
"uri": "https://api.github.com/repos/simonw/github-contents-demo/git/refs/heads/demo-branch"
28+
},
29+
"response": {
30+
"body": {
31+
"encoding": "utf-8",
32+
"base64_string": "H4sIAAAAAAAAA62OMW/CMBCF/4tngg04bckMC1JAoLaoU2X7LtiA7Sg+BBTx33HEXKlDlxuevnfvu7EOG1b1N3GLChIH9LHQnQrGsgELEfDbQUbqWf3y4T99/X641LN1ufyZi5XfHPX2jb7Cwmq/jLlw6o4ZtkRtqjhXrRvuHNmTHproeYdtTDw5H8OZP/PCxEAYKBX9cB9m6heZqPdoiFU3lqzKK0LCSLxOtWrKUjayNApRyNFYjtUEpAQwUyNBi6xF1xZzI0t4R/+j+fyV+J8t7vcHx2KUJ28BAAA="
33+
},
34+
"headers": {
35+
"Date": [
36+
"Fri, 13 Mar 2020 21:44:17 GMT"
37+
],
38+
"Content-Type": [
39+
"application/json; charset=utf-8"
40+
],
41+
"Transfer-Encoding": [
42+
"chunked"
43+
],
44+
"Server": [
45+
"github.com"
46+
],
47+
"Status": [
48+
"200 OK"
49+
],
50+
"X-RateLimit-Limit": [
51+
"5000"
52+
],
53+
"X-RateLimit-Remaining": [
54+
"4164"
55+
],
56+
"X-RateLimit-Reset": [
57+
"1584137530"
58+
],
59+
"Cache-Control": [
60+
"private, max-age=60, s-maxage=60"
61+
],
62+
"Vary": [
63+
"Accept, Authorization, Cookie, X-GitHub-OTP",
64+
"Accept-Encoding, Accept, X-Requested-With"
65+
],
66+
"ETag": [
67+
"W/\"bc57761084a5cd95ce1e8e6f783b4080\""
68+
],
69+
"Last-Modified": [
70+
"Sun, 09 Jun 2019 20:51:13 GMT"
71+
],
72+
"X-Poll-Interval": [
73+
"300"
74+
],
75+
"X-OAuth-Scopes": [
76+
"repo"
77+
],
78+
"X-Accepted-OAuth-Scopes": [
79+
"repo"
80+
],
81+
"X-GitHub-Media-Type": [
82+
"github.v3; format=json"
83+
],
84+
"Access-Control-Expose-Headers": [
85+
"ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset"
86+
],
87+
"Access-Control-Allow-Origin": [
88+
"*"
89+
],
90+
"Strict-Transport-Security": [
91+
"max-age=31536000; includeSubdomains; preload"
92+
],
93+
"X-Frame-Options": [
94+
"deny"
95+
],
96+
"X-Content-Type-Options": [
97+
"nosniff"
98+
],
99+
"X-XSS-Protection": [
100+
"1; mode=block"
101+
],
102+
"Referrer-Policy": [
103+
"origin-when-cross-origin, strict-origin-when-cross-origin"
104+
],
105+
"Content-Security-Policy": [
106+
"default-src 'none'"
107+
],
108+
"Content-Encoding": [
109+
"gzip"
110+
],
111+
"X-GitHub-Request-Id": [
112+
"CDD4:0DCD:BA4952:D8AA7A:5E6BFEB1"
113+
]
114+
},
115+
"status": {
116+
"code": 200,
117+
"message": "OK"
118+
},
119+
"url": "https://api.github.com/repos/simonw/github-contents-demo/git/refs/heads/demo-branch"
120+
},
121+
"recorded_at": "2020-03-13T21:44:17"
122+
},
123+
{
124+
"request": {
125+
"body": {
126+
"encoding": "utf-8",
127+
"string": ""
128+
},
129+
"headers": {
130+
"User-Agent": [
131+
"python-requests/2.23.0"
132+
],
133+
"Accept-Encoding": [
134+
"gzip, deflate"
135+
],
136+
"Accept": [
137+
"*/*"
138+
],
139+
"Connection": [
140+
"keep-alive"
141+
],
142+
"Authorization": [
143+
"token XXX"
144+
]
145+
},
146+
"method": "GET",
147+
"uri": "https://api.github.com/repos/simonw/github-contents-demo/git/refs/heads/demo-branch"
148+
},
149+
"response": {
150+
"body": {
151+
"encoding": "utf-8",
152+
"base64_string": "H4sIAAAAAAAAA62OMW/CMBCF/4tngg04bckMC1JAoLaoU2X7LtiA7Sg+BBTx33HEXKlDlxuevnfvu7EOG1b1N3GLChIH9LHQnQrGsgELEfDbQUbqWf3y4T99/X641LN1ufyZi5XfHPX2jb7Cwmq/jLlw6o4ZtkRtqjhXrRvuHNmTHproeYdtTDw5H8OZP/PCxEAYKBX9cB9m6heZqPdoiFU3lqzKK0LCSLxOtWrKUjayNApRyNFYjtUEpAQwUyNBi6xF1xZzI0t4R/+j+fyV+J8t7vcHx2KUJ28BAAA="
153+
},
154+
"headers": {
155+
"Date": [
156+
"Fri, 13 Mar 2020 21:44:27 GMT"
157+
],
158+
"Content-Type": [
159+
"application/json; charset=utf-8"
160+
],
161+
"Transfer-Encoding": [
162+
"chunked"
163+
],
164+
"Server": [
165+
"github.com"
166+
],
167+
"Status": [
168+
"200 OK"
169+
],
170+
"X-RateLimit-Limit": [
171+
"5000"
172+
],
173+
"X-RateLimit-Remaining": [
174+
"4163"
175+
],
176+
"X-RateLimit-Reset": [
177+
"1584137530"
178+
],
179+
"Cache-Control": [
180+
"private, max-age=60, s-maxage=60"
181+
],
182+
"Vary": [
183+
"Accept, Authorization, Cookie, X-GitHub-OTP",
184+
"Accept-Encoding, Accept, X-Requested-With"
185+
],
186+
"ETag": [
187+
"W/\"bc57761084a5cd95ce1e8e6f783b4080\""
188+
],
189+
"Last-Modified": [
190+
"Sun, 09 Jun 2019 20:51:13 GMT"
191+
],
192+
"X-Poll-Interval": [
193+
"300"
194+
],
195+
"X-OAuth-Scopes": [
196+
"repo"
197+
],
198+
"X-Accepted-OAuth-Scopes": [
199+
"repo"
200+
],
201+
"X-GitHub-Media-Type": [
202+
"github.v3; format=json"
203+
],
204+
"Access-Control-Expose-Headers": [
205+
"ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset"
206+
],
207+
"Access-Control-Allow-Origin": [
208+
"*"
209+
],
210+
"Strict-Transport-Security": [
211+
"max-age=31536000; includeSubdomains; preload"
212+
],
213+
"X-Frame-Options": [
214+
"deny"
215+
],
216+
"X-Content-Type-Options": [
217+
"nosniff"
218+
],
219+
"X-XSS-Protection": [
220+
"1; mode=block"
221+
],
222+
"Referrer-Policy": [
223+
"origin-when-cross-origin, strict-origin-when-cross-origin"
224+
],
225+
"Content-Security-Policy": [
226+
"default-src 'none'"
227+
],
228+
"Content-Encoding": [
229+
"gzip"
230+
],
231+
"X-GitHub-Request-Id": [
232+
"CDE0:44AB:984EA1:B3D2D5:5E6BFEBB"
233+
]
234+
},
235+
"status": {
236+
"code": 200,
237+
"message": "OK"
238+
},
239+
"url": "https://api.github.com/repos/simonw/github-contents-demo/git/refs/heads/demo-branch"
240+
},
241+
"recorded_at": "2020-03-13T21:44:27"
242+
}
243+
],
244+
"recorded_with": "betamax/0.8.1"
245+
}

github_contents.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ class NotFound(Exception):
99
class UnknownError(Exception):
1010
pass
1111

12-
def __init__(self, owner, repo, token):
12+
def __init__(self, owner, repo, token, branch=None):
1313
self.owner = owner
1414
self.repo = repo
1515
self.token = token
16+
self.branch = branch
1617
self.session = Session()
1718

1819
def base_url(self):
@@ -145,3 +146,13 @@ def write_large(self, filepath, content_bytes, commit_message="", committer=None
145146
headers=self.headers(),
146147
).json()
147148
return created_blob["sha"], created_commit["sha"]
149+
150+
def branch_exists(self):
151+
assert self.branch
152+
return (
153+
self.session.get(
154+
self.base_url() + "/git/refs/heads/{}".format(self.branch),
155+
headers=self.headers(),
156+
).status_code
157+
== 200
158+
)

0 commit comments

Comments
 (0)