/Redmine/plugin.py

https://github.com/gholms/supybot-plugins · Python · 96 lines · 56 code · 10 blank · 30 comment · 9 complexity · 84d34dbdc0fa46f20766834ba32ae5f1 MD5 · raw file

  1. ###
  2. # Redistribution and use in source and binary forms, with or without
  3. # modification, are permitted provided that the following conditions are met:
  4. #
  5. # * Redistributions of source code must retain the above copyright notice,
  6. # this list of conditions, and the following disclaimer.
  7. # * Redistributions in binary form must reproduce the above copyright notice,
  8. # this list of conditions, and the following disclaimer in the
  9. # documentation and/or other materials provided with the distribution.
  10. # * Neither the name of the author of this software nor the name of
  11. # contributors to this software may be used to endorse or promote products
  12. # derived from this software without specific prior written consent.
  13. #
  14. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. # POSSIBILITY OF SUCH DAMAGE.
  25. ###
  26. import supybot.utils as utils
  27. from supybot.commands import *
  28. import supybot.plugins as plugins
  29. import supybot.ircutils as ircutils
  30. import supybot.callbacks as callbacks
  31. from supybot.i18n import PluginInternationalization, internationalizeDocstring
  32. import json
  33. import urllib2
  34. from urlparse import urljoin
  35. _ = PluginInternationalization('Redmine')
  36. @internationalizeDocstring
  37. class Redmine(callbacks.Plugin):
  38. """Add the help for "@plugin help Redmine" here
  39. This should describe *how* to use this plugin."""
  40. threaded = True
  41. def __init__(self, irc):
  42. super(self.__class__, self).__init__(irc)
  43. def getissue(self, irc, msg, args, issueno):
  44. """<id>
  45. Display information about an issue in Redmind along with a link to
  46. it on the web.
  47. """
  48. base_uri = self.registryValue('uri')
  49. rest_uri = urljoin(base_uri, 'issues/{0}.json'.format(issueno))
  50. try:
  51. response = urllib2.urlopen(rest_uri)
  52. response_content = response.read()
  53. response_json = json.loads(response_content)
  54. except urllib2.HTTPError as e:
  55. if str(e.code).startswith('4'):
  56. irc.error('issue {0} does not exist.'.format(issueno))
  57. else:
  58. self.log.error('GET on URI {uri} yielded HTTP {code} {msg}'
  59. .format(uri=rest_uri, code=e.code, msg=e.msg))
  60. irc.error('failed to retrieve issue data')
  61. return
  62. except ValueError:
  63. self.log.error('Response from server is not JSON: ' + response_content)
  64. irc.error('failed to retrieve issue data')
  65. return
  66. if 'issue' in response_json:
  67. issue = response_json['issue']
  68. else:
  69. self.log.error("Response lacks an 'issue' key: " + response_content)
  70. irc.error('failed to retrieve issue data')
  71. return
  72. msg_bits = ['Issue']
  73. issue_flags = []
  74. msg_bits.append(str(issueno))
  75. if issue.get('status'):
  76. issue_flags.append(issue['status']['name'])
  77. if issue_flags:
  78. msg_bits.append('(' + ', '.join(issue_flags) + ')')
  79. msg_bits[-1] += ':'
  80. msg_bits.append(issue.get('subject', '(no subject)'))
  81. msg_bits.append('-')
  82. msg_bits.append(urljoin(base_uri, 'issues/{0}'.format(issueno)))
  83. irc.reply(' '.join(msg_bits))
  84. getissue = wrap(getissue, ['positiveInt'])
  85. Class = Redmine