Skip to content

Commit

Permalink
Format changed files with black
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelehab committed Jul 24, 2024
1 parent 211d15b commit 569e2f2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
13 changes: 7 additions & 6 deletions vulntotal/datasources/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ def datasource_advisory(self, purl) -> Iterable[VendorData]:

yml_files = [file for file in directory_files if file["name"].endswith(".yml")]

interesting_advisories = parse_interesting_advisories(
yml_files, purl
)
interesting_advisories = parse_interesting_advisories(yml_files, purl)
return interesting_advisories

@classmethod
Expand All @@ -67,24 +65,28 @@ def supported_ecosystem(cls):
"pypi": "pypi",
}


def fetch_directory_contents(package_slug):
project_id = "12006272"
url = f"https://gitlab.com/api/v4/projects/{project_id}/repository/tree?path={package_slug}"
response = requests.get(url)
if response.status_code == 200:
return response.json()



def construct_yml_url(file_path):
namespace = "gitlab-org"
project = "security-products/gemnasium-db"
branch = "master"
return f"https://gitlab.com/{namespace}/{project}/-/raw/{branch}/{file_path}"


def fetch_yaml(url):
response = requests.get(url)
if response.status_code == 200:
return response.text


def get_package_slug(purl):
"""
Constructs a package slug from a given purl.
Expand All @@ -109,7 +111,6 @@ def get_package_slug(purl):
return f"{ecosystem}/{package_name}"



def get_casesensitive_slug(path, package_slug):
payload = [
{
Expand Down Expand Up @@ -193,4 +194,4 @@ def parse_interesting_advisories(yml_files, purl) -> Iterable[VendorData]:
aliases=gitlab_advisory["identifiers"],
affected_versions=[affected_range],
fixed_versions=gitlab_advisory["fixed_versions"],
)
)
24 changes: 13 additions & 11 deletions vulntotal/tests/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from vulnerabilities.tests import util_tests
from vulntotal.datasources import gitlab


class TestGitlab(testcase.FileBasedTesting):
test_data_dir = str(Path(__file__).resolve().parent / "test_data" / "gitlab")

Expand All @@ -31,15 +32,21 @@ def test_generate_package_advisory_url(self):
expected_file = self.get_test_loc("package_advisory_url-expected.json", must_exist=False)
util_tests.check_results_against_json(results, expected_file)

@mock.patch('vulntotal.datasources.gitlab.fetch_yaml')
@mock.patch("vulntotal.datasources.gitlab.fetch_yaml")
def test_parse_interesting_advisories(self, mock_fetch_yaml):
# Mock the yaml file responses
advisory_folder = Path(__file__).resolve().parent.joinpath("test_data/gitlab/temp_vulntotal_gitlab_datasource/gemnasium-db-master-pypi-Jinja2/pypi/Jinja2")
advisory_folder = (
Path(__file__)
.resolve()
.parent.joinpath(
"test_data/gitlab/temp_vulntotal_gitlab_datasource/gemnasium-db-master-pypi-Jinja2/pypi/Jinja2"
)
)
yaml_files = []
sorted_files = sorted(advisory_folder.iterdir(), key=lambda x: x.name)
for file in sorted_files:
if file.suffix == '.yml':
with open(file, 'r') as f:
if file.suffix == ".yml":
with open(file, "r") as f:
yaml_files.append(f.read())

mock_fetch_yaml.side_effect = yaml_files
Expand All @@ -50,15 +57,10 @@ def test_parse_interesting_advisories(self, mock_fetch_yaml):
{"name": "CVE-2014-1402.yml", "path": "path/to/CVE-2014-1402.yml"},
{"name": "CVE-2016-10745.yml", "path": "path/to/CVE-2016-10745.yml"},
{"name": "CVE-2019-10906.yml", "path": "path/to/CVE-2019-10906.yml"},
{"name": "CVE-2020-28493.yml", "path": "path/to/CVE-2020-28493.yml"}
{"name": "CVE-2020-28493.yml", "path": "path/to/CVE-2020-28493.yml"},
]

results = [
adv.to_dict()
for adv in gitlab.parse_interesting_advisories(
yml_files, purl
)
]
results = [adv.to_dict() for adv in gitlab.parse_interesting_advisories(yml_files, purl)]

expected_file = self.get_test_loc("parsed_advisory-expected.json", must_exist=False)
util_tests.check_results_against_json(results, expected_file)

0 comments on commit 569e2f2

Please sign in to comment.