Conditional statement with Jira custom field <> mapping to Github labels

Hello,

We track severity in github using labels. We have four options:
Sev0-urgent, Sev1-high, Sev2-normal, Sev3-low.

Tracking Severity in Jira using labels did not work for us, so we created a custom field named “Severity”. Options are: Sev0, Sev1, Sev2, Sev3. (I could not map the values one to one as those were already used by other teams in the org).

In my scripts, i want to map this to be directional with conditional statements. When i try the following for example, nothing gets updated on the Jira side. See example taken from my script below:
Note: i also tried replacing issue.customFields.“customfield_1234”.value = [value: “Sev0”] by issue.customFields.“Severity”.value = [value: “Sev0”]. That did not work either.
Thank you for your help!

// Assign the Severity field based on labels

    if (labelList.contains("Sev0-urgent")) {

issue.customFields."customfield_1234".value = [value: "Sev0"]

    } else if (labelList.contains("Sev1-high")) {

issue.customFields."customfield_1234".value = [value: "Sev1"]

    } else if (labelList.contains("Sev2-normal")) {

issue.customFields."customfield_1234".value = [value: "Sev2"]

    } else if (labelList.contains("Sev3-low")) {

issue.customFields."customfield_1234".value = [value: "Sev3"]

    }

}

SearchDocumentationTool(query=“map Jira custom field to GitHub label using script conditional”)
Based on the documentation, mapping a Jira custom field to GitHub labels using conditional statements in your sync script is possible, but there are a few important details to ensure it works correctly:

  • The correct way to reference a custom field in Jira is usually by its custom field ID (e.g., customfield_10340), not by its name.
  • When setting the value for a single-select custom field, you should assign the value directly, not as a map (i.e., use a string, not [value: “Sev0”]).
  • The script should be placed in the appropriate sync rule (e.g., the outgoing sync on GitHub or the incoming sync on Jira, depending on the direction you want).

Here’s how you can structure your script for the incoming sync on the Jira side:

// Map GitHub labels to Jira Severity custom field
def labelList = replica.labels ?: []

if (labelList.contains("Sev0-urgent")) {
    issue.customFields."customfield_10340".value = "Sev0"
} else if (labelList.contains("Sev1-high")) {
    issue.customFields."customfield_10340".value = "Sev1"
} else if (labelList.contains("Sev2-medium")) {
    issue.customFields."customfield_10340".value = "Sev2"
} else if (labelList.contains("Sev3-low")) {
    issue.customFields."customfield_10340".value = "Sev3"
}

A few things to check if it’s not working:

  1. Make sure you’re using the correct custom field ID for your “Severity” field. You can find this in Jira by inspecting the custom field configuration.
  2. Ensure the script is in the correct sync rule (usually the incoming sync on Jira).
  3. The value assigned should match exactly one of the options configured for the custom field in Jira.
  4. The label names in your script must match exactly what’s used in GitHub.

For more details, see the documentation on mapping custom fields in Jira and using conditional logic in sync scripts.

If you still encounter issues, double-check the field ID and the value format, as these are the most common sources of problems.

I tried implementing this, and it did not work. I confirmed this is indeed in my Jira incoming script, i confirmed the values in the script match the field configurations and the field values in GitHub.

Hi @adrigug

My name is Kevin and I will assist you with you query.

I reproduce you scenario and I came up with the following script that is working well for me I tried to make as fail proof as possible, so there are some parts that you might want to remove.

GitHub Incoming Script

//============== Git Label to Severity CF ====================//
def severityMapping = [
   "Sev0-urgent": "Sev0", 
   "Sev1-high": "Sev1",
   "Sev2-medium": "Sev2",
   "Sev3-low": "Sev3"
]

def severityLabelsFound = [] 
def otherLabels = [] 

if (replica.labels) {
    replica.labels.each { labelObject ->
        def currentLabel = labelObject.label.toString().trim()

        if (severityMapping.containsKey(currentLabel)) {
            severityLabelsFound.add(currentLabel)
        } else {
            otherLabels.add(nodeHelper.getLabel(currentLabel.replaceAll(" ", "-"))) // Collect all other labels to sync to the standard Jira Labels field
        }
    }
}

if (severityLabelsFound.size() > 1)
 {
    debug.error("Conflict detected: The source item has multiple severity labels (${severityLabelsFound.join(', ')}). Only one is allowed.")
} 
else {
    issue.labels = otherLabels
        if (severityLabelsFound.size() == 1) {
        def githubLabelKey = severityLabelsFound[0]
        def mappedSeverity = severityMapping[githubLabelKey]
        issue.customFields."TS Ticket"?.value = mappedSeverity
    }
}

For Example I added a handle for labels that are not part of the mapping so Exalate can still sync other labels that are not for the severity and also added a debug.error in case more of one severity label is added, which is also optional, it will depend on the type of field that you are using on Jira.

For the Jira to GitHub the code is a little bit more straightforward

if (replica.sevField){
  def sevMapping = [
   "Sev0" : "Sev0-urgent", 
   "Sev1" : "Sev1-high",
   "Sev2" : "Sev2-medium",
   "Sev3" : "Sev3-low"
  ]
  def targetSev = sevMapping[replica.sevField.value]
  issue.labels += nodeHelper.getLabel(targetSev)
}

Let me know if this works for you.

Best,
Kevin