Completed
Push — init ( 8d8544...7062f1 )
by Michael
07:06
created

GitRepository.pull_requests()   A

Complexity

Conditions 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
1
import re
2
import shlex
3
4
import semantic_version
0 ignored issues
show
Configuration introduced by
The import semantic_version could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
5
import uritemplate
0 ignored issues
show
Configuration introduced by
The import uritemplate could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
6
import requests
7
import giturlparse
0 ignored issues
show
Configuration introduced by
The import giturlparse could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
8
from plumbum.cmd import git
0 ignored issues
show
Configuration introduced by
The import plumbum.cmd could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
9
10
MERGED_PULL_REQUEST = re.compile(
11
    r'^([0-9a-f]{5,40}) Merge pull request #(\w+)'
12
)
13
14
PULL_REQUEST_API = 'https://api.github.com/repos{/owner}{/repo}/pulls{/number}'
15
16
17
class PullRequest:
18
    title = None
19
    description = None
20
    author = None
21
    labels = []
22
23
    def __init__(self, pr_number, committish, **kwargs):
24
        self.number = pr_number
25
        self.committish = committish
26
        self.title = kwargs['title']
27
        self.description = kwargs['body']
28
        self.author = kwargs['user']['login']
29
        self.labels = [
30
            label['name']
31
            for label in kwargs['labels']
32
        ]
33
34
35
class GitRepository:
36
    auth_token = None
37
38
    def __init__(self, url=None):
39
        self.parsed_repo = url or giturlparse.parse(
40
            git(shlex.split('config --get remote.origin.url'))
41
        )
42
        self.commit_history = git(shlex.split(
43
            'log --oneline --merges --no-color'
44
        )).split('\n')
45
46
        self.tags = git(shlex.split('tag --list')).split('\n')
47
48
        self.versions = sorted([
49
            semantic_version.Version(tag)
50
            for tag in self.tags
51
            if tag
52
        ])
53
54
    @property
55
    def latest_version(self):
56
        return self.versions[-1] if self.versions else semantic_version.Version('0.0.0')
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (88/79).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
57
58
    def get_pull_request(self, pr_num):
59
        return requests.get(
60
            uritemplate.expand(
61
                PULL_REQUEST_API,
62
                dict(
63
                    owner=self.owner,
64
                    repo=self.repo,
65
                    number=pr_num
66
                ),
67
            ),
68
            headers={
69
                'Authorization': 'token {}'.format(self.auth_token)
70
            },
71
        ).json()
72
73
    @property
74
    def pull_requests(self):
75
        pull_requests = []
76
77
        for index, commit_msg in enumerate(self.commit_history):
78
            matches = MERGED_PULL_REQUEST.findall(commit_msg)
79
            if matches:
80
                committish, pr_number = matches[0]
81
82
                pr = self.get_pull_request(pr_number)
0 ignored issues
show
Coding Style Naming introduced by
The name pr does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
83
84
                pull_requests.append(
85
                    PullRequest(
86
                        pr_number,
87
                        committish,
88
                        **pr
89
                    )
90
                )
91
92
        return pull_requests
93
94
    @property
95
    def repo(self):
96
        return self.parsed_repo.repo
97
98
    @property
99
    def owner(self):
100
        return self.parsed_repo.owner
101
102
    @property
103
    def github(self):
104
        return self.parsed_repo.github
105
106
    @property
107
    def bitbucket(self):
108
        return self.parsed_repo.bitbucket
109