Conditions | 12 |
Total Lines | 86 |
Lines | 0 |
Ratio | 0 % |
Tests | 27 |
CRAP Score | 13.7202 |
Changes | 10 | ||
Bugs | 1 | Features | 0 |
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:
If many parameters/temporary variables are present:
Complex classes like stage() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | 3 | import difflib |
|
60 | 3 | def stage(draft, release_name='', release_description=''): |
|
61 | |||
62 | 3 | repository = changes.project_settings.repository |
|
63 | 3 | bumpversion_part, release_type, proposed_version = changes_to_release_type(repository) |
|
64 | |||
65 | 3 | if not repository.changes_since_last_version: |
|
66 | error("There aren't any changes to release since {}".format(proposed_version)) |
||
67 | return |
||
68 | |||
69 | 3 | info('Staging [{}] release for version {}'.format( |
|
70 | release_type, |
||
71 | proposed_version |
||
72 | )) |
||
73 | |||
74 | 3 | if BumpVersion.read_from_file(Path('.bumpversion.cfg')).current_version == str(proposed_version): |
|
75 | info('Version already bumped to {}'.format(proposed_version)) |
||
76 | else: |
||
77 | 3 | bumpversion_arguments = ( |
|
78 | BumpVersion.DRAFT_OPTIONS if draft |
||
79 | else BumpVersion.STAGE_OPTIONS |
||
80 | ) + [bumpversion_part] |
||
81 | |||
82 | 3 | info('Running: bumpversion {}'.format( |
|
83 | ' '.join(bumpversion_arguments) |
||
84 | )) |
||
85 | 3 | bumpversion.main(bumpversion_arguments) |
|
86 | |||
87 | 3 | info('Generating Release') |
|
88 | # prepare context for changelog documentation |
||
89 | 3 | project_labels = changes.project_settings.labels |
|
90 | 3 | for label, properties in project_labels.items(): |
|
91 | 3 | pull_requests_with_label = [ |
|
92 | pull_request |
||
93 | for pull_request in repository.changes_since_last_version |
||
94 | if label in pull_request.labels |
||
95 | ] |
||
96 | |||
97 | 3 | project_labels[label]['pull_requests'] = pull_requests_with_label |
|
98 | |||
99 | 3 | release = Release( |
|
100 | name=release_name, |
||
101 | release_date=date.today().isoformat(), |
||
102 | version=str(proposed_version), |
||
103 | description=release_description, |
||
104 | changes=project_labels, |
||
105 | ) |
||
106 | |||
107 | # TODO: if project_settings.release_notes_template is None |
||
108 | 3 | release_notes_template = pkg_resources.resource_string( |
|
109 | changes.__name__, |
||
110 | 'templates/release_notes_template.md' |
||
111 | ).decode('utf8') |
||
112 | |||
113 | 3 | release_notes = Template(release_notes_template).render(release=release) |
|
114 | |||
115 | 3 | releases_directory = Path(changes.project_settings.releases_directory) |
|
116 | 3 | if not releases_directory.exists(): |
|
117 | 3 | releases_directory.mkdir(parents=True) |
|
118 | |||
119 | 3 | release_notes_path = releases_directory.joinpath( |
|
120 | '{}.md'.format(release.version) |
||
121 | ) |
||
122 | |||
123 | 3 | if draft: |
|
124 | 3 | info('Would have created {}:'.format(release_notes_path)) |
|
125 | 3 | debug(release_notes) |
|
126 | else: |
||
127 | 3 | info('Writing release notes to {}'.format(release_notes_path)) |
|
128 | 3 | if release_notes_path.exists(): |
|
129 | release_notes_content = release_notes_path.read_text(encoding='utf-8') |
||
130 | if release_notes_content != release_notes: |
||
131 | info('\n'.join(difflib.unified_diff( |
||
132 | release_notes_content.splitlines(), |
||
133 | release_notes.splitlines(), |
||
134 | fromfile=str(release_notes_path), |
||
135 | tofile=str(release_notes_path) |
||
136 | ))) |
||
137 | if click.confirm( |
||
138 | click.style( |
||
139 | '{} has modified content, overwrite?'.format(release_notes_path), |
||
140 | **STYLES['error'] |
||
141 | ) |
||
142 | ): |
||
143 | release_notes_path.write_text(release_notes, encoding='utf-8') |
||
144 | else: |
||
145 | release_notes_path.write_text(release_notes, encoding='utf-8') |
||
146 |
Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.