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
def create_task(args):
    # INITIALIZE DRIVERS AND HELPERS
    wrike_gateway = Wrike(wait_for_redirect=True, data_filepath=Helper.get_data_filepath("wrike"))
    github_helper = GithubAPIHelper()

    # GET FOLDER ID
    id_folder = args.id_folder
    should_check_folder_id_validity = True
    if id_folder is None:
        res = WrikeAPIHelper(wrike=wrike_gateway).get_folders_list(args.title_folder)
        if res["id"] is None:
            print 'Folder "{0}" could not be found'.format(res["prepend"])
            sys.exit(-1)
        else:
            id_folder = res["id"]
            should_check_folder_id_validity = False

    if should_check_folder_id_validity:
        f = wrike_gateway.get_folder(id_folder)
        if f is None:
            print "Wrong folder id"
            sys.exit(-1)

    # CREATE TASK
    create_task_params = {"description": args.description}
    if args.self_assign:
        contact = wrike_gateway.get_current_contact()
        create_task_params.update({"responsibles": "['{0}']".format(contact["id"])})
    task = wrike_gateway.create_task(id_folder, args.title, create_task_params)
    print task["permalink"]

    # CREATE ISSUE
    if args.github_issue:
        issue = github_helper.issue_from_task_object(task, args.self_assign)
        print issue["html_url"]
        wrike_gateway.create_task_comment(task["id"], issue["html_url"])
        branch_name = Helper.branch_name(issue)
        if not args.nobranch:
            Helper.create_branch(branch_name)
        else:
            print branch_name

    wrike_gateway.redirect(task["permalink"])
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'])
      })