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

Commit ff2e8dd

Browse files
authored
Remove unused methods from ArchiveService and unused ArchiveField (#793)
1 parent f4e4a18 commit ff2e8dd

File tree

4 files changed

+7
-505
lines changed

4 files changed

+7
-505
lines changed

services/archive.py

+7-140
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import json
21
import logging
32
from base64 import b16encode
43
from enum import Enum
54
from hashlib import md5
65
from uuid import uuid4
76

8-
from django.conf import settings
97
from django.utils import timezone
10-
from shared.utils.ReportEncoder import ReportEncoder
118

129
from services.storage import StorageService
1310
from utils.config import get_config
@@ -17,11 +14,7 @@
1714

1815
class MinioEndpoints(Enum):
1916
chunks = "{version}/repos/{repo_hash}/commits/{commitid}/chunks.txt"
20-
json_data = "{version}/repos/{repo_hash}/commits/{commitid}/json_data/{table}/{field}/{external_id}.json"
21-
json_data_no_commit = (
22-
"{version}/repos/{repo_hash}/json_data/{table}/{field}/{external_id}.json"
23-
)
24-
raw = "v4/raw/{date}/{repo_hash}/{commit_sha}/{reportid}.txt"
17+
2518
raw_with_upload_id = (
2619
"v4/raw/{date}/{repo_hash}/{commit_sha}/{reportid}/{uploadid}.txt"
2720
)
@@ -31,7 +24,6 @@ class MinioEndpoints(Enum):
3124
static_analysis_single_file = (
3225
"{version}/repos/{repo_hash}/static_analysis/files/{location}"
3326
)
34-
3527
test_results = "test_results/v1/raw/{date}/{repo_hash}/{commit_sha}/{uploadid}.txt"
3628

3729
def get_path(self, **kwaargs):
@@ -71,21 +63,6 @@ def __init__(self, repository, ttl=None):
7163
self.storage = StorageService()
7264
self.storage_hash = self.get_archive_hash(repository)
7365

74-
"""
75-
Accessor for underlying StorageService. You typically shouldn't need
76-
this for anything.
77-
"""
78-
79-
def storage_client(self):
80-
return self.storage
81-
82-
"""
83-
Getter. Returns true if the current configuration is enterprise.
84-
"""
85-
86-
def is_enterprise(self):
87-
return settings.IS_ENTERPRISE
88-
8966
"""
9067
Generates a hash key from repo specific information.
9168
Provides slight obfuscation of data in minio storage
@@ -109,133 +86,23 @@ def get_archive_hash(cls, repository):
10986
_hash.update(val)
11087
return b16encode(_hash.digest()).decode()
11188

112-
def write_json_data_to_storage(
113-
self,
114-
commit_id,
115-
table: str,
116-
field: str,
117-
external_id: str,
118-
data: dict,
119-
*,
120-
encoder=ReportEncoder,
121-
):
122-
if commit_id is None:
123-
# Some classes don't have a commit associated with them
124-
# For example Pull belongs to multiple commits.
125-
path = MinioEndpoints.json_data_no_commit.get_path(
126-
version="v4",
127-
repo_hash=self.storage_hash,
128-
table=table,
129-
field=field,
130-
external_id=external_id,
131-
)
132-
else:
133-
path = MinioEndpoints.json_data.get_path(
134-
version="v4",
135-
repo_hash=self.storage_hash,
136-
commitid=commit_id,
137-
table=table,
138-
field=field,
139-
external_id=external_id,
140-
)
141-
stringified_data = json.dumps(data, cls=encoder)
142-
self.write_file(path, stringified_data)
143-
return path
144-
145-
"""
146-
Writes a generic file to the archive -- it's typically recommended to
147-
not use this in lieu of the convenience methods write_raw_upload and
148-
write_chunks
149-
"""
150-
151-
def write_file(self, path, data, reduced_redundancy=False, gzipped=False):
152-
self.storage.write_file(
153-
self.root,
154-
path,
155-
data,
156-
reduced_redundancy=reduced_redundancy,
157-
gzipped=gzipped,
158-
)
159-
160-
"""
161-
Convenience write method, writes a raw upload to a destination.
162-
Returns the path it writes.
163-
"""
164-
165-
def write_raw_upload(self, commit_sha, report_id, data, gzipped=False):
166-
# create a custom report path for a raw upload.
167-
# write the file.
168-
path = "/".join(
169-
(
170-
"v4/raw",
171-
timezone.now().strftime("%Y-%m-%d"),
172-
self.storage_hash,
173-
commit_sha,
174-
"%s.txt" % report_id,
175-
)
176-
)
177-
178-
self.write_file(path, data, gzipped=gzipped)
179-
180-
return path
181-
182-
"""
183-
Convenience method to write a chunks.txt file to storage.
184-
"""
185-
186-
def write_chunks(self, commit_sha, data):
187-
path = MinioEndpoints.chunks.get_path(
188-
version="v4", repo_hash=self.storage_hash, commitid=commit_sha
189-
)
190-
191-
self.write_file(path, data)
192-
return path
193-
194-
"""
195-
Generic method to read a file from the archive
196-
"""
197-
19889
def read_file(self, path):
90+
"""
91+
Generic method to read a file from the archive
92+
"""
19993
contents = self.storage.read_file(self.root, path)
20094
return contents.decode()
20195

202-
"""
203-
Generic method to delete a file from the archive.
204-
"""
205-
206-
def delete_file(self, path):
207-
self.storage.delete_file(self.root, path)
208-
209-
"""
210-
Deletes an entire repository's contents
211-
"""
212-
213-
def delete_repo_files(self):
214-
path = "v4/repos/{}".format(self.storage_hash)
215-
objects = self.storage.list_folder_contents(self.root, path)
216-
for obj in objects:
217-
self.storage.delete_file(self.root, obj.object_name)
218-
219-
"""
220-
Convenience method to read a chunks file from the archive.
221-
"""
222-
22396
def read_chunks(self, commit_sha):
97+
"""
98+
Convenience method to read a chunks file from the archive.
99+
"""
224100
path = MinioEndpoints.chunks.get_path(
225101
version="v4", repo_hash=self.storage_hash, commitid=commit_sha
226102
)
227103
log.info("Downloading chunks from path %s for commit %s", path, commit_sha)
228104
return self.read_file(path)
229105

230-
"""
231-
Delete a chunk file from the archive
232-
"""
233-
234-
def delete_chunk_from_archive(self, commit_sha):
235-
path = "v4/repos/{}/commits/{}/chunks.txt".format(self.storage_hash, commit_sha)
236-
237-
self.delete_file(path)
238-
239106
def create_presigned_put(self, path):
240107
return self.storage.create_presigned_put(self.root, path, self.ttl)
241108

services/tests/test_archive.py

-84
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import json
21
from pathlib import Path
32
from unittest.mock import patch
43

54
from django.test import TestCase
6-
from shared.storage import MinioStorageService
75

86
from core.tests.factories import RepositoryFactory
97
from services.archive import ArchiveService
@@ -18,85 +16,3 @@ def test_create_raw_upload_presigned_put(self, create_presigned_put_mock):
1816
repo = RepositoryFactory.create()
1917
service = ArchiveService(repo)
2018
assert service.create_raw_upload_presigned_put("ABCD") == "presigned url"
21-
22-
23-
class TestWriteData(object):
24-
def test_write_report_details_to_storage(self, mocker, db):
25-
repo = RepositoryFactory()
26-
mock_write_file = mocker.patch.object(MinioStorageService, "write_file")
27-
28-
data = [
29-
{
30-
"filename": "file_1.go",
31-
"file_index": 0,
32-
"file_totals": [0, 8, 5, 3, 0, "62.50000", 0, 0, 0, 0, 10, 2, 0],
33-
"diff_totals": None,
34-
},
35-
{
36-
"filename": "file_2.py",
37-
"file_index": 1,
38-
"file_totals": [0, 2, 1, 0, 1, "50.00000", 1, 0, 0, 0, 0, 0, 0],
39-
"diff_totals": None,
40-
},
41-
]
42-
archive_service = ArchiveService(repository=repo)
43-
commitid = "some-commit-sha"
44-
external_id = "some-uuid4-id"
45-
path = archive_service.write_json_data_to_storage(
46-
commit_id=commitid,
47-
table="reports_reportsdetails",
48-
field="files_array",
49-
external_id=external_id,
50-
data=data,
51-
)
52-
assert (
53-
path
54-
== f"v4/repos/{archive_service.storage_hash}/commits/{commitid}/json_data/reports_reportsdetails/files_array/{external_id}.json"
55-
)
56-
mock_write_file.assert_called_with(
57-
archive_service.root,
58-
path,
59-
json.dumps(data),
60-
gzipped=False,
61-
reduced_redundancy=False,
62-
)
63-
64-
def test_write_report_details_to_storage_no_commitid(self, mocker, db):
65-
repo = RepositoryFactory()
66-
mock_write_file = mocker.patch.object(MinioStorageService, "write_file")
67-
68-
data = [
69-
{
70-
"filename": "file_1.go",
71-
"file_index": 0,
72-
"file_totals": [0, 8, 5, 3, 0, "62.50000", 0, 0, 0, 0, 10, 2, 0],
73-
"diff_totals": None,
74-
},
75-
{
76-
"filename": "file_2.py",
77-
"file_index": 1,
78-
"file_totals": [0, 2, 1, 0, 1, "50.00000", 1, 0, 0, 0, 0, 0, 0],
79-
"diff_totals": None,
80-
},
81-
]
82-
archive_service = ArchiveService(repository=repo)
83-
commitid = None
84-
external_id = "some-uuid4-id"
85-
path = archive_service.write_json_data_to_storage(
86-
commit_id=commitid,
87-
table="reports_reportsdetails",
88-
field="files_array",
89-
external_id=external_id,
90-
data=data,
91-
)
92-
assert (
93-
path
94-
== f"v4/repos/{archive_service.storage_hash}/json_data/reports_reportsdetails/files_array/{external_id}.json"
95-
)
96-
mock_write_file.assert_called_with(
97-
archive_service.root,
98-
path,
99-
json.dumps(data),
100-
gzipped=False,
101-
reduced_redundancy=False,
102-
)

0 commit comments

Comments
 (0)