Skip to content

Commit

Permalink
Merge pull request #252 from GeekMasher/depbot-pr-update
Browse files Browse the repository at this point in the history
Dependabot PR Update
  • Loading branch information
GeekMasher authored Aug 15, 2024
2 parents d4e5795 + e4e2e03 commit beecb41
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
9 changes: 8 additions & 1 deletion examples/dependabot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

GitHub.init(
os.environ.get("GITHUB_REPOSITORY", "GeekMasher/ghastoolkit"),
reference=os.environ.get("GITHUB_REF", "refs/heads/main"),
)

dependabot = Dependabot()
Expand All @@ -12,7 +13,13 @@
print("Dependabot is not enabled")
exit(1)

alerts = dependabot.getAlerts()
if GitHub.repository.isInPullRequest():
print("Dependabot Alerts from Pull Request")
alerts = dependabot.getAlertsInPR()
else:
print("Dependabot Alerts from Repository")
alerts = dependabot.getAlerts()

print(f"Total Alerts :: {len(alerts)}")

for alert in alerts:
Expand Down
28 changes: 28 additions & 0 deletions src/ghastoolkit/octokit/dependabot.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,34 @@ def getAlerts(
docs="https://docs.github.com/en/rest/dependabot/alerts",
)

def getAlertsInPR(self) -> list[DependencyAlert]:
"""Get All Dependabot alerts from REST API in Pull Request."""
logger.debug("Dependabot Alerts from Pull Request using DependencyGraph API")

from ghastoolkit import DependencyGraph

depgraph = DependencyGraph(repository=self.repository)

pr_info = self.repository.getPullRequestInfo()
pr_base = pr_info.get("base", {}).get("ref", "")
pr_head = pr_info.get("head", {}).get("ref", "")

if pr_base == "" or pr_head == "":
raise GHASToolkitError(
"Failed to get base and head branch of pull request",
permissions=[
'"Contents" repository permissions (read)',
'"Pull requests" permissions (read)',
],
docs="https://docs.github.com/en/rest/reference/repos#get-a-repository",
)

dependencies = depgraph.getDependenciesInPR(pr_base, pr_head)
alerts = []
for dep in dependencies:
alerts.extend(dep.alerts)
return alerts

def getAlertsGraphQL(self) -> list[DependencyAlert]:
"""Get All Dependabot alerts from GraphQL API using the `GetDependencyAlerts` query."""
results = []
Expand Down

0 comments on commit beecb41

Please sign in to comment.