Completed
Pull Request — master (#563)
by
unknown
03:08
created

CreateReleaseAction.run()   B

Complexity

Conditions 5

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
dl 0
loc 62
rs 8.3192

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
2
import time
3
import datetime
4
5
from lib.base import BaseGithubAction
6
7
8
class CreateReleaseAction(BaseGithubAction):
9
    def run(self, api_user, repository, name, body,
10
            target_commitish="master", version_increase="patch",
11
            draft=False, prerelease=False):
12
13
        if api_user:
14
            self.token = self._get_user_token(api_user)
15
16
        release = self._request("GET",
17
                                "/repos/{}/releases/latest".format(repository),
18
                                None,
19
                                self.token)
20
21
        (major, minor, patch) = release['tag_name'].split(".")
22
        major = int(major.replace("v", ""))
23
        minor = int(minor)
24
        patch = int(patch)
25
26
        if version_increase == "major":
27
            major += 1
28
            minor = 0
29
            patch = 0
30
        elif version_increase == "minor":
31
            minor += 1
32
            patch = 0
33
        elif version_increase == "patch":
34
            patch += 1
35
36
        tag_name = "v{}.{}.{}".format(major,
37
                                      minor,
38
                                      patch)
39
40
        payload = {"tag_name": tag_name,
41
                   "target_commitish": target_commitish,
42
                   "name": name,
43
                   "body": body,
44
                   "draft": draft,
45
                   "prerelease": prerelease}
46
47
        release = self._request("POST",
48
                                "/repos/{}/releases".format(repository),
49
                                payload,
50
                                self.token)
51
        ts_published_at = time.mktime(
52
            datetime.datetime.strptime(
53
                release['published_at'],
54
                "%Y-%m-%dT%H:%M:%SZ").timetuple())
55
56
        results = {'author': release['author']['login'],
57
                   'html_url': release['html_url'],
58
                   'tag_name': release['tag_name'],
59
                   'target_commitish': release['target_commitish'],
60
                   'name': release['name'],
61
                   'body': release['body'],
62
                   'draft': release['draft'],
63
                   'prerelease': release['prerelease'],
64
                   'created_at': release['created_at'],
65
                   'published_at': release['published_at'],
66
                   'ts_published_at': ts_published_at,
67
                   'total_assets': len(release['assets'])}
68
        results['response'] = release
69
70
        return results
71