Ejemplo n.º 1
0
class GithubAPIHelper(object):
  def __init__(self, **gateways):
    self._github_gateway = GithubAPIGateway(*Helper.owner_and_repo()) if gateways.get('github') is None else gateways['github']

  def issue_from_task_id(self, task_id):
    # TODO
    print 'TO BE IMPLEMENTED'
    return None

  def issue_from_task_object(self, task_object, self_assign=False):
    body = '### {0}\n___\n\n{1}'.format(task_object['permalink'].encode('utf-8'), task_object['description'].encode('utf-8'))
    return self._github_gateway.create_issue(task_object['title'], self_assign, {'body': body})

  def get_ids_addressed(self, by_user, all_comments):
    ret = set()
    for current_user_comment in all_comments.get(by_user) or []:
      match = re.search(r'discussion_r(\d+)|issuecomment-(\d+)', current_user_comment['body'])
      if match is not None:
        ret.add(match.group(1) or match.group(2))
    return ret

  def get_lg_data(self):
    current_user = self._github_gateway.get_user()['login']
    all_comments = self._github_gateway.get_pr_and_review_comments(Helper.current_branch())
    comment_ids_addressed = self.get_ids_addressed(current_user, all_comments)
    all_comments.pop(current_user, None)
    ret = {}
    ret['lgs_count'] = 0
    ret['has_unaddressed_comments'] = False
    ret['has_nonregular_lgs'] = False
    ret['comments'] = {}
    for comments_user, comments in all_comments.iteritems():
      unaddressed_comments = []
      lgd = False
      lgcomment = None
      for comment in reversed(comments):
        match = re.search(r'\bLG\b', comment['body'], flags=re.IGNORECASE)
        if match is not None:
          lgcomment = comment['body']
          if len(unaddressed_comments) <= 0:
            ret['lgs_count'] += 1
            if comment['body'].upper().strip() != 'LG':
              ret['has_nonregular_lgs'] = True
          break
        else:
          if str(comment['id']) not in comment_ids_addressed:
            unaddressed_comments.append(comment)

      if len(unaddressed_comments) > 0:
        ret['has_unaddressed_comments'] = True

      ret['comments'][comments_user] = {
        'unaddressed_comments': unaddressed_comments,
        'lgcomment': lgcomment
      }

    return ret
def issue_from_wrike(args):
  # GET TASKS
  tasks = []
  wrike_gateway = Wrike(data_filepath=Helper.get_data_filepath('wrike'), wait_for_redirect=True)
  github_gateway = GithubAPIGateway(*Helper.owner_and_repo())
  for taskid in args.taskids:
    task = wrike_gateway.get_task(taskid)
    if task is None:
      print "'{0}' is not a valid taskid or it cannot be found".format(taskid)
      sys.exit(-1)
    tasks.append(task)

  # BODY OF ISSUE
  body = ''
  for task in tasks:
    body += '### {0}\n___\n\n{1}\n'.format(task['permalink'].encode('utf-8'), task['description'].encode('utf-8'))

  # TITLE OF ISSUE
  title = tasks[0]['title']
  if len(tasks) > 1:
    title += ' (+{0} Wrike tasks)'.format(len(tasks) - 1)

  # CREATE ISSUE
  issue = github_gateway.create_issue(title, True, {'body': body})
  print issue['html_url']
  branch_name = Helper.branch_name(issue)
  if args.nobranch == False:
    Helper.create_branch(branch_name)
  else:
    print branch_name

  # WRITE LINK TO ISSUE ON EVERY TASK AND SET ASIGNEE
  wrike_gateway.redirect(issue['html_url'])
  contact = wrike_gateway.get_current_contact()
  for task in tasks:
    wrike_gateway.create_task_comment(task['id'], issue['html_url'])
    if contact['id'] not in task['responsibleIds']:
      wrike_gateway.change_task(task['id'], {
        'addResponsibles': "['{0}']".format(contact['id'])
      })