~jenkaas-hackers/jenkins-launchpad-plugin/trunk

« back to all changes in this revision

Viewing changes to jlp/commands/voteOnMergeProposal.py

Adjust messaging to launchpad to use jinja2 templates.

Enable a user to customize the messages sent to launchpad based on results.
Adds the run requirement of Jinja2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from argparse import ArgumentParser
2
2
import atexit
3
 
from jlp import launchpadutils, get_launchpad, logger
 
3
import jinja2
 
4
from jlp import launchpadutils, get_launchpad, logger, get_config_option, get_jinja_environment
4
5
import re
5
6
import os
6
7
from shutil import rmtree
19
20
                        help="URL of the merge proposal to update")
20
21
    parser.add_argument('-i', '--skip-message', action='store_true',
21
22
                        help="Skip checking if a commit message is set")
 
23
    parser.add_argument('-t', '--template-dir',
 
24
                        help='Optional directory of alternate jinja2 templates.',
 
25
                        default=None)
22
26
    args = vars(parser.parse_args())
23
27
 
24
28
    # launchpadlib is not thread/process safe so we are creating launchpadlib
41
45
 
42
46
    # this is the status from tests
43
47
    overal_status = args['status']
44
 
    # by default reason is empty as it is usually just a failed build
45
 
    reason = ''
46
48
 
47
49
    #override the status to FAILED and set reason if no commit message is set
48
50
    match = re.match('^(.*)/job/([^/]*)/.*$', args['build_url'])
 
51
    template = 'ci_test.j2'
49
52
    if args['skip_message']:
50
53
        logger.debug('Skipping check for empty commit message')
51
54
    elif match:
54
57
        if not launchpadutils.is_commit_message_set(mp,
55
58
                                                    jenkins_job, jenkins_url):
56
59
            overal_status = 'FAILED'
57
 
            reason = "No commit message was specified in the merge " + \
58
 
                     "proposal. Click on the following link and set the " + \
59
 
                     "commit message (if you want a jenkins rebuild you " +\
60
 
                     "need to trigger it yourself):\n" + \
61
 
                     args['merge_proposal'] + "/+edit-commit-message\n"
 
60
            template = 'no_commit.j2'
62
61
            logger.debug('Commit message not set. Failing CI.')
63
62
    else:
64
63
        logger.debug('Unable to get job name from build_url.' +
65
64
                     'Skipping check for empty commit message')
66
65
 
67
 
    if overal_status == 'PASSED':
68
 
        launchpadutils.approve_mp(mp, args['revision'], args['build_url'])
69
 
    else:  # status == False corresponds to NOT 'PASSED'
70
 
        launchpadutils.disapprove_mp(mp,
71
 
                                     args['revision'],
72
 
                                     args['build_url'],
73
 
                                     reason)
 
66
    template_args = launchpadutils.get_template_args(
 
67
        overal_status,
 
68
        args['revision'],
 
69
        args['build_url'],
 
70
        args['merge_proposal']
 
71
    )
 
72
    env = get_jinja_environment(args['template_dir'])
 
73
    real_template = env.get_template(template)
 
74
    msg = real_template.render(args=template_args)
 
75
    if overal_status != 'PASSED':
 
76
        launchpadutils.report_to_launchpad(
 
77
            mp,
 
78
            msg,
 
79
            args['revision'],
 
80
            vote=launchpadutils.LaunchpadVote.NEEDS_FIXING
 
81
        )
 
82
    else:
 
83
        launchpadutils.report_to_launchpad(
 
84
            mp,
 
85
            msg,
 
86
            args['revision'],
 
87
            vote=launchpadutils.LaunchpadVote.APPROVE
 
88
        )
74
89
 
75
90
    return 0