|
| 1 | +import json |
| 2 | +import logging |
| 3 | +from typing import Any, Dict, Optional, Union |
| 4 | + |
| 5 | +log = logging.getLogger("socketdev") |
| 6 | + |
| 7 | +class DiffScans: |
| 8 | + def __init__(self, api): |
| 9 | + self.api = api |
| 10 | + |
| 11 | + def list(self, org_slug: str, params: Optional[Dict[str, Any]] = None) -> dict: |
| 12 | + """List all diff scans for an organization.""" |
| 13 | + path = f"orgs/{org_slug}/diff-scans" |
| 14 | + if params: |
| 15 | + import urllib.parse |
| 16 | + path += "?" + urllib.parse.urlencode(params) |
| 17 | + response = self.api.do_request(path=path, method="GET") |
| 18 | + if response.status_code == 200: |
| 19 | + return response.json() |
| 20 | + log.error(f"Error listing diff scans: {response.status_code}, message: {response.text}") |
| 21 | + return {} |
| 22 | + |
| 23 | + def get(self, org_slug: str, diff_scan_id: str) -> dict: |
| 24 | + """Fetch a diff scan by ID.""" |
| 25 | + path = f"orgs/{org_slug}/diff-scans/{diff_scan_id}" |
| 26 | + response = self.api.do_request(path=path, method="GET") |
| 27 | + if response.status_code == 200: |
| 28 | + return response.json() |
| 29 | + log.error(f"Error fetching diff scan: {response.status_code}, message: {response.text}") |
| 30 | + return {} |
| 31 | + |
| 32 | + def create_from_repo(self, org_slug: str, repo_slug: str, files: list, params: Optional[Dict[str, Any]] = None) -> dict: |
| 33 | + """Create a diff scan from repo HEAD, uploading files as multipart form data.""" |
| 34 | + import urllib.parse |
| 35 | + path = f"orgs/{org_slug}/diff-scans/from-repo/{repo_slug}" |
| 36 | + if params: |
| 37 | + path += "?" + urllib.parse.urlencode(params) |
| 38 | + response = self.api.do_request(path=path, method="POST", files=files) |
| 39 | + if response.status_code in (200, 201): |
| 40 | + return response.json() |
| 41 | + log.error(f"Error creating diff scan from repo: {response.status_code}, message: {response.text}") |
| 42 | + return {} |
| 43 | + |
| 44 | + def create_from_ids(self, org_slug: str, params: Dict[str, Any]) -> dict: |
| 45 | + """Create a diff scan from two full scan IDs using query params.""" |
| 46 | + import urllib.parse |
| 47 | + path = f"orgs/{org_slug}/diff-scans/from-ids" |
| 48 | + if params: |
| 49 | + path += "?" + urllib.parse.urlencode(params) |
| 50 | + response = self.api.do_request(path=path, method="POST") |
| 51 | + if response.status_code in (200, 201): |
| 52 | + return response.json() |
| 53 | + log.error(f"Error creating diff scan from IDs: {response.status_code}, message: {response.text}") |
| 54 | + return {} |
| 55 | + |
| 56 | + def gfm(self, org_slug: str, diff_scan_id: str) -> dict: |
| 57 | + """Fetch GFM (GitHub Flavored Markdown) comments for a diff scan.""" |
| 58 | + path = f"orgs/{org_slug}/diff-scans/{diff_scan_id}/gfm" |
| 59 | + response = self.api.do_request(path=path, method="GET") |
| 60 | + if response.status_code == 200: |
| 61 | + return response.json() |
| 62 | + log.error(f"Error fetching diff scan GFM: {response.status_code}, message: {response.text}") |
| 63 | + return {} |
| 64 | + |
| 65 | + def delete(self, org_slug: str, diff_scan_id: str) -> bool: |
| 66 | + """Delete a diff scan by ID.""" |
| 67 | + path = f"orgs/{org_slug}/diff-scans/{diff_scan_id}" |
| 68 | + response = self.api.do_request(path=path, method="DELETE") |
| 69 | + if response.status_code == 200: |
| 70 | + if "status" in response.json() and response.json()["status"] == "ok": |
| 71 | + return True |
| 72 | + log.error(f"Error deleting diff scan: {response.status_code}, message: {response.text}") |
| 73 | + return False |
0 commit comments