-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbug.py
More file actions
executable file
·260 lines (232 loc) · 10.7 KB
/
Copy pathbug.py
File metadata and controls
executable file
·260 lines (232 loc) · 10.7 KB
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
#!/usr/bin/env python3
import sys
import click
from client import get_thread, get_bug_summary, post_comment, search_bugs, update_bug, get_activity, get_comments, parse_since, resolve_me
from render import render_thread, render_search_results, render_activity
# Allow `bug <id>` as a shorthand for `bug show <id>`
if len(sys.argv) > 1 and sys.argv[1].lstrip("-").isdigit():
sys.argv.insert(1, "show")
@click.group()
def cli():
"""Bugzilla command line interface.
\b
View a bug:
bug <id>
Post a comment:
bug comment <id> -m "your text"
"""
@cli.command("show")
@click.argument("bug_id", type=int)
@click.option("--summary", is_flag=True, help="Print only the bug summary")
def show_bug(bug_id, summary):
"""Display a bug thread."""
try:
if summary:
click.echo(f"Bug {bug_id} - {get_bug_summary(bug_id)}")
return
thread = get_thread(bug_id)
except Exception as e:
click.echo(f"Error: {e}", err=True)
sys.exit(1)
output = render_thread(thread)
if sys.stdout.isatty():
click.echo_via_pager(output)
else:
click.echo(output)
@cli.command("comment")
@click.argument("bug_id", type=int)
@click.option("-m", "--message", default=None, help="Comment text (opens editor if omitted)")
def add_comment(bug_id, message):
"""Post a comment on a bug."""
if not message:
message = click.edit()
if not message or not message.strip():
click.echo("Aborted: empty message.", err=True)
sys.exit(1)
try:
comment_id = post_comment(bug_id, message)
click.echo(f"Comment {comment_id} posted on bug {bug_id}.")
except Exception as e:
click.echo(f"Error: {e}", err=True)
sys.exit(1)
@cli.command("search")
@click.option("--assigned-to", default=None, help="Assignee email (or 'me')")
@click.option("--reporter", default=None, help="Reporter email (or 'me')")
@click.option("--cc", default=None, help="CC email (or 'me')")
@click.option("--product", default=None, help="Product name")
@click.option("--component", default=None, help="Component name")
@click.option("--status", multiple=True, help="Status (repeatable, e.g. --status NEW --status ASSIGNED)")
@click.option("--priority", multiple=True, help="Priority (repeatable, e.g. --priority P1 --priority P2)")
@click.option("--severity", multiple=True, help="Severity (repeatable)")
@click.option("--milestone", default=None, help="Target milestone")
@click.option("--text", default=None, help="Free-text search (quicksearch)")
@click.option("--mentions", default=None, help="Word or phrase appearing in any comment or description")
@click.option("--commenter", default=None, help="Bugs where this person has commented (or 'me')")
@click.option("--since", default=None, metavar="DATE", help="Bugs active after this date (YYYY-MM-DD)")
@click.option("--until", default=None, metavar="DATE", help="Bugs not touched after this date (YYYY-MM-DD)")
@click.option("--limit", default=0, show_default=True, help="Max results (0 = no limit)")
@click.option("--format", "fmt", default="table", show_default=True,
type=click.Choice(["table", "ids", "json"], case_sensitive=False),
help="Output format")
def search(assigned_to, reporter, cc, product, component, status, priority, severity, milestone, text, mentions, commenter, since, until, limit, fmt):
"""Search for bugs."""
try:
bugs = search_bugs(
assigned_to=assigned_to,
reporter=reporter,
cc=cc,
product=product,
component=component,
status=status,
priority=priority,
severity=severity,
target_milestone=milestone,
text=text,
mentions=mentions,
commenter=commenter,
changed_after=since,
changed_before=until,
limit=limit,
)
except Exception as e:
click.echo(f"Error: {e}", err=True)
sys.exit(1)
click.echo(render_search_results(bugs, fmt=fmt))
@cli.command("activity")
@click.option("--since", default="24h", show_default=True, metavar="TIMESPEC",
help="Show comments since this point (e.g. 24h, 2d, 1w, or YYYY-MM-DD)")
@click.option("--product", default=None, help="Filter by product")
@click.option("--component", default=None, help="Filter by component")
@click.option("--limit", default=0, show_default=True, help="Max bugs to scan (0 = no limit)")
@click.option("--format", "fmt", default="table", show_default=True,
type=click.Choice(["table", "json"], case_sensitive=False),
help="Output format")
@click.option("--output", "outfile", default=None, metavar="FILE",
help="Write output to a file instead of stdout")
def activity(since, product, component, limit, fmt, outfile):
"""Show recent comments across the tracker."""
try:
since_dt = parse_since(since)
items = get_activity(since_dt, product=product, component=component, limit=limit)
except Exception as e:
click.echo(f"Error: {e}", err=True)
sys.exit(1)
output = render_activity(items, since_dt, fmt=fmt)
if outfile:
with open(outfile, "w") as f:
f.write(output + "\n")
click.echo(f"Written to {outfile}", err=True)
elif sys.stdout.isatty():
click.echo_via_pager(output)
else:
click.echo(output)
@cli.command("comments")
@click.option("--author", default=None, help="Only show comments by this person (or 'me')")
@click.option("--since", default="24h", show_default=True, metavar="TIMESPEC",
help="Comments created after this point (e.g. 24h, 2d, 1w, or YYYY-MM-DD)")
@click.option("--until", default=None, metavar="TIMESPEC",
help="Comments created before this point (e.g. 2026-05-29)")
@click.option("--product", default=None, help="Filter by product")
@click.option("--component", default=None, help="Filter by component")
@click.option("--limit", default=0, show_default=True, help="Max bugs to scan (0 = no limit)")
@click.option("--format", "fmt", default="table", show_default=True,
type=click.Choice(["table", "json"], case_sensitive=False),
help="Output format")
@click.option("--output", "outfile", default=None, metavar="FILE",
help="Write output to a file instead of stdout")
def comments(author, since, until, product, component, limit, fmt, outfile):
"""Show comments filtered by author and time window."""
try:
since_dt = parse_since(since)
until_dt = parse_since(until) if until else None
resolved_author = resolve_me(author) if author else None
items = get_comments(since_dt, until_dt=until_dt, author=resolved_author, product=product, component=component, limit=limit)
except Exception as e:
click.echo(f"Error: {e}", err=True)
sys.exit(1)
output = render_activity(items, since_dt, fmt=fmt)
if outfile:
with open(outfile, "w") as f:
f.write(output + "\n")
click.echo(f"Written to {outfile}", err=True)
elif sys.stdout.isatty():
click.echo_via_pager(output)
else:
click.echo(output)
@cli.command("edit")
@click.argument("bug_id", type=int)
@click.option("--status", default=None, help="New status (e.g. ASSIGNED, RESOLVED)")
@click.option("--resolution", default=None, help="Resolution (e.g. FIXED, WONTFIX) — required when setting status to RESOLVED")
@click.option("--priority", default=None, help="Priority (e.g. P1, P2)")
@click.option("--severity", default=None, help="Severity (e.g. critical, major, normal)")
@click.option("--assigned-to", default=None, help="Assignee email (or 'me')")
@click.option("--product", default=None, help="Product name")
@click.option("--component", default=None, help="Component name")
@click.option("--milestone", default=None, help="Target milestone")
@click.option("--cc", multiple=True, help="Add to CC (repeatable, supports 'me')")
@click.option("--cc-remove", multiple=True, help="Remove from CC (repeatable, supports 'me')")
@click.option("-m", "--comment", default=None, help="Comment to attach alongside the edit")
def edit_bug(bug_id, status, resolution, priority, severity, assigned_to, product, component, milestone, cc, cc_remove, comment):
"""Edit bug fields."""
if not any([status, resolution, priority, severity, assigned_to, product, component, milestone, cc, cc_remove, comment]):
click.echo("No fields specified. Use --help to see available options.", err=True)
sys.exit(1)
try:
update_bug(
bug_id,
status=status,
resolution=resolution,
priority=priority,
severity=severity,
assigned_to=assigned_to,
product=product,
component=component,
target_milestone=milestone,
cc_add=cc,
cc_remove=cc_remove,
comment=comment,
)
except Exception as e:
click.echo(f"Error: {e}", err=True)
sys.exit(1)
click.echo(f"Bug {bug_id} updated.")
@cli.command("watch")
@click.option("--since", default=None, metavar="TIMESPEC",
help="Look back on startup (e.g. 15m, 1h). Default: now")
@click.option("--interval", default=120, show_default=True, metavar="SECONDS",
help="Poll interval in seconds")
@click.option("--product", default=None, help="Filter by product")
@click.option("--component", default=None, help="Filter by component")
def watch_activity(since, interval, product, component):
"""Watch for new comments, polling every INTERVAL seconds."""
import time
from datetime import datetime, timezone
if not sys.stdout.isatty():
click.echo("Error: 'watch' requires a TTY.", err=True)
sys.exit(1)
try:
last_poll = parse_since(since) if since else datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
except ValueError as e:
click.echo(f"Error: {e}", err=True)
sys.exit(1)
click.echo(f"Watching for activity since {last_poll}. Ctrl+C to stop.\n")
try:
while True:
fetch_start = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
click.echo("\rpolling...", nl=False)
try:
items = get_activity(last_poll, product=product, component=component)
except Exception as e:
click.echo(f"\rError: {e}", err=True)
time.sleep(interval)
continue
since_label = last_poll
last_poll = fetch_start
if items:
click.echo("\r ") # clear "polling..." line
click.echo(render_activity(items, since_label))
time.sleep(interval)
except KeyboardInterrupt:
click.echo("\rStopped.")
if __name__ == "__main__":
cli()