Completed
Push — refactor-and-polish ( df8cc9...bb91fa )
by Michael
07:26
created

GitHub.auth_token()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
1
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
import requests
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
from pathlib import Path
5
6
7
EXT_TO_MIME_TYPE = {
8
    '.gz': 'application/x-gzip',
9
    '.whl': 'application/zip',
10
    '.zip': 'application/zip',
11
}
12
13
14
@attr.s
15
class GitHub(object):
16
    ISSUE_ENDPOINT = (
17
        'https://api.github.com/repos{/owner}{/repo}/issues{/number}'
18
    )
19
    LABELS_ENDPOINT = (
20
        'https://api.github.com/repos{/owner}{/repo}/labels'
21
    )
22
    RELEASES_ENDPOINT = (
23
        'https://api.github.com/repos{/owner}{/repo}/releases'
24
    )
25
26
    repository = attr.ib()
27
28
    @property
29
    def owner(self):
30
        return self.repository.owner
31
32
    @property
33
    def repo(self):
34
        return self.repository.repo
35
36
    @property
37
    def auth_token(self):
38
        return self.repository.auth_token
39
40
    def pull_request(self, pr_num):
41
        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
        return requests.get(
51
            pull_request_api_url,
52
            headers={
53
                'Authorization': 'token {}'.format(self.auth_token)
54
            },
55
        ).json()
56
57
    def labels(self):
58
        labels_api_url = uritemplate.expand(
59
            self.LABELS_ENDPOINT,
60
            dict(
61
                owner=self.owner,
62
                repo=self.repo,
63
            ),
64
        )
65
66
        return requests.get(
67
            labels_api_url,
68
            headers={
69
                'Authorization': 'token {}'.format(self.auth_token)
70
            },
71
        ).json()
72
73
    def create_release(self, release, uploads=None):
74
        params = {
75
            'tag_name': release.version,
76
            'name': release.name,
77
            'body': release.description,
78
            # 'prerelease': True,
79
        }
80
81
        releases_api_url = uritemplate.expand(
82
            self.RELEASES_ENDPOINT,
83
            dict(
84
                owner=self.owner,
85
                repo=self.repo,
86
            )
87
        )
88
89
        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