-
Notifications
You must be signed in to change notification settings - Fork 9
/
lpmpmessage.py
151 lines (124 loc) · 5.13 KB
/
lpmpmessage.py
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
#!/usr/bin/env python
"""
lpmpmessage script will display the formatted merge commit message for the
chosen merge proposal
lpmpmessage depends on ``launchpadlib``, which isn't
necessarily up-to-date in PyPI, so we install it from the archive::
`sudo apt-get install python-launchpadlib` OR
`sudo apt-get install python3-launchpadlib` OR
As we're using ``launchpadlib`` from the archive (which is therefore
installed in the system), you'll need to create your virtualenvs
with the ``--system-site-packages`` option.
Activate your virtualenv and install the requirements::
`pip install -r requirements.txt`
"""
import click
import urwid
from lpshipit import (
build_commit_msg,
_format_git_branch_name,
_get_launchpad_client,
_set_urwid_widget,
)
# Global var to store the chosen MP's commit message
MP_MESSAGE_OUTPUT = None
def summarize_all_mps(mps):
mp_content = []
for mp in mps:
review_vote_parts = []
approval_count = 0
for vote in mp.votes:
if not vote.is_pending:
review_vote_parts.append(vote.reviewer.name)
if vote.comment.vote == 'Approve':
approval_count += 1
description = '' if not mp.description else mp.description
commit_message = description if not mp.commit_message \
else mp.commit_message
short_commit_message = '' if not commit_message \
else commit_message.splitlines()[0]
if getattr(mp, 'source_git_repository', None):
source_repo = '{}/'.format(mp.source_git_repository.display_name)
target_repo = '{}/'.format(mp.target_git_repository.display_name)
source_branch = _format_git_branch_name(mp.source_git_path)
target_branch = _format_git_branch_name(mp.target_git_path)
else:
source_repo = ''
target_repo = ''
source_branch = mp.source_branch.display_name
target_branch = mp.target_branch.display_name
mp_summary = {
'author': mp.registrant.name,
'commit_message': commit_message,
'short_commit_message': short_commit_message,
'reviewers': sorted(review_vote_parts),
'approval_count': approval_count,
'web': mp.web_link,
'target_branch': target_branch,
'source_branch': source_branch,
'target_repo': target_repo,
'source_repo': source_repo,
'date_created': mp.date_created
}
summary = "{source_repo}{source_branch}" \
"\n->{target_repo}{target_branch}" \
"\n {short_commit_message}" \
"\n {approval_count} approvals ({str_reviewers})" \
"\n {date_created} - {web}" \
.format(**mp_summary, str_reviewers=","
.join(mp_summary['reviewers']))
mp_summary['summary'] = summary
mp_content.append(mp_summary)
sorted_mps = sorted(mp_content,
key=lambda k: k['date_created'],
reverse=True)
return sorted_mps
@click.command()
@click.option('--mp-owner', help='LP username of the owner of the MP '
'(Defaults to system configured user)',
default=None)
@click.option('--debug/--no-debug', default=False)
def lpmpmessage(mp_owner, debug):
lp = _get_launchpad_client()
lp_user = lp.me
print('Retrieving Merge Proposals from Launchpad...')
person = lp.people[lp_user.name if mp_owner is None else mp_owner]
mps = person.getMergeProposals(status=['Needs review', 'Approved'])
if debug:
print('Debug: Launchad returned {} merge proposals'.format(len(mps)))
mp_summaries = summarize_all_mps(mps)
if mp_summaries:
def urwid_exit_on_q(key):
if key in ('q', 'Q'):
raise urwid.ExitMainLoop()
def mp_chosen(button, chosen_mp):
global MP_MESSAGE_OUTPUT
MP_MESSAGE_OUTPUT = build_commit_msg(
author=chosen_mp['author'],
reviewers=",".join(
chosen_mp['reviewers']),
source_branch=chosen_mp['source_branch'],
target_branch=chosen_mp['target_branch'],
commit_message=chosen_mp[
'commit_message'],
mp_web_link=chosen_mp['web']
)
raise urwid.ExitMainLoop()
listwalker = urwid.SimpleFocusListWalker(list())
listwalker.append(urwid.Text(u'Merge Proposal to Merge'))
listwalker.append(urwid.Divider())
for mp in mp_summaries:
button = urwid.Button(mp['summary'])
urwid.connect_signal(button, 'click', mp_chosen, mp)
listwalker.append(button)
mp_box = urwid.ListBox(listwalker)
try:
_set_urwid_widget(mp_box, urwid_exit_on_q)
finally:
if MP_MESSAGE_OUTPUT:
print(MP_MESSAGE_OUTPUT)
else:
print("You have no Merge Proposals in either "
"'Needs review' or 'Approved' state")
if __name__ == "__main__":
lpmpmessage()