Skip to content

Instantly share code, notes, and snippets.

@iman4000
Last active June 25, 2024 16:38
Show Gist options
  • Save iman4000/6c55a9842ba8c78febf9f577270605bc to your computer and use it in GitHub Desktop.
Save iman4000/6c55a9842ba8c78febf9f577270605bc to your computer and use it in GitHub Desktop.
python code for DependencyTrack interaction to manage teams and projects
### DT with list
import requests
API_BASE_URL = "http://your-api-url/api"
API_KEY = "your-api-key"
def get_headers():
return {
'X-Api-Key': API_KEY,
'Content-Type': 'application/json'
}
def create_team(team_name):
url = f"{API_BASE_URL}/v1/team"
payload = {"name": team_name}
response = requests.post(url, json=payload, headers=get_headers())
if response.status_code == 201:
return response.json()['uuid']
else:
print(f"Error creating team: {response.status_code} - {response.text}")
return None
def get_team_uuid(team_name):
url = f"{API_BASE_URL}/v1/team"
response = requests.get(url, headers=get_headers())
if response.status_code == 200:
teams = response.json()
for team in teams:
if team['name'] == team_name:
return team['uuid']
return create_team(team_name)
def upload_bom(project_name, project_version, bom_file_path, auto_create=True):
url = f"{API_BASE_URL}/v1/bom"
with open(bom_file_path, 'rb') as bom_file:
files = {
'bom': bom_file,
'projectName': (None, project_name),
'projectVersion': (None, project_version),
'autoCreate': (None, str(auto_create).lower())
}
response = requests.post(url, files=files, headers={'X-Api-Key': API_KEY})
if response.status_code == 200:
return response.json()['project']['uuid']
else:
print(f"Error uploading BOM: {response.status_code} - {response.text}")
return None
def map_project_to_team(project_uuid, team_uuid):
url = f"{API_BASE_URL}/v1/acl/mapping"
payload = {
"team": team_uuid,
"project": project_uuid
}
response = requests.put(url, json=payload, headers=get_headers())
if response.status_code == 200:
print(f"Successfully mapped project {project_uuid} to team {team_uuid}")
else:
print(f"Error mapping project: {response.status_code} - {response.text}")
def set_project_property(project_uuid, manager, namespace, docker_image_link):
url = f"{API_BASE_URL}/v1/project/{project_uuid}/property"
properties = [
{"key": "project_manager", "value": manager},
{"key": "namespace", "value": namespace},
{"key": "docker_image_link", "value": docker_image_link}
]
for prop in properties:
response = requests.put(url, json=prop, headers=get_headers())
if response.status_code != 201:
print(f"Error setting property {prop['key']} for project {project_uuid}: {response.status_code} - {response.text}")
def set_project_tag(project_uuid, tag_name):
url = f"{API_BASE_URL}/v1/project/{project_uuid}/tag"
payload = {"name": tag_name}
response = requests.put(url, json=payload, headers=get_headers())
if response.status_code == 201:
print(f"Successfully set tag {tag_name} for project {project_uuid}")
else:
print(f"Error setting tag {tag_name} for project {project_uuid}: {response.status_code} - {response.text}")
def main(project_team_map, project_bom_files, project_properties, project_tags):
for project, team in project_team_map.items():
project_name, project_version = project.split(':')
team_uuid = get_team_uuid(team)
bom_file_path = project_bom_files.get(project)
if bom_file_path:
project_uuid = upload_bom(project_name, project_version, bom_file_path)
if project_uuid and team_uuid:
map_project_to_team(project_uuid, team_uuid)
if project in project_properties:
properties = project_properties[project]
set_project_property(project_uuid, properties['manager'], properties['namespace'], properties['docker_image_link'])
if project in project_tags:
set_project_tag(project_uuid, project_tags[project])
else:
print(f"BOM file not found for project {project}")
if __name__ == "__main__":
project_team_map = {
"ProjectA:1.0": "TeamA",
"ProjectB:2.0": "TeamB",
# Add more projects and their respective teams here
}
project_bom_files = {
"ProjectA:1.0": "path/to/your/bom/fileA",
"ProjectB:2.0": "path/to/your/bom/fileB",
# Add more projects and their BOM file paths here
}
project_properties = {
"ProjectA:1.0": {
"manager": "ManagerA",
"namespace": "NamespaceA",
"docker_image_link": "docker.registry.com/namespacea/projecta"
},
"ProjectB:2.0": {
"manager": "ManagerB",
"namespace": "NamespaceB",
"docker_image_link": "docker.registry.com/namespaceb/projectb"
}
# Add more projects and their properties here
}
project_tags = {
"ProjectA:1.0": "SpecialTagA",
"ProjectB:2.0": "SpecialTagB",
# Add more projects and their tags here
}
main(project_team_map, project_bom_files, project_properties, project_tags)
#########################################################################################
#########################################################################################
#########################################################################################
### DT with one by one project insertion
import requests
API_BASE_URL = "http://your-api-url/api"
API_KEY = "your-api-key"
def get_headers():
return {
'X-Api-Key': API_KEY,
'Content-Type': 'application/json'
}
def create_team(team_name):
url = f"{API_BASE_URL}/v1/team"
payload = {"name": team_name}
response = requests.post(url, json=payload, headers=get_headers())
if response.status_code == 201:
return response.json()['uuid']
else:
print(f"Error creating team: {response.status_code} - {response.text}")
return None
def get_team_uuid(team_name):
url = f"{API_BASE_URL}/v1/team"
response = requests.get(url, headers=get_headers())
if response.status_code == 200:
teams = response.json()
for team in teams:
if team['name'] == team_name:
return team['uuid']
return create_team(team_name)
def upload_bom(project_name, project_version, bom_file_path, auto_create=True):
url = f"{API_BASE_URL}/v1/bom"
with open(bom_file_path, 'rb') as bom_file:
files = {
'bom': bom_file,
'projectName': (None, project_name),
'projectVersion': (None, project_version),
'autoCreate': (None, str(auto_create).lower())
}
response = requests.post(url, files=files, headers={'X-Api-Key': API_KEY})
if response.status_code == 200:
return response.json()['project']['uuid']
else:
print(f"Error uploading BOM: {response.status_code} - {response.text}")
return None
def map_project_to_team(project_uuid, team_uuid):
url = f"{API_BASE_URL}/v1/acl/mapping"
payload = {
"team": team_uuid,
"project": project_uuid
}
response = requests.put(url, json=payload, headers=get_headers())
if response.status_code == 200:
print(f"Successfully mapped project {project_uuid} to team {team_uuid}")
else:
print(f"Error mapping project: {response.status_code} - {response.text}")
def set_project_property(project_uuid, manager, namespace, docker_image_link):
url = f"{API_BASE_URL}/v1/project/{project_uuid}/property"
properties = [
{"key": "project_manager", "value": manager},
{"key": "namespace", "value": namespace},
{"key": "docker_image_link", "value": docker_image_link}
]
for prop in properties:
response = requests.put(url, json=prop, headers=get_headers())
if response.status_code != 201:
print(f"Error setting property {prop['key']} for project {project_uuid}: {response.status_code} - {response.text}")
def set_project_tag(project_uuid, tag_name):
url = f"{API_BASE_URL}/v1/project/{project_uuid}/tag"
payload = {"name": tag_name}
response = requests.put(url, json=payload, headers=get_headers())
if response.status_code == 201:
print(f"Successfully set tag {tag_name} for project {project_uuid}")
else:
print(f"Error setting tag {tag_name} for project {project_uuid}: {response.status_code} - {response.text}")
def process_project(project_name, project_version, team_name, bom_file_path, properties, tag_name):
team_uuid = get_team_uuid(team_name)
if not team_uuid:
print(f"Failed to get or create team: {team_name}")
return
project_uuid = upload_bom(project_name, project_version, bom_file_path)
if not project_uuid:
print(f"Failed to upload BOM for project: {project_name}")
return
map_project_to_team(project_uuid, team_uuid)
set_project_property(project_uuid, properties['manager'], properties['namespace'], properties['docker_image_link'])
set_project_tag(project_uuid, tag_name)
if __name__ == "__main__":
project_name = "ProjectA"
project_version = "1.0"
team_name = "TeamA"
bom_file_path = "path/to/your/bom/fileA"
properties = {
"manager": "ManagerA",
"namespace": "NamespaceA",
"docker_image_link": "docker.registry.com/namespacea/projecta"
}
tag_name = "SpecialTagA"
process_project(project_name, project_version, team_name, bom_file_path, properties, tag_name)
# Repeat the above block for other projects
project_name = "ProjectB"
project_version = "2.0"
team_name = "TeamB"
bom_file_path = "path/to/your/bom/fileB"
properties = {
"manager": "ManagerB",
"namespace": "NamespaceB",
"docker_image_link": "docker.registry.com/namespaceb/projectb"
}
tag_name = "SpecialTagB"
process_project(project_name, project_version, team_name, bom_file_path, properties, tag_name)
# Continue for other projects
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment