How to remove leading white space from ADO labels array

Originally asked by Sebastian Mennen on 20 June 2023 (original question)


We are experiencing an issue with our outgoing sync from ADO to Jira Cloud. When the workItem.labels collection has more than one label in it all labels except the first one gets a leading space. I’ve tried this:

replica.labels = workItem.labels.collect { label ->
def ml = label.value.trim()
def nLabel = new com.exalate.basic.domain.hubobject.v1.BasicHubLabel().setLabel(ml)
label = nLabel
}

this causes no error but the output is still this:

"labels": [
      {
        "label": "Label1"
      },
      {
        "label": " Label2"
      }
    ],

Answer by Javier Pozuelo on 14 September 2023

Hello Sebastian,

You can place the following code in the Outgoing Sync on Azure DevOps. It will remove (trim) the whitespaces at the beginning and end of the string, and it will replace the spaces between words for an underscore.

Example:

ADO: " Label Test" →. Jira: “Label_Test”

replica.labels = workItem.labels.collect { it.label = it.label.trim().replace(" ", "_"); it }

Kind regards,

Javier