#!/usr/bin/env python2
# Copyright 2017 Google Inc.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import json
import re
import subprocess
import sys
import urllib

# TODO(halcanary): document functions and script usage.

def retrieve_changeid(commit_or_branch):
  try:
    cmd = ['git', 'log', '-1', '--format=%B', commit_or_branch, '--']
    body = subprocess.check_output(cmd)
  except OSError:
    raise Exception('git not found')
  except subprocess.CalledProcessError:
    raise Exception('`%s` failed' % ' '.join(cmd))
  match = re.search(r'^Change-Id: *(.*) *$', body, re.MULTILINE)
  if match is None:
    raise Exception('Change-Id field missing from commit %s' % commit_or_branch)
  return match.group(1)


def gerrit_change_id_to_number(site, cid):
  url = 'https://%s/changes/?q=change:%s' % (site, cid)
  try:
    content = urllib.urlopen(url).read()
  except IOError:
    raise Exception('error reading "%s"' % url)
  try:
    parsed = json.loads(content[content.find('['):])
  except ValueError:
    raise Exception('unable to parse content\n"""\n%s\n"""' % content)
  try:
    return parsed[0]['_number']
  except (IndexError, KeyError):
    raise Exception('Content missing\n"""\n%s\n"""' %
                    json.dumps(parsed, indent=2))


def args_to_changeid(argv):
  if len(argv) == 2 and len(argv[1]) == 41 and argv[1][0] == 'I':
    return argv[1]
  else:
    return retrieve_changeid(argv[1] if len(argv) == 2 else 'HEAD')


if __name__ == '__main__':
  try:
    sys.stdout.write('%d\n' %
        gerrit_change_id_to_number('skia-review.googlesource.com',
                                   args_to_changeid(sys.argv)))
  except Exception as e:
    sys.stderr.write('%s\n' % e)
    sys.exit(1)