def find_bugs(launchpad, git_log, args): '''Find bugs referenced in the git log and return related tasks. Our regular expression is composed of three major parts: part1: Matches only at start-of-line (required). Optionally matches any word or hyphen separated words. part2: Matches the words 'bug' or 'lp' on a word boundary (required). part3: Matches a whole number (required). The following patterns will be matched properly: bug # 555555 Closes-Bug: 555555 Fixes: bug # 555555 Resolves: bug 555555 Partial-Bug: lp bug # 555555 :returns: an iterable containing Task objects. ''' project = args.project if p.is_no_launchpad_bugs(project): return [] projects = p.project_to_groups(project) part1 = r'^[\t ]*(?P<prefix>[-\w]+)?[\s:]*' part2 = r'(?:\b(?:bug|lp)\b[\s#:]*)+' part3 = r'(?P<bug_number>\d+)\s*?$' regexp = part1 + part2 + part3 matches = re.finditer(regexp, git_log, flags=re.I | re.M) # Extract unique bug tasks and associated prefixes. bugtasks = {} for match in matches: prefix = match.group('prefix') bug_num = match.group('bug_number') if bug_num not in bugtasks: try: lp_bug = launchpad.bugs[bug_num] for lp_task in lp_bug.bug_tasks: if lp_task.bug_target_name in projects: bugtasks[bug_num] = Task(lp_task, prefix) break except KeyError: # Unknown bug. pass return bugtasks.values()