Completed
Pull Request — master (#120)
by Michael
13:50 queued 13:15
created

Release   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 16
ccs 12
cts 13
cp 0.9231
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A title() 0 3 1
1 3
import re
2 3
import shlex
3
4 3
import attr
0 ignored issues
show
Configuration introduced by
The import attr 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 3
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...
6 3
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...
7 3
import requests
8 3
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...
9 3
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...
10
11 3
MERGED_PULL_REQUEST = re.compile(
12
    r'^([0-9a-f]{5,40}) Merge pull request #(\w+)'
13
)
14
15 3
GITHUB_PULL_REQUEST_API = (
16
    'https://api.github.com/repos{/owner}{/repo}/issues{/number}'
17
)
18
19
20 3
def changes_to_release_type(repository):
21 3
    pull_request_labels = set()
22 3
    changes = repository.changes_since_last_version
23
24 3
    for change in changes:
25 3
        for label in change.labels:
26 3
            pull_request_labels.add(label)
27
28 3
    change_descriptions = [
29
        '\n'.join([change.title, change.description]) for change in changes
30
    ]
31
32 3
    current_version = repository.latest_version
33 3
    if 'BREAKING CHANGE' in change_descriptions:
34
        return 'major', Release.BREAKING_CHANGE, current_version.next_major()
35 3
    elif 'enhancement' in pull_request_labels:
36
        return 'minor', Release.FEATURE, current_version.next_minor()
37 3
    elif 'bug' in pull_request_labels:
38 3
        return 'patch', Release.FIX, current_version.next_patch()
39
    else:
40 3
        return None, Release.NO_CHANGE, current_version
41
42
43 3
class Release:
44 3
    NO_CHANGE = 'nochanges'
45 3
    BREAKING_CHANGE = 'breaking'
46 3
    FEATURE = 'feature'
47 3
    FIX = 'fix'
48
49 3
    version = '<current_version>'
50 3
    name = None
51 3
    title = "{formatted string}"
52 3
    title_format = ''
53 3
    description = "(optional)Release description"
54 3
    changes = []
55
56 3
    @property
57
    def title(self):
58
        return
59
60 3
@attr.s
61
class PullRequest:
62 3
    number = attr.ib()
63 3
    title = attr.ib()
64 3
    description = attr.ib()
65
    # default is 'body' key
66 3
    author = attr.ib()
67 3
    labels = attr.ib(default=attr.Factory(list))
68
69 3
    @classmethod
70
    def from_github(cls, api_response):
71 3
        return PullRequest(
72
            number = api_response['number'],
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
number = api_response['number'],
^
Loading history...
73
            title = api_response['title'],
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
title = api_response['title'],
^
Loading history...
74
            description = api_response['body'],
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
description = api_response['body'],
^
Loading history...
75
            author = api_response['user']['login'],
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
author = api_response['user']['login'],
^
Loading history...
76
            labels = [
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
labels = [
^
Loading history...
77
                label['name']
78
                for label in api_response['labels']
79
                # label['colour'] => https://gist.github.com/MicahElliott/719710
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (80/79).

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

Loading history...
80
            ],
81
            # labels need a description => map for default github tags
82
        )
83
84
85 3
@attr.s
86
class GitRepository:
87 3
    VERSION_ZERO = semantic_version.Version('0.0.0')
88
    # TODO: handle multiple remotes (cookiecutter [non-owner maintainer])
89 3
    REMOTE_NAME = 'origin'
90
91 3
    auth_token = attr.ib(default=None)
92
93 3
    @property
94
    def parsed_repo(self):
95 3
        return giturlparse.parse(self.remote_url)
96
97 3
    @property
98
    def remote_url(self):
99 3
        return git(shlex.split('config --get remote.{}.url'.format(
100
            self.REMOTE_NAME
101
        )))
102
103 3
    @property
104
    def commit_history(self):
105
        return [
106
            commit_message
107
            for commit_message in git(shlex.split(
108
                'log --oneline --no-color'
109
            )).split('\n')
110
            if commit_message
111
        ]
112
113 3
    @property
114
    def first_commit_sha(self):
115
        return git(
116
            'rev-list', '--max-parents=0', 'HEAD'
117
        )
118
119 3
    @property
120
    def tags(self):
121 3
        return git(shlex.split('tag --list')).split('\n')
122
123 3
    @property
124
    def versions(self):
125 3
        versions = []
126 3
        for tag in self.tags:
127 3
            try:
128 3
                versions.append(semantic_version.Version(tag))
129 3
            except ValueError:
130 3
                pass
131 3
        return versions
132
133 3
    @property
134 3
    def latest_version(self) -> semantic_version.Version:
135 3
        return max(self.versions) if self.versions else self.VERSION_ZERO
136
137 3
    def merges_since(self, version=None):
138 3
        if version == semantic_version.Version('0.0.0'):
139
            version = self.first_commit_sha
140
141 3
        revision_range = ' {}..HEAD'.format(version) if version else ''
142
143 3
        merge_commits = git(shlex.split(
144
            'log --oneline --merges --no-color{}'.format(revision_range)
145
        )).split('\n')
146 3
        return merge_commits
147
148
    # TODO: pull_requests_since(version=None)
149 3
    @property
150
    def changes_since_last_version(self):
151 3
        pull_requests = []
152
153 3
        for index, commit_msg in enumerate(self.merges_since(self.latest_version)):
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (83/79).

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

Loading history...
154 3
            matches = MERGED_PULL_REQUEST.findall(commit_msg)
155
156 3
            if matches:
157 3
                _, pull_request_number = matches[0]
158
159 3
                pull_requests.append(PullRequest.from_github(
160
                    self.github_pull_request(pull_request_number)
161
                ))
162 3
        return pull_requests
163
164 3
    def github_labels(self):
165
        # GET /repos/:owner/:repo/labels
166
        """
167
        [
168
          {
169
            "id": 208045946,
170
            "url": "https://api.github.com/repos/octocat/Hello-World/labels/bug",
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (81/79).

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

Loading history...
171
            "name": "bug",
172
            "color": "f29513",
173
            "default": true
174
          }
175
        ]
176
        """
177
        pass
178
179 3
    def github_pull_request(self, pr_num):
180 3
        pull_request_api_url = uritemplate.expand(
181
            GITHUB_PULL_REQUEST_API,
182
            dict(
183
                owner=self.owner,
184
                repo=self.repo,
185
                number=pr_num
186
            ),
187
        )
188
189 3
        return requests.get(
190
            pull_request_api_url,
191
            headers={
192
                'Authorization': 'token {}'.format(self.auth_token)
193
            },
194
        ).json()
195
196 3
    @property
197
    def repo(self):
198 3
        return self.parsed_repo.repo
199
200 3
    @property
201
    def owner(self):
202 3
        return self.parsed_repo.owner
203
204 3
    @property
205
    def github(self):
206 3
        return self.parsed_repo.github
207
208 3
    @property
209
    def bitbucket(self):
210
        return self.parsed_repo.bitbucket
211