-
Notifications
You must be signed in to change notification settings - Fork 1
/
githublfs.py
303 lines (257 loc) · 8.7 KB
/
githublfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
from dataclasses import dataclass
from hashlib import sha256
from typing import Optional, Callable, Iterable, List
import requests
from github import Github, UnknownObjectException
from github.Repository import Repository
from github.ContentFile import ContentFile
__all__ = ["commit_lfs_file"]
def commit_lfs_file(
repo: str, token: str, path: str, content: bytes, branch: str, message: str
):
"""
Commits a file to a GitHub repository using LFS
:param repo: Complete name of the GitHub repository. Example "AnesFoufa/githublfs"
:param token: GitHub Personal access token with "repo" scope
:param path: path to the file to commit. Example "assets/logo.jpg"
:param content: Binary content of the file to update
:param branch: Name of the branch to commit to. Example "main".
:param message: Commit message. Example "update logo"
:return: None
"""
return _upload_and_commit(
repository=repo,
token=token,
branch=branch,
content=content,
message=message,
path=path,
)
@dataclass(frozen=True)
class URLHeaders:
url: str
headers: dict
@dataclass
class UploadAndVerifyData:
upload: URLHeaders
verify: Optional[URLHeaders] = None
@dataclass(frozen=True)
class FileData:
digest: str
content: bytes
@property
def size(self):
return len(self.content)
@dataclass(frozen=True)
class GHRepo:
name: str
token: str
class UploadAndCommitLFS:
def __init__(self, upload_lfs, commit_pointer):
self.upload_lfs = upload_lfs
self.commit_pointer = commit_pointer
def __call__(
self,
repository: str,
token: str,
path: str,
content: bytes,
branch: str,
message: str,
):
digest = self._hash_content(content)
file_data = FileData(digest=digest, content=content)
gh_repo = GHRepo(name=repository, token=token)
self.upload_lfs(file_data=file_data, gh_repo=gh_repo)
self.commit_pointer(
gh_repo=gh_repo,
file_data=file_data,
branch=branch,
message=message,
path=path,
)
@staticmethod
def _hash_content(content: bytes) -> str:
hasher = sha256()
hasher.update(content)
return hasher.hexdigest()
class CheckUploadAndVerify:
def __init__(
self,
check_uploaded: Callable[..., Optional[UploadAndVerifyData]],
upload,
verify,
):
self.check_uploaded = check_uploaded
self.upload = upload
self.verify = verify
def __call__(
self,
file_data: FileData,
gh_repo: GHRepo,
):
upload_and_verify_data = self.check_uploaded(
file_data=file_data, gh_repo=gh_repo
)
if upload_and_verify_data:
self.upload(
url_headers=upload_and_verify_data.upload, content=file_data.content
)
if upload_and_verify_data.verify:
self.verify(
url_headers=upload_and_verify_data.verify, file_data=file_data
)
class CheckFileInLFSServer:
def __init__(
self,
request_file_in_server: Callable[..., dict],
parse_response: Callable[..., Optional[UploadAndVerifyData]],
):
self.file_in_server = request_file_in_server
self.parse_response = parse_response
def __call__(
self, gh_repo: GHRepo, file_data: FileData
) -> Optional[UploadAndVerifyData]:
response = self.file_in_server(gh_repo=gh_repo, file_data=file_data)
return self.parse_response(response=response)
class CommitLFSPointer:
def __init__(self, commit_file: Callable[..., None]):
self._commit_file = commit_file
def __call__(
self, gh_repo: GHRepo, file_data: FileData, message: str, path: str, branch: str
):
pointer_content = (
f"version https://git-lfs.github.com/spec/v1"
f"\noid sha256:{file_data.digest}\nsize {file_data.size}\n"
)
self._commit_file(
gh_repo=gh_repo,
path=path,
content=pointer_content,
branch=branch,
message=message,
)
class CommitFile:
def __init__(
self, get_file_content: Callable[..., Optional[Iterable[ContentFile]]]
):
self._get_file_content = get_file_content
def __call__(
self,
gh_repo: GHRepo,
path: str,
content: str,
branch: str,
message: str,
):
g = Github(gh_repo.token)
repository: Repository = g.get_repo(full_name_or_id=gh_repo.name)
maybe_contents = self._get_file_content(
repository=repository, path=path, branch=branch
)
if maybe_contents is not None: # if file exists in repository
for file_content in maybe_contents:
repository.update_file(
path=file_content.path,
message=message,
content=content,
sha=file_content.sha,
branch=branch,
)
else:
repository.create_file(
path=path, message=message, content=content, branch=branch
)
def _post_pre_upload_request_to_lfs_server(file_data: FileData, gh_repo: GHRepo):
url = f"https://github.com/{gh_repo.name}.git/info/lfs/objects/batch"
headers = {
"Content-type": "application/json",
"Accept": "application/vnd.git-lfs+json",
}
payload = {
"operation": "upload",
"transfers": ["basic"],
"objects": [
{
"oid": file_data.digest,
"size": file_data.size,
}
],
}
response = requests.post(
url=url, headers=headers, json=payload, auth=(gh_repo.token, "")
)
assert 200 <= response.status_code < 300, _error_message(response)
json_response = response.json()
return json_response
def _parse_upload_and_verify_from_response(
response,
) -> Optional[UploadAndVerifyData]:
first_object = response["objects"][0]
res = None
if "actions" in first_object and "upload" in first_object["actions"]:
actions_dict = first_object["actions"]
upload_dict = actions_dict["upload"]
upload_url_and_headers = URLHeaders(
headers=upload_dict["header"], url=upload_dict["href"]
)
res = UploadAndVerifyData(upload=upload_url_and_headers)
if "verify" in actions_dict:
verify_dict = actions_dict["verify"]
verify_url_and_headers = URLHeaders(
url=verify_dict["href"], headers=verify_dict["header"]
)
res.verify = verify_url_and_headers
return res
def _upload_file_to_lfs_server(url_headers: URLHeaders, content: bytes):
"""
Uploads file content to lfs server
:param url_headers: URL and headers returned by LFS server to upload
:param content: file content to upload.
:return: None
:raise AssertionError if the server doesn't return 200 status code.
"""
headers = url_headers.headers.copy()
headers["Content-Type"] = "application/octet-stream"
response = requests.put(headers=headers, url=url_headers.url, data=content)
assert response.status_code == 200, _error_message(response)
def _verify_file_uploaded(url_headers: URLHeaders, file_data: FileData):
headers = url_headers.headers.copy()
headers["Content-Type"] = "application/vnd.git-lfs+json"
post_data = {"oid": file_data.digest, "size": file_data.size}
response = requests.post(
url=url_headers.url,
headers=headers,
data=post_data,
)
assert response.status_code == 200, _error_message(response)
def _get_github_content(
repository: Repository, path: str, branch: str
) -> Optional[List[ContentFile]]:
try:
old_contents = repository.get_contents(path=path, ref=branch)
if isinstance(old_contents, list):
res = old_contents
else:
res = [old_contents]
except UnknownObjectException:
res = None
return res
def _error_message(response: requests.Response) -> str:
return (
f"Expected success, got {response.status_code} status code. \n{response.text}"
)
_upload_lfs = CheckUploadAndVerify(
check_uploaded=CheckFileInLFSServer(
request_file_in_server=_post_pre_upload_request_to_lfs_server,
parse_response=_parse_upload_and_verify_from_response,
),
upload=_upload_file_to_lfs_server,
verify=_verify_file_uploaded,
)
_upload_and_commit = UploadAndCommitLFS(
upload_lfs=_upload_lfs,
commit_pointer=CommitLFSPointer(
commit_file=CommitFile(get_file_content=_get_github_content)
),
)