Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions ghascompliance/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

__HERE__ = os.path.dirname(os.path.realpath(__file__))
LICENSES = [os.path.join(__HERE__, "data", "clearlydefined.json")]
GRAPHQL_QUERIES = [os.path.join(__HERE__, "octokit", "graphql")]


class Checks:
Expand Down Expand Up @@ -193,8 +192,6 @@ def checkDependabot(self):
dependabot_violations = []

dependabot = Dependabot()
# Load the GraphQL Queries from the repo
dependabot.graphql.loadQueries(GRAPHQL_QUERIES)

depgraph = DependencyGraph()

Expand Down Expand Up @@ -224,8 +221,16 @@ def checkDependabot(self):
"Dependabot REST API returned 400; retrying with GraphQL alerts API"
)
alerts = dependabot.getAlertsGraphQL()
# Dependencies
dependencies = depgraph.getDependencies()

# Dependencies are only needed to resolve the alerts, skip the
# (expensive) Dependency Graph requests if there are no alerts
if alerts:
dependencies = depgraph.getDependencies()
else:
Octokit.debug(
"No Dependabot alerts, skipping Dependency Graph requests"
)
dependencies = Dependencies()

Octokit.info("Total Dependabot Alerts :: " + str(len(alerts)))

Expand Down
34 changes: 0 additions & 34 deletions ghascompliance/octokit/graphql/GetDependencyAlerts.graphql

This file was deleted.

40 changes: 0 additions & 40 deletions ghascompliance/octokit/graphql/GetDependencyInfo.graphql

This file was deleted.

70 changes: 70 additions & 0 deletions tests/test_checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import os
import sys
import unittest
from unittest.mock import MagicMock, patch

sys.path.append(".")

from ghastoolkit import GitHub
from ghastoolkit.octokit.graphql import QUERIES

from ghascompliance import checks as checks_module
from ghascompliance.checks import Checks
from ghascompliance.policy import Policy


class TestGraphQLQueries(unittest.TestCase):
def testNoLocalQueryOverrides(self):
"""Local queries would override the paginated ghastoolkit queries."""
path = os.path.join(
os.path.dirname(checks_module.__file__), "octokit", "graphql"
)
self.assertFalse(os.path.exists(path))

def testDependencyInfoQueryIsPaginated(self):
"""Un-paginated queries time out (502) on large repositories."""
query = QUERIES.get("GetDependencyInfo", "")

self.assertIn("dependencyGraphManifests(first:", query)
self.assertIn("$manifests_cursor", query)
self.assertIn("$dependencies_cursor", query)


class TestDependabotChecks(unittest.TestCase):
def setUp(self) -> None:
GitHub.init(
"advanced-security/policy-as-code",
reference="refs/heads/main",
retrieve_metadata=False,
)
self.checks = Checks(Policy("error"))
super().setUp()

def testSkipDependencyGraphWithoutAlerts(self):
depgraph = MagicMock()
dependabot = MagicMock()
dependabot.getAlerts.return_value = []

with patch.object(checks_module, "Dependabot", return_value=dependabot):
with patch.object(checks_module, "DependencyGraph", return_value=depgraph):
violations = self.checks.checkDependabot()

self.assertEqual(violations, 0)
depgraph.getDependencies.assert_not_called()

def testFetchDependencyGraphWithAlerts(self):
alert = MagicMock()
alert.get.return_value = None
alert.purl = "pkg:npm/lodash"

depgraph = MagicMock()
depgraph.getDependencies.return_value.findPurl.return_value = None
dependabot = MagicMock()
dependabot.getAlerts.return_value = [alert]

with patch.object(checks_module, "Dependabot", return_value=dependabot):
with patch.object(checks_module, "DependencyGraph", return_value=depgraph):
violations = self.checks.checkDependabot()

self.assertEqual(violations, 0)
depgraph.getDependencies.assert_called_once()
Loading