-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply-tags.py
74 lines (53 loc) · 2.03 KB
/
apply-tags.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import requests
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("--org-id", dest="org_id", help="your Snyk Org ID", required=True)
parser.add_argument("--snyk-token", dest="snyk_token", help="your Snyk Token", required=True)
parser.add_argument("--origin", dest="origin", help="SCM type. Possible values: github, github-enterprise, azure-repos, bitbucket, gitlab", required=True)
args = parser.parse_args()
SNYK_TOKEN = args.snyk_token
ORG_ID = args.org_id
ORIGIN = args.origin
BASE_URL = "https://api.snyk.io"
def get_projects_page(next_url):
# Add "next url" on to the BASE URL
url = BASE_URL + next_url
headers = {
'Accept': 'application/vnd.api+json',
'Authorization': f'token {SNYK_TOKEN}'
}
return requests.request("GET", url, headers=headers)
def get_all_projects():
next_url = f"/rest/orgs/{ORG_ID}/projects?version=2024-03-12&limit=100&origins={ORIGIN}"
all_projects = []
while next_url is not None:
res = get_projects_page(next_url).json()
if 'links' in res and 'next' in res['links']:
next_url = res['links']['next']
else:
next_url = None
# add to list
if 'data' in res:
all_projects.extend(res['data'])
return all_projects
def tag_project(project_id, key, value):
url = f'https://api.snyk.io/v1/org/{ORG_ID}/project/{project_id}/tags'
payload = {
"key": key,
"value": value
}
headers = {
'Content-Type': 'application/json',
'Authorization': f'token {SNYK_TOKEN}'
}
return requests.request("POST", url, headers=headers, json=payload)
def main():
projects = get_all_projects()
for p in projects:
project_id = p['id']
repo = p['attributes']['name'].split(":")[0].split("(")[0]
branch = p['attributes']['target_reference']
tag_value = f'pkg:{repo}@{branch}'
res = tag_project(project_id, 'component', tag_value)
print(res.status_code, project_id, tag_value)
main()