~asbalderson/jenkins-launchpad-plugin/private-repo

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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import re
from argparse import ArgumentParser
import argparse
import atexit
import git
from jlp.launchpadutils import (build_state, LaunchpadVote, get_vote_subject,
                                get_target_branch, get_source_branch,
                                is_git_project)
from jlp import (launchpadutils, Branch,
                 DputRunner, jenkinsutils, get_launchpad,
                 get_config_option, logger)
from tarmac.exceptions import TarmacMergeError, BranchHasConflicts
from bzrlib.errors import LockFailed
import os
from shutil import rmtree
import tempfile

BUG_MESSAGE_UPSTREAM_COMMIT_TMPL = '''
This bug is fixed with commit {commitish} to {project} on branch {branch}.
To view that commit see the following URL:
https://git.launchpad.net/{project}/commit/?id={commitish}
'''
GIT_MAX_WIDTH = 74


class StringSplitAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        setattr(namespace, self.dest, values.split(','))


class StringStripAction(argparse.Action):
    """Strips double quotes added by Jenkins for variables ending with '}'"""
    def __call__(self, parser, namespace, values, option_string=None):
        setattr(namespace, self.dest, values.strip('"'))


def get_fixes_commit_message(mp):
    if is_git_project(mp):
        return ''

    ret = ''
    for b in mp.source_branch.linked_bugs_collection:
        ret = ret + ', ' + b.web_link
    ret = ret.replace(', ', '', 1)
    if ret == '':
        return ''
    else:
        return ' Fixes: ' + ret


def get_approved_by_commit_message(mp):
    ret = ''
    for vote in mp.votes:
        if vote.comment and vote.comment.vote == LaunchpadVote.APPROVE:
            ret = ret + ', ' + vote.reviewer.display_name
    ret = ret.replace(', ', '', 1)
    return ret


def format_commit_message(mp, commit_message):
    formatted_message = u"{commit_message}.{fixes}"
    #get rid of the trailing '.' so we don't end up with '..' when
    #people use '.' at the end of their commit message
    commit_message = commit_message.rstrip('.')
    formatted_message = formatted_message.format(
        commit_message=commit_message,
        fixes=get_fixes_commit_message(mp))
    approved_by = get_approved_by_commit_message(mp)
    if mp and not approved_by:
        approved_by = mp.reviewer.display_name
    if approved_by:
        formatted_message += u"\n\nApproved by {approved_by}.".format(
            approved_by=approved_by)
    return formatted_message


def change_mp_status(mp, reason, args, vote):
    args[reason] = True
    mp_state, message = build_state(args)
    message = jenkinsutils.format_message_for_mp_update(
        args['build_job_url'],
        message,
        include_rebuild_link=False)
    launchpadutils.change_mp_status(mp, mp_state, message, args['revision'],
                                    vote)


def merge_and_commit(mp, args, lp_handle):
    # if there is no reviewed_revid then something went wrong
    if not mp.reviewed_revid:
        logger.debug('Approved revid is not set '
                     '(maybe a permission problem?). Failing autolanding.')
        change_mp_status(mp, 'invalid_revid',
                         args, LaunchpadVote.NEEDS_FIXING)
        return 1

    if is_git_project(mp):
        git_dir = tempfile.mkdtemp()
        try:
            return _merge_and_commit_git(mp, args, git_dir, lp_handle)
        except git.exc.GitCommandError as e:
            logger.critical('%s\n%s\n%s' % (e.stdout, e.stderr, e.command))
            return 1
        finally:
            rmtree(git_dir)
    else:
        return _merge_and_commit_bzr(mp, args)


def _git_source_info(mp):
    """Get the current sha, author, and email of the source branch.

    @param mp: merge proposal under review
    @return: the sha, author, and email of the latest revision
    """
    tmp_dir = tempfile.mkdtemp()
    try:
        proposal = git.Repo.clone_from(
            mp.source_git_repository.git_https_url,
            tmp_dir, branch=get_source_branch(mp))
        revid = proposal.head.object.hexsha
        author = proposal.head.commit.author.name
        email = proposal.head.commit.author.email
    finally:
        rmtree(tmp_dir)

    return revid, author, email


def lint_commit_message(commit_message):
    """Attempt to lint commit message."""
    errors = []

    lines = commit_message.splitlines()
    if len(lines) > 1 and lines[1].strip() != '':
        errors.append('Expected empty line on line 2 of the commit message')

    commit_msg = []
    for line_num, line in enumerate(lines):
        overflow_count = len(line) - GIT_MAX_WIDTH
        if overflow_count > 0:
            # TODO actually fix line width and carry overflow to next line.
            line_beginning = line[:line.find(' ', 20)]
            errors.append(
                'Line #{line_num} has {overflow_count} too many characters.'
                ' Line starts with: "{beginning}"...'.format(
                    line_num=line_num, overflow_count=overflow_count,
                    beginning=line_beginning))
        commit_msg.append(line)

    return '\n'.join(commit_msg), errors


def get_author(msg):
    """Grab 'Author:' line from commit_msg if present.

    return tuple of (author, updated_msg).
    """
    authpre = 'Author:'
    author = None

    for line in msg.splitlines():
        if line.startswith(authpre):
            author = line.partition(":")[2].strip()
    if author:
        msg = '\n'.join([l for l in msg.splitlines()
                         if not l.startswith(authpre)]) + '\n'
        msg.replace(r"\n\n\n\+", "\n\n")
        msg = msg.strip() + "\n"

    return author, msg


def _merge_and_commit_git(mp, args, git_dir, lp_handle):
    """Merge and commit a git repo.

    @param mp: merge proposal under review
    @param args: program args
    @param git_dir: directory to use to check out and do the merge
    @return: result code
    """
    logger.debug('mp.reviewed_revid is %s' % mp.reviewed_revid)
    source_revid, source_author, source_email = _git_source_info(mp)
    logger.debug('source hash is %s' % source_revid)
    if source_revid != mp.reviewed_revid:
        logger.debug('Unapproved changes made after approval. '
                     'Failing autolanding.')
        change_mp_status(mp, 'unapproved_changes',
                         args, LaunchpadVote.NEEDS_FIXING)
        return 1

    # create a git+ssh://user@git.launchpad.net/repo like url
    url = mp.target_git_repository.git_ssh_url.replace(
        'ssh://', 'ssh://%s@' % get_config_option('launchpad_login')
    )
    target = git.Repo.clone_from(url, git_dir, branch=get_target_branch(mp))
    source = target.create_remote(
        'source', mp.source_git_repository.git_https_url
    )
    source.fetch()

    logger.debug('Merging %s to %s' % (
        get_source_branch(mp), get_target_branch(mp)
    ))

    raw_msg = launchpadutils.get_commit_message(
            mp, args['use_description_for_commit']
        )

    # If the message sets the author, use that otherwise use commit
    author_string, message = get_author(raw_msg)
    if not author_string:
        author_string = '%s <%s>' % (source_author, source_email)

    # Lint the commit message
    message, errors = lint_commit_message(message)
    if errors:
        error_msg = 'Commit message lints:\n - ' + ' - '.join(errors)
        logger.debug(error_msg)
        mp.createComment(
            subject='Invalid Commit Message',
            content=error_msg,
            vote=LaunchpadVote.NEEDS_FIXING
        )
        mp.setStatus(status='Needs review', revid=mp.reviewed_revid)
        mp.lp_save()

        return 1

    if args['disable_squash']:
        target.git.merge('source/%s' % get_source_branch(mp))
        target.git.commit(amend=True, author=author_string, message=message)
    else:
        target.git.merge('source/%s' % get_source_branch(mp), squash=True)
        target.git.commit(all=True, author=author_string, message=message)

    target.git.push()

    logger.debug('New revision is: %s' % target.head.object.hexsha)

    mp.setStatus(status='Merged')

    logger.debug('Closing bugs')
    project = mp.target_git_repository.git_ssh_url.split('/')[-1]
    bug_comment = BUG_MESSAGE_UPSTREAM_COMMIT_TMPL.format(
        commitish=str(target.commit().hexsha)[:8],
        project=project,
        branch=get_target_branch(mp)
    )
    launchpadutils.close_project_bugs(lp_handle, project, message, bug_comment)

    return 0


def _merge_and_commit_bzr(mp, args):
    target = Branch.create(mp.target_branch, config=False, create_tree=True)
    source = Branch.create(mp.source_branch, config=False, target=target)
    revid_str = str(mp.reviewed_revid)
    approved = source.bzr_branch.revision_id_to_revno(revid_str)

    logger.debug('mp.reviewed_revid is %s' % mp.reviewed_revid)
    #if unapproved changes made after approval do not land
    if source.bzr_branch.revno() > approved:
        logger.debug('Unapproved changes made after approval. '
                     'Failing autolanding.')
        change_mp_status(mp, 'unapproved_changes',
                         args, LaunchpadVote.NEEDS_FIXING)
        return 1

    try:
        logger.debug('Merging %(source)s to %(target)s' % {
                     'source': str(mp.source_branch.bzr_identity),
                     'target': str(mp.target_branch.bzr_identity)})
        target.merge(source, str(mp.reviewed_revid))
        reviews = launchpadutils.get_reviews(mp)
        logger.debug('Commiting previous merge')
        commit_message = launchpadutils.get_commit_message(
            mp,
            args['use_description_for_commit'])
        msg = format_commit_message(mp, commit_message)
        target.commit(msg,
                      revprops={'merge_url': mp.web_link},
                      authors=source.authors,
                      reviews=reviews)
        revision = target.bzr_branch.revno()
        logger.debug('New revision is: ' + str(revision))
        logger.debug('Closing bugs')
        launchpadutils.close_bugs(mp)
        dputResult = True
        if args['ppa']:
            # try to extract the build job number from the build_job_url
            #its usually the last number in the URL
            jenkins_build_number = 0
            if args['build_job_url']:
                m = re.match('.*/([0-9]+)/?$', args['build_job_url'])
                if m:
                    jenkins_build_number = m.group(1)

            dputRunner = DputRunner(
                ppas=args['ppa'],
                trunk=target.branch_url,
                packagingBranch=args['packaging_branch'],
                revision=revision,
                DEBEMAIL=get_config_option('DEBEMAIL'),
                DEBFULLNAME=get_config_option('DEBFULLNAME'),
                distributions=args['ppa_distributions'],
                versionFormat=args['version_string_format'],
                jenkinsBuildNumber=jenkins_build_number,
                hooks=args['hooks'],
                release_only=args['release_only'])
            dputResult = dputRunner.dput()
        if not dputResult:
            logger.debug('Dput failed')
            return 1
    except BranchHasConflicts:
        logger.info('Merging failed - branch has conflicts')
        change_mp_status(mp, 'merge_failed', args,
                         LaunchpadVote.NEEDS_FIXING)
        return 1
    except TarmacMergeError:
        logger.info('Landing failed')
        change_mp_status(mp, 'landing_failed', args,
                         LaunchpadVote.NEEDS_FIXING)
        return 1
    except LockFailed:
        logger.info('Bazaar error: LockFailed. Jenkins user is maybe not ' +
                    'allowed to push into target?')
        change_mp_status(mp, 'landing_failed', args,
                         LaunchpadVote.NEEDS_FIXING)
        return 1
    finally:
        target.cleanup()

    return 0


def get_last_mp_vote(mp):
    for vote in mp.votes:
        launchpad_login = get_config_option('launchpad_login')
        if vote.registrant.name == launchpad_login and vote.comment:
            return vote.comment.vote
    return None


def autoland():
    parser = ArgumentParser(description="Start autolanding process " +
                            "resulting from a merge proposal.")
    parser.add_argument('-p', '--ppa',
                        help="comma separated list of PPAs where the " +
                             "merged result shuld be dput-ed",
                        action=StringSplitAction)
    parser.add_argument('-a', '--packaging-branch',
                        help="Packaging branch to be used for dput-ing")
    parser.add_argument('-d', '--ppa-distributions', default=None,
                        help="Comma separated list of distributions " +
                        "(e.g. precise,quantal)",
                        action=StringSplitAction)
    parser.add_argument('-o', '--hooks', default=[],
                        help="Comma separated list of hack hooks" +
                        "that should be used before dput " +
                        "(e.g. H40native_hack.py,H10autoreconf)",
                        action=StringSplitAction)
    parser.add_argument('-r', '--test-result', required=True,
                        help="Result of previous testing" +
                        "Autolanding will be stopped if this is FAILED")
    parser.add_argument('-v', '--revision', required=True,
                        help="Revision of source branch that should be merged")
    parser.add_argument('-s', '--source-branch',
                        help='Not used but kept for backwards compatibility.')
    parser.add_argument('-m', '--merge-proposal', required=True,
                        help="Merge proposal which is supposed to be merged")
    parser.add_argument('-b', '--build-job-url',
                        help="URL to the build/test job which was " +
                             "previously executed")
    parser.add_argument('-f', '--version-string-format',
                        help="Format string for the version which will be " +
                              "used to dput the package " +
                              "(only used when --ppa specified)",
                        action=StringStripAction)
    parser.add_argument('-u', '--use-description-for-commit',
                        default=False,
                        action='store_true',
                        help="Use the description field of merge proposal " +
                             "if no commit message is specified")
    parser.add_argument('-l', '--release-only',
                        default=False,
                        action='store_true',
                        help="Only do dch --release when dputing into PPA " +
                             "and don't modify the changlog in any other way")
    parser.add_argument('--disable-squash', action='store_true',
                        help='Disable squashing during a merge')

    args = vars(parser.parse_args())

    # launchpadlib is not thread/process safe so we are creating launchpadlib
    # cache in /tmp per process which gets cleaned up at the end
    # see also lp:459418 and lp:1025153
    launchpad_cachedir = os.path.join('/tmp',
                                      str(os.getpid()),
                                      '.launchpadlib')

    # `launchpad_cachedir` is leaked upon unexpected exits
    # adding this cleanup to stop directories filling up `/tmp/`
    atexit.register(rmtree, os.path.join('/tmp',
                                         str(os.getpid())),
                    ignore_errors=True)
    lp_handle = get_launchpad(launchpadlib_dir=launchpad_cachedir)
    mp = launchpadutils.get_mp_handle_from_url(lp_handle,
                                               args['merge_proposal'])

    #check for test_result and fail if previous testing failed
    if args['test_result'] == 'FAILED':
        logger.debug('Previous test failed. Failing autolanding.')
        change_mp_status(mp, 'landing_failed', args,
                         LaunchpadVote.NEEDS_FIXING)
        return 0

    #check for commit message and fail if it is empty
    #if --use-description-for-commit is specified fallback for description
    # and if description is not set then fail
    if not (launchpadutils.get_commit_message(
            mp, args['use_description_for_commit'])):
        logger.debug('Commit message empty. Failing autolanding.')
        change_mp_status(mp, 'commit_message_empty',
                         args, LaunchpadVote.NEEDS_FIXING)
        return 1

    # Approve the merge proposal in case the last jenkins vote was
    # different from Approve (this includes no vote)
    if get_last_mp_vote(mp) != LaunchpadVote.APPROVE:
        mp.createComment(
            review_type=get_config_option('launchpad_review_type'),
            vote=LaunchpadVote.APPROVE,
            subject=get_vote_subject(mp))

    # merging starts here
    ret = merge_and_commit(mp, args, lp_handle)
    return ret