Completed
Pull Request — master (#127)
by Michael
11:23 queued 10:49
created

GitHubApi   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 103
ccs 22
cts 28
cp 0.7856
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A pull_request() 0 14 1
B create_release() 0 44 2
A labels() 0 13 1
A repo() 0 3 1
A owner() 0 3 1
A auth_token() 0 3 1
1 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...
2 3
import requests
3 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...
4 3
from pathlib import Path
5
6
7 3
EXT_TO_MIME_TYPE = {
8
    '.gz': 'application/x-gzip',
9
    '.whl': 'application/zip',
10
    '.zip': 'application/zip',
11
}
12
13
14 3
@attr.s
15 3
class GitHubApi(object):
16 3
    ISSUE_ENDPOINT = (
17
        'https://api.github.com/repos{/owner}{/repo}/issues{/number}'
18
    )
19 3
    LABELS_ENDPOINT = (
20
        'https://api.github.com/repos{/owner}{/repo}/labels'
21
    )
22 3
    RELEASES_ENDPOINT = (
23
        'https://api.github.com/repos{/owner}{/repo}/releases'
24
    )
25
26 3
    repository = attr.ib()
27
28 3
    @property
29
    def owner(self):
30 3
        return self.repository.owner
31
32 3
    @property
33
    def repo(self):
34 3
        return self.repository.repo
35
36 3
    @property
37
    def auth_token(self):
38 3
        return self.repository.auth_token
39
40 3
    def pull_request(self, pr_num):
41 3
        pull_request_api_url = uritemplate.expand(
42
            self.ISSUE_ENDPOINT,
43
            dict(
44
                owner=self.owner,
45
                repo=self.repo,
46
                number=pr_num
47
            ),
48
        )
49
50 3
        return requests.get(
51
            pull_request_api_url,
52
            headers={
53
                'Authorization': 'token {}'.format(self.auth_token)
54
            },
55
        ).json()
56
57 3
    def labels(self):
58 3
        labels_api_url = uritemplate.expand(
59
            self.LABELS_ENDPOINT,
60
            dict(
61
                owner=self.owner,
62
                repo=self.repo,
63
            ),
64
        )
65
66 3
        return requests.get(
67
            labels_api_url,
68
            headers={
69
                'Authorization': 'token {}'.format(self.auth_token)
70
            },
71
        ).json()
72
73 3
    def create_release(self, release, uploads=None):
74 3
        params = {
75
            'tag_name': release.version,
76
            'name': release.name,
77
            'body': release.description,
78
            # 'prerelease': True,
79
        }
80
81 3
        releases_api_url = uritemplate.expand(
82
            self.RELEASES_ENDPOINT,
83
            dict(
84
                owner=self.owner,
85
                repo=self.repo,
86
            )
87
        )
88
89 3
        return requests.post(
90
            releases_api_url,
91
            headers={
92
                'Authorization': 'token {}'.format(self.auth_token)
93
            },
94
            # auth=(self.auth_token, 'x-oauth-basic'),
95
            json=params,
96
        ).json()
97
98
        upload_responses = []
99
        upload_url = response['upload_url']
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'response'
Loading history...
100
        for upload in uploads:
101
            upload = Path(upload)
102
            upload_responses.append(requests.post(
103
                uritemplate.expand(
104
                    upload_url,
105
                    dict(name=upload.name)
106
                ),
107
                # auth=(gh_token, 'x-oauth-basic'),
108
                headers={
109
                    'authorization': 'token {}'.format(self.auth_token),
110
                    'content-type': EXT_TO_MIME_TYPE[distribution.ext],
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'distribution'
Loading history...
111
                },
112
                data=upload.read_bytes(),
0 ignored issues
show
Bug introduced by
The Instance of Path does not seem to have a member named read_bytes.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
113
                verify=False,
114
            ))
115
116
        return response, upload_responses
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'response'
Loading history...
117