-
Notifications
You must be signed in to change notification settings - Fork 46
/
binaryCompatibility.py
78 lines (64 loc) · 2.72 KB
/
binaryCompatibility.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
# Copyright Amazon.com Inc. or its affiliates.
# SPDX-License-Identifier: Apache-2.0
import argparse
import json
import os
import xml.etree.ElementTree as ET
from agithub.GitHub import GitHub
def findall_recursive(node, element):
for item in node.findall(element):
yield item
for item in list(node):
yield from findall_recursive(item, element)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--input', type=argparse.FileType('r'), help='Input file to parse')
parser.add_argument('--token', type=str, help='GitHub token')
parser.add_argument('--pr', type=str, help='GitHub PR')
parser.add_argument('--sha', type=str, help='GitHub SHA')
args = parser.parse_args()
file = args.input
my_body_dedupe = "Produced by binaryCompatability.py"
body = f"Binary incompatibility detected for commit {args.sha}.\n" \
f" See the uploaded artifacts from the action for details. You must bump the version number.\n\n"
incompatible = False
tree = ET.parse(file)
for clazz in tree.getroot().findall("./classes/class"):
any_incompatible = False
body += f"{clazz.attrib['fullyQualifiedName']} is "
if clazz.attrib['binaryCompatible'] == "false":
body += "binary incompatible"
incompatible = True
any_incompatible = True
if clazz.attrib['sourceCompatible'] == "false":
if any_incompatible:
body += " and is "
body += "source incompatible"
any_incompatible = True
if any_incompatible:
body += f" because of " \
f"{', '.join({x.text for x in findall_recursive(clazz, 'compatibilityChanges/compatibilityChange')})}"
else:
body += "fully compatible"
body += "\n"
body += "\n" + my_body_dedupe
token = args.token
pr = args.pr
gh = GitHub(token=token)
existing_comments = gh.repos[os.getenv("GITHUB_REPOSITORY")].issues[pr].comments.get()
if existing_comments[0] == 200:
existing_comments = list(filter(lambda i: my_body_dedupe in i["body"], existing_comments[1]))
else:
existing_comments = []
if existing_comments:
comment_id = existing_comments[0]["id"]
if incompatible:
updated_issue = gh.repos[os.getenv("GITHUB_REPOSITORY")].issues.comments[comment_id].patch(body={"body": body})
print(updated_issue, flush=True)
else:
gh.repos[os.getenv("GITHUB_REPOSITORY")].issues.comments[comment_id].delete()
elif incompatible:
issue = gh.repos[os.getenv("GITHUB_REPOSITORY")].issues[pr].comments.post(body={"body": body})
print(issue, flush=True)
if __name__ == '__main__':
main()