go-jira / jira

simple jira command line client in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to parse strange value from customfield in template

SteveF4 opened this issue · comments

Hi all,

I'm building a custom view template, but seeing a strange value in a customfield from our JIRA instance. Does anyone know what kind of value it is, and how to parse it in a go template?

When I get an issue from JIRA, the value of .fields.customfield_10544 is:

[com.atlassian.greenhopper.service.sprint.Sprint@4c101cbb[id=2504,rapidViewId=1082,state=ACTIVE,name=S143 
- Apr 22 - May 12,startDate=2021-04-22T07:12:16.507-07:00,endDate=2021-05-12T22:55:00.000-07:00,
completeDate=<null>,sequence=2483,goal=<null>]]

I've now spent several hours reading through issues in the go-jira/jira project, the Netflix go-jira project, and reading up on go templates in general, but can't figure out what this structure is, or how to parse it for use in a go-jira template. I'm hoping to be able to extract the value of the 'name' field: S143 - Apr 22 - May 12 for use in a go-jira custom view template.

Thanks in advance for any pointers, advice or links to where I can solve this mystery for myself.

For completeness, in the issue returned from JIRA I see this:

{
    ...
    "fields": {
        ...
        "customfield_10544": [
      "com.atlassian.greenhopper.service.sprint.Sprint@4c101cbb[id=2504,rapidViewId=1082,state=ACTIVE,name=S143 - Apr 22 - May 12,startDate=2021-04-22T07:12:16.507-07:00,endDate=2021-05-12T22:55:00.000-07:00,completeDate=<null>,sequence=2483,goal=<null>]"
        ],
        ...
    }
    ...
}

In my go-jira view template, I have this line:

{{if .fields.customfield_10544 -}}Sprint:         {{ .fields.customfield_10544 }}{{end}}

When I use the template, it prints this:

Sprint:         [com.atlassian.greenhopper.service.sprint.Sprint@4c101cbb[id=2504,rapidViewId=1082,state=ACTIVE,name=S143 - Apr 22 - May 12,startDate=2021-04-22T07:12:16.507-07:00,endDate=2021-05-12T22:55:00.000-07:00,completeDate=<null>,sequence=2483,goal=<null>]]

I'd really like it to print this instead:

Sprint:         S143 - Apr 22 - May

It looks like I'm not the only one with questions about this, so I'll be sure to document exactly how I get around this issue. Thanks!

What you need is a custom command that calls the Jira Agile API, which manages boards, sprints, etc.

Based on the custom sprint-add command that @coryb supplied in #230 I created a similar command to show the active or next sprint:

custom_commands:
  - name: sprint-show
    aliases:
      - sprint-view
    help: Shows the active or next sprint for project
    options:
      - name: next
        short: 'n'
        type: bool
        help: add issue to next sprint, rather than the active one
      - name: type
        short: 't'
        type: string
        help: board type, default is simple. Can also be scrum or kanban.
        default: simple
    script: |
      state={{if options.next}}future{{else}}active{{end}}
      board_id=$({{jira}} req "/rest/agile/1.0/board?projectKeyOrId=$JIRA_PROJECT&type={{options.type}}" --gjq values.0.id)
      sprint_response=$({{jira}} req "/rest/agile/1.0/board/$board_id/sprint?state=$state")
      sprint_id=$(jq -r '.values[0].id' <<< ${sprint_response})
      if [ "$sprint_id" = "" ]; then
        echo "ERROR: No $state sprint" >&2
        exit 1
      fi
      sprint_name=$(jq -r '.values[0].name' <<< ${sprint_response})
      echo "Sprint #${sprint_id}: ${sprint_name}"

I defaulted the board type to simple because that's what most of my projects use.