Skip to content

Commit bcad005

Browse files
authored
Doug/add new endpoints (#36)
* Adding new endpoints * Added new endpoints * Added new endpoints to docs
1 parent 979606c commit bcad005

File tree

9 files changed

+420
-4
lines changed

9 files changed

+420
-4
lines changed

README.rst

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,3 +575,205 @@ Retrieve the Issues associated with a package and version.
575575
576576
- **package (str)** - The name of the NPM package.
577577
- **version (str)** - The version of the NPM Package.
578+
579+
labels.list(org_slug)
580+
"""""""""""""""""""""""
581+
List all repository labels for the given organization.
582+
583+
**Usage:**
584+
585+
.. code-block:: python
586+
587+
from socketdev import socketdev
588+
589+
socket = socketdev(token="REPLACE_ME")
590+
print(socket.labels.list("org_slug"))
591+
592+
**PARAMETERS:**
593+
594+
- **org_slug (str)** – The organization name
595+
596+
labels.post(org_slug, label_name)
597+
"""""""""""""""""""""""""""""""""""
598+
Create a new label in the organization.
599+
600+
**Usage:**
601+
602+
.. code-block:: python
603+
604+
print(socket.labels.post("org_slug", "my-label"))
605+
606+
**PARAMETERS:**
607+
608+
- **org_slug (str)** – The organization name
609+
- **label_name (str)** – Name of the label to create
610+
611+
labels.get(org_slug, label_id)
612+
"""""""""""""""""""""""""""""""""
613+
Retrieve a single label by its ID.
614+
615+
**Usage:**
616+
617+
.. code-block:: python
618+
619+
print(socket.labels.get("org_slug", "label_id"))
620+
621+
**PARAMETERS:**
622+
623+
- **org_slug (str)** – The organization name
624+
- **label_id (str)** – The label ID
625+
626+
labels.delete(org_slug, label_id)
627+
"""""""""""""""""""""""""""""""""""
628+
Delete a label by ID.
629+
630+
**Usage:**
631+
632+
.. code-block:: python
633+
634+
print(socket.labels.delete("org_slug", "label_id"))
635+
636+
**PARAMETERS:**
637+
638+
- **org_slug (str)** – The organization name
639+
- **label_id (str)** – The label ID
640+
641+
labels.associate(org_slug, label_id, repo_id)
642+
"""""""""""""""""""""""""""""""""""""""""""""""
643+
Associate a label with a repository.
644+
645+
**Usage:**
646+
647+
.. code-block:: python
648+
649+
print(socket.labels.associate("org_slug", 1234, "repo_id"))
650+
651+
**PARAMETERS:**
652+
653+
- **org_slug (str)** – The organization name
654+
- **label_id (int)** – The label ID
655+
- **repo_id (str)** – The repository ID
656+
657+
labels.disassociate(org_slug, label_id, repo_id)
658+
"""""""""""""""""""""""""""""""""""""""""""""""""
659+
Disassociate a label from a repository.
660+
661+
**Usage:**
662+
663+
.. code-block:: python
664+
665+
print(socket.labels.disassociate("org_slug", 1234, "repo_id"))
666+
667+
**PARAMETERS:**
668+
669+
- **org_slug (str)** – The organization name
670+
- **label_id (int)** – The label ID
671+
- **repo_id (str)** – The repository ID
672+
673+
labels.setting.get(org_slug, label_id, setting_key)
674+
"""""""""""""""""""""""""""""""""""""""""""""""""""""
675+
Get a setting for a specific label.
676+
677+
**Usage:**
678+
679+
.. code-block:: python
680+
681+
print(socket.labels.setting.get("org_slug", 1234, "severity"))
682+
683+
**PARAMETERS:**
684+
685+
- **org_slug (str)** – The organization name
686+
- **label_id (int)** – The label ID
687+
- **setting_key (str)** – The key of the setting
688+
689+
labels.setting.put(org_slug, label_id, settings)
690+
"""""""""""""""""""""""""""""""""""""""""""""""""""
691+
Update settings for a specific label.
692+
693+
**Usage:**
694+
695+
.. code-block:: python
696+
697+
settings = {"severity": {"value": {"level": "high"}}}
698+
print(socket.labels.setting.put("org_slug", 1234, settings))
699+
700+
**PARAMETERS:**
701+
702+
- **org_slug (str)** – The organization name
703+
- **label_id (int)** – The label ID
704+
- **settings (dict)** – A dictionary of label settings
705+
706+
labels.setting.delete(org_slug, label_id, setting_key)
707+
"""""""""""""""""""""""""""""""""""""""""""""""""""""""
708+
Delete a setting from a label.
709+
710+
**Usage:**
711+
712+
.. code-block:: python
713+
714+
print(socket.labels.setting.delete("org_slug", 1234, "severity"))
715+
716+
**PARAMETERS:**
717+
718+
- **org_slug (str)** – The organization name
719+
- **label_id (int)** – The label ID
720+
- **setting_key (str)** – The setting key to delete
721+
722+
historical.list(org_slug, query_params=None)
723+
"""""""""""""""""""""""""""""""""""""""""""""""
724+
List historical alerts for an organization.
725+
726+
**Usage:**
727+
728+
.. code-block:: python
729+
730+
print(socket.historical.list("org_slug", {"repo": "example-repo"}))
731+
732+
**PARAMETERS:**
733+
734+
- **org_slug (str)** – The organization name
735+
- **query_params (dict, optional)** – Optional query parameters
736+
737+
historical.trend(org_slug, query_params=None)
738+
"""""""""""""""""""""""""""""""""""""""""""""""
739+
Retrieve alert trend data across time.
740+
741+
**Usage:**
742+
743+
.. code-block:: python
744+
745+
print(socket.historical.trend("org_slug", {"range": "30d"}))
746+
747+
**PARAMETERS:**
748+
749+
- **org_slug (str)** – The organization name
750+
- **query_params (dict, optional)** – Optional query parameters
751+
752+
historical.snapshots.create(org_slug)
753+
""""""""""""""""""""""""""""""""""""""""
754+
Create a new snapshot of historical data.
755+
756+
**Usage:**
757+
758+
.. code-block:: python
759+
760+
print(socket.historical.snapshots.create("org_slug"))
761+
762+
**PARAMETERS:**
763+
764+
- **org_slug (str)** – The organization name
765+
766+
historical.snapshots.list(org_slug, query_params=None)
767+
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
768+
List all historical snapshots for an organization.
769+
770+
**Usage:**
771+
772+
.. code-block:: python
773+
774+
print(socket.historical.snapshots.list("org_slug", {"repo": "example-repo"}))
775+
776+
**PARAMETERS:**
777+
778+
- **org_slug (str)** – The organization name
779+
- **query_params (dict, optional)** – Optional query parameters

example-socket-export.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import json
2+
import os
3+
import logging
4+
from socketdev import socketdev
5+
logging.basicConfig(level=logging.INFO)
6+
7+
8+
sdk = socketdev(token=os.getenv("SOCKET_SECURITY_API_KEY"))
9+
10+
orgs = sdk.org.get()
11+
if len(orgs) > 0:
12+
org_id = None
13+
org_slug = None
14+
for org_key in orgs['organizations']:
15+
org = orgs['organizations'][org_key]
16+
org_id = org_key
17+
org_slug = org['slug']
18+
else:
19+
print("Something went wrong with getting org info")
20+
exit(1)
21+
per_page = 100
22+
response = sdk.repos.get(org_slug, per_page=per_page)
23+
next_page = response.get("nextPage")
24+
repos = response.get("results")
25+
while next_page is not None and next_page != 0:
26+
response = sdk.repos.get(org_slug, per_page=per_page, page=next_page)
27+
next_page = response.get("nextPage")
28+
repos.extend(response.get("results"))
29+
if len(response.get("results", [])) == 0:
30+
break
31+
32+
# repos = repos[:20]
33+
head_full_scans_ids = []
34+
for repo in repos:
35+
head_full_scans_id = repo.get("head_full_scan_id")
36+
if head_full_scans_id:
37+
head_full_scans_ids.append(head_full_scans_id)
38+
39+
socket_results = {}
40+
for head_full_scan_id in head_full_scans_ids:
41+
full_scan_metadata = sdk.fullscans.metadata(org_slug=org_slug, full_scan_id=head_full_scan_id)
42+
full_scan_result = {
43+
"repo": full_scan_metadata.get("repo"),
44+
"branch": full_scan_metadata.get("branch"),
45+
"commit_hash": full_scan_metadata.get("commit_hash"),
46+
"commit_message": full_scan_metadata.get("commit_message"),
47+
"pull_request_url": full_scan_metadata.get("pull_request_url"),
48+
"committers": full_scan_metadata.get("committers"),
49+
"created_at": full_scan_metadata.get("created_at"),
50+
"results": sdk.fullscans.stream(org_slug=org_slug, full_scan_id=head_full_scan_id) or None
51+
}
52+
socket_results[head_full_scan_id] = full_scan_result
53+
54+
print(json.dumps(socket_results, indent=4))

socketdev/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from socketdev.triage import Triage
1717
from socketdev.utils import Utils, IntegrationType, INTEGRATION_TYPES
1818
from socketdev.version import __version__
19+
from socketdev.labels import Labels
1920
from socketdev.log import log
2021

2122

@@ -57,6 +58,7 @@ def __init__(self, token: str, timeout: int = 1200):
5758
self.settings = Settings(self.api)
5859
self.triage = Triage(self.api)
5960
self.utils = Utils()
61+
self.labels = Labels(self.api)
6062

6163
@staticmethod
6264
def set_timeout(timeout: int):

socketdev/apitokens/__init__.py

Whitespace-only changes.

socketdev/auditlog/__init__.py

Whitespace-only changes.

socketdev/historical/__init__.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class Historical:
88
def __init__(self, api):
99
self.api = api
10+
self.snapshots = self.Snapshots(api)
1011

1112
def list(self, org_slug: str, query_params: dict = None) -> dict:
1213
"""Get historical alerts list for an organization.
@@ -15,7 +16,7 @@ def list(self, org_slug: str, query_params: dict = None) -> dict:
1516
org_slug: Organization slug
1617
query_params: Optional dictionary of query parameters
1718
"""
18-
path = f"orgs/{org_slug}/alerts/historical"
19+
path = f"orgs/{org_slug}/historical/alerts"
1920
if query_params:
2021
path += "?" + urlencode(query_params)
2122

@@ -28,13 +29,13 @@ def list(self, org_slug: str, query_params: dict = None) -> dict:
2829
return {}
2930

3031
def trend(self, org_slug: str, query_params: dict = None) -> dict:
31-
"""Get historical alerts trend data for an organization.
32+
"""Get historical alert trends data for an org.
3233
3334
Args:
3435
org_slug: Organization slug
3536
query_params: Optional dictionary of query parameters
3637
"""
37-
path = f"orgs/{org_slug}/alerts/historical/trend"
38+
path = f"orgs/{org_slug}/historical/alerts/trend"
3839
if query_params:
3940
path += "?" + urlencode(query_params)
4041

@@ -45,3 +46,39 @@ def trend(self, org_slug: str, query_params: dict = None) -> dict:
4546
log.error(f"Error getting historical trend: {response.status_code}")
4647
log.error(response.text)
4748
return {}
49+
50+
class Snapshots:
51+
"""Submodule for managing historical snapshots."""
52+
53+
def __init__(self, api):
54+
self.api = api
55+
56+
def create(self, org_slug: str) -> dict:
57+
"""Create a new snapshot for an organization.
58+
59+
Args:
60+
org_slug: Organization slug
61+
data: Dictionary containing snapshot data
62+
"""
63+
path = f"orgs/{org_slug}/historical/snapshots"
64+
response = self.api.do_request(path=path, method="POST")
65+
if response.status_code == 200:
66+
return response.json()
67+
68+
log.error(f"Error creating snapshot: {response.status_code}")
69+
log.error(response.text)
70+
return {}
71+
72+
def list(self, org_slug: str, query_params: dict = None) -> dict:
73+
"""List historical snapshots for an organization."""
74+
path = f"orgs/{org_slug}/historical/snapshots"
75+
if query_params:
76+
path += "?" + urlencode(query_params)
77+
78+
response = self.api.do_request(path=path)
79+
if response.status_code == 200:
80+
return response.json()
81+
82+
log.error(f"Error listing snapshots: {response.status_code}")
83+
log.error(response.text)
84+
return {}

0 commit comments

Comments
 (0)