-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpg
More file actions
executable file
·1040 lines (904 loc) · 32.3 KB
/
pg
File metadata and controls
executable file
·1040 lines (904 loc) · 32.3 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
from __future__ import print_function
from __future__ import absolute_import
import click
import dateutil.parser
import os
import pdb
import pprint
import pygit2
import re
import requests
import subprocess
import sys
from tabulate import tabulate
import time
import traceback
import six.moves.urllib.request, six.moves.urllib.parse, six.moves.urllib.error
import six.moves.urllib.parse
import six
BASEURL = 'https://pagure.io'
if six.PY2:
# XXX - encoding hack
reload(sys)
sys.setdefaultencoding("utf-8")
def pcall(path, query=None, api=0, baseurl=BASEURL):
url = "%s/api/%s/%s" % (baseurl, api, path)
if isinstance(query, six.string_types):
url = "%s?%s" % (url, query)
elif query is not None:
query = six.moves.urllib.parse.urlencode(query, True)
url = "%s?%s" % (url, query)
r = requests.get(url)
try:
data = r.json()
except Exception:
print("Server returned bad value")
print(r)
raise
if 'error_code' in data:
s = "%(error_code)s: %(error)s" % data
raise Exception(s)
return data
def all_pages(path, query=None, api=0, baseurl=BASEURL):
'''Query all pages and combine results'''
if query is None:
query = {}
query['page'] = 1
query['per_page'] = 100
data = {}
while True:
page = pcall(path, query, api, baseurl)
for key in page:
if isinstance(page[key], list):
# XXX should we worry about dups here?
data.setdefault(key, []).extend(page[key])
if page['pagination']['pages'] <= query['page']:
break
query['page'] += 1
return data
@click.group()
def pg():
# main command
pass
@pg.group()
def pr():
pass
def get_repo(cwd=None):
if cwd is None:
cwd = os.getcwd()
try:
path = pygit2.discover_repository(cwd)
except KeyError:
return None
return pygit2.Repository(path)
def get_project(cwd=None):
repo = get_repo(cwd)
remote = pick_pagure_remote(repo)
if remote is None:
return None
try:
info = parse_pagure_url(remote.url)
except ValueError:
return None
return info['project']
def pick_pagure_remote(repo):
choices = {}
for remote in repo.remotes:
url = remote.url
if 'pagure.io' in url: # XXX should be smarter
choices[remote.name] = remote
elif remote.name == 'pagure':
choices[remote.name] = remote
if 'origin' in choices:
return choices['origin']
elif choices:
return list(choices.values())[0]
else:
return None
def parse_pagure_url(url):
"""Parse a pagure git url"""
ret = {}
info = six.moves.urllib.parse.urlparse(url)
ret['info'] = info
parts = info.path.split('/')
parts = [p for p in parts if p]
if parts[0] == 'forks':
ret['parent'] = parts[1]
parts = parts[2:]
if len(parts) > 1:
ret['namespace'] = parts[0]
parts = parts[1:]
if len(parts) > 1:
raise ValueError('Too many url path components: %s' % url)
project = parts[0]
if project.endswith('.git'):
project = project[:-4]
ret['project'] = project
return ret
@pr.command('info')
@click.argument('prid')
@click.option('-p', '--project')
@click.option('--json/--no-json')
@click.option('--flags/--no-flags')
def prinfo(prid, project, **opts):
if project is None:
project = get_project()
if project is None:
raise Exception('Please specify a project')
pr = pcall("%s/pull-request/%s" % (project, prid))
if opts['flags']:
flags = pcall("%s/pull-request/%s/flag" % (project, prid))
else:
flags = None
if opts['json']:
pprint.pprint(pr)
if flags:
print('Flags:')
pprint.pprint(flags)
return
print("PR#%(id)i: %(title)s" % pr)
print("https://pagure.io/{project[name]}/pull-request/{id}".format(**pr))
print("Status: %(status)s" % pr)
print("User: %s" % pr['user']['name'])
print("Created: %s" % time.asctime(time.localtime(float(pr['date_created']))))
print("Updated: %s" % time.asctime(time.localtime(float(pr['updated_on']))))
if pr.get('repo_from'):
# should we use fullname field?
print("Fork: %s/%s" % (BASEURL, pr['repo_from']['fullname']))
elif pr.get('remote_git'):
print("Remote: %(remote_git)s" % pr)
else:
print("Can't determine repo?")
sys.exit(1)
print("%(branch_from)s -> %(branch)s" % pr)
if pr['status'] != 'Open':
print('...')
if flags:
print("Flags:")
for flag in flags['flags']:
# web shows: username, comment, status/percent, date_updated
flag['_tstr'] = time.asctime(time.localtime(float(flag['date_updated'])))
print("%(_tstr)s %(username)s\t" % flag)
print(" %(comment)s" % flag)
print(" %(status)s(%(percent)s%%)" % flag)
print()
print("Description:")
print(pr['initial_comment'])
for comment in pr['comments']:
print()
cts = float(comment['date_created'])
comment['_username'] = comment['user']['name']
comment['_tstr'] = time.asctime(time.localtime(float(cts)))
print("Comment by %(_username)s on %(_tstr)s:" % comment)
print()
print(comment['comment'])
print()
@pr.command('fetchall')
def pr_fetchall():
# fetch = +refs/pull/*/head:refs/remotes/origin/pull/*
repo = get_repo()
remote = pick_pagure_remote(repo)
name = remote.name
cmd = ['git', 'fetch', name, '+refs/pull/*/head:refs/pagure-pull/%s/*' % name]
subprocess.check_call(cmd)
def do_fetch_pr(remote, prid):
"""fetch the pr ref (not to a branch)"""
src_ref = 'refs/pull/%s/head' % prid
dst_ref = 'refs/pagure-pull/%s/%s' % (remote.name, prid)
# note: dst_ref should match fetchall command
refspec = '+%s:%s' % (src_ref, dst_ref)
cmd = ['git', 'fetch', remote.name, refspec]
subprocess.check_call(cmd)
return dst_ref
@pr.command('fetch')
@click.argument('prid')
def pr_fetch(prid):
"""Just fetch the given pull request"""
# figure out repo and project info
repo = get_repo()
remote = pick_pagure_remote(repo)
if remote is None:
raise Exception('Cannot determine pagure remote')
dst_ref = do_fetch_pr(remote, prid)
print("PR #%s fetched to %s" % (prid, dst_ref))
@pr.command('checkout')
@click.argument('prid')
@click.option('--local/--no-local', default=True)
@click.option('--reset/--no-reset', default=False,
help='reset branch to PR if it has deviated')
def pr_checkout(prid, **opts):
"""Checkout the given pull request"""
# figure out repo and project info
repo = get_repo()
remote = pick_pagure_remote(repo)
if remote is None:
raise Exception('Cannot determine pagure remote')
try:
info = parse_pagure_url(remote.url)
except ValueError:
raise Exception('Cannot determine determine project')
project = info['project']
# get the pr info
pr = pcall("%s/pull-request/%s" % (project, prid))
url, remote_branch = get_pr_source(pr)
# Note if branch matches last ref before we re-fetch
pr_ref = 'refs/pagure-pull/%s/%s' % (remote.name, prid)
try:
last_ref = repo.lookup_reference(pr_ref).resolve().target
except KeyError:
# we might not have fetch this before
last_ref = None
# fetch the pr ref (not to a branch yet)
dst_ref = do_fetch_pr(remote, prid)
# find or make our branch
local_brs = repo.listall_branches(pygit2.GIT_BRANCH_LOCAL)
for _case in [1]:
# see if our specially named branch already exists
def_branch = "pagure/pr/%s" % prid
if def_branch in local_brs:
branch = def_branch
print("Reusing branch: %s" % branch)
break
# see if we have the original branch
# XXX: really need a better check here
# unrelated branches could easily have overlapping names
if (opts['local']
and remote_branch != 'master' # why do people do this?
and remote_branch in local_brs):
branch = remote_branch
print("Reusing local branch: %s" % branch)
break
# otherwise make our own
branch = def_branch
cmd = ['git', 'branch', branch, dst_ref]
subprocess.check_call(cmd)
print("New branch: %s" % branch)
# check out, unless we're already on it
fullref = 'refs/heads/%s' % branch
if repo.head.name == fullref:
print("Already on branch %s" % branch)
else:
cmd = ['git', 'checkout', branch]
subprocess.check_call(cmd)
# now that we are checked out, see if we need updating
obj = repo.get(pr['commit_stop'])
ref = repo.head
safe_reset = False
if ref.target == obj.id:
print("PR branch %s is up to date" % branch)
elif contains(obj, peel_ref(ref)):
# we're behind
print("Fast-forwarding...")
cmd = ['git', 'merge', '--ff-only', dst_ref]
subprocess.check_call(cmd)
ref = repo.head
elif contains(peel_ref(ref), obj):
# we're ahead
print("Warning: local branch is ahead of PR: %s" % obj.id)
cmd = ['git', '--no-pager', 'log', '--graph', '--oneline',
'--first-parent', '%s..HEAD' % dst_ref]
subprocess.call(cmd)
elif last_ref and peel_ref(ref).id == last_ref:
# We have a previous version of the PR, which has likely been rebased
print("WARNING: the PR has been rebased. No local changes detected.")
safe_reset = True
else:
print("WARNING: local branch has deviated from PR")
print('Here is the reflog:')
cmd = ['git', '--no-pager', 'reflog', branch]
subprocess.call(cmd)
if (opts['reset'] or safe_reset) and ref.target != obj.id:
print("RESETTING branch to match PR")
print("Ref was: %s" % ref.target)
cmd = ['git', 'reset', '--keep', dst_ref]
subprocess.check_call(cmd)
# Note the --keep. From the man page:
# "If a file that is different between <commit> and HEAD has local
# changes, reset is aborted."
def contains(a, b):
"""Determine if commit a contains commit b
a,b: pygit2 commit objects
"""
stack = [a]
seen = set()
while stack:
obj = stack.pop(0)
if obj.id in seen:
# we may have gotten here via another path already
continue
seen.add(obj.id)
if obj.id == b.id:
return True
else:
stack.extend(obj.parents)
return False
def peel_ref(ref):
"""Compat function for api variation"""
if hasattr(ref, 'get_object'):
return ref.get_object()
return ref.peel()
# XXX DEBUGGING
@pg.command('contains')
@click.argument('a')
@click.argument('b')
def test_contains(a, b):
repo = get_repo()
A = peel_ref(repo.lookup_reference(a))
B = peel_ref(repo.lookup_reference(b))
print(contains(A,B))
@pr.command('merge')
@click.option('--to', '--target', 'target')
@click.option('--pr', '--pull-request', 'pull_request')
@click.option('--force/--no-force', '-f/')
@click.option('--rebase/--no-rebase', '-r/')
@click.option('--check-flags/--no-check-flags', default=True)
def pr_merge(**opts):
project = get_project()
if project is None:
raise Exception('Must be run from pagure checkout')
repo = get_repo()
# determine pr from branch
if not repo.head.name.startswith('refs/heads/'):
raise Exception("HEAD is not a branch")
branch = repo.head.name[11:]
prid = opts.get('pull_request')
if prid is None:
prid = get_pr_from_branch(project, repo, branch)
print("PR branch for %s" % prid)
pr = pcall("%s/pull-request/%s" % (project, prid))
url, remote_branch = get_pr_source(pr)
if opts['check_flags']:
flags = pcall("%s/pull-request/%s/flag" % (project, prid))['flags']
if flags:
# TODO: the flags we care about should be configurable
# for now, just check the first (most recent)
flag = flags[0]
if flag['status'] != 'success':
print('Last flag unsuccessful: %(comment)s' % flag)
if not opts['force']:
sys.exit(1)
# fetch for comparison
cmd = ['git', 'fetch', url, remote_branch]
subprocess.check_call(cmd)
# pygit2 can't give me FETCH_HEAD
cmd = ['git', 'rev-parse', 'FETCH_HEAD']
remote_ref = subprocess.check_output(cmd).strip()
if six.PY3:
remote_ref = remote_ref.decode('ascii')
remote_obj = repo.get(remote_ref)
if remote_obj.id != repo.head.target:
print("Warning: current branch does match remote")
# TODO: show differences
if opts['rebase']:
# for safety, we will not rebase a modified branch
raise Exception("Auto-rebase not safe on modified branch")
if not opts['force']:
raise Exception('Use --force to merge a modified PR branch')
if opts['target']:
target_branch = opts['target']
if target_branch not in repo.branches.local:
raise Exception('No such branch: %s' % target_branch)
else:
# get target branch
target_branch = pr['branch']
if target_branch != 'master':
print('Warning: merge target is %s' % target_branch)
# check our local branch matches
lbr = repo.branches.local[target_branch]
rbr = repo.branches.remote['origin/%s' % target_branch]
if lbr.target != rbr.target:
raise Exception('Target branch %s does not match remote' % target_branch)
if opts['rebase']:
cmd = ['git', 'rebase', target_branch]
subprocess.check_call(cmd)
# checkout our target branch
cmd = ['git', 'checkout', target_branch]
subprocess.check_call(cmd)
# XXX: we should have a better sanity check when there is an explicit target
if not opts['target']:
# TODO: this should probably be an option
cmd = ['git', 'pull', '--ff-only']
subprocess.check_call(cmd)
# generate standard commit message
pr = pcall("%s/pull-request/%s" % (project, prid))
msg = (
"PR#{id}: {title}\n"
"\n"
"Merges #{id}\n"
"https://pagure.io/{project[name]}/pull-request/{id}\n"
).format(**pr)
parts = [msg]
# issues
fixes, relates = get_pr_issues(project, pr)
parts.extend(format_pr_issues(pr, fixes, relates))
# finally, do the merge
msg = '\n'.join(parts)
cmd = ['git', 'merge', '--edit', '-m', msg, '--no-ff', branch]
subprocess.check_call(cmd)
def issue_url(project, issue):
if isinstance(project, six.string_types):
ppath = project
else:
ppath = project_path(project)
return "%s/%s/issue/%s" % (BASEURL, ppath, issue['id'])
def get_pr_from_branch(project, repo, branch):
if branch.startswith('pagure/pr/'):
return branch[10:]
# check to see if a PR head matches repo.head.target
data = all_pages('%s/pull-requests' % project)
matches = []
for pr in data['requests']:
url, pr_branch = get_pr_source(pr)
if pr_branch != branch:
continue
print("Branch name match: %(id)s" % pr)
obj = repo.get(pr['commit_stop'])
if not obj:
continue
print("We have the ref")
# XXX presuming repo.head matches branch
if obj.id != repo.head.target:
print("branch does not match PR")
print("pr: %s" % obj.id)
print("us: %s" % repo.head.target)
continue
matches.append(pr)
if not matches:
raise Exception("Not a pr branch: %s" % branch)
elif len(matches) > 1:
raise Exception("Branch matches multiple PRs")
# else
pr = matches[0]
return pr['id']
def get_pr_source(prinfo):
"""Returns a pair [url, branch]"""
branch = prinfo['branch_from']
if prinfo.get('repo_from'):
# apparently, for this we can use the fullname
url = "https://pagure.io/%(fullname)s.git" % prinfo['repo_from']
elif prinfo.get('remote_git'):
url = prinfo['remote_git']
else:
raise RuntimeError("Can't determine pr source")
return url, branch
ISSUE_BRANCH_RE = re.compile(r'issue[_-]?(\d+)', re.I)
ISSUE_FIX_RE = re.compile(
r'''(?:.*\s+)? # junk at beginning
(?:fixe?[sd]?|close?[sd]?) # verb
(?:issue|bug)? # optional descriptor
:?\s* # optional separation
(?:https?://.*/(\w+)/issue/)? # optional url
[#]?(\d+) # issue number
''', re.X | re.I)
ISSUE_RELATE_RE = re.compile(
r'''(?:.*\s+)? # junk at beginning
(?:relate[sd]?(?:\s+to)?) # optional verb
(?:issue|bug)? # optional descriptor
:?\s* # optional separation
(?:https?://.*/(\w+)/issue/)? # optional url
[#]?(\d+) # issue number
''', re.X | re.I)
def format_pr_issues(pr, fixes, relates):
"""Format the output of get_pr_issues
Returns a list of strings that should be joined with '\n'
"""
parts = []
if fixes or relates:
parts.append(
"# The issues below were derived from the pull request\n"
"# Please verify their accuracy before committing\n"
)
else:
parts.append('# (no referenced issues found)\n')
def issuetext(info):
lines = ["%(_verb)s: #%(id)s"]
if info['_verb'] != 'Fixes':
lines.append("# Fixes #%(id)s")
lines.extend([
"%(_url)s",
"%(title)s",
"# Status: %(status)s",
"",
])
return '\n'.join(lines) % issue
for issue in fixes:
issue['_url'] = issue_url(pr['project'], issue)
issue['_verb'] = 'Fixes'
parts.append(issuetext(issue))
for issue in relates:
issue['_url'] = issue_url(pr['project'], issue)
issue['_verb'] = 'Relates'
parts.append(issuetext(issue))
return parts
def get_pr_issues(project, prinfo):
"""Return issues that seem to be related to the pr"""
fixes = []
relates = []
# check branch
branch = prinfo['branch_from']
m = ISSUE_BRANCH_RE.match(branch)
if m:
fixes.append(m.group(1))
comments = ([prinfo['initial_comment'] or '']
+ [c['comment'] for c in prinfo['comments']])
for text in comments:
for line in text.splitlines():
m = ISSUE_FIX_RE.match(line)
if m:
if m.group(1) and m.group(1) != prinfo['project']['name']:
# XXX
print("cross project issue: %s" % line)
else:
fixes.append(m.group(2))
m = ISSUE_RELATE_RE.match(line)
if m:
if m.group(1) and m.group(1) != prinfo['project']['name']:
# XXX
print("cross project issue: %s" % line)
else:
relates.append(m.group(2))
fixes = set(fixes)
relates = set(relates) - fixes
issues = {}
for issueid in fixes.union(relates):
try:
data = pcall("%s/issue/%s" % (project, issueid))
except Exception:
data = None
issues[issueid] = data
fixes = [issues[n] for n in fixes if issues[n]]
relates = [issues[n] for n in relates if issues[n]]
return fixes, relates
# XXX test command
@pr.command('issues')
@click.argument('prid')
@click.option('-p', '--project')
def pr_issues(prid, project, **opts):
if project is None:
project = get_project()
if project is None:
raise Exception('Please specify a project')
pr = pcall("%s/pull-request/%s" % (project, prid))
fixes, relates = get_pr_issues(project, pr)
parts = format_pr_issues(pr, fixes, relates)
print('\n'.join(parts))
# XXX test command
@pr.command('relnote')
@click.argument('prid')
@click.option('-p', '--project')
def pr_relnote(prid, project, **opts):
"""Generate a *very* crude release note for the PR"""
if project is None:
project = get_project()
if project is None:
raise Exception('Please specify a project')
pr = pcall("%s/pull-request/%s" % (project, prid))
print('** %(title)s **' % pr)
# print('^' * len(pr['title']))
print('')
print('| PR: https://pagure.io/{project[name]}/pull-request/{id}'.format(**pr))
print('')
print(pr['initial_comment'])
print('')
fixes, relates = get_pr_issues(project, pr)
for issue in fixes + relates:
issue['project'] = project
print(('..\n'
' https://pagure.io/{project}/issue/{id}\n'
' {title}\n\n'
'{content}\n'.format(**issue)))
@pr.command('list')
@click.option('-p', '--project')
@click.option('-o', '--order', default='utime')
def pr_list(project, order):
if project is None:
project = get_project()
if project is None:
raise Exception('Please provide a project')
data = all_pages('%s/pull-requests' % project)
# count = data['total_requests']
prs = data['requests']
for pr in prs:
ts = int(pr['updated_on'])
for comment in pr.get('comments', []):
cts1 = int(comment['date_created'] or 0)
cts2 = int(comment['edited_on'] or 0)
ts = max(ts, cts1, cts2)
pr['utime'] = ts
if order:
o_keys = order.split(',')
prs.sort(key=lambda pr: [pr.get(k) for k in o_keys])
for pr in prs:
print("PR#%(id)i: %(title)s [%(status)s]" % pr)
@pr.command('recent')
@click.option('-p', '--project')
@click.option('-s', '--since', default="-7")
@click.option('-c', '--closer', metavar='USER',
help='ignore if final comment by user')
def pr_recent(project, since, closer):
if project is None:
project = get_project()
if project is None:
raise Exception('Please provide a project')
data = all_pages('%s/pull-requests' % project)
prs = data['requests']
for pr in prs:
ts = int(pr['updated_on'])
for comment in pr.get('comments', []):
cts1 = int(comment['date_created'] or 0)
cts2 = int(comment['edited_on'] or 0)
ts = max(ts, cts1, cts2)
pr['utime'] = ts
cutoff = get_cutoff_ts(since)
prs.sort(key=lambda p: p['utime'])
for pr in prs:
if pr['utime'] < cutoff:
continue
if closer:
# check final comment
comments = pr.get('comments', [])
if comments and comments[-1]['user']['name'] == closer:
continue
header = [
"PR#%(id)i: %(title)s [%(status)s]" % pr,
"https://pagure.io/{project[name]}/pull-request/{id}".format(**pr),
"Status: %(status)s" % pr,
"User: %s" % pr['user']['name'],
]
lines = []
if int(pr['date_created']) > cutoff:
header.append(
"Created: %s" % time.asctime(time.localtime(float(pr['date_created']))))
if pr.get('repo_from'):
# should we use fullname field?
header.append(
"Fork: %s/%s" % (BASEURL, pr['repo_from']['fullname']))
elif pr.get('remote_git'):
header.append(
"Remote: %(remote_git)s" % pr)
else:
print("WARNING: Can't determine repo?")
sys.exit(1)
header.append(
"%(branch_from)s -> %(branch)s" % pr)
lines.append("Description:")
lines.append(pr['initial_comment'])
elif int(pr['updated_on']) > cutoff:
header.append(
"Updated: %s" % time.asctime(time.localtime(float(pr['updated_on']))))
print(tabulate([[l] for l in header], tablefmt="psql"))
for l in lines:
print(l)
for comment in pr['comments']:
cts = float(comment['date_created'])
if cts < cutoff:
continue
comment['_username'] = comment['user']['name']
comment['_tstr'] = time.asctime(time.localtime(float(cts)))
print("\n## Comment by %(_username)s on %(_tstr)s:\n" % comment)
print(comment['comment'])
print()
def get_cutoff_ts(since):
now = time.time()
try:
_since = int(since)
if _since < 0:
return now + (_since * 3600 * 24)
else:
return since
except ValueError:
pass
return dateutil.parser.parse(since)
def project_path(project):
parts = []
if project.get('parent'):
parts.append('fork')
parts.append(project['user']['name'])
if project.get('namespace'):
parts.append(project.get('namespace'))
parts.append(project['name'])
return '/'.join(parts)
@pg.command(name='list-projects')
@click.argument('pattern')
@click.option('--fork/--no-fork', default=False)
def list_projects(pattern, **opts):
if pattern:
opts['pattern'] = pattern
data = all_pages('projects', opts)
# count = data['total_projects']
# XXX - revert this
idx = {}
for proj in data['projects']:
proj['_path'] = project_path(proj)
# print "%(_path)s: %(description)s" % proj
idx.setdefault(proj['name'], []).append(proj)
counts = [(len(idx[n]), n) for n in sorted(idx)]
for count, name in sorted(counts):
print('%3i: %s' % (count, name))
@pg.group()
def project():
pass
@project.command('git-urls')
@click.option('-p', '--project')
def git_urls(project):
if project is None:
project = get_project()
if project is None:
raise Exception('Please provide a project')
data = pcall("%s/git/urls" % project)
for url in data['urls']: # not a list for some reason
print("%s: %s" % (url, data['urls'][url]))
@project.command('git-tags')
@click.option('-p', '--project')
def git_tags(project):
if project is None:
project = get_project()
if project is None:
raise Exception('Please provide a project')
data = pcall("%s/git/tags" % project)
for tag in data['tags']:
print(tag)
@project.command('git-branches')
@click.option('-p', '--project')
def git_branches(project):
if project is None:
project = get_project()
if project is None:
raise Exception('Please provide a project')
data = pcall("%s/git/branches" % project)
for br in data['branches']:
print(br)
@project.command()
@click.option('-p', '--project')
def watchers(project):
if project is None:
project = get_project()
if project is None:
raise Exception('Please provide a project')
data = pcall("%s/watchers" % project)
for row in data['watchers']: # dict, not list
wtype = data['watchers'][row]
print("%s: %s" % (row, wtype))
@project.command("info")
@click.option('-p', '--project')
def projectinfo(project):
if project is None:
project = get_project()
if project is None:
raise Exception('Please provide a project')
data = pcall(project)
pprint.pprint(data)
@pg.group()
def issue():
pass
def invert_opt(val):
if val is None:
return None
return not val
@issue.command(name='list')
@click.option('-p', '--project')
@click.option('-o', '--order', default='updated_on')
@click.option('-f', '--format', 'fmt', default='{id}: {title}')
@click.option('--status', help="filter by status")
@click.option('--tags', help="filter by tags")
@click.option('--author', help="filter by author")
@click.option('--assignee', help="filter by assignee")
@click.option('--priority', help="filter by priority")
@click.option('--since', help="filter by date")
@click.option('-m', '--milestones', help="filter by milestone", multiple=True)
@click.option('--stones/--no-stones', default=None,
help="issues with no milestone")
@click.option('--pr', '--with-pr', 'with_pr', help="issues with prs", is_flag=True)
def issuelist(project, order, fmt, with_pr, **opts):
if project is None:
project = get_project()
if project is None:
raise Exception('Please provide a project')
opts['no_stones'] = invert_opt(opts['stones'])
del opts['stones']
for k in list(opts):
if opts[k] is None:
del opts[k]
if 'since' in opts:
val = opts['since']
dt = dateutil.parser.parse(val)
opts['since'] = int(time.mktime(dt.timetuple()))
data = all_pages("%s/issues" % project, opts)
issues = data['issues']
if with_pr:
pr_map = get_pr_map(project)
for n, issue in enumerate(issues):
if issue['id'] not in pr_map:
# no pr refers to this
del issues[n]
for issue in issues:
url = issue_url(project, issue)
issue.setdefault('url', url)
if order:
o_keys = order.split(',')
issues.sort(key=lambda row: [row.get(k) for k in o_keys])
for row in issues:
print(fmt.format(**row))
def get_pr_map(project):
# XXX: lots of duplicate lookups in with caller
# XXX: sloooooow
data = all_pages('%s/pull-requests' % project)
pr_map = {}
for pr in data['requests']:
fixes, relates = get_pr_issues(project, pr)
for issue in fixes:
pr_map.setdefault(
issue['id'], {}).setdefault(
'fixes', {}).setdefault(
pr['id'], pr)
for issue in relates:
pr_map.setdefault(
issue['id'], {}).setdefault(
'relates', {}).setdefault(
pr['id'], pr)
return pr_map
@issue.command('recent')
@click.option('-p', '--project')
@click.option('-s', '--since', default="-7")
def issue_recent(project, since):
if project is None:
project = get_project()
if project is None:
raise Exception('Please provide a project')
# not sure if I trust the api's since filter
data = all_pages("%s/issues" % project)
issues = data['issues']
for issue in issues:
ts1 = int(issue['date_created'] or 0)
ts2 = int(issue['last_updated'] or 0)
ts = max(ts1, ts2)
for comment in issue.get('comments', []):
cts1 = int(comment['date_created'] or 0)
cts2 = int(comment['edited_on'] or 0)
ts = max(ts, cts1, cts2)
issue['utime'] = ts
cutoff = get_cutoff_ts(since)
issues.sort(key=lambda p: p['utime'])
for issue in issues:
if issue['utime'] < cutoff:
continue
issue['project'] = project
print("-------------------")
print("Issue#%(id)i: %(title)s [%(status)s]" % issue)