 _async = require 'async'
 _Issue = require './issue'
 _ = require 'underscore'
+_common = require '../common'
 
 #统一处理log，便于可以统一禁掉
 _log = (log)->

    super

  #分析commit message中的标签，来关联对应的issue
  analysisCommitMessage: (message, member_id, cb)->
    #提取issue_id
    issue_id = if message.match /#(\d+)/i then RegExp.$1 else 0
    #提取done标签是否存在
    isDone = /#(done|ok)/i.test(message)
    #提取new标签
    isNew = /#new/i.test(message)
    #提取doing标签
    isDoing = /#doing/i.test(message)
    _log "issue id -> #{issue_id}, done: #{isDone}, new: #{isNew}, doing: #{isDoing}"

    queue = []
    queue.push(
      (done)->
        done null if not isNew
        #如果包含isNew，则创建一个issue
        #替换掉message中的标签
        data =
          title: message.replace /#(new|doing|done|ok)/ig, ''
          status: if isDoing then 'doing' else 'new'
          member_id: member_id
          owner: member_id
          timestamp: new Date()
          tag: ''

    )
    #如果没有issue_id或没有完成，则不做任何处理
    return cb null, issue_id if not done

    #如果有done标签，则完成这个issue
    issue = new _Issue(@member)
    issue.finishedIssue issue_id, ()-> cb null, issue_id

  #根据git用户名查找对应的用户id
  findMemberWithGitUser: (gitUser, cb)->

      (member_id, done)->
        _log "#{commit.author.email}->#{member_id}"
        #分析message中的信息，例如关于到issue，或者完成某个issue等
        self.analysisCommitMessage projectId, commit.message, member_id, (err, issue_id)->
          _log "#{commit.message}"
          done err, member_id, issue_id
    )
