def run():
  parser = argparse.ArgumentParser(prog = get_script_name_from_python_file(__file__))
  parser.add_argument("-d", "--description")
  parser.add_argument("-n", "--nobranch", action="store_true")
  parser.add_argument("title")
  args = parser.parse_args()
  if len(args.title) < 5:
    print "The title should be 5 characters or longer"
    parser.print_usage()
    sys.exit(2)

  owner, repo = Helper.owner_and_repo()
  api = GithubAPIGateway(owner, repo, token=os.environ['GITHUB_TOKEN'])
  username = api.call('user')[0]['login']

  data = {
    'title': args.title,
    'assignee': username
  }

  if args.description is not None:
    data.update(body=args.description)

  issue = api.call('create_issue', owner=owner, repo=repo, data=data)[0]
  print issue['html_url']

  branch_name = Helper.branch_name(issue)
  if args.nobranch == False:
    Helper.create_branch(branch_name)
  else:
    print branch_name
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 __init__(self, args, issue_number):
   super(IssueHandler, self).__init__(args)
   self.issue_number = issue_number
   owner, repo = Helper.owner_and_repo()
   api = GithubAPIGateway(owner, repo, token=os.environ['GITHUB_TOKEN'])
   result = api.call('list_issue', owner=owner, repo=repo, number=issue_number)
   if result[1] != 200:
     raise ObjectNotFoundException('issue', issue_number)
   else:
     self.obj = result[0]
def run():
  h2 = Helper2()
  owner, repo = h2.owner_and_repo()
  api = GithubAPIGateway(owner, repo, token=os.environ['GITHUB_TOKEN'])
  issue = None
  if len(sys.argv) <= 1:
    issue = api.call('list_issues', org=owner)[0][0]
  else:
    issue = api.call('list_issue', owner=owner, repo=repo, number=sys.argv[1])[0]

  branch_name = str(issue['number']) + '-' + inflection.parameterize(issue['title'])[:30].strip('-')
  result = h2.create_branch(branch_name)
  if result[0] != 0:
    print(result[1])
    sys.exit(result[0])
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'])
      })
def run():
  parser = argparse.ArgumentParser(prog = get_script_name_from_python_file(__file__))
  parser.add_argument("-l", "--list-only", action="store_true")
  args = parser.parse_args()
  h2 = Helper2()

  owner, repo = h2.owner_and_repo()
  api = GithubAPIGateway(owner, repo, token=os.environ['GITHUB_TOKEN'])
  branch = str(h2.current_branch())
  prs = api.call('list_pr', owner=owner, repo=repo, data={
    'head': branch
  })[0]
  url = None
  for pr in prs:
    if pr['head']['ref'] == branch:
      url = pr['html_url']
      break

  if url is not None:
    print url
    if not args.list_only:
      webbrowser.open(url)
  else:
    print "No PRs on this branch"
Exemple #7
0
from githubgateway import GithubAPIGateway
import pdb

gateway = GithubAPIGateway('owner', 'repo')

pdb.set_trace()
 def __init__(self, **gateways):
   self._github_gateway = GithubAPIGateway(*Helper.owner_and_repo()) if gateways.get('github') is None else gateways['github']