|
9 | 9 | from pontos.github.models.code_scanning import ( |
10 | 10 | AlertSort, |
11 | 11 | AlertState, |
| 12 | + Analysis, |
12 | 13 | CodeScanningAlert, |
13 | 14 | DismissedReason, |
14 | 15 | Instance, |
@@ -309,3 +310,148 @@ async def instances( |
309 | 310 | async for response in self._client.get_all(api, params=params): |
310 | 311 | for alert in response.json(): |
311 | 312 | yield Instance.from_dict(alert) |
| 313 | + |
| 314 | + async def analyses( |
| 315 | + self, |
| 316 | + repo: str, |
| 317 | + *, |
| 318 | + tool_name: Optional[str] = None, |
| 319 | + tool_guid: Optional[str] = "", |
| 320 | + ref: Optional[str] = None, |
| 321 | + sarif_id: Optional[str] = None, |
| 322 | + direction: Union[str, SortOrder] = SortOrder.DESC, |
| 323 | + ) -> AsyncIterator[Analysis]: |
| 324 | + """ |
| 325 | + Lists the details of all code scanning analyses for a repository, |
| 326 | + starting with the most recent. |
| 327 | +
|
| 328 | + https://docs.github.com/en/rest/code-scanning/code-scanning#list-code-scanning-analyses-for-a-repository |
| 329 | +
|
| 330 | + Args: |
| 331 | + repo: GitHub repository (owner/name) |
| 332 | + tool_name: The name of a code scanning tool. Only results by this |
| 333 | + tool will be listed. You can specify the tool by using either |
| 334 | + tool_name or tool_guid, but not both. |
| 335 | + tool_guid: The GUID of a code scanning tool. Only results by this |
| 336 | + tool will be listed. Note that some code scanning tools may not |
| 337 | + include a GUID in their analysis data. You can specify the tool |
| 338 | + by using either tool_guid or tool_name, but not both |
| 339 | + ref: The Git reference for the analyses you want to list. The ref |
| 340 | + for a branch can be formatted either as refs/heads/<branch name> |
| 341 | + or simply <branch name>. To reference a pull request use |
| 342 | + refs/pull/<number>/merge. |
| 343 | + sarif_id: Filter analyses belonging to the same SARIF upload |
| 344 | +
|
| 345 | + Raises: |
| 346 | + HTTPStatusError: A httpx.HTTPStatusError is raised if the request |
| 347 | + failed. |
| 348 | +
|
| 349 | + Returns: |
| 350 | + An async iterator yielding the code scanning alert analysis data |
| 351 | +
|
| 352 | + Example: |
| 353 | + .. code-block:: python |
| 354 | +
|
| 355 | + from pontos.github.api import GitHubAsyncRESTApi |
| 356 | +
|
| 357 | + async with GitHubAsyncRESTApi(token) as api: |
| 358 | + async for data in api.code_scanning.analyses( |
| 359 | + "org/repo" |
| 360 | + ): |
| 361 | + print(data) |
| 362 | + """ |
| 363 | + |
| 364 | + api = f"/repos/{repo}/code-scanning/analyses" |
| 365 | + params: dict[str, Union[str, None]] = {"per_page": "100"} |
| 366 | + |
| 367 | + if tool_name: |
| 368 | + params["tool_name"] = tool_name |
| 369 | + if tool_guid or tool_guid is None: |
| 370 | + params["tool_guid"] = tool_guid |
| 371 | + if ref: |
| 372 | + params["ref"] = ref |
| 373 | + if sarif_id: |
| 374 | + params["sarif_id"] = sarif_id |
| 375 | + if direction: |
| 376 | + params["direction"] = enum_or_value(direction) |
| 377 | + |
| 378 | + async for response in self._client.get_all(api, params=params): |
| 379 | + response.raise_for_status() |
| 380 | + |
| 381 | + for alert in response.json(): |
| 382 | + yield Analysis.from_dict(alert) |
| 383 | + |
| 384 | + async def analysis( |
| 385 | + self, |
| 386 | + repo: str, |
| 387 | + analysis_id: Union[int, str], |
| 388 | + ) -> Analysis: |
| 389 | + """ |
| 390 | + Gets a specified code scanning analysis for a repository |
| 391 | +
|
| 392 | + https://docs.github.com/en/rest/code-scanning/code-scanning#get-a-code-scanning-analysis-for-a-repository |
| 393 | +
|
| 394 | + Args: |
| 395 | + repo: GitHub repository (owner/name) |
| 396 | + analysis_id: The ID of the analysis |
| 397 | +
|
| 398 | + Raises: |
| 399 | + HTTPStatusError: A httpx.HTTPStatusError is raised if the request |
| 400 | + failed. |
| 401 | +
|
| 402 | + Returns: |
| 403 | + Code scanning alert analysis data |
| 404 | +
|
| 405 | + Example: |
| 406 | + .. code-block:: python |
| 407 | +
|
| 408 | + from pontos.github.api import GitHubAsyncRESTApi |
| 409 | +
|
| 410 | + async with GitHubAsyncRESTApi(token) as api: |
| 411 | + analysis = await api.code_scanning.analysis( |
| 412 | + "org/repo", 123 |
| 413 | + ) |
| 414 | + print(analysis) |
| 415 | + """ |
| 416 | + |
| 417 | + api = f"/repos/{repo}/code-scanning/analyses/{analysis_id}" |
| 418 | + response = await self._client.get(api) |
| 419 | + response.raise_for_status() |
| 420 | + return Analysis.from_dict(response.json()) |
| 421 | + |
| 422 | + async def delete_analysis( |
| 423 | + self, |
| 424 | + repo: str, |
| 425 | + analysis_id: Union[int, str], |
| 426 | + ) -> dict[str, str]: |
| 427 | + """ |
| 428 | + Delete a specified code scanning analysis from a repository |
| 429 | +
|
| 430 | + https://docs.github.com/en/rest/code-scanning/code-scanning#delete-a-code-scanning-analysis-from-a-repository |
| 431 | +
|
| 432 | + Args: |
| 433 | + repo: GitHub repository (owner/name) |
| 434 | + analysis_id: The ID of the analysis |
| 435 | +
|
| 436 | + Raises: |
| 437 | + HTTPStatusError: A httpx.HTTPStatusError is raised if the request |
| 438 | + failed. |
| 439 | +
|
| 440 | + Returns: |
| 441 | + See the GitHub documentation for the response object |
| 442 | +
|
| 443 | + Example: |
| 444 | + .. code-block:: python |
| 445 | +
|
| 446 | + from pontos.github.api import GitHubAsyncRESTApi |
| 447 | +
|
| 448 | + async with GitHubAsyncRESTApi(token) as api: |
| 449 | + await api.code_scanning.delete( |
| 450 | + "org/repo", 123 |
| 451 | + ) |
| 452 | + """ |
| 453 | + |
| 454 | + api = f"/repos/{repo}/code-scanning/analyses/{analysis_id}" |
| 455 | + response = await self._client.delete(api) |
| 456 | + response.raise_for_status() |
| 457 | + return response.json() |
0 commit comments