-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
206 lines (161 loc) · 6.57 KB
/
main.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
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
import configparser
import html
import webbrowser
from datetime import datetime
import feedparser
import rumps
from __about__ import __version__
APP_NAME = "Merge Requests Monitor"
VERSION = __version__
DESCRIPTION = "A System Tray app that monitors your merge requests and let you access them quickly."
ICON_PATH = "media/icon.png"
DEFAULT_REFRESH_INTERVAL = "5m"
DEFAULT_FEED_URL = "https://gitlab.com/<username>/<repo>/-/merge_requests.atom?feed_token=<token>&state=opened"
class MergeRequestsMonitorApp(rumps.App):
def __init__(self):
super().__init__(
name=APP_NAME,
title="0",
quit_button=None,
icon=ICON_PATH,
template=True,
)
# initialize default variables & loan config values
self.last_updated = "Never"
self.merge_requests = []
config = self.get_or_create_config()
self.refresh_interval_label = config["refresh_interval"]
self.feed_url = config["feed"]
# make this app do what it must do!
self.build_menu()
self.update_title()
self.start_timer()
def update_title(self):
self.title = f"{len(self.merge_requests)}"
def build_menu(self):
self.menu.clear()
self.menu.add(rumps.MenuItem(f"Last updated: {self.last_updated}"))
refresh_menu = rumps.MenuItem(
f"Refresh Interval: {self.refresh_interval_label}",
callback=self.set_refresh_interval,
)
for freq in ["60s", "5m", "10m", "30m", "1h", "3h", "6h"]:
refresh_menu.add(rumps.MenuItem(freq, callback=self.set_refresh_interval))
self.menu.add(refresh_menu)
self.menu.add(rumps.rumps.SeparatorMenuItem())
if len(self.merge_requests) == 0:
self.menu.add(rumps.MenuItem("No pending MRs"))
else:
draft_merge_requests = [mr for mr in self.merge_requests if "Draft: " in mr.title]
merge_requests = [mr for mr in self.merge_requests if "Draft: " not in mr.title]
if len(merge_requests) > 0:
# This acts as section title
self.menu.add(rumps.MenuItem("Merge Requests"))
for merge_request in merge_requests:
title = html.unescape(merge_request.title)
self.menu.add(rumps.MenuItem(title, callback=self.open_url))
if len(merge_requests) > 0 and len(draft_merge_requests) > 0:
self.menu.add(rumps.rumps.SeparatorMenuItem())
if len(draft_merge_requests) > 0:
# This acts as section title
self.menu.add(rumps.MenuItem("Draft Merge Requests"))
for merge_request in draft_merge_requests:
title = html.unescape(merge_request.title)
self.menu.add(rumps.MenuItem(title, callback=self.open_url))
self.menu.add(rumps.rumps.SeparatorMenuItem())
self.menu.add(rumps.MenuItem("Preferences", callback=self.set_preferences))
self.menu.add(rumps.MenuItem("About", callback=self.about))
self.menu.add(rumps.MenuItem("Quit", key="q", callback=self.quit_application))
def start_timer(self):
freq_interval = self.get_refresh_interval(self.refresh_interval_label)
self.timer = rumps.Timer(self.refresh, freq_interval)
self.timer.start()
def save_config(self):
with self.open("config.ini", "w") as f:
config = configparser.ConfigParser()
config["Gitlab"] = {
"feed": self.feed_url,
"refresh_interval": self.refresh_interval_label,
}
config.write(f)
def get_or_create_config(self):
def _get_config():
with self.open("config.ini") as f:
config.read_file(f)
return config["Gitlab"]
config = configparser.ConfigParser()
try:
return _get_config()
except FileNotFoundError:
with self.open("config.ini", "w") as f:
config["Gitlab"] = {
"feed": DEFAULT_FEED_URL,
"refresh_interval": DEFAULT_REFRESH_INTERVAL,
}
config.write(f)
return _get_config()
def get_refresh_interval(self, label):
return {
"60s": 60,
"5m": 60 * 5,
"10m": 60 * 10,
"30m": 60 * 30,
"1h": 60 * 60,
"3h": 60 * 60 * 3,
"6h": 60 * 60 * 12,
}[label]
def refresh(self, sender):
# retrieve the feed entries
document = feedparser.parse(self.feed_url)
if document.bozo:
self.title = "⚠️"
else:
self.merge_requests = document.entries
self.last_updated = datetime.now().strftime("%H:%M")
# rebuild the menu
self.build_menu()
self.update_title()
@rumps.clicked("Preferences")
def set_preferences(self, sender):
response = rumps.Window(
title="Set Preferences",
message="Enter your Gitlab's merge requests feed URL:",
default_text=self.feed_url,
ok="Save",
cancel="Cancel",
).run()
if response.clicked:
self.feed_url = response.text
self.save_config()
self.refresh(None)
@rumps.clicked("Quit")
def quit_application(self, sender=None):
self.timer.stop()
rumps.quit_application(sender)
def open_url(self, sender):
for merge_req in self.merge_requests:
if html.unescape(merge_req.title) == sender.title:
webbrowser.open_new_tab(merge_req.link)
def set_refresh_interval(self, sender):
sender.state = 1 # set the selected item as checked
refresh_interval_menu = self.menu.values()[0]
print(refresh_interval_menu.__dict__)
self.refresh_interval = self.get_refresh_interval(sender.title)
self.timer.stop()
self.timer.interval = self.refresh_interval
self.timer.start()
self.refresh_interval_label = sender.title
refresh_interval_menu.title = f"Refresh Interval: {self.refresh_interval_label}"
self.save_config()
@rumps.clicked("About")
def about(self, _):
rumps.alert(
f"{APP_NAME}\n",
f"{DESCRIPTION}\n\n"
f"Version {VERSION}\n\n"
"Author: Matias Agustin Mendez <matagus@gmail.com>\n\n"
"https://github.com/matagus/merge-requests-monitor",
icon_path=ICON_PATH,
)
if __name__ == "__main__":
MergeRequestsMonitorApp().run()