ndaversa / hubot-jira-bot

Lets you search for JIRA tickets, open them, transition them thru different states, comment on them, rank them up or down, start or stop watching them or change who is assigned to a ticket. Also, notifications for assignments, mentions and watched tickets.
https://www.npmjs.com/package/hubot-jira-bot
MIT License
61 stars 43 forks source link

Utils.JiraBot.adapter.getUsers() not getting correct information #34

Closed Office-Manager closed 7 years ago

Office-Manager commented 7 years ago

Hi ,

Noticing an issue when trying out the hubot with our JIra instance ( self hosted if that makes a difference)

alfred> alfred jira search test
alfred> Unable to search for `test` :sadpanda:
[Wed May 10 2017 14:29:41 GMT+0000 (UTC)] ERROR TypeError: Cannot read property 'email' of undefined
  at /home/ansible/workspace/alfred-hubot/alfred/node_modules/hubot-jira-bot/src/utils.coffee:61:69, <js>:115:36
  at Function.Utils.lookupUserWithJira (/home/ansible/workspace/alfred-hubot/alfred/node_modules/hubot-jira-bot/src/utils.coffee:61:5, <js>:120:11)
  at Ticket.toAttachment (/home/ansible/workspace/alfred-hubot/alfred/node_modules/hubot-jira-bot/src/jira/ticket.coffee:41:16, <js>:64:26)
  at /home/ansible/workspace/alfred-hubot/alfred/node_modules/hubot-jira-bot/src/index.coffee:392:24, <js>:440:36
  at /home/ansible/workspace/alfred-hubot/alfred/node_modules/hubot-jira-bot/src/index.coffee:392:9, <js>:443:13
  at process._tickCallback (internal/process/next_tick.js:109:7)

Added some debugging to see what it's seeing and what's being sent.

@lookupUserWithJira: (jira, fallback=no) ->
    users = Utils.JiraBot.adapter.getUsers()
    console.log(users)
    console.log(Config.jira.username)
    console.log(Config.jira.password)
    result = (users[user] for user of users when users[user].profile.email is jira.emailAddress) if jira
    if result?.length is 1
      return if fallback then result[0].name else "<@#{result[0].id}>"
    else if jira
      return jira.displayName
    else
      return "Unassigned"

which outputs

08:51 $ ./bin/hubot -a shell
alfred> [Thu May 11 2017 08:51:53 GMT+0000 (UTC)] INFO /home/ansible/workspace/alfred-hubot/alfred/scripts/sorry.coffee is using deprecated documentation syntax
[Thu May 11 2017 08:51:53 GMT+0000 (UTC)] INFO hubot-redis-brain: Using default redis on localhost:6379

alfred> alfred jira search test
alfred> { '1': User { id: '1', name: 'Shell', room: 'Shell' } }
SVC-JIRA
password1
{ '1': User { id: '1', name: 'Shell', room: 'Shell' } }
SVC-JIRA
password1
Unable to search for `test` :sadpanda:
[Thu May 11 2017 08:52:32 GMT+0000 (UTC)] ERROR TypeError: Cannot read property 'email' of undefined
  at /home/ansible/workspace/alfred-hubot/alfred/node_modules/hubot-jira-bot/src/utils.coffee:64:69, <js>:118:36
  at Function.Utils.lookupUserWithJira (/home/ansible/workspace/alfred-hubot/alfred/node_modules/hubot-jira-bot/src/utils.coffee:64:5, <js>:123:11)
  at Ticket.toAttachment (/home/ansible/workspace/alfred-hubot/alfred/node_modules/hubot-jira-bot/src/jira/ticket.coffee:41:16, <js>:64:26)
  at /home/ansible/workspace/alfred-hubot/alfred/node_modules/hubot-jira-bot/src/index.coffee:392:24, <js>:440:36
  at /home/ansible/workspace/alfred-hubot/alfred/node_modules/hubot-jira-bot/src/index.coffee:392:9, <js>:443:13
  at process._tickCallback (internal/process/next_tick.js:109:7)

Any ideas on what could be the issue? I can confirm the username and password are working on the instance

mchennamsetty commented 7 years ago

Note that HUBOT_JIRA_USERNAME should be the JIRA username, this is not necessarily the username used if you log in via the web. To determine a user's username, log in as that user via the web, and check the user profile. Frequently, users may log in using an email address such as 'bob@somewhere.com' or a stem, such as 'bob'; these may or may not match the username in JIRA.

Office-Manager commented 7 years ago

Thanks @mchennamsetty , I've tried it making sure the user is the one which logs in and appears in the profile username but still to no success :/

ndaversa commented 7 years ago

Ah I see the problem here.... So behind the scenes as its trying to format the output its trying to resolve the jira user whom reported the issue into the equivalent chat user. This integration is used heavily with hubot-slack where there is a email address field that is expected to be the same between the chat and jira users. However since you are using a Shell instead of hubot-slack adapter the field isn't there and there isn't a guard to protect against it.

Happy to accept pull requests to fix the issue, I suspect some ? existence checks here: https://github.com/ndaversa/hubot-jira-bot/blob/master/src/utils.coffee#L62

Would resolve the issue

Let me know if that works for you locally and maybe, if you are so inclined, you can get a PR together?

iris3th commented 7 years ago

@ndaversa - I've found out the issue in the code too, just have no ideas for fixing it. Can we do a check other than looking for the email address? The point is, that the email address is defined in Jira for each user so it should work.

iris3th commented 7 years ago

@ndaversa: seems I made it working changing the code as per the following:

@lookupUserWithJira: (jira, fallback=no) -> users = Utils.JiraBot.adapter.getUsers() result = (users[user] for user of users when users[user].name is jira.name) if jira if result?.length is 1 return if fallback then result[0].name else "<@#{result[0].id}>" else if jira return jira.displayName else return "Unassigned"

It now gets the correct value, although I think you need something else here. Can you please check and maybe update your code?