Conditions | 15 |
Total Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 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, build_parameters=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 | |||
27 | # build parameters are pass-trhrough to circleci |
||
28 | if build_parameters: |
||
29 | if data is None: |
||
30 | data = {} |
||
31 | data['build_parameters'] = build_parameters |
||
32 | |||
33 | if data: |
||
34 | data = json.dumps(data) |
||
35 | |||
36 | response = self._perform_request(path, method='POST', data=data) |
||
37 | |||
38 | if response.status_code != httplib.CREATED: |
||
39 | message = response.json().get('message', 'Unknown reason.') |
||
40 | raise Exception('Failed to run build : %s' % message) |
||
41 | |||
42 | return response.json() |
||
43 |