Added simple implementation of the pre-checker
[mdk.git] / mdk / commands / precheck.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 sys
26 import logging
27 from .. import tools, jira
28 from ..ci import CI, CIException
29 from ..command import Command
30
31
32 class PrecheckCommand(Command):
33
34 _arguments = [
35 (
36 ['-b', '--branch'],
37 {
38 'metavar': 'branch',
39 'help': 'the branch to pre-check. Defaults to the current branch.'
40 }
41 ),
42 (
43 ['-p', '--push'],
44 {
45 'action': 'store_true',
46 'help': 'if set, the branch will be pushed to your default remote.'
47 }
48 ),
49 (
50 ['name'],
51 {
52 'default': None,
53 'help': 'name of the instance',
54 'metavar': 'name',
55 'nargs': '?'
56 }
57 )
58 ]
59 _description = 'Pre-checks a branch on the CI server'
60
61 FAILED = -1
62
63 def run(self, args):
64 M = self.Wp.resolve(args.name)
65 if not M:
66 raise Exception('This is not a Moodle instance')
67
68 against = M.get('stablebranch')
69 branch = args.branch or M.currentBranch()
70 if branch == 'HEAD':
71 raise Exception('Cannot pre-check the HEAD branch')
72 elif branch == against:
73 raise Exception('Cannot pre-check the stable branch')
74
75 parsedbranch = tools.parseBranch(branch)
76 if not parsedbranch:
77 raise Exception('Could not parse the branch')
78
79 issue = parsedbranch['issue']
80
81 if args.push:
82 J = jira.Jira()
83 if J.isSecurityIssue('MDL-%s' % (issue)):
84 raise Exception('Security issues cannot be pre-checked')
85
86 remote = self.C.get('myRemote')
87 logging.info('Pushing branch \'%s\' to remote \'%s\'', branch, remote)
88 result = M.git().push(remote, branch)
89 if result[0] != 0:
90 raise Exception('Could not push the branch:\n %s' % result[2])
91
92 ci = CI()
93 try:
94 # TODO Remove that ugly hack to get the read-only remote.
95 logging.info('Invoking the build on the CI server...')
96 build = ci.precheckRemoteBranch(self.C.get('repositoryUrl'), branch, against, 'MDL-%s' % issue)
97 except CIException as e:
98 raise e
99
100 logging.info('Waiting for the build to complete, please wait...')
101 build.block_until_complete(3)
102
103 if build.is_good():
104 logging.info('Precheck passed, good work!')
105 sys.exit(0)
106 else:
107 logging.warning('Precheck failed, refer to:\n %s', build.baseurl)
108 sys.exit(self.FAILED)