1#!/usr/bin/python
2
3#
4# Copyright 2020, The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19from __future__ import print_function
20
21from argparse import ArgumentParser
22import subprocess
23import sys
24
25
26def is_in_aosp():
27    branches = subprocess.check_output(['git', 'branch', '-vv']).splitlines()
28
29    for branch in branches:
30        # current branch starts with a '*'
31        if branch.startswith('*'):
32            return '[aosp/' in branch
33
34    # otherwise assume in AOSP
35    return True
36
37
38def is_commit_msg_valid(commit_msg):
39    for line in commit_msg.splitlines():
40        line = line.strip().lower()
41        if line.startswith('updated-pdd'):
42            return True
43
44    return False
45
46
47def main():
48    parser = ArgumentParser(description='Check if the Privacy Design Doc (PDD) has been updated')
49    parser.add_argument('metrics_file', type=str, help='path to the metrics Protobuf file')
50    parser.add_argument('commit_msg', type=str, help='commit message')
51    parser.add_argument('commit_files', type=str, nargs='*', help='files changed in the commit')
52    args = parser.parse_args()
53
54    metrics_file = args.metrics_file
55    commit_msg = args.commit_msg
56    commit_files = args.commit_files
57
58    if is_in_aosp():
59        return 0
60
61    if metrics_file not in commit_files:
62        return 0
63
64    if is_commit_msg_valid(commit_msg):
65        return 0
66
67    print('This commit has changed {metrics_file}.'.format(metrics_file=metrics_file))
68    print('If this change added/changed/removed metrics collected from the device,')
69    print('please update the Wifi Metrics Privacy Design Doc (PDD) at go/wifi-metrics-pdd')
70    print('and acknowledge you have done so by adding this line to your commit message:')
71    print()
72    print('Updated-PDD: TRUE')
73    print()
74    print('Otherwise, please explain why the PDD does not need to be updated:')
75    print()
76    print('Updated-PDD: Not applicable - reformatted file')
77    print()
78    print('Please reach out to the OWNERS for more information about the Wifi Metrics PDD.')
79    return 1
80
81
82if __name__ == '__main__':
83    exit_code = main()
84    sys.exit(exit_code)
85