1#!/usr/bin/python3
2
3#  Copyright (C) 2022 The Android Open Source Project
4#
5#  Licensed under the Apache License, Version 2.0 (the "License");
6#  you may not use this file except in compliance with the License.
7#  You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#  Unless required by applicable law or agreed to in writing, software
12#  distributed under the License is distributed on an "AS IS" BASIS,
13#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#  See the License for the specific language governing permissions and
15#  limitations under the License.
16
17from __future__ import print_function
18
19from argparse import ArgumentParser
20import subprocess
21import sys
22
23BASE_DIR = "service/ServiceWifiResources/res/"
24OVERLAY_FILE = BASE_DIR + "values/overlayable.xml"
25CONFIG_FILE = BASE_DIR + "values/config.xml"
26STRING_FILE = BASE_DIR + "values/strings.xml"
27STYLES_FILE = BASE_DIR + "values/styles.xml"
28DRAWABLE_DIR = BASE_DIR + "drawable/"
29LAYOUT_DIR = BASE_DIR + "layout/"
30
31BASE_WIFI_SERVICE_DIR = "service/java/com/android/server/wifi/"
32XML_UTIL_FILE = BASE_WIFI_SERVICE_DIR + "util/XmlUtil.java"
33CLOUD_BACKUP_RESTORE_FILE = BASE_WIFI_SERVICE_DIR + "WifiBackupRestore.java"
34CLOUD_BACKUP_PARSER_FILE = BASE_WIFI_SERVICE_DIR + "WifiBackupDataV1Parser.java"
35
36def is_commit_msg_valid(commit_msg, checkResource, checkXmlTag):
37    isValid = True
38    for line in commit_msg.splitlines():
39        line = line.strip().lower()
40        if checkResource:
41            if line.startswith('updated-overlayable'):
42                isValid = True
43                checkResource = False
44        if checkXmlTag:
45            isValid = False
46            if line.startswith('reviewed-cloud-b&r'):
47                isValid = True
48                checkXmlTag = False
49
50    return isValid
51
52def is_in_aosp():
53    branches = subprocess.check_output(['git', 'branch', '-vv']).splitlines()
54
55    for branch in branches:
56        # current branch starts with a '*'
57        if branch.startswith(b'*'):
58            return b'[aosp/' in branch
59
60    # otherwise assume in AOSP
61    return True
62
63def get_changed_resource_file(commit_files):
64    for commit_file in commit_files:
65        if commit_file == STRING_FILE:
66            return STRING_FILE
67        if commit_file == CONFIG_FILE:
68            return commit_file
69        if commit_file == STYLES_FILE:
70            return commit_file
71        if commit_file.startswith(DRAWABLE_DIR):
72            return commit_file
73        if commit_file.startswith(LAYOUT_DIR):
74            return commit_file
75    return None
76
77def get_changed_xml_related_file(commit_files):
78    for commit_file in commit_files:
79        if commit_file == XML_UTIL_FILE:
80            return commit_file
81        if commit_file == CLOUD_BACKUP_RESTORE_FILE:
82            return commit_file
83        if commit_file == CLOUD_BACKUP_PARSER_FILE:
84            return commit_file
85    return None
86
87def is_commit_msg_has_translation_bug_id(commit_msg):
88    for line in commit_msg.splitlines():
89        line = line.strip().lower()
90        if line.startswith('bug: 294871353'):
91            return True
92    return False
93
94def is_xml_tag_in_commits():
95    cmd_run = subprocess.Popen('git show | grep -iE "XML_TAG"',
96        shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE
97    )
98    out, err = cmd_run.communicate()
99    for line in out.splitlines():
100        if line.startswith(b'+') or line.startswith(b'-'):
101            print('This commit has xml tag changed: "{changed_line}".'.format(changed_line=line.decode("utf-8")))
102            print()
103            return True
104    return False
105
106def main():
107    parser = ArgumentParser(description='Check if the dependency file has been updated')
108    parser.add_argument('commit_msg', type=str, help='commit message')
109    parser.add_argument('commit_files', type=str, nargs='*', help='files changed in the commit')
110    args = parser.parse_args()
111
112    commit_msg = args.commit_msg
113    commit_files = args.commit_files
114    if is_in_aosp():
115        return 0
116
117    changed_resource_file = get_changed_resource_file(commit_files)
118
119    if changed_resource_file:
120        if changed_resource_file == STRING_FILE:
121            if not is_commit_msg_has_translation_bug_id(commit_msg):
122                print('This commit has changed: "{changed_file}".'.format(changed_resource_file=changed_resource_file))
123                print()
124                print('Please add the following line to your commit message')
125                print()
126                print('Bug: 294871353')
127                print()
128                return 1
129
130        if not is_commit_msg_valid(commit_msg, True, False):
131            print('This commit has changed: "{changed_file}".'.format(changed_file=changed_resource_file))
132            print()
133            print('If this change added/changed/removed overlayable resources used by the Wifi Module, ')
134            print('please update the "{overlay_file}".'.format(overlay_file=OVERLAY_FILE))
135            print('and acknowledge you have done so by adding this line to your commit message:')
136            print()
137            print('Updated-Overlayable: TRUE')
138            print()
139            print('Otherwise, please explain why the Overlayable does not need to be updated:')
140            print()
141            print('Updated-Overlayable: Not applicable - changing default value')
142            print()
143            return 1
144    changed_xml_related_file = get_changed_xml_related_file(commit_files)
145    if changed_xml_related_file and is_xml_tag_in_commits():
146        if not is_commit_msg_valid(commit_msg, False, True):
147            print('This commit has changed: "{changed_file}".'.format(changed_file=changed_xml_related_file))
148            print()
149            print('If this change added/changed/removed xml resources used by the Wifi Module, ')
150            print('please review if it may break/miss the cloud B&R , check "{b_r_file}".'.format(b_r_file=CLOUD_BACKUP_PARSER_FILE))
151            print('and acknowledge you have done so by adding this line to your commit message:')
152            print()
153            print('Reviewed-Cloud-B&R: TRUE')
154            print()
155            print('Otherwise, please explain why the cloud B&R does not need to be updated:')
156            print()
157            print('Reviewed-Cloud-B&R: Not applicable - not xml format change')
158            print()
159            return 1
160
161    return 0
162
163if __name__ == '__main__':
164    exit_code = main()
165    sys.exit(exit_code)
166