Added simple implementation of the pre-checker
[mdk.git] / mdk / ci.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 Moodle Development Kit
6
7 Copyright (c) 2014 Frédéric Massart - FMCorz.net
8
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22 http://github.com/FMCorz/mdk
23 """
24
25 import logging
26 from jenkinsapi import jenkins
27 from jenkinsapi.custom_exceptions import JenkinsAPIException, TimeOut
28 from .config import Conf
29
30 C = Conf()
31
32
33 class CI(object):
34 """Wrapper for Jenkins"""
35
36 _jenkins = None
37 url = None
38 token = None
39
40 def __init__(self, url=None, token=None, load=True):
41 self.url = url or C.get('ci.url')
42 self.token = token or C.get('ci.token')
43 if load:
44 self.load()
45
46 @property
47 def jenkins(self):
48 """The Jenkins object"""
49 return self._jenkins
50
51 def load(self):
52 """Loads the Jenkins object"""
53
54 # Resets the logging level.
55 logger = logging.getLogger('jenkinsapi.job')
56 logger.setLevel(logging.WARNING)
57 logger = logging.getLogger('jenkinsapi.build')
58 logger.setLevel(logging.WARNING)
59
60 # Loads the jenkins object.
61 self._jenkins = jenkins.Jenkins(self.url)
62
63 def precheckRemoteBranch(self, remote, branch, integrateto, issue=None):
64 """Runs the precheck job and returns the build object"""
65 params = {
66 'remote': remote,
67 'branch': branch,
68 'integrateto': integrateto
69 }
70 if issue:
71 params['issue'] = issue
72
73 job = self.jenkins.get_job('Precheck remote branch')
74
75 try:
76 invoke = job.invoke(build_params=params, securitytoken=self.token, invoke_pre_check_delay=0)
77 invoke.block_until_not_queued(60, 2)
78 except JenkinsAPIException:
79 raise CIException('Failed to invoke the build, check your permissions.')
80 except TimeOut:
81 raise CIException('The build has been in queue for more than 60s. Aborting, please refer to: %s' % job.baseurl)
82
83 build = invoke.get_build()
84 return build
85
86
87 class CIException(Exception):
88 pass