Skip to content

Commit 5a9e2b6

Browse files
committed
Avoid duplication of package type inference in github importer
Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
1 parent ceff88d commit 5a9e2b6

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

vulnerabilities/importers/github.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,6 @@ def __init__(self, *args, **kwargs):
9797
except KeyError:
9898
raise GitHubTokenError("Environment variable GH_TOKEN is missing")
9999

100-
self.ecosytem_type = {
101-
"RUBYGEMS": "gem",
102-
"NUGET": "nuget",
103-
"PIP": "pypi",
104-
"MAVEN": "maven",
105-
"COMPOSER": "composer",
106-
}
107-
108100
def __enter__(self):
109101
self.advisories = self.fetch()
110102

@@ -177,7 +169,7 @@ def process_response(self) -> List[Advisory]:
177169
adv_list = []
178170
for ecosystem in self.advisories:
179171
self.set_version_api(ecosystem)
180-
pkg_type = self.ecosytem_type[ecosystem]
172+
pkg_type = self.version_api.package_type
181173
for resp_page in self.advisories[ecosystem]:
182174
for adv in resp_page["data"]["securityVulnerabilities"]["edges"]:
183175
name = adv["node"]["package"]["name"]

vulnerabilities/package_managers.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def get(self, package_name: str) -> Set[str]:
4141

4242

4343
class LaunchpadVersionAPI(VersionAPI):
44+
45+
package_type = "deb"
46+
4447
async def load_api(self, pkg_set):
4548
async with ClientSession(raise_for_status=True) as session:
4649
await asyncio.gather(
@@ -75,6 +78,9 @@ async def set_api(self, pkg, session):
7578

7679

7780
class PypiVersionAPI(VersionAPI):
81+
82+
package_type = "pypi"
83+
7884
async def load_api(self, pkg_set):
7985
async with ClientSession(raise_for_status=True) as session:
8086
await asyncio.gather(
@@ -96,6 +102,9 @@ async def fetch(self, pkg, session):
96102

97103

98104
class CratesVersionAPI(VersionAPI):
105+
106+
package_type = "cargo"
107+
99108
async def load_api(self, pkg_set):
100109
async with ClientSession(raise_for_status=True) as session:
101110
await asyncio.gather(
@@ -114,6 +123,9 @@ async def fetch(self, pkg, session):
114123

115124

116125
class RubyVersionAPI(VersionAPI):
126+
127+
package_type = "gem"
128+
117129
async def load_api(self, pkg_set):
118130
async with ClientSession(raise_for_status=True) as session:
119131
await asyncio.gather(
@@ -135,6 +147,9 @@ async def fetch(self, pkg, session):
135147

136148

137149
class NpmVersionAPI(VersionAPI):
150+
151+
package_type = "npm"
152+
138153
async def load_api(self, pkg_set):
139154
async with ClientSession(raise_for_status=True) as session:
140155
await asyncio.gather(
@@ -156,6 +171,9 @@ async def fetch(self, pkg, session):
156171

157172

158173
class DebianVersionAPI(VersionAPI):
174+
175+
package_type = "deb"
176+
159177
async def load_api(self, pkg_set):
160178
# Need to set the headers, because the Debian API upgrades
161179
# the connection to HTTP 2.0
@@ -189,6 +207,9 @@ async def set_api(self, pkg, session, retry_count=5):
189207

190208

191209
class MavenVersionAPI(VersionAPI):
210+
211+
package_type = "maven"
212+
192213
async def load_api(self, pkg_set):
193214
async with ClientSession(raise_for_status=True) as session:
194215
await asyncio.gather(
@@ -242,6 +263,9 @@ def extract_versions(xml_response: ET.ElementTree) -> Set[str]:
242263

243264

244265
class NugetVersionAPI(VersionAPI):
266+
267+
package_type = "nuget"
268+
245269
async def load_api(self, pkg_set):
246270
async with ClientSession(raise_for_status=True) as session:
247271
await asyncio.gather(
@@ -274,6 +298,9 @@ def extract_versions(resp: dict) -> Set[str]:
274298

275299

276300
class ComposerVersionAPI(VersionAPI):
301+
302+
package_type = "composer"
303+
277304
async def load_api(self, pkg_set):
278305
async with ClientSession(raise_for_status=True) as session:
279306
await asyncio.gather(
@@ -304,6 +331,9 @@ def extract_versions(resp: dict, pkg_name: str) -> Set[str]:
304331

305332

306333
class GitHubTagsAPI(VersionAPI):
334+
335+
package_type = "github"
336+
307337
async def load_api(self, repo_set):
308338
async with ClientSession(raise_for_status=True) as session:
309339
await asyncio.gather(

vulnerabilities/tests/test_github.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ def test_process_response(self):
303303
]
304304

305305
mock_version_api = MagicMock()
306+
mock_version_api.package_type = "maven"
306307
mock_version_api.get = lambda x: {'1.2.0', '9.0.2'}
307308
with patch('vulnerabilities.importers.github.MavenVersionAPI', return_value=mock_version_api): # nopep8
308309
with patch('vulnerabilities.importers.github.GitHubAPIDataSource.set_api'):

0 commit comments

Comments
 (0)