Conditions | 14 |
Total Lines | 100 |
Lines | 0 |
Ratio | 0 % |
Changes | 15 | ||
Bugs | 0 | Features | 2 |
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 create_changelog() 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 | # -*- coding: utf-8 -*- |
||
99 | def create_changelog(repo=None, |
||
100 | username=None, |
||
101 | password=None, |
||
102 | token=None, |
||
103 | milestone=None, |
||
104 | since_tag=None, |
||
105 | until_tag=None, |
||
106 | branch=None, |
||
107 | output_format='changelog', |
||
108 | issue_label_regex='', |
||
109 | pr_label_regex='', |
||
110 | template_file=None, |
||
111 | issue_label_groups=None, |
||
112 | batch=None): |
||
113 | """Create changelog data for single and batched mode.""" |
||
114 | gh = GitHubRepo( |
||
115 | username=username, |
||
116 | password=password, |
||
117 | token=token, |
||
118 | repo=repo, ) |
||
119 | |||
120 | all_changelogs = [] |
||
121 | version_tag_prefix = 'v' |
||
122 | |||
123 | if batch: |
||
124 | # This will get all the issues, might eat up the api rate limit! |
||
125 | base_issues = issues = gh.issues(state='closed', branch=branch) |
||
126 | if batch == 'milestones': |
||
127 | milestones = [i.get('title') for i in gh.milestones()] |
||
128 | empty_items = [None] * len(milestones) |
||
129 | items = zip(*(milestones, empty_items, empty_items)) |
||
130 | elif batch == 'tags': |
||
131 | tags = [ |
||
132 | i.get('ref', '').replace('refs/tags/', '') for i in gh.tags() |
||
133 | ] |
||
134 | since_tags = [None] + tags |
||
135 | until_tags = tags + [None] |
||
136 | empty_items = [None] * len(since_tags) |
||
137 | items = zip(*(empty_items, since_tags, until_tags)) |
||
138 | else: |
||
139 | base_issues = None |
||
140 | if milestone: |
||
141 | items = [(milestone, None, None)] |
||
142 | else: |
||
143 | items = [(None, since_tag, until_tag)] |
||
144 | |||
145 | for (milestone, since_tag, until_tag) in reversed(items): |
||
146 | version = until_tag or None |
||
147 | closed_at = None |
||
148 | since = None |
||
149 | until = None |
||
150 | |||
151 | # Set milestone or from tag |
||
152 | if milestone and not since_tag: |
||
153 | milestone_data = gh.milestone(milestone) |
||
154 | closed_at = milestone_data['closed_at'] |
||
155 | version = milestone |
||
156 | |||
157 | if version.startswith(version_tag_prefix): |
||
158 | version = version[len(version_tag_prefix):] |
||
159 | |||
160 | elif not milestone and since_tag: |
||
161 | since = gh.tag(since_tag)['tagger']['date'] |
||
162 | if until_tag: |
||
163 | until = gh.tag(until_tag)['tagger']['date'] |
||
164 | closed_at = until |
||
165 | |||
166 | # This returns issues and pull requests |
||
167 | issues = gh.issues( |
||
168 | milestone=milestone, |
||
169 | state='closed', |
||
170 | since=since, |
||
171 | until=until, |
||
172 | branch=branch, |
||
173 | base_issues=base_issues, ) |
||
174 | |||
175 | # Filter by regex if available |
||
176 | filtered_prs = filter_prs_by_regex(issues, pr_label_regex) |
||
177 | filtered_issues = filter_issues_by_regex(issues, issue_label_regex) |
||
178 | |||
179 | # If issue label grouping, filter issues |
||
180 | new_filtered_issues, issue_label_groups = filter_issue_label_groups( |
||
181 | filtered_issues, issue_label_groups) |
||
182 | |||
183 | ch = render_changelog( |
||
184 | repo, |
||
185 | new_filtered_issues, |
||
186 | filtered_prs, |
||
187 | version, |
||
188 | closed_at=closed_at, |
||
189 | output_format=output_format, |
||
190 | template_file=template_file, |
||
191 | issue_label_groups=issue_label_groups) |
||
192 | |||
193 | all_changelogs.append(ch) |
||
194 | |||
195 | changelog = '\n'.join(all_changelogs) |
||
196 | write_changelog(changelog=changelog) |
||
197 | |||
198 | return changelog |
||
199 | |||
260 |