Commits

Takumi IINO committed c4fe7fe

working, but buggy

  • Participants
  • Parent commits 4a1a3e7

Comments (0)

Files changed (2)

     
     [backloog]
     # REQUIRED ITEMS:
-    server   = https://demo.backlog.jp/
-    user     = ... # username for login
-    password = ...
+    spacename = demo # backlog space name
+    user      = ...  # username for login
+    password  = ...
+    key       = ...  # issue key
     
-    template = \n>details:   {baseurl}{webroot}/rev/{node|short}\n>branches:  {branches}\n>changeset: {rev}:{node|short}\n>user:      {author}\n>date:      {date|date}\n>description:\n{desc|firstline}\n
+    template = \ndetails: {baseurl}/rev/{node|short}\nbranches: {branches}\nchangeset: {rev}:{node|short}\nuser: {author}\ndate: {date|date}\ndescription:\n{desc}\n
 
 usage
 =====
 
 コミットコメントに課題番号を追記し、変更を中央リポジトリに反映します。
-反映後ポストコミットフックが起動しBacklogの該当の課題コメントが追記されます。
+反映後ポストコミットフックが起動しBacklogの該当の課題コメントが追記されます。
 
 コミットフックを利用する以外に`hg backlog` サブコマンドでリビジョン単位で
-実行できます。
+実行できます。
 
 コミットコメントに特別なキーワードを含めると、該当する課題の状態を更新します。
 

File backlog/__init__.py

 
 unbundle()
 
+import re
 import backloglib
 from mercurial.i18n import _
-from mercurial import patch, cmdutil, templater, util, mail
+from mercurial import patch, cmdutil, templater, util, scmutil
 
 cmdtable = {}
 command = cmdutil.command(cmdtable)
 
+default_template = '''
+details: {baseurl}/rev/{node|short}
+branches: {branches}
+changeset: {rev}:{node|short}
+user: {author}
+date: {date|date}
+description:
+{desc}
+'''
+
+class BacklogComment(object):
+
+    def __init__(self, ui, issue, status, content):
+        self.ui = ui
+        self.issue = issue
+        self.status = status
+        self.content = content
+
+    def key(self):
+        return self.issue
+
+    def need_switch_status(self):
+        return not self.status is None
+
 class BacklogNotifier(object):
+    commands = {
+        'fix':    backloglib.Status.COMPLETED,
+        'fixes':  backloglib.Status.COMPLETED,
+        'fixed':  backloglib.Status.COMPLETED,
+        'close':  backloglib.Status.DONE,
+        'closes': backloglib.Status.DONE,
+        'closed': backloglib.Status.DONE,
+    }
+
+    comments = []
 
     def __init__(self, ui, repo, hooktype):
         self.ui = ui
-        cfg = self.ui.config('backlog', 'config')
         self.repo = repo
         self.hooktype = hooktype
+        self.spacename = self.ui.config('backlog', 'spacename')
+        self.username = self.ui.config('backlog', 'username')
+        self.password = self.ui.config('backlog', 'password')
+        self.key = self.ui.config('backlog', 'key');
+        issue_prefix = '(?:\[{0,2})'
+        issue_suffix = '(?:\]{0,2})'
+        self.issue_re = re.compile(issue_prefix + '(%s-\d+)' % self.key + issue_suffix)
+        self.commands_re = re.compile('\#(' + '|'.join(self.commands.keys()) + ')')
 
-    def node(self, ctx):
-        return True
+        mapfile = self.ui.config('backlog', 'style')
+        template = (self.ui.config('backlog', hooktype) or
+                    self.ui.config('backlog', 'template'))
+        self.t = cmdutil.changeset_templater(self.ui, self.repo,
+                                             False, None, mapfile, False)
+        if not mapfile and not template:
+           template = default_template
+        if template:
+           template = templater.parsestring(template, quoted=False)
+           self.t.use_template(template)
 
-    def notify(self, ctx, count):
-        return True
+    def node(self, ctx, **props):
+        issues = {}
+        for line in ctx.description().split('\n'):
+            for issue in self.issue_re.findall(line):
+                issues[issue] = None
+                for command in self.commands_re.findall(line):
+                    issues[issue] = self.commands[command]
+        for issue, status in issues.iteritems():
+            self.ui.pushbuffer()
+            self.t.show(ctx, changes=ctx.changeset(),
+                        baseurl=self.ui.config('web', 'baseurl'),
+                        **props)
+            content = self.ui.popbuffer()
+            self.comments.append(BacklogComment(self.ui, issue, status, content))
+
+    def notify(self):
+        backlog = backloglib.Backlog(self.spacename, self.username, self.password)
+        for comment in self.comments:
+            if (comment.need_switch_status()):
+                backlog.switch_status({
+                    "key": comment.key(),
+                    "statusId": comment.status,
+                    "assignerId": None,
+                    "resolutionId": None,
+                    "comment": comment.content,
+                })
+            else:
+                backlog.add_comment({
+                    "key": comment.key(),
+                    "content": comment.content,
+                })
 
 @command('backlog',
     [],
     """notify changesets to backlog
 
     """
-    ui.write('run hg backlog command\n')
+    bn = BacklogNotifier(ui, repo, None)
+    for rev in scmutil.revrange(repo, revs):
+        bn.node(repo[rev])
+    bn.notify()
 
 def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
     """hook for backlog
     
     """
-
     bn = BacklogNotifier(ui, repo, hooktype)
     ctx = repo[node]
-
-    count = 0
     if hooktype == 'changegroup':
         start, end = ctx.rev(), len(repo)
         for rev in xrange(start, end):
-            if bn.node(repo[rev]):
-                count += 1
-            else:
-                pass
-    if count:
-        bn.notify(ctx, count)
+            bn.node(repo[rev])
+    bn.notify()