Skip to content

Instantly share code, notes, and snippets.

@gus3inov
Last active February 2, 2023 14:59
Show Gist options
  • Save gus3inov/e2eb375d8b1a578edae61ab63418c967 to your computer and use it in GitHub Desktop.
Save gus3inov/e2eb375d8b1a578edae61ab63418c967 to your computer and use it in GitHub Desktop.
YOUTRACK Insert Enum Bundle Values
import requests
import os
import sys
YOUTRACK_BASE_URL = os.getenv('YOUTRACK_BASE_URL')
YOUTRACK_AUTH_TOKEN = os.getenv('YOUTRACK_AUTH_TOKEN')
def get_project_id(project_name: str) -> str:
params = f"fields=id&query={project_name}"
try:
req = requests.get(
f"{YOUTRACK_BASE_URL}/api/admin/projects?{params}",
headers={
'Authorization': f"Bearer {YOUTRACK_AUTH_TOKEN}",
},
)
if req.status_code != 200:
print(f"Something went wrong while getting project_id: {project_name}", req.content)
else:
body = req.json()
project = body[0]
return project["id"]
except Exception as error:
print(f"Something went wrong while getting project_id: {project_name}", error)
def find_bundle_id(custom_fields, enum_name):
bundle_id = None
for custom_field in custom_fields:
if not custom_field:
continue
if "bundle" not in custom_field:
continue
if "field" not in custom_field:
continue
field = custom_field["field"]
if "name" in field and field["name"] == enum_name:
bundle = custom_field["bundle"]
bundle_id = bundle["id"]
break
if bundle_id is None:
print(f"Cannot found bundle_id for enum: {enum_name}")
return None
return bundle_id
def get_bundle_id(project_name, enum_name):
try:
project_id = get_project_id(project_name)
req = requests.get(
f"{YOUTRACK_BASE_URL}/api/admin/projects/{project_id}/customFields?fields=id,bundle(id),field(name,fieldType($type))",
headers={
'Authorization': f"Bearer {YOUTRACK_AUTH_TOKEN}",
},
)
if req.status_code != 200:
print(f"Something went wrong while getting bundle_id of enum: {enum_name}", req.content)
else:
body = req.json()
bundle_id = find_bundle_id(body, enum_name)
return bundle_id
except Exception as error:
print(f"Something went wrong while getting bundle_id of enum: {enum_name}", error)
if __name__ == "__main__":
project_name = sys.argv[1]
enum_name = sys.argv[2]
bundle_id = get_bundle_id(project_name, enum_name)
print(f"Bundle ID of enum: {enum_name} is {bundle_id}")
import requests
import os
import sys
YOUTRACK_BASE_URL = os.getenv('YOUTRACK_BASE_URL')
YOUTRACK_AUTH_TOKEN = os.getenv('YOUTRACK_AUTH_TOKEN')
API_URL = "{}/api/admin/customFieldSettings/bundles/enum/{}/values?fields=id,name"
def insert_bundle(bundle_id, values):
for value in values:
body = {
"name": value,
}
try:
req = requests.post(
API_URL.format(YOUTRACK_BASE_URL, bundle_id),
headers={
'Authorization': f"Bearer {YOUTRACK_AUTH_TOKEN}",
},
json=body,
)
if req.status_code != 200:
print(f"Something went wrong while inserting value: {value}", req.content)
else:
print(f"Value: {value} is successfully inserted")
except Exception as error:
print(f"Something went wrong while inserting value: {value}", error)
def get_values_from_file(filepath):
read_values = []
with open(filepath, "r") as values:
lines = values.readlines()
for line in lines:
read_value = line.replace("\n", "")
read_values.append(read_value)
return read_values
if __name__ == "__main__":
bundle_id = sys.argv[1]
filepath_to_values = sys.argv[2]
values = get_values_from_file(filepath_to_values)
insert_bundle(bundle_id, values)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment