1
0
-1

Hello,


I'm trying to set labels in GitHub depending on Jira issue type and vice versa, but don't know where my mistake is.


Here is the code I tried it with:

GitHub Incomming
def typeMapping = ["Bug":"bug",
                   "Support":"question",
                   "Story":"enhancement"]

if(firstSync){
  def remoteTypeName = replica.issueType.name
  issue.labels == nodeHelper.getLabel(typeMapping[remoteTypeName])
}


I also tried like this:

GitHub Incomming
if(firstSync){
  
  if (replica.issueType.name == "Bug") { issue.labels += nodeHelper.getLabel("bug") }
  if (replica.issueType.name == "Story") { issue.labels += nodeHelper.getLabel("enhancement") }
  if (replica.issueType.name == "Support") { issue.labels += nodeHelper.getLabel("question") }
}


I tried the same thing in Jira, except that I want to set the issue type based on the label.

Jira Incomming
def typeMapping = ["bug":"Bug",
                   "question":"Support",
                   "enhancement":"Story"]
if(firstSync){
   def remoteTypeName = replica.labels
   issue.setTypeName(typeMapping[remoteTypeName])
}


Can someone tell me where my mistake is or how to solve it?

I would be very grateful for your answers and tips.


Best regards,

Thorsten

    CommentAdd your comment...

    3 answers

    1.  
      2
      1
      0

      Hi Thorsten,


      The issue is 'labels' is an array and not a string. Therefore, using the entire 'labels' array to map and set 'issue.typeName' won't work as expected


      Please try this in Jira incoming sync:

      if(firstSync){
         replica.labels.each {
            def mappedTypeName = typeMapping[it.label]
            if (mappedTypeName) {
               issue.typeName = mappedTypeName
               return
           }
         }
      }



      This will set the issue type in Jira base on the first matching label it finds in the GitHub issue.



      For the Github query, please try this in Github incoming:

      if(firstSync){
        def remoteTypeName = replica.issueType.name
        issue.labels += nodeHelper.getLabel(typeMapping[remoteTypeName])
      }

      This will add a label to the GitHub issue based on the issue type from Jira.



      Thank you.

      Kind regards,
      Mathieu Lepoutre

        CommentAdd your comment...
      1.  
        2
        1
        0

        Hi Mathieu Lepoutre ,


        I solved the issue with unlabeled Github tickets in the trigger, so that these tickets are not synchronized as long as they do not have a label. The only thing that remains is the point of changing the label/type of an existing issue, I don't have an idea here yet.


        Best regards,

        Thorsten

        1. Mathieu Lepoutre

          Do you want the issue to change types aswell when you change the github label? If yes, please put the above code outside of the firstSync codeblock.

        2. Thorsten

          Mathieu Lepoutre , I tried it outside the firstSync block but it's not working:

          Jira Incoming
          def typeMapping = ["bug":"Bug",
                             "question":"Support",
                             "enhancement":"Story",
                             "epic":"Epic"]
          
          if(firstSync){
             issue.projectKey   = "EGTS"
          }
          
          replica.labels.each {
                def mappedTypeName = typeMapping[it.label]
                if (mappedTypeName) {
                   issue.typeName = mappedTypeName
                   return
               }
             }
        3. Thorsten

          On GitHub side, the label is added on the GitHub page which is also not correct.

          GitHub Incoming
          def typeMapping = ["Bug":"bug",
                             "Support":"question",
                             "Story":"enhancement",
                             "Epic":"epic"]
          
          if(firstSync){
            issue.repository   = "exalate-test"
          }
          def remoteTypeName = replica.issueType.name
          issue.labels += nodeHelper.getLabel(typeMapping[remoteTypeName])

          When I change the operator the label doesn't change.

          issue.labels == nodeHelper.getLabel(typeMapping[remoteTypeName])
        4. Mathieu Lepoutre

          Hi Thorsten 


          To transform Jira issue types into labels for Github:

          I have added my entire incoming code.


          Please let me know how it goes;

          if(firstSync){
          issue.repository   = "mathieulepoutrebelgium/Exalate"
          }
          def labelsMap = [
              "Bug": ["bug"],
              "Support": ["question"],
              "Story": ["enhancement"],
              "Epic": ["epic"]
          ]
          def labelsToAdd = labelsMap[replica.issueType.name]
          if (labelsToAdd) {
              issue.labels = issue.labels.findAll { label -> labelsToAdd.contains(label) }
              issue.labels += labelsToAdd.collect { nodeHelper.getLabel(it) }
          }
          
          
          issue.summary      = replica.summary
          issue.description  = replica.description
          issue.comments     = commentHelper.mergeComments(issue, replica)
        5. Mathieu Lepoutre

          For Jira. It doesn't look like you can change issueTypes after creation, we can use jira-automation for it.

        6. Thorsten

          Mathieu Lepoutre ,

          thanks for the code. It works to swap the GitHub label when I change the issue type in Jira. (smile)


          I'm just wondering why it shouldn't work the other way around, because it also works without jira-automation in ADO <-> Jira, for example. The statuses are mapped directly here, but in principle this shouldn't make any difference.


          Regards,

          Thorsten

        7. Mathieu Lepoutre

          Hi Thorsten 


          With HttpClient we can change the issueTypes after creation, with Exalate.


          Check this code out; 

          // Gets issueTypes in project
          def list = httpClient.get("/rest/api/3/project/${issue.projectKey}")?.issueTypes
          def issueTypeIdMap = [:]
          
          for(int i = 0; i < list.size(); i++){
              // Create a Type map with right Id's dynamically from rest API, gets every issueType with id from the projectKey
             issueTypeIdMap[list?.name[i]] = list?.id[i]
          }
          
          def type = replica.customFields."Demo"?.value
          String domain = "/rest/api/3/issue/${issue.key}/"
          String data = "{\"fields\":{\"issuetype\":{\"id\":${issueTypeIdMap[type]}}}}"
          
          httpClient.put(domain, data)

          Now we just have to insert it after the previous code I shared, and then it will work to change issue Types after creation.


          Thanks Christophe De Beule 

        CommentAdd your comment...
      2.  
        1
        0
        -1

        Hi Mathieu Lepoutre ,


        I tested your suggestion and it works if the label/issue type is set during creation. But it doesn't work if I need to change the type/label after the creation. What I didn't consider on Github is that tickets can be created here without a label and that they will only be given the correct label later. Is there perhaps a way to do this?


        Best regards,

        Thorsten

          CommentAdd your comment...