Completed
Push — publish ( d61419...17b633 )
by Michael
09:17
created

GitHub.pull_request()   A

Complexity

Conditions 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
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
    release = attr.ib()
28
29
    @property
30
    def owner(self):
31
        return self.repository.owner
32
33
    @property
34
    def repo(self):
35
        return self.repository.repo
36
37
    @property
38
    def auth_token(self):
39
        return self.repository.auth_token
40
41
    def pull_request(self, pr_num):
42
        pull_request_api_url = uritemplate.expand(
43
            self.ISSUE_ENDPOINT,
44
            dict(
45
                owner=self.owner,
46
                repo=self.repo,
47
                number=pr_num
48
            ),
49
        )
50
51
        return requests.get(
52
            pull_request_api_url,
53
            headers={
54
                'Authorization': 'token {}'.format(self.auth_token)
55
            },
56
        ).json()
57
58
    def labels(self):
59
        labels_api_url = uritemplate.expand(
60
            self.LABELS_ENDPOINT,
61
            dict(
62
                owner=self.owner,
63
                repo=self.repo,
64
            ),
65
        )
66
67
        return requests.get(
68
            labels_api_url,
69
            headers={
70
                'Authorization': 'token {}'.format(self.auth_token)
71
            },
72
        ).json()
73
74
    def create_release(self, uploads=None):
75
        params = {
76
            'tag_name': self.release.version,
77
            'name': self.release.name,
78
            'body': self.release.description,
79
            # 'prerelease': True,
80
        }
81
82
        releases_api_url = uritemplate.expand(
83
            self.RELEASES_ENDPOINT,
84
            dict(
85
                owner=self.owner,
86
                repo=self.repo,
87
            )
88
        )
89
90
        return requests.post(
91
            releases_api_url,
92
            headers={
93
                'Authorization': 'token {}'.format(self.auth_token)
94
            },
95
            # auth=(self.auth_token, 'x-oauth-basic'),
96
            json=params,
97
        ).json()
98
99
        upload_responses = []
100
        upload_url = response['upload_url']
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'response'
Loading history...
101
        for upload in uploads:
102
            upload = Path(upload)
103
            upload_responses.append(requests.post(
104
                uritemplate.expand(
105
                    upload_url,
106
                    dict(name=upload.name)
107
                ),
108
                # auth=(gh_token, 'x-oauth-basic'),
109
                headers={
110
                    'authorization': 'token {}'.format(self.auth_token),
111
                    'content-type': EXT_TO_MIME_TYPE[distribution.ext],
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'distribution'
Loading history...
112
                },
113
                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...
114
                verify=False,
115
            ))
116
117
        return response, upload_responses
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'response'
Loading history...
118