Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Remove duplicative custom fields

Karim July 25, 2024

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.

1 answer

0 votes
Alie Sesay
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 13, 2024

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 queries
Then, 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 requests
import base64

# Define your variables here
cloudId = "Your-Cloud-ID"
api_token = "API-TOKEN"
email = "Your-email"
domain = "Your-site.atlassian.net"

# Base64 encode the email and token for Basic Authentication
auth_string = f"{email}:{api_token}"
auth_bytes = auth_string.encode('utf-8')
auth_base64 = base64.b64encode(auth_bytes).decode('utf-8')

# GraphQL endpoint
graphql_url = f'https://{domain}/gateway/api/graphql'

# Define the GraphQL query to fetch components and custom fields
graphql_query = '''
query searchCompassComponents($cloudId: String!, $query: CompassSearchComponentQuery) {
compass {
searchComponents(cloudId: $cloudId, query: $query) {
... on CompassSearchComponentConnection {
nodes {
link
component {
name
description
typeId
ownerId
links {
id
type
name
url
}
labels {
name
}
customFields {
definition {
id
name
}
...on CompassCustomBooleanField {
booleanValue
}
...on CompassCustomTextField {
textValue
}
...on CompassCustomNumberField {
numberValue
}
...on CompassCustomUserField {
userValue {
id
name
picture
accountId
canonicalAccountId
accountStatus
}
}
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
... on QueryError {
message
extensions {
statusCode
errorType
}
}
}
}
}
'''
# Define the variables for the GraphQL query
variables = {
"cloudId": cloudId,
"query": {
"fieldFilters": [
{
"name": "type",
"filter": {
"eq": "SERVICE"
}
}
]
}
}

# Set up the headers with Basic Authentication and additional headers
headers = {
'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 API
response = requests.post(graphql_url, json={'query': graphql_query, 'variables': variables}, headers=headers)

# Check for request success
if response.status_code == 200:
data = response.json()

# Extract custom field IDs from the response
custom_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 IDs
unique_custom_field_ids = list(set(custom_field_ids))

# Output the unique custom field IDs
print("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-tokens
email = "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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events