Blame progit/lib/link.py

Pierre-Yves Chibon c5ee5f
# -*- coding: utf-8 -*-
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
"""
Pierre-Yves Chibon c5ee5f
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
 Authors:
Pierre-Yves Chibon c5ee5f
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
"""
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
import re
Pierre-Yves Chibon c5ee5f
import os
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
import progit.exceptions
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
FIXES = [
Pierre-Yves Chibon c5ee5f
    re.compile('fixe?[sd]?:?\s?#(\d+)', re.I),
Pierre-Yves Chibon c5ee5f
    re.compile('.*\s+fixe?[sd]?:?\s?#(\d+)', re.I),
Pierre-Yves Chibon c5ee5f
    re.compile('fixe?[sd]?:?\s?https?://.*/(\w+)/issue/(\d+)', re.I),
Pierre-Yves Chibon c5ee5f
    re.compile('.*\s+fixe?[sd]?:?\s?https?://.*/(\w+)/issue/(\d+)', re.I),
Pierre-Yves Chibon c5ee5f
]
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
RELATES = [
Pierre-Yves Chibon c5ee5f
    re.compile('.*\s+relate[sd]?:?\s?(?:to)?\s?#(\d+)', re.I),
Pierre-Yves Chibon c5ee5f
    re.compile('.*\s+relate[sd]?:?\s?#(\d+)', re.I),
Pierre-Yves Chibon c5ee5f
    re.compile(
Pierre-Yves Chibon c5ee5f
        '.*\s+relate[sd]?:?\s?(?:to)?\s?https?://.*/(\w+)/issue/(\d+)', re.I),
Pierre-Yves Chibon c5ee5f
    re.compile('.*\s+relate[sd]?:?\s?(?:to)?\s?https?://.*/(\w+)/issue/(\d+)', re.I),
Pierre-Yves Chibon c5ee5f
]
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
def get_relation(session, reponame, username, text, reftype='relates'):
Pierre-Yves Chibon c5ee5f
    ''' For a given text, searches using regex if the text contains
Pierre-Yves Chibon c5ee5f
    reference to another issue in this project or another one.
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
    Returns the list of issues referenced (possibly empty).
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
    By default it searches for references of type: `relates`, for example:
Pierre-Yves Chibon c5ee5f
    ``this commits relates to #2``.
Pierre-Yves Chibon c5ee5f
    Another reference type is: `fixes` refering to text including for
Pierre-Yves Chibon c5ee5f
    example: ``this commits fixes #3``.
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
    '''
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
    repo = progit.lib.get_project(session, reponame, user=username)
Pierre-Yves Chibon c5ee5f
    if not repo:
Pierre-Yves Chibon c5ee5f
        return []
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
    regex = RELATES
Pierre-Yves Chibon c5ee5f
    if reftype == 'fixes':
Pierre-Yves Chibon c5ee5f
        regex = FIXES
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
    issues = []
Pierre-Yves Chibon c5ee5f
    for motif in regex:
Pierre-Yves Chibon c5ee5f
        issueid = None
Pierre-Yves Chibon c5ee5f
        project = None
Pierre-Yves Chibon c5ee5f
        if motif.match(text):
Pierre-Yves Chibon c5ee5f
            if len(motif.match(text).groups()) >= 2:
Pierre-Yves Chibon c5ee5f
                issueid = motif.match(text).group(2)
Pierre-Yves Chibon c5ee5f
                project = motif.match(text).group(1)
Pierre-Yves Chibon c5ee5f
            else:
Pierre-Yves Chibon c5ee5f
                issueid = motif.match(text).group(1)
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
        if issueid:
Pierre-Yves Chibon c5ee5f
            issue = progit.lib.search_issues(
Pierre-Yves Chibon c5ee5f
                session, repo=repo, issueid=issueid)
Pierre-Yves Chibon c5ee5f
            if issue is None or issue.project.name not in [project, repo.name]:
Pierre-Yves Chibon c5ee5f
                continue
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon 19636e
            if issue not in issues:
Pierre-Yves Chibon 19636e
                issues.append(issue)
Pierre-Yves Chibon c5ee5f
Pierre-Yves Chibon c5ee5f
    return issues