#!/usr/bin/env python

import sys
import re

import requests
from requests.exceptions import (
    ConnectionError,
    Timeout
    )

BUG_PREFIX_REGEX = r'\[bug (\d+)\]'


def are_lines_not_more_than_79_chars(contents):
    long_lines = [line for line in contents if len(line) > 79]
    if long_lines:
        print('Error:')
        print('The commit message should not have more than '
              '79 characters per line')
        return False
    return True


def is_bug_number_in_right_format(contents):
    summary = contents[0]
    if (re.compile(r'\bbug\b', flags=re.IGNORECASE).search(summary) and
            re.search(r'\b#{0,1}\d+\b', summary)):
        bug_format_regex = re.compile(BUG_PREFIX_REGEX,
                                      flags=re.IGNORECASE)
        valid_bug_format = bug_format_regex.match(summary)
        if not valid_bug_format:
            print('Specify the bug number in the format '
                  '[bug xxxxxxx] at the beginning of the commit summary')
            return False
    return True


def print_bug_info(contents):
    summary = contents[0]
    bug_id_regex = re.compile(BUG_PREFIX_REGEX,
                              flags=re.IGNORECASE)
    is_a_bug = bug_id_regex.match(summary)
    if is_a_bug:
        bug_id = is_a_bug.group(1)
        url = 'https://bugzilla.mozilla.org/rest/bug/{}'.format(bug_id)
        try:
            response = requests.get(url, timeout=60.0)
            response_dict = response.json()
            if ('error' in response_dict and response_dict['error']):
                print('Bug with id {} not found.'.format(bug_id))
                return False
            bug_info = response_dict['bugs'][0]
            print('{} - {}'.format(bug_id,
                                   bug_info['summary']))
            print('Assigned to: {}'.format(bug_info['assigned_to']))
        except ValueError:
            print('Error parsing response from Bugzilla REST API')
        except (ConnectionError, Timeout):
            print('Unable to contact Mozilla Bugzilla')
    return True

LINT_FUNCTIONS = [is_bug_number_in_right_format,
                  are_lines_not_more_than_79_chars,
                  print_bug_info]


def lint_commit_msg(commit_msg_file):
    with open(commit_msg_file) as commit_contents:
        commit_msg = [line for line in commit_contents.readlines()
                      if not line.strip().startswith('#')]
        if commit_msg:
            return_values = map(lambda x: x(commit_msg), LINT_FUNCTIONS)
            if not all(return_values):
                return 1
    return 0

if __name__ == '__main__':
    commit_msg_file = sys.argv[1]
    errors_found = lint_commit_msg(commit_msg_file)
    if errors_found:
        sys.exit(1)

@@ -125,14 +125,10 @@
 out%0A
-    
 )%0A
+
 %0ABUG
@@ -415,16 +415,17 @@
 per line
+.
 ')%0A     
@@ -988,16 +988,17 @@
  summary
+.
 ')%0A     
@@ -1370,16 +1370,73 @@
    try:%0A
+            print('Looking up bug %7B%7D...'.format(bug_id))%0A
         
@@ -1626,19 +1626,8 @@
 'Bug
- with id %7B%7D
  not
@@ -1632,17 +1632,17 @@
 ot found
-.
+!
 '.format
@@ -1676,24 +1676,52 @@
 eturn False%0A
+            print('Found!')%0A
             
@@ -1779,12 +1779,20 @@
 nt('
-%7B%7D -
+    Summary:
  %7B%7D'
@@ -1803,51 +1803,8 @@
 mat(
-bug_id,%0A                                   
 bug_
@@ -1840,16 +1840,20 @@
  print('
+    
 Assigned
@@ -2104,16 +2104,17 @@
 n True%0A%0A
+%0A
 LINT_FUN
@@ -2123,16 +2123,21 @@
 IONS = %5B
+%0A    
 is_bug_n
@@ -2159,30 +2159,16 @@
 format,%0A
-              
     are_
@@ -2197,30 +2197,16 @@
 _chars,%0A
-              
     prin
@@ -2215,16 +2215,17 @@
 bug_info
+%0A
 %5D%0A%0A%0Adef 
@@ -2595,16 +2595,17 @@
 eturn 1%0A
+%0A
     retu
@@ -2610,16 +2610,17 @@
 turn 0%0A%0A
+%0A
 if __nam

Tweak commit msg linter