Esempio 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