Our instance has *a lot* of custom fields that have collected over many years. I'm looking at ways to safely remove custom fields that are no longer needed but I'm not sure what the best approach would be. I'm happy to use APIs to help with this but before I get started I wanted to check if there are any specific best practices someone could recommend.
Hi Karim,
I apologize for the delayed response and any inconvenience it may have caused.
The most effective method to accomplish this is by using our Compass GraphQL API. You can utilize it to retrieve all component and custom field IDs. Reference: Compass Graphql queriesThen, with a Python script, you can iterate through to eliminate any duplicate custom fields using their unique IDs.I have already prepared a Python script to carry out this task.Here is the code:
import requestsimport base64# Define your variables herecloudId = "Your-Cloud-ID"api_token = "API-TOKEN"email = "Your-email"domain = "Your-site.atlassian.net"# Base64 encode the email and token for Basic Authenticationauth_string = f"{email}:{api_token}"auth_bytes = auth_string.encode('utf-8')auth_base64 = base64.b64encode(auth_bytes).decode('utf-8')# GraphQL endpointgraphql_url = f'https://{domain}/gateway/api/graphql'# Define the GraphQL query to fetch components and custom fieldsgraphql_query = '''query searchCompassComponents($cloudId: String!, $query: CompassSearchComponentQuery) {compass {searchComponents(cloudId: $cloudId, query: $query) {... on CompassSearchComponentConnection {nodes {linkcomponent {namedescriptiontypeIdownerIdlinks {idtypenameurl}labels {name}customFields {definition {idname}...on CompassCustomBooleanField {booleanValue}...on CompassCustomTextField {textValue}...on CompassCustomNumberField {numberValue}...on CompassCustomUserField {userValue {idnamepictureaccountIdcanonicalAccountIdaccountStatus}}}}}pageInfo {hasNextPageendCursor}}... on QueryError {messageextensions {statusCodeerrorType}}}}}'''
# Define the variables for the GraphQL queryvariables = {"cloudId": cloudId,"query": {"fieldFilters": [{"name": "type","filter": {"eq": "SERVICE"}}]}}# Set up the headers with Basic Authentication and additional headersheaders = {'Authorization': f'Basic {auth_base64}','Content-Type': 'application/json','Accept': 'application/json','X-ExperimentalApi': 'compass-beta' # Opt-in to Compass beta API if needed}# Send the request to the GraphQL APIresponse = requests.post(graphql_url, json={'query': graphql_query, 'variables': variables}, headers=headers)# Check for request successif response.status_code == 200:data = response.json()# Extract custom field IDs from the responsecustom_field_ids = []for component in data['data']['compass']['searchComponents']['nodes']:for custom_field in component['component']['customFields']:custom_field_id = custom_field['definition']['id']custom_field_ids.append(custom_field_id)# Remove duplicate custom field IDsunique_custom_field_ids = list(set(custom_field_ids))# Output the unique custom field IDsprint("Unique Custom Field IDs:")for field_id in unique_custom_field_ids:print(field_id)else:print(f"Query failed with status code {response.status_code}")print(response.text)
Replace the values:cloudId = "Your-Cloud-ID"api_token = "API-TOKEN" you can generate one from here: https://id.atlassian.com/manage-profile/security/api-tokensemail = "Your-email"domain = "Your-site.atlassian.net"(!) Please be aware that I created the script above, and it is not officially endorsed or directly supported by Atlassian or our entire Support team. It is offered on a best-effort basis to assist you as much as possible. We advise having your development team thoroughly review and understand its objectives before implementation. If you wish to modify or enhance the script's functionality, you are welcome to do so.Kind regards,Alie
It looks like you're new here. Sign in or register to get started.