Conditions | 12 |
Total Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Complex classes like RunBuild.run() 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 | import httplib |
||
9 | def run(self, project, branch=None, tag=None, vcs_revision=None): |
||
10 | """ |
||
11 | Run build for a SHA in project. |
||
12 | """ |
||
13 | |||
14 | # Add some explicit mutually-exclusive checks. |
||
15 | if not(branch or tag or vcs_revision): |
||
16 | raise Exception('At least one of branch, tag or vcs_revision should be provided.') |
||
17 | if (branch and (tag or vcs_revision)) or (tag and vcs_revision): |
||
18 | raise Exception('Only one of branch, tag or vcs_revision should be provided.') |
||
19 | |||
20 | data = None |
||
21 | if branch: |
||
22 | path = 'project/%s/tree/%s' % (project, branch) |
||
23 | else: |
||
24 | path = 'project/%s' % project |
||
25 | data = {'tag': tag} if tag else {'revision': vcs_revision} |
||
26 | data = json.dumps(data) |
||
27 | |||
28 | response = self._perform_request( |
||
29 | path, method='POST', data=data |
||
30 | ) |
||
31 | |||
32 | if response.status_code != httplib.CREATED: |
||
33 | message = response.json().get('message', 'Unknown reason.') |
||
34 | raise Exception('Failed to run build : %s' % message) |
||
35 | |||
36 | return response.json() |
||
37 |