Re-structure env-variables to correct format for JSON

Hi all, I’m fairly new in coding. Anyway, I am trying to compare two branches and get the JIRA Tickets or JIRA Keys between those two branches whenever a pull request is made and I have already set as env-variable. However, whenever I pass it’s value to JSON, it isn’t correctly formatted. Is this possible? Please see the sample below.

#Git Command to query the Jira tickets.
 - name: Tickets
      run: echo ::set-env name=Tickets::$(git log origin/BranchA..origin/BranchB --pretty=oneline | perl -ne '{ /(\w+)-(\d+)/ && print "$1-$2\n" }' | sort | uniq)

#Webhook to JIRA
 - name: Update tickets
      uses: sendwebhook
      with:
        url: ""
        json: '{"issues": "${{ env.Tickets }}", "and some field to update"}'

The value that was retrieved is like this:

json: {"issues": "JIRAKey-1 JIRAKey-2 JIRAKey-3" }

instead of like this:

json: {"issues": "JIRAKey-1", "JIRAKey-2", "JIRAKey-3" }

When you use ${{ env.Tickets }} what happens is a simple string substitution, the literal value of the Tickets environment variable is placed at the location. If you want it contain a JSON structure you’ll have to create that yourself. Note that to get a valid JSON array of issues your json field should look like this:

{"issues": ["JIRAKey-1", "JIRAKey-2", "JIRAKey-3"]}

I like Python, so I’d use something like this:

- name: Create ticket list
  shell: python3 {0}
  run: |
    import json
    import os
    print(f'::set-env name=TICKET_LIST::{json.dumps(os.environ["Tickets"].split())}')

Well, I’d probably integrate that with parsing the git log output, but I think the idea is visible.

Thanks! This works very well. I’m thinking of investing time on Python more as well. :slight_smile: Have a great day!

1 Like