def run():
  parser = argparse.ArgumentParser(prog = get_script_name_from_python_file(__file__))
  parser.add_argument("taskid")
  args = parser.parse_args()
  taskid = args.taskid

  match = re.match(r'^https:\/\/www\.wrike\.com\/open\.htm\?id=(\d+)$', taskid)
  if match is not None:
    taskid = match.group(1)

  wrike_gateway = Wrike(data_filepath=Helper.get_data_filepath('wrike'))
  task = wrike_gateway.get_task(taskid)
  if task is not None:
    branch_name = Helper.branch_name_from_task(taskid, task['title'])
    result = Helper2().create_branch(branch_name)
    if result[0] != 0:
      print(result[1])
      sys.exit(result[0])
  else:
    print "Task not found"
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'])
      })