RunBuild   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 15

1 Method

Rating   Name   Duplication   Size   Complexity  
F run() 0 34 15
1
import httplib
2
import json
3
4
from lib.action import CircleCI
5
6
7
class RunBuild(CircleCI):
8
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