#!/usr/bin/python3

"""Test if associated PR has a changelog entry for the changes."""

import os
import re
import requests
import sys
import json


URL_TEMPLATE = \
    'https://api.github.com/repos/CanonicalLtd/ubuntu-image/pulls/{}'
URL_TEMPLATE_NOAPI = \
    'https://github.com/CanonicalLtd/ubuntu-image/pull/{}'
LABEL = 'trivial'


# When the test is being run as part of a GH pull request, the infra sets the
# UPSTREAM_PULL_REQUEST envirnoment variable to the PR number.  This is needed
# as otherwise there's no easy way to guess which pull request is being
# referenced
pr_number = os.environ.get('UPSTREAM_PULL_REQUEST')
if not pr_number:
    # Skip the test if we're not running in a git environment.
    print('Skipping test, not ran as part of a pull request.')
    sys.exit(0)

have_tag = False
api_data = requests.get(URL_TEMPLATE.format(pr_number)).text
# First try using the github API, but since we're using unauthenticated
# requests there's a crazy low rate limit for given IPs (which for autopkgtests
# can be depleted really fast).
pull = json.loads(api_data)
if 'labels' not in pull:
    # In case it doesn't work, we fallback to a workaround of hand-parsing the
    # HTML github PR. This can actually stop working at any time when the
    # format of the label changes.
    print('Got an incomplete response from the API call, this might mean API '
          'rate limit exceeded.  Retrying with a HTML-parsing method.')
    www_data = requests.get(URL_TEMPLATE_NOAPI.format(pr_number)).text
    match = re.search(r'<a.*?title="{}".*?\/CanonicalLtd\/ubuntu-image\/'
                      'labels\/{}.*?>'.format(LABEL, LABEL),
                      www_data)
    if match:
        have_tag = True
else:
    try:
        next(label for label in pull['labels'] if label['name'] == LABEL)
        have_tag = True
    except StopIteration:
        pass
if have_tag:
    print('The {} label found on the pull request, skipping check for '
          'changelog entries.'.format(LABEL))
    sys.exit(0)
# No trivial label added, make sure the changelog has been touched and
# that a LP bug has been attached to changelog entry.
content = requests.get('{}.diff'.format(
    URL_TEMPLATE_NOAPI.format(pr_number))).text
has_changelog = False
bug_linked = False
bug_no = re.compile('^\+.*LP: \#\d+')
for line in content.splitlines():
    if not has_changelog:
        if line.startswith(
                'diff --git a/debian/changelog b/debian/changelog'):
            has_changelog = True
            continue
    elif line.startswith('diff --git '):
        break
    elif bug_no.match(line):
        bug_linked = True
assert has_changelog
assert bug_linked
