Return to Hacking - Copy Rules from one Project to another

giphy

So I wrote Hacking/Parsing Automation Rules with JSON for Fun and Profit a few years ago, but haven't actually done much hacking of rules lately.

But just today I had an interesting challenge:

  • A Project Admin has ~60 rules that she'd like copied from project X to project Y, but not enabled.
  • She does not want us to simply expand the scope of the rules, because the rules (ex: conditions) will be slightly different in the new projects.
  • All of the rule names start with the term "CloneRoute" 

Ah, this sounds like a job for my favorite tool, jq!

So here's what I did:

  • Export Rules. (Only Jira Admins can do this.)
  • Poke around in the documentation for jq, and also my old article, until I figured this out:

jq '[.rules[] | select(.name | contains("CloneRoute"))]' automation-rules-202412121711.json > cloneroutes.json

  • Look at the exported file, and realize it needs a bit of surrounding business, so update my command to add that bit:

jq '. | { "cloud":true, "rules": [.rules[] | select(.name | contains("CloneRoute"))] }' automation-rules-202412121711.json > cloneroutes.json

And from there, I opened in the cloneroutes.json file in a text editor and bulk-replaced the Project Id for project X (which was 10618) the one for project Y (10183) in the Scope section for each rule:

"ruleScope": {
     "resources": [ "ari:cloud:jira:MYCLOUDID:project/10183" ]
},

I also did a bulk replace of "CloneRoute" with the Project Y key, so that we don't have run into duplicate named rules:

"name": "Y CloneRoute i18n Bugs: Leave in Loc",

Then I did an Import Rules, confirmed that the new Names and Project was correct, selected All, checked the box to "Import rule owners", and boom, click Import.

What does this mean?

giphy

I guess my point here is I wanted to add another practical use of "hacking" your automation rules with jq.

Yes, it can be a little intimidating, but there is truly a lot of power in the tool.

ALSO! If you didn't want to fully "hack" JSON, you could also use jq to simply reformat rules into something that you could edit by hand.

This:

jq . automation-rules-20241213.json > formattedrules.json

will give you a formattedrules.json that is a lot easier to read. You could even replicate what my longer jq command did by manually looking for rules that start with "CloneRoute" and copy-paste them into a new file, call it: rulestoimport.json

Each rule would start with something like this:

    {
      "id": 19580617,
      "clientKey": "xxxxx-xxxx-xxx-xx-xx",
      "name": "CloneRoute Foo Bugs: Leave in Bar",
      "state": "ENABLED",

And end with this:

      "writeAccessType": "UNRESTRICTED",
      "collaborators": [],
      "billingType": "NORMAL",
      "idUuid": "xyioasdf-sda-asd-sad-asdasdf"
    }

So again, you just need to KEEP everything for each rule, and then insert them into this structure:

{ "cloud": true,
"rules": [

HERE IS WHERE ALL THE RULES GO

  ]
}

Oof. That is a LOT. Sorry. Hope it helps somebody.

0 comments

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events