Completed
Push — pyup-update-pytest-3.2.3-to-3.... ( 5144af )
by Michael
07:18 queued 07:12
created

determine_release()   A

Complexity

Conditions 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
c 3
b 0
f 0
dl 0
loc 9
ccs 0
cts 0
cp 0
crap 20
rs 9.2
1
"""Generates a github changelog, tags and uploads your python library"""
2
from datetime import date
3 3
from pathlib import Path
4 3
5 3
from changes.config import Changes, Project
6 3
from changes.models import Release, ReleaseType
7
from changes.models.repository import GitHubRepository
8
9 3
__version__ = '0.7.0'
10
__url__ = 'https://github.com/michaeljoseph/changes'
11
__author__ = 'Michael Joseph'
12
__email__ = '[email protected]'
13
14
15
from .cli import main  # noqa
16
17
settings = None
18
project_settings = None
19
20
21
def initialise():
22
    """
23
    Detects, prompts and initialises the project.
24
25
    Stores project and tool configuration in the `changes` module.
26
    """
27
    global settings, project_settings
28
29
    # Global changes settings
30
    settings = Changes.load()
31
32
    # Project specific settings
33
    project_settings = Project.load(
34
        GitHubRepository(
35
            auth_token=settings.auth_token
36
        )
37
    )
38
39
40
def release_from_pull_requests():
41
    global project_settings
42
43
    repository = project_settings.repository
44
45
    pull_requests = repository.pull_requests_since_latest_version
46
47
    labels = set([
48
        label_name
49
        for pull_request in pull_requests
50
        for label_name in pull_request.label_names
51
    ])
52
53
    descriptions = [
54
        '\n'.join([
55
            pull_request.title, pull_request.description
56
        ])
57
        for pull_request in pull_requests
58
    ]
59
60
    bumpversion_part, release_type, proposed_version = determine_release(
61
        repository.latest_version,
62
        descriptions,
63
        labels
64
    )
65
66
    releases_directory = Path(project_settings.releases_directory)
67
    if not releases_directory.exists():
68
        releases_directory.mkdir(parents=True)
69
70
    release = Release(
71
        release_date=date.today().isoformat(),
72
        version=str(proposed_version),
73
        bumpversion_part=bumpversion_part,
74
        release_type=release_type,
75
    )
76
77
    release_files = [
78
        release_file for release_file in releases_directory.glob('*.md')]
79
    if release_files:
80
        release_file = release_files[0]
81
        release.release_file_path = Path(
82
            project_settings.releases_directory).joinpath(release_file.name)
83
        release.description = release_file.read_text()
84
85
    return release
86
87
88
def determine_release(latest_version, descriptions, labels):
89
    if 'BREAKING CHANGE' in descriptions:
90
        return 'major', ReleaseType.BREAKING_CHANGE, latest_version.next_major()
91
    elif 'enhancement' in labels:
92
        return 'minor', ReleaseType.FEATURE, latest_version.next_minor()
93
    elif 'bug' in labels:
94
        return 'patch', ReleaseType.FIX, latest_version.next_patch()
95
    else:
96
        return None, ReleaseType.NO_CHANGE, latest_version
97