|
6 | 6 | import json
|
7 | 7 | import logging
|
8 | 8 | from collections.abc import AsyncGenerator
|
9 |
| -from datetime import UTC, datetime |
10 | 9 | from enum import Enum
|
11 | 10 | from typing import Any
|
12 | 11 |
|
13 | 12 | import httpx
|
14 |
| -from dateutil.relativedelta import relativedelta |
15 | 13 | from limits import RateLimitItem, RateLimitItemPerMinute
|
16 | 14 | from limits.aio.storage import MemoryStorage
|
17 | 15 | from limits.aio.strategies import MovingWindowRateLimiter, RateLimiter
|
@@ -74,68 +72,6 @@ def _fetch_problem(title: str, e: httpx.HTTPError):
|
74 | 72 | logger.warning("Problem fetching %s", title, exc_info=e, stacklevel=2)
|
75 | 73 |
|
76 | 74 |
|
77 |
| -def validate_lookback_period(lookback_period: dict[str, int]) -> dict[str, int]: |
78 |
| - """Sanitize the lookback period to only include valid keys.""" |
79 |
| - |
80 |
| - def validate_positive_int(value: int) -> int: |
81 |
| - converted = int(value) |
82 |
| - if converted <= 0: |
83 |
| - negative_value_exception_msg = ( |
84 |
| - f"Lookback period values must be positive: {value}" |
85 |
| - ) |
86 |
| - raise ValueError(negative_value_exception_msg) |
87 |
| - return converted |
88 |
| - |
89 |
| - try: |
90 |
| - return {k: validate_positive_int(v) for k, v in lookback_period.items()} |
91 |
| - except Exception as e: |
92 |
| - exception_msg = "Formatting lookback period failed" |
93 |
| - raise ValueError(exception_msg) from e |
94 |
| - |
95 |
| - |
96 |
| -def build_search_phrase( |
97 |
| - actions: list[str], |
98 |
| - actors: list[str], |
99 |
| - exclude_actors: list[str], |
100 |
| - lookback_period: dict[str, int], |
101 |
| -) -> str: |
102 |
| - # adding action-based filtering |
103 |
| - actions_phrase = "" |
104 |
| - if actions: |
105 |
| - actions_phrase = " ".join(f"action:{action}" for action in actions) |
106 |
| - |
107 |
| - # adding lookback_period based filtering |
108 |
| - date_filter = "" |
109 |
| - if lookback_period: |
110 |
| - lookback_period = validate_lookback_period(lookback_period) |
111 |
| - date_filter = ( |
112 |
| - f"created:>={(datetime.now(tz=UTC) - relativedelta(**lookback_period)) |
113 |
| - .strftime('%Y-%m-%d')}" |
114 |
| - if lookback_period |
115 |
| - else "" |
116 |
| - ) |
117 |
| - |
118 |
| - # adding actor-based filtering |
119 |
| - actors_phrase = "" |
120 |
| - if actors: |
121 |
| - actors_phrase = " ".join(f"actor:{actor}" for actor in actors) |
122 |
| - |
123 |
| - # adding exclude_actors based filtering |
124 |
| - exclude_actors_phrase = "" |
125 |
| - if exclude_actors: |
126 |
| - exclude_actors_phrase = " ".join(f"-actor:{actor}" for actor in exclude_actors) |
127 |
| - return " ".join( |
128 |
| - section |
129 |
| - for section in [ |
130 |
| - actions_phrase, |
131 |
| - date_filter, |
132 |
| - actors_phrase, |
133 |
| - exclude_actors_phrase, |
134 |
| - ] |
135 |
| - if section |
136 |
| - ).strip() |
137 |
| - |
138 |
| - |
139 | 75 | class GithubRestApiClient:
|
140 | 76 | def __init__(
|
141 | 77 | self,
|
@@ -402,25 +338,13 @@ async def fetch_all_organizations(self) -> AsyncGenerator[types.GithubOrg]:
|
402 | 338 | async def fetch_enterprise_audit_log(
|
403 | 339 | self,
|
404 | 340 | enterprise_name: str,
|
405 |
| - actions: list[str], |
406 |
| - actors: list[str], |
407 |
| - exclude_actors: list[str], |
408 |
| - lookback_period: dict[str, int], |
| 341 | + search_phrase: str | None = None, |
409 | 342 | ) -> AsyncGenerator[types.GithubAuditLog]:
|
410 | 343 | """Fetches enterprise-wide audit log data
|
411 |
| -
|
412 |
| - https://docs.github.com/en/enterprise-cloud@latest/rest/enterprise-admin/audit-log?apiVersion=2022-11-28#get-the-audit-log-for-an-enterprise |
| 344 | + https://docs.github.com/en/[email protected]/rest/enterprise-admin/audit-log?apiVersion=2022-11-28#get-the-audit-log-for-an-enterprise |
413 | 345 | """
|
414 | 346 | try:
|
415 |
| - search_phrase = build_search_phrase( |
416 |
| - actions=actions, |
417 |
| - actors=actors, |
418 |
| - exclude_actors=exclude_actors, |
419 |
| - lookback_period=lookback_period, |
420 |
| - ) |
421 |
| - |
422 | 347 | params = {"phrase": search_phrase} if search_phrase else {}
|
423 |
| - |
424 | 348 | async for audit in self._get_paginated(
|
425 | 349 | f"enterprises/{enterprise_name}/audit-log", params=params
|
426 | 350 | ):
|
|
0 commit comments