Completed
Push — refactor-and-polish ( 926e59...a06d09 )
by Michael
09:09
created

release_from_pull_requests()   C

Complexity

Conditions 7

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
c 1
b 0
f 0
dl 0
loc 44
rs 5.5
1
"""Generates a github changelog, tags and uploads your python library"""
2
from datetime import date
3
from pathlib import Path
4
5
from changes.config import Changes, Project
6
from changes.models import Release, ReleaseType
7
from changes.models.repository import GitHubRepository
8
9
__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 = [release_file for release_file in releases_directory.glob('*.md')]
78
    if release_files:
79
        release_file = release_files[0]
80
        release.release_file_path = Path(project_settings.releases_directory).joinpath(release_file.name)
81
        release.description = release_file.read_text()
82
83
    return release
84
85
86
def determine_release(latest_version, descriptions, labels):
87
    if 'BREAKING CHANGE' in descriptions:
88
        return 'major', ReleaseType.BREAKING_CHANGE, latest_version.next_major()
89
    elif 'enhancement' in labels:
90
        return 'minor', ReleaseType.FEATURE, latest_version.next_minor()
91
    elif 'bug' in labels:
92
        return 'patch', ReleaseType.FIX, latest_version.next_patch()
93
    else:
94
        return None, ReleaseType.NO_CHANGE, latest_version
95