-
Notifications
You must be signed in to change notification settings - Fork 2
/
tools.py
922 lines (829 loc) · 30.7 KB
/
tools.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
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
import os, requests, openai, emoji
from urllib.parse import urlparse
GPT_MODEL="gpt-4-1106-preview"
PROMPT_SYSTEM="Rephrase all user input text into simple, easy to understand, and technically toned English. Never answer questions but only translate or rephrase text to English."
TRANS_MAGIC="TRANS_BY_GPT4"
TRANS_DELIMETER = '\n\n'
TRANS_DELIMETER_PR = '---------'
LABEL_TRANS_NAME="TransByAI"
LABEL_REFINED_NAME="RefinedByAI"
LABEL_ENGLISH_NATIVE="EnglishNative"
def github_token_init(token):
if token is not None:
os.environ["GITHUB_TOKEN"] = token
if os.environ.get("GITHUB_TOKEN") is None:
raise Exception("GITHUB_TOKEN is not set")
def openai_init(key, proxy):
if key is not None:
openai.api_key = key
elif os.environ.get("OPENAI_API_KEY") is not None:
openai.api_key = os.environ.get("OPENAI_API_KEY")
else:
raise Exception("OPENAI_API_KEY is not set")
if os.environ.get("OPENAI_ORGANIZATION") is not None:
openai.organization = os.environ.get("OPENAI_ORGANIZATION")
print(f"Use OPENAI_ORGANIZATION={openai.organization}")
if os.environ.get("OPENAI_CHAT_MODEL") is not None:
global GPT_MODEL
GPT_MODEL = os.environ.get("OPENAI_CHAT_MODEL")
print(f"Use OPENAI_CHAT_MODEL={GPT_MODEL}")
if proxy is not None:
openai.api_base = "http://" + proxy + "/v1/"
elif os.environ.get("OPENAI_PROXY") is not None:
openai.api_base = "http://" + os.environ.get("OPENAI_PROXY") + "/v1/"
else:
print("Warning: OPENAI_PROXY is not set")
def already_english(str):
for c in str:
if len(c) != len(c.encode('utf-8')) and emoji.emoji_count(c) == 0:
return False
return True
def split_segments(body):
lines = body.split('\n')
matches = []
current_matches = []
is_english = already_english(lines[0])
for line in lines:
if line == '':
current_matches.append('\n')
continue
if already_english(line) == is_english:
current_matches.append(line)
else:
matches.append('\n'.join(current_matches))
current_matches = [line]
is_english = already_english(line)
matches.append('\n'.join(current_matches))
return matches
def wrap_magic(body, extra_delimeter=''):
if TRANS_MAGIC in body:
return body
magic = ''
if extra_delimeter != '':
magic = f"{TRANS_DELIMETER}{extra_delimeter}"
magic = f"{magic}{TRANS_DELIMETER}`{TRANS_MAGIC}`"
return f"{body}{magic}"
def gpt_translate(plaintext, trans_by_gpt):
segments = split_segments(plaintext)
final_trans = []
real_translated = False
messages = []
for segment in segments:
# Directly keep the empty line.
if segment.strip() == '':
final_trans.append(segment)
continue
print(f"\n<<<<<<<<<<<< {segment.strip()} >>>>>>>>>>>>")
if TRANS_MAGIC in segment:
trans_by_gpt = True
print(f"<<<<<<<<<<<< Already translated, skip >>>>>>>>>>>>\n")
final_trans.append(segment)
elif already_english(segment):
print(f"<<<<<<<<<<<< Already English, skip >>>>>>>>>>>>\n")
final_trans.append(segment)
else:
real_translated = trans_by_gpt = True
messages.append({"role": "user", "content": segment})
if len(messages) > 3:
messages = messages[-3:]
retry = 3
add_to_messages = False
for i in range(retry):
try:
(segment_trans, add_to_messages) = do_gpt_translate(segment, PROMPT_SYSTEM, messages)
break
except Exception as e:
if i == retry - 1:
raise e
print(f"Warning!!! GPT retry {i+1} times, ignore {e}")
print(f"<<<<<<<<<<<< {segment_trans.strip()} >>>>>>>>>>>>\n")
if add_to_messages:
messages.append({"role": "assistant", "content": segment_trans})
final_trans.append(segment_trans)
plaintext_trans = "\n".join(final_trans).strip('\n')
return (plaintext_trans, trans_by_gpt, real_translated)
def do_gpt_translate(plaintext, system, messages):
try:
prompts = messages.copy()
if system is not None:
prompts.insert(0, {"role": "system", "content": system})
completion = openai.ChatCompletion.create(
model=GPT_MODEL,
messages=prompts,
temperature=0,
)
translated = completion.choices[0].message.content.strip('\'"')
# Filter:
# 'safari推流rtc失败' translates to 'Safari streaming RTC failed' in English
# to:
# Safari streaming RTC failed
if f"{plaintext}' translates to '" in translated:
translated = translated.split("' translates to '")[1]
if "' in English" in translated:
translated = translated.split("' in English")[0]
# Filter:
# We can discuss more clearly on Discord.
# Please ensure to maintain the markdown structure.
# to:
# We can discuss more clearly on Discord.
lines = translated.split('\n')
if len(lines) > 0 and 'maintain' in lines[-1] and 'markdown structure' in lines[-1]:
translated = '\n'.join(lines[:-1])
return (translated, True)
except openai.InvalidRequestError as e:
if e.code == 'context_length_exceeded':
print(f"Warning!!! Use source text for GPT context_length_exceeded, length={len(plaintext)}")
return (plaintext, False)
raise e
def gpt_refine_pr(plaintext):
messages = []
messages.append({"role": "system", "content": PROMPT_SYSTEM})
messages.append({"role": "user", "content": plaintext})
completion = openai.ChatCompletion.create(
model=GPT_MODEL,
messages=messages,
temperature=0,
)
trans = completion.choices[0].message.content.strip('\'"')
return trans
def get_graphql_headers():
return {
"Content-Type": "application/json",
"Authorization": f"Bearer {os.environ.get('GITHUB_TOKEN')}",
"X-Github-Next-Global-ID": "1",
}
def parse_repository_url(url):
"""
:param url: GitHub repository URL, for example, https://github.com/your-org/your-repository
"""
return {
'owner': urlparse(url).path.strip('/').split('/')[0],
'name': urlparse(url).path.strip('/').split('/')[1]
}
def parse_issue_url(url):
"""
:param url: GitHub issue URL, for example, https://github.com/your-org/your-repository/issues/1235
"""
return {
'owner': urlparse(url).path.strip('/').split('/')[0],
'name': urlparse(url).path.strip('/').split('/')[1],
'number': int(urlparse(url).path.strip('/').split('/')[3])
}
def parse_discussion_url(url):
"""
:param url: GitHub issue URL, for example, https://github.com/your-org/your-repository/discussions/4421
"""
return parse_issue_url(url)
def parse_pullrequest_url(url):
"""
:param url: GitHub PullRequest URL, for example, https://github.com/your-org/your-repository/pull/3699
"""
return parse_issue_url(url)
class GithubGraphQLException(Exception):
def __init__(self, message, res):
super().__init__(message)
self.res = res
self.text = res.text
self.json = res.json()
self.errors = None
if 'errors' in self.json:
self.errors = self.json['errors']
def is_forbidden(self):
if self.errors is not None and len(self.errors) > 0:
for error in self.errors:
if 'type' not in error:
continue
# For example: Resource not accessible by personal access token
if error['type'] == 'FORBIDDEN':
return True
# For example: Cannot be modified since the issue has been converted to a discussion.
if error['type'] == 'UNPROCESSABLE':
return True
return False
def query_repository_id(owner, name):
query = '''
query ($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
id
}
}
'''
res = requests.post('https://api.github.com/graphql', json={"query": query, "variables": {
"owner": owner, "name": name,
}}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
repository_id = j_res['data']['repository']['id']
return repository_id
def create_issue(repository_id, title, body):
query = '''
mutation ($repositoryId: ID!, $title: String!, $body: String!) {
createIssue(input: {repositoryId: $repositoryId, title: $title, body: $body}) {
issue {
id
url
}
}
}
'''
res = requests.post('https://api.github.com/graphql', json={"query": query, "variables": {
"repositoryId": repository_id, "title": title, "body": body,
}}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
issue_id = j_res['data']['createIssue']['issue']['id']
issue_url = j_res['data']['createIssue']['issue']['url']
return (issue_id, issue_url)
def create_discussion(repository_id, title, body, category_id):
query = '''
mutation ($repositoryId: ID!, $title: String!, $body: String!, $categoryId: ID!) {
createDiscussion(input: {repositoryId: $repositoryId, title: $title, body: $body, categoryId: $categoryId}) {
discussion {
id
url
}
}
}
'''
res = requests.post('https://api.github.com/graphql', json={"query": query, "variables": {
"repositoryId": repository_id, "title": title, "body": body, "categoryId": category_id,
}}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
discussion_id = j_res['data']['createDiscussion']['discussion']['id']
discussion_url = j_res['data']['createDiscussion']['discussion']['url']
return (discussion_id, discussion_url)
def query_issue(owner, name, issue_number):
query = '''
query ($owner: String!, $name: String!, $number: Int!) {
repository(name: $name, owner: $owner) {
issue(number: $number) {
id
title
body
labels(first: 100) {
totalCount
nodes {
id
name
}
}
comments(first: 100) {
totalCount
nodes {
id
author {
login
}
url
body
}
}
}
}
}
'''
res = requests.post('https://api.github.com/graphql', json={"query": query, "variables": {
"owner": owner, "name": name, "number": issue_number,
}}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
total_labels = j_res['data']['repository']['issue']['labels']['totalCount']
if total_labels > 100:
raise Exception(f"too many labels, count={total_labels} {j_res}")
total_comments = j_res['data']['repository']['issue']['comments']['totalCount']
if total_comments > 100:
raise Exception(f"too many comments, count={total_comments} {j_res}")
# See https://github.com/ghost
for c in j_res['data']['repository']['issue']['comments']['nodes']:
if 'author' not in c or c['author'] is None:
c['author'] = {'login': 'ghost'}
return {
'id': j_res['data']['repository']['issue']['id'],
'title': j_res['data']['repository']['issue']['title'],
'body': j_res['data']['repository']['issue']['body'],
'labels': j_res['data']['repository']['issue']['labels']['nodes'],
"comments": j_res['data']['repository']['issue']['comments']['nodes'],
}
def update_issue_comment(id, body):
query = '''
mutation ($id: ID!, $body:String!) {
updateIssueComment(input: {id: $id, body: $body}) {
issueComment {
id
}
}
}
'''
res = requests.post('https://api.github.com/graphql', json={"query": query, "variables": {
"id": id, "body": body,
}}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
return j_res['data']['updateIssueComment']['issueComment']['id']
def update_issue(id, title, body):
query = '''
mutation ($id: ID!, $title:String!, $body: String!) {
updateIssue(input: {id: $id, body: $body, title: $title}) {
issue {
id
}
}
}
'''
res = requests.post('https://api.github.com/graphql', json={"query": query, "variables": {
"id": id, "title": title, "body": body,
}}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
return j_res['data']['updateIssue']['issue']['id']
def query_label_id(owner, name, label):
query = '''
query($name: String!, $owner: String!, $label: String!) {
repository(name: $name, owner: $owner) {
label(name: $label) {
id
}
}
}
'''
res = requests.post('https://api.github.com/graphql', json={"query": query, "variables": {
"name": name, "owner": owner, "label": label
}}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
id = j_res['data']['repository']['label']['id']
return id
def query_catetory_id(owner, name, category_slug):
query = '''
query ($name:String!, $owner: String!, $slug: String!) {
repository(name: $name, owner: $owner) {
discussionCategory(slug:$slug) {
id
}
}
}
'''
res = requests.post('https://api.github.com/graphql', json={"query": query, "variables": {
"name": name, "owner": owner, "slug": category_slug
}}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
id = j_res['data']['repository']['discussionCategory']['id']
return id
def add_label(owner_id, label_id):
query = '''
mutation ($id: ID!, $labelIds: [ID!]!) {
addLabelsToLabelable(
input: {labelableId: $id, labelIds: $labelIds}
) {
labelable {
labels {
totalCount
}
}
}
}
'''
res = requests.post('https://api.github.com/graphql', json={"query": query, "variables": {
"id": owner_id, "labelIds": [label_id]
}}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
return j_res['data']['addLabelsToLabelable']['labelable']['labels']['totalCount']
def query_discussion(owner, name, discussion_number):
query = '''
query($name: String!, $owner: String!, $number: Int!) {
repository(name: $name, owner: $owner) {
discussion(number: $number) {
id
body
title
number
labels(first: 100) {
totalCount
nodes {
id
name
}
}
comments(first: 100) {
totalCount
nodes {
id
author {
login
}
url
body
replies(first: 100) {
totalCount
nodes {
id
author {
login
}
url
body
}
pageInfo {
endCursor
startCursor
}
}
}
pageInfo {
endCursor
startCursor
}
}
}
}
}
'''
res = requests.post('https://api.github.com/graphql', json={"query": query, "variables": {
"name": name, "owner": owner, "number": discussion_number
}}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
totalCount = j_res['data']['repository']['discussion']['comments']['totalCount']
if totalCount > 100:
raise Exception(f"comments.totalCount > 100, {totalCount} of {j_res}")
totalCount = j_res['data']['repository']['discussion']["labels"]['totalCount']
if totalCount > 100:
raise Exception(f"labels.totalCount > 100, {totalCount} of {j_res}")
j_nodes = j_res['data']['repository']['discussion']['comments']['nodes']
for index, j_node in enumerate(j_nodes):
totalCount = j_node["replies"]["totalCount"]
if totalCount > 100:
raise Exception(f"comments[{index}].replies.totalCount > 100, {totalCount} of {j_node}")
# See https://github.com/ghost
for c in j_res['data']['repository']['discussion']['comments']['nodes']:
if 'author' not in c or c['author'] is None:
c['author'] = {'login': 'ghost'}
for r in c['replies']['nodes']:
if r['author'] is None:
r['author'] = {'login': 'ghost'}
return {
"id": j_res['data']['repository']['discussion']['id'],
"title": j_res['data']['repository']['discussion']['title'],
"body": j_res['data']['repository']['discussion']['body'],
"labels": j_res['data']['repository']['discussion']["labels"]["nodes"],
"comments": j_res['data']['repository']['discussion']['comments']['nodes'],
}
def update_discussion_comment(id, body):
query = '''
mutation ($id: ID!, $body: String!) {
updateDiscussionComment(
input: {commentId: $id, body: $body}
) {
comment {
id
}
}
}
'''
variables = {
"id": id,
'body': body,
}
res = requests.post('https://api.github.com/graphql', json={
"query": query, "variables": variables
}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
return j_res['data']['updateDiscussionComment']['comment']['id']
def update_discussion(id, title, body):
query = '''
mutation ($id: ID!, $title: String!, $body: String!) {
updateDiscussion(
input: {discussionId: $id, title: $title, body: $body}
) {
discussion {
id
}
}
}
'''
res = requests.post('https://api.github.com/graphql', json={"query": query, "variables": {
"id": id,
"title": title,
'body': body,
}}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
return j_res['data']['updateDiscussion']['discussion']['id']
def search_issues(owner, name, isf, sort, labels, count):
'''
Search GitHub issues, like https://github.com/your-org/your-repository/issues?q=is:issue+sort:comments-desc+-label:TransByAI+
:param owner: For example, ossrs
:param name: For example, srs
:param isf: For example, is:issue is:pr is:discussion
:param sort: For example, sort:comments-desc
:param label: For example, -label:TransByAI
'''
query = '''
query ($query: String!, $type:SearchType!, $first: Int!) {
search(
query: $query
type: $type
first: $first
) {
nodes {
... on Discussion {
id
title
url
comments {
totalCount
}
}
... on Issue {
id
title
url
comments {
totalCount
}
}
... on PullRequest {
id
title
url
comments {
totalCount
}
}
}
}
}
'''
search_type = 'ISSUE'
if 'discussion' in isf:
search_type = 'DISCUSSION'
filter = f"repo:{owner}/{name} {isf} {sort} {' '.join(labels)}"
res = requests.post('https://api.github.com/graphql', json={"query": query, "variables": {
"query": filter, "type": search_type, "first": count
}}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
return j_res['data']['search']['nodes']
def query_pullrequest(owner, name, pr_number):
query = '''
query ($name: String!, $owner: String!, $number: Int!) {
repository(name: $name, owner: $owner) {
pullRequest(number: $number) {
id
title
body
mergeable
author {
login
}
baseRef {
name
repository {
name
owner {
login
}
}
}
headRef {
name
repository {
name
owner {
login
}
}
}
participants(first: 100) {
totalCount
nodes {
login
}
}
labels(first: 100) {
totalCount
nodes {
id
name
}
}
}
}
}
'''
res = requests.post('https://api.github.com/graphql', json={"query": query, "variables": {
"name": name, "owner": owner, "number": pr_number
}}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
total_labels = j_res['data']['repository']['pullRequest']['labels']['totalCount']
if total_labels > 100:
raise Exception(f"too many labels, count={total_labels} {j_res}")
total_participants = j_res['data']['repository']['pullRequest']['participants']['totalCount']
if total_participants > 100:
raise Exception(f"too many participants, count={total_participants} {j_res}")
# See https://github.com/ghost
for c in j_res['data']['repository']['pullRequest']['participants']['nodes']:
if 'author' not in c or c['author'] is None:
c['author'] = {'login': 'ghost'}
return {
"id": j_res['data']['repository']['pullRequest']['id'],
"title": j_res['data']['repository']['pullRequest']['title'],
"body": j_res['data']['repository']['pullRequest']['body'],
"mergeable": j_res['data']['repository']['pullRequest']['mergeable'] == 'MERGEABLE',
"author": j_res['data']['repository']['pullRequest']['author']['login'],
"baseRef": j_res['data']['repository']['pullRequest']['baseRef'],
"headRef": j_res['data']['repository']['pullRequest']['headRef'],
"participants": j_res['data']['repository']['pullRequest']['participants']['nodes'],
"labels": j_res['data']['repository']['pullRequest']["labels"]["nodes"],
}
def query_pullrequest_all_in_one(owner, name, pr_number):
query = '''
query ($name: String!, $owner: String!, $number: Int!) {
repository(name: $name, owner: $owner) {
pullRequest(number: $number) {
id
title
body
labels(first: 100) {
totalCount
nodes {
id
name
}
}
comments(first: 100) {
totalCount
nodes {
id
url
body
}
}
reviews(first: 100) {
totalCount
nodes {
id
url
body
comments(first: 100) {
totalCount
nodes {
id
url
body
}
}
}
}
}
}
}
'''
res = requests.post('https://api.github.com/graphql', json={"query": query, "variables": {
"name": name, "owner": owner, "number": pr_number
}}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
total_labels = j_res['data']['repository']['pullRequest']['labels']['totalCount']
if total_labels > 100:
raise Exception(f"too many labels, count={total_labels} {j_res}")
total_comments = j_res['data']['repository']['pullRequest']['comments']['totalCount']
if total_comments > 100:
raise Exception(f"too many comments, count={total_comments} {j_res}")
total_reviews = j_res['data']['repository']['pullRequest']['reviews']['totalCount']
if total_reviews > 100:
raise Exception(f"too many reviews, count={total_reviews} {j_res}")
reviews = []
for review in j_res['data']['repository']['pullRequest']['reviews']['nodes']:
total_review_comments = review['comments']['totalCount']
if total_review_comments > 100:
raise Exception(f"too many review comments, count={total_review_comments} {j_res}")
return {
"id": j_res['data']['repository']['pullRequest']['id'],
"title": j_res['data']['repository']['pullRequest']['title'],
"body": j_res['data']['repository']['pullRequest']['body'],
"labels": j_res['data']['repository']['pullRequest']["labels"]["nodes"],
"comments": j_res['data']['repository']['pullRequest']["comments"]["nodes"],
"reviews": j_res['data']['repository']['pullRequest']['reviews']['nodes'],
}
def update_pullrequest(id, title, body):
query = '''
mutation ($id: ID!, $title: String!, $body: String!) {
updatePullRequest(
input: {pullRequestId: $id, title: $title, body: $body}
) {
pullRequest {
id
}
}
}
'''
res = requests.post('https://api.github.com/graphql', json={"query": query, "variables": {
"id": id, "title": title, "body": body
}}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
return j_res['data']['updatePullRequest']['pullRequest']['id']
def update_pullrequest_review(id, body):
query = '''
mutation ($id: ID!, $body: String!) {
updatePullRequestReview(
input: {pullRequestReviewId: $id, body: $body}
) {
pullRequestReview {
id
}
}
}
'''
variables = {
"id": id,
'body': body,
}
res = requests.post('https://api.github.com/graphql', json={
"query": query, "variables": variables
}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
return j_res['data']['updatePullRequestReview']['pullRequestReview']['id']
def update_pullrequest_review_comment(id, body):
query = '''
mutation ($id: ID!, $body: String!) {
updatePullRequestReviewComment(
input: {pullRequestReviewCommentId: $id, body: $body}
) {
pullRequestReviewComment {
id
}
}
}
'''
variables = {
"id": id,
'body': body,
}
res = requests.post('https://api.github.com/graphql', json={
"query": query, "variables": variables
}, headers=get_graphql_headers())
if res.status_code != 200:
raise Exception(f"request failed, code={res.status_code}")
j_res = res.json()
if 'errors' in j_res:
raise GithubGraphQLException(f"request failed, {j_res}", res)
return j_res['data']['updatePullRequestReviewComment']['pullRequestReviewComment']['id']