Skip to content

Commit f41876a

Browse files
committed
master is no longer hard-coded, refs #5
1 parent 8bb91a1 commit f41876a

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

github_contents.py

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

12-
def __init__(self, owner, repo, token, branch=None):
12+
def __init__(self, owner, repo, token, branch="main"):
1313
self.owner = owner
1414
self.repo = repo
1515
self.token = token
16-
self.branch = branch
1716
self.session = Session()
17+
self.branch = branch
1818

1919
def base_url(self):
2020
return "https://api.github.com/repos/{}/{}".format(self.owner, self.repo)
@@ -42,11 +42,13 @@ def read(self, filepath):
4242
raise self.UnknownError(response.content)
4343

4444
def read_large(self, filepath):
45-
master = self.session.get(
46-
self.base_url() + "/git/trees/master?recursive=1", headers=self.headers()
45+
"Returns (file_contents_in_bytes, sha1)"
46+
default_tree = self.session.get(
47+
self.base_url() + "/git/trees/{}?recursive=1".format(self.branch),
48+
headers=self.headers(),
4749
).json()
4850
try:
49-
tree_entry = [t for t in master["tree"] if t["path"] == filepath][0]
51+
tree_entry = [t for t in default_tree["tree"] if t["path"] == filepath][0]
5052
except IndexError:
5153
raise self.NotFound(filepath)
5254
data = self.session.get(tree_entry["url"], headers=self.headers()).json()
@@ -108,15 +110,16 @@ def write_large(self, filepath, content_bytes, commit_message="", committer=None
108110
},
109111
headers=self.headers(),
110112
).json()
111-
# Retrieve master tree sha
112-
master_sha = self.session.get(
113-
self.base_url() + "/git/trees/master?recursive=1", headers=self.headers()
113+
# Retrieve default tree sha
114+
default_branch_sha = self.session.get(
115+
self.base_url() + "/git/trees/{}?recursive=1".format(self.branch),
116+
headers=self.headers(),
114117
).json()["sha"]
115118
# Construct a new tree
116119
created_tree = self.session.post(
117120
self.base_url() + "/git/trees",
118121
json={
119-
"base_tree": master_sha,
122+
"base_tree": default_branch_sha,
120123
"tree": [
121124
{
122125
"mode": "100644", # file (blob),
@@ -131,7 +134,7 @@ def write_large(self, filepath, content_bytes, commit_message="", committer=None
131134
# Create a commit which references the new tree
132135
payload = {
133136
"message": commit_message,
134-
"parents": [master_sha],
137+
"parents": [default_branch_sha],
135138
"tree": created_tree["sha"],
136139
}
137140
if committer:
@@ -141,7 +144,7 @@ def write_large(self, filepath, content_bytes, commit_message="", committer=None
141144
).json()
142145
# Move HEAD reference on master to the new commit
143146
self.session.patch(
144-
self.base_url() + "/git/refs/heads/master",
147+
self.base_url() + "/git/refs/heads/{}".format(self.branch),
145148
json={"sha": created_commit["sha"]},
146149
headers=self.headers(),
147150
).json()

test_github_contents.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
def test_read_small_file():
20-
github = GithubContents("simonw", "github-contents-demo", TOKEN)
20+
github = GithubContents("simonw", "github-contents-demo", TOKEN, branch="master")
2121
with Betamax(github.session) as vcr:
2222
vcr.use_cassette("get-file")
2323
content, sha = github.read("hello.txt")
@@ -26,7 +26,7 @@ def test_read_small_file():
2626

2727

2828
def test_write_small_file():
29-
github = GithubContents("simonw", "github-contents-demo", TOKEN)
29+
github = GithubContents("simonw", "github-contents-demo", TOKEN, branch="master")
3030
with Betamax(github.session) as vcr:
3131
vcr.use_cassette("write-file")
3232
assert (
@@ -41,7 +41,7 @@ def test_write_small_file():
4141

4242

4343
def test_write_large():
44-
github = GithubContents("simonw", "github-contents-demo", TOKEN)
44+
github = GithubContents("simonw", "github-contents-demo", TOKEN, branch="master")
4545
with Betamax(github.session) as vcr:
4646
vcr.use_cassette("write-file-large")
4747
assert (

0 commit comments

Comments
 (0)