summaryrefslogtreecommitdiff
path: root/gerritbot/patchpickup
blob: ddacb73ea25d776fdbc744ccc1edc5863fa97e9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

import email
import io
import logging
import minilog
import os.path
import smtplib
import string
import subprocess
import sys
import tempfile
import StringIO
import shutil
from  email.mime.text import MIMEText


sourceurl = 'ssh://logerrit/core'
desturl = 'ssh://logerrit/core'
majors = [ '3-5', '3-6', '4-0', '4-1', '4-2', '4-3' ] # should be enough until autumn 2014

legalbranches = [ 'master' ]
for major in majors:
    for minor in range(8):
        legalbranches.append('libreoffice-%s-%d' % (major, minor))

logfile=os.path.join(os.environ['HOME'], 'patchpickup.log')
loglevel='INFO'

class PatchPusher():
    def __init__(self, branch, logger):
        self.tmpdir = tempfile.mkdtemp()
        self.workrepo = os.path.join(self.tmpdir, 'repo')
        self.branch = branch
        self.logger = logger

    def clone_base(self, sourceurl):
        minilog.logged_call( \
            ['git', 'clone', '--branch', self.branch, sourceurl, self.workrepo], \
             self.logger, \
            {})
        
    def fetch_all(self):
        minilog.logged_call( \
            ['git','--work-tree=%s' % self.workrepo, 'fetch', '--all'], \
            self.logger, \
            {})

    def checkout(self, branch):
        minilog.logged_call( \
            ['git','--work-tree=%s' % self.workrepo, 'checkout', branch], \
            self.logger, \
            {})

    def disable_gc(self):
        os.chdir(self.workrepo)
        minilog.logged_call( \
            ['git', 'config', 'gc.auto', '0'], \
            self.logger, \
            {})

    def cherry_pick(self, commit):
        minilog.logged_call( \
            ['git','--work-tree=%s' % self.workrepo, 'cherry-pick', commit], \
            self.logger, \
            {})

    def status(self):
        minilog.logged_call( \
            ['git','--work-tree=%s' % self.workrepo, 'status'], \
            self.logger, \
            {})
        
    def apply_message(self, message):
        messagefilename = os.path.join(self.tmpdir,'message')
        messagefile = open(messagefilename, 'w')
        logger.info('filename: %s' % messagefilename)
        messagefile.write(str(message))
        messagefile.close()
        os.chdir(self.workrepo)
        minilog.logged_call( \
            ['git', 'am', messagefilename], \
            self.logger, \
            {})

    def upload_change(self, desturl):
        minilog.logged_call( \
            ['git','--work-tree=%s' % self.workrepo, 'push', desturl, 'HEAD:refs/for/%s' % self.branch], \
            self.logger, \
            {})

    def dispose(self):
        shutil.rmtree(self.tmpdir)

class EmailCommand():
    def __init__(self, message, sourceurl, desturl, logger):
        self.message = message
        self.sourceurl = sourceurl
        self.desturl = desturl
        self.logger = logger
        self.command = None
        self.branch = 'master'
        self.commit = None
        self.success = False
        if not message.has_key('subject'):
            raise Exception('message has no subject header -- ignoring.')
        subject = message['subject']
        self.parse_params(subject)
        self.parse_command(subject)

    def parse_params(self, subject):
        state = None
        words = subject.translate(string.maketrans('\r\n','  ')).split(' ')
        for word in words:
            if word == '':
                continue
            if state == 'branch':
                self.branch = word
                state = None
            if state == 'commit':
                self.commit = word
                state = None
            elif state is None and word == 'branch':
                state = 'branch'
            if state is None and word == 'commit':
                state = 'commit'
        if self.commit is None and len(words) > 1:
            self.commit = words[1]
        if not self.branch in legalbranches:
            raise Exception('%s does not look like a legal branch to me.' % self.branch)

    def parse_command(self, subject):
        if subject.startswith('[PATCH]'):
            self.command = 'apply'
        elif subject.startswith('[CHERRYPICK]'):
            self.command = 'cherrypick'
        elif subject.startswith('[HELP]'):
            self.command = 'help'

    def execute(self):
        self.logger.info('executing command: %s, branch: %s, commit: %s' % (self.command, self.branch, self.commit))
        try:
            getattr(self, 'do_%s' % self.command)()
            self.success = True
        except Exception as e:
            self.logger.error(e)
        return self

    def do_apply(self):
        patchpusher = PatchPusher(self.branch, self.logger)
        try:
            patchpusher.clone_base(self.sourceurl)
            patchpusher.disable_gc()
            patchpusher.apply_message(self.message)
            patchpusher.upload_change(self.desturl)
        except Exception as e:
            patchpusher.dispose()
            raise e

    def do_cherrypick(self):
        if self.commit is None:
            raise Exception('could not find what to cherrypick from subject: %s' % subject)
        patchpusher = PatchPusher(self.branch, self.logger)
        try:
            patchpusher.clone_base(self.sourceurl)
            patchpusher.disable_gc()
            patchpusher.fetch_all()
            patchpusher.checkout(self.branch)
            try:
                patchpusher.cherry_pick(self.commit)
                patchpusher.upload_change(self.desturl)
            except Exception as e:
                patchpusher.status() 
                raise e
        except Exception as e:
            patchpusher.dispose()
            raise e

    def do_help(self):
        self.logger.info('To upload a patch, send a mail with subject starting with [PATCH]')
        self.logger.info('add the word \'branch\' followed by the target branch to the subject too, if needed')
        self.logger.info('')
        self.logger.info('To cherry-pick a change to a release branch, send a mail with subject starting with [CHERRYPICK]')
        self.logger.info('and the word \'commit\' followed by the SHA1 of the commit to cherrypick')
        self.logger.info('add the word \'branch\' followed by the target branch to the subject too.')

    def do_None(self):
        self.do_help()


logger = logging.getLogger('patchpickup')
logstring = StringIO.StringIO()
loghandler = logging.StreamHandler(logstring)
loghandler.setFormatter(logging.Formatter('%(asctime)s %(levelname)-8s %(message)s'))
logger.addHandler(loghandler)
logfilehandler = logging.FileHandler(logfile)
logfilehandler.setFormatter(logging.Formatter('%(asctime)s %(levelname)-8s %(message)s'))
logger.addHandler(logfilehandler)
logger.setLevel(loglevel)

success = False
sucesstext = { True : 'SUCCESS', False : 'FAILED' }


try:
    message = email.message_from_file(sys.stdin)
    logger.info('Handling incoming mail from %s' % message['From'])
    logger.info('subject: \'%s\'' % message['Subject'])
    success = EmailCommand(message, sourceurl, desturl, logger).execute().success
except Exception as e:
    logger.error(e)

reply = MIMEText(logstring.getvalue())
reply['To'] = message['From']
if message.has_key('Reply-To'):
    reply['To'] = message['Reply-To']
reply['From'] = 'gerrit@libreoffice.org'
reply['Cc'] = message['Cc']
if success:
    status = 'SUCCESS'

reply['Subject'] = '[CHANGEUPLOAD %s] %s ' % (sucesstext[success], message['Subject'])
receivers = []
try:
    s = smtplib.SMTP('localhost')
    receivers = reply['To'].split(',')
    if reply['Cc']:
        receivers = receivers + reply['Cc'].split(',')
    s.sendmail(reply['From'], receivers, reply.as_string())
    logger.info('successfully send reply to %s.' % ','.join(receivers))
    s.quit()
except Exception as e:
    logger.info('failed to send reply to %s: %s.' % (','.join(receivers), str(e)))