~fginther/jenkins-launchpad-plugin/getPackageVersion-dev

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
#!/usr/bin/env python
import sys
from argparse import ArgumentParser
from settings import LOCK_FILE, logger
from lockfile import FileLock
from launchpad import LaunchpadTrigger


class UpdateMergeProposalException(Exception):
    pass


def build_state(args):
    """Return a tuple containing merge proposal state, message, and
    subject."""

    if 'merge_failed' in args and args['merge_failed']:
        return ('Needs review',
                "FAILED: Autolanding.\n" +
                "Merging failed. More details in the following jenkins job:\n",
                "Re: [Merge] autolanding failed (merge failed)")
    elif 'push_failed' in args and args['push_failed']:
        return ('Needs review',
                "FAILED: Autolanding.\n" +
                "Pushing failed. More details in the following jenkins job:\n",
                "Re: [Merge] autolanding failed (push failed)")
    elif 'landing_failed' in args and args['landing_failed']:
        return ('Needs review',
                "FAILED: Autolanding.\n" +
                "More details in the following jenkins job:\n",
                "Re: [Merge] autolanding failed")
    elif 'merged' in args and args['merged']:
        return ('Merged',
                "PASSED: Autolanding.\nBranch merged.",
                "Re: [Merge] branch merged")
    elif 'commit_message_empty' in args and args['commit_message_empty']:
        return ('Needs review',
                "FAILED: Autolanding.\nNo commit message was specified.\n",
                "Re: [Merge] autolanding failed",)
    elif 'unapproved_changes' in args and args['unapproved_changes']:
        return ('Needs review',
                "FAILED: Autolanding.\n" +
                "Unapproved changes made after approval.\n",
                "Re: [Merge] autolanding failed",)
    else:
        raise UpdateMergeProposalException("unrecognized parameters")


def main():

    parser = ArgumentParser(description="Update a Launchpad merge proposal.")
    parser.add_argument('-j', '--job-url',
                        help="URL of the Jenkins job")
    parser.add_argument('-b', '--branch',
                        help="Launchpad branch proposed for merging")
    parser.add_argument('-o', '--vote',
                        help="merge proposal vote")
    parser.add_argument('-m', '--merge-proposal', required=True,
                        help="URL of the merge proposal to update")
    parser.add_argument('-v', '--revision', required=True,
                        help="merge proposal candidate revision")
    parser.add_argument('-e', '--merge-failed',
                        action='store_true',
                        help="report that the merge failed")
    parser.add_argument('-f', '--push-failed',
                        action='store_true',
                        help="report that the push failed")
    parser.add_argument('-l', '--landing-failed',
                        action='store_true',
                        help="report that autolanding failed")
    parser.add_argument('-r', '--merged',
                        action='store_true',
                        help="report that the merge proposal was merged")
    parser.add_argument('-c', '--commit-message-empty',
                        action='store_true',
                        help="report that the commit message was left empty")
    parser.add_argument('-u', '--unapproved-changes',
                        action='store_true',
                        help="report that the commit message was left empty")
    args = vars(parser.parse_args())

    launchpad = LaunchpadTrigger()

    try:
        mp_state, message, subject = build_state(args)
    except UpdateMergeProposalException:
        parser.error("Error: you must specify one of " +
                     "(--merge-failed, --push-failed, " +
                     "--merged, --commit-message-empty)")
    if not args['branch']:
        #if branch was not specified try to get it from merge proposal
        args['branch'] = launchpad.get_branch_from_mp(args['merge_proposal'])
        if not args['branch']:
            parser.error('Error: unable to get branch from merge proposal.\n' +
                            'Please specify manually using --branch.')
    mp = launchpad.get_merge_proposal(args['branch'], args['merge_proposal'])
    if not mp:
        parser.error('Error: merge proposal relted ' +
                      'to this branch was not found')

    message = launchpad.format_message_for_mp_update(args['job_url'], message) 
    launchpad.change_mp_status(mp, mp_state, message, subject,
                               args['revision'], args['vote'])
    return 0


if __name__ == "__main__":
    # this is here because launchpadlib is not thread safe (yet)
    # see also lp:459418 and lp:1025153
    logger.debug('Going to acquire launchpad lock for updateMergeProposal')
    with FileLock(LOCK_FILE):
        logger.debug('Lock acquired for updateMergeProposal')
        sys.exit(main())