-
Notifications
You must be signed in to change notification settings - Fork 9
/
pivotal_tracker.py
125 lines (108 loc) · 5.71 KB
/
pivotal_tracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#
# Copyright (c) 2011 Ben Belchak <ben@belchak.com>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
import re
from campy import settings
from plugins import CampyPlugin
try:
from pytracker import Tracker
from pytracker import Story
from pytracker import HostedTrackerAuth
except ImportError:
raise ImportError("Requries pytracker module. http://code.google.com/p/pytracker/")
class PivotalTracker(CampyPlugin):
def __init__(self):
self.auth = HostedTrackerAuth(settings.PT_USERNAME, settings.PT_PASSWORD)
def send_help(self, campfire, room, message, speaker):
help_text = """%s: Here is your help for the PivotalTracker plugin:
pt story create "Title" "Description" bug|feature|chore|release -- Create a story
pt getmine started|finished|delivered|accepted|rejected|unstarted|unscheduled -- Get a list of all stories that belong to you
pt start #story_id -- Start a particular story.
pt start|tell next bug|feature|chore [mine] -- Start or tell the next story of a given type. Optionally supply 'mine' at the end of the message to only work on your own stories.
""" % speaker['user']['name']
room.paste(help_text)
def handle_message(self, campfire, room, message, speaker):
project_name = room.data['name']
tracker = Tracker(settings.PT_ROOM_TO_PROJECT_MAP[project_name], self.auth)
body = message['body']
if not body:
return
if not body.startswith(settings.CAMPFIRE_BOT_NAME):
return
m = re.match('%s: pt story create "(?P<title>.*)" "(?P<description>.*)" (?P<type>.*)$' %
settings.CAMPFIRE_BOT_NAME, body)
if m:
story = Story()
story.SetRequestedBy(speaker['user']['name'])
story.SetName(m.group('title'))
story.SetDescription(m.group('description'))
story.SetStoryType(m.group('type'))
new_story = tracker.AddNewStory(story)
self.speak_new_story(room, new_story, speaker)
return
m = re.match("%s: pt getmine (?P<state>started|finished|delivered|accepted|rejected|unstarted|unscheduled)$" %
settings.CAMPFIRE_BOT_NAME, body)
if m:
stories = tracker.GetStories('owner:"%s" state:%s' % (speaker['user']['name'], m.group('state')))
if len(stories) > 0:
speak_text = "%s: Here are your stories:\r\n" % speaker['user']['name']
room.speak(speak_text)
speak_text = ''
for story in stories:
speak_text += '[#%i] "%s" (%s)\r\n\r\n' % (story.GetStoryId(),
story.GetName(), story.GetUrl())
room.paste(speak_text)
else:
speak_text = "%s: You have no stories matching that query." % speaker['user']['name']
room.speak(speak_text)
return
m = re.match("%s: pt start #?(?P<id>\d+)$" % settings.CAMPFIRE_BOT_NAME, body)
if m:
story = tracker.GetStory(int(m.group('id')))
story.SetCurrentState('started')
story.SetOwnedBy(speaker['user']['name'])
tracker.UpdateStory(story)
room.speak("%s: [#%i] started by you! Get to work!" % (speaker['user']['name'],
story.GetStoryId()))
return
m = re.match("%s: pt (?P<command>start|tell) next (?P<story_type>bug|feature|chore)(\s+)?(?P<mine>mine)?" %
settings.CAMPFIRE_BOT_NAME, body)
if m:
filter = "type:%s state:unstarted" % m.group('story_type')
if m.group('mine') == 'mine':
filter += ' owner:"%s"' % speaker['user']['name']
try:
next = tracker.GetStories(filter)[0]
except IndexError:
room.speak("%s: There were no stories matching your query." % speaker['user']['name'])
if m.group('command') == 'start':
next.SetCurrentState('started')
next.SetOwnedBy(speaker['user']['name'])
tracker.UpdateStory(next)
room.speak("%s: [#%i] started by you! Get to work!" % (speaker['user']['name'],
next.GetStoryId()))
elif m.group('command') == 'tell':
room.speak("%s: [#%i] %s (%s)" % (speaker['user']['name'], next.GetStoryId(),
next.GetName(), next.GetUrl()))
return
def reply(self, room, new_story, speaker):
room.speak(unicode("%s: Your story has been created successfully." % speaker['user']['name']))