|
| 1 | +import json |
| 2 | +import logging |
| 3 | + |
| 4 | + |
| 5 | +log = logging.getLogger("socketdev") |
| 6 | + |
| 7 | + |
| 8 | +class LicenseMetadata: |
| 9 | + def __init__(self, api): |
| 10 | + self.api = api |
| 11 | + |
| 12 | + def post(self, licenses: list) -> dict: |
| 13 | + path = f"license-metadata" |
| 14 | + payload = json.dumps(licenses) |
| 15 | + response = self.api.do_request(path=path, method="POST", payload=payload) |
| 16 | + |
| 17 | + if response.status_code == 200: |
| 18 | + result = response.json() |
| 19 | + return result |
| 20 | + |
| 21 | + error_message = response.json().get("error", {}).get("message", "Unknown error") |
| 22 | + log.error(f"Failed to create license metadata: {response.status_code}, message: {error_message}") |
| 23 | + return {} |
| 24 | + |
| 25 | + def get(self, org_slug: str, label_id: str) -> dict: |
| 26 | + path = f"orgs/{org_slug}/repos/labels/{label_id}" |
| 27 | + response = self.api.do_request(path=path) |
| 28 | + if response.status_code == 200: |
| 29 | + result = response.json() |
| 30 | + return result |
| 31 | + |
| 32 | + error_message = response.json().get("error", {}).get("message", "Unknown error") |
| 33 | + log.error(f"Failed to get repository label: {response.status_code}, message: {error_message}") |
| 34 | + return {} |
| 35 | + |
| 36 | + def delete(self, org_slug: str, label_id: str) -> dict: |
| 37 | + path = f"orgs/{org_slug}/repos/labels/{label_id}" |
| 38 | + response = self.api.do_request(path=path, method="DELETE") |
| 39 | + if response.status_code == 200: |
| 40 | + return response.json() |
| 41 | + |
| 42 | + error_message = response.json().get("error", {}).get("message", "Unknown error") |
| 43 | + log.error(f"Error deleting repository label: {response.status_code}, message: {error_message}") |
| 44 | + return {} |
| 45 | + |
| 46 | + |
| 47 | + def associate(self, org_slug: str, label_id: int, repo_id: str) -> dict: |
| 48 | + path = f"orgs/{org_slug}/repos/labels/{label_id}/associate" |
| 49 | + payload = json.dumps({"repository_id": repo_id}) |
| 50 | + response = self.api.do_request(path=path, method="POST", payload=payload) |
| 51 | + if response.status_code == 200: |
| 52 | + return response.json() |
| 53 | + |
| 54 | + error_message = response.json().get("error", {}).get("message", "Unknown error") |
| 55 | + log.error(f"Error associating repository label: {response.status_code}, message: {error_message}") |
| 56 | + return {} |
| 57 | + |
| 58 | + def disassociate(self, org_slug: str, label_id: int, repo_id: str) -> dict: |
| 59 | + path = f"orgs/{org_slug}/repos/labels/{label_id}/disassociate" |
| 60 | + payload = json.dumps({"repository_id": repo_id}) |
| 61 | + response = self.api.do_request(path=path, method="POST", payload=payload) |
| 62 | + if response.status_code == 200: |
| 63 | + return response.json() |
| 64 | + |
| 65 | + error_message = response.json().get("error", {}).get("message", "Unknown error") |
| 66 | + log.error(f"Error associating repository label: {response.status_code}, message: {error_message}") |
| 67 | + return {} |
0 commit comments