floralvikings / jira-connector

NodeJS Wrapper for the Jira REST API
http://floralvikings.github.io/jira-connector/
MIT License
373 stars 180 forks source link

editIssue transition troubles #237

Closed deadlysyn closed 4 years ago

deadlysyn commented 4 years ago

hi folks,

first, thanks for the hard work on jira-connector... very useful for my current project!

i am most likely missing something (just started poking at jira rest api), but wanted to toss this out and see if it's a gap, known issue, or pebkac.

i have a function which attempts to post a comment and do a status transition. i can do this with curl and the following json:

{
    "update": {
        "comment": [
            {
                "add": {
                    "body": "Test comment and status change"
                }
            }
        ]
    },
    "transition": {
        "id": "21"
    }
}

however, to get that working with curl i have to POST to the transitions endpoint. something like:

curl -u $JIRA_USERNAME:$JIRA_PASSWORD -X POST --data @test/test-comment-status.json -H "Content-Type: application/json" https://$JIRA_HOST/rest/api/2/issue/$ISSUE/transitions

using jira-connector, i first wanted to do it in two steps:

however, transitionIssue is mentioned in README but i could not find the implementation in the repo...i think i am missing something (or maybe this is a chance for a PR, or good reason it is not there?). 🤔

that's ok though, because from editIssue signature it looks like i can pass in opts.transition. i am just having problem getting the right format. i am hoping this is something silly i have missed, but wanted to toss this out to clarify usage. here's my code:

const startOnboardingIssue = async issueKey => {
  const opts = {
    issueKey,
    issue: {
      update: {
        comment: [
          {
            add: {
              body: 'Test automated comment',
            },
          },
        ],
      },
    },
    transition: {
      id: '21',
    },
  }
  await jira.issue.editIssue(opts)
}

this is probably naive since it just mirrors the json content. i've tried a few variations on opts.transition, but haven't had luck. however, editIssue uses PUT to the issue vs POST to transitions, and even with curl i can never seem to get transitions to work unless i use the POST variant. not sure if this is the root cause, or i've simply got opts wrong. using this reference:

https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-rest-api-3-issue-issueIdOrKey-transitions-post

when i run the above function, the comment appears and i get 200 OK but the issue does not transition (same behavior with curl and PUT vs POST).

thanks for any insight!

deadlysyn commented 4 years ago

i must have stared at this too long, and got the opts format wrong above (i think)...i had opts.transition vs opts.issue.transition per function signature/docs. however, this still didn't do it (get 200 OK w/o transition, only comment appears):

  const opts = {
    issueKey,
    issue: {
      update: {
        comment: [
          {
            add: {
              body: 'Test automated comment',
            },
          },
        ],
      },
      transition: {
        id: '21',
      },
    },
  }

i've verified the transitionIds via https://${jiraHost}/rest/api/2/issue/${issueKey}/transitions?expand=transitions.fields. it seems others have problems with transitions using PUT vs POST as well:

for now, i've created a helper function that just uses request to handle transitions (should something like this be added as transitionIssue?):

const transitionIssue = async ({ ...args }) => {
  const { transitionId, issueKey } = args

  const jsonData = `{
    "transition": {
      "id": "${transitionId}"
    }
  }`

  const opts = {
    method: 'POST',
    url: `https://${jiraHost}/rest/api/2/issue/${issueKey}/transitions`,
    auth: {
      username: process.env.JIRA_USERNAME,
      password: process.env.JIRA_PASSWORD,
    },
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
    },
    body: jsonData,
  }

  try {
    await request(opts)
  } catch (e) {
    throw new Error(e.message)
  }
}
deadlysyn commented 4 years ago

while refactoring some related code, i had a chance to circle back and give this another try. despite my opts.issue.transition looking ok per jira rest docs, it never seems to work with editIssue. i can curl just fine, but need to POST to transitions endpoint vs editIssue's PUT. do you have a good example of how to make this work with editIssue? it would be awesome to be able to remove my one-off request-based function just for transitioning. TIA!

deadlysyn commented 4 years ago

not sure how i missed transitionIssue when i first searched the repo (saw it in docs but not issue.js :-( ) -- sorry for the noise. i never could get a transition to work with editIssue, but just using addComment + transitionIssue now which is A-O-K.