Anushree Ganjam | da9c624 | 2023-10-19 14:33:35 -0700 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
| 2 | |
| 3 | import sys |
| 4 | import re |
| 5 | import argparse |
| 6 | |
| 7 | # partially copied from tools/repohooks/rh/hooks.py |
| 8 | |
| 9 | TEST_MSG = """Commit message is missing a "Flag:" line. It must match one of the |
| 10 | following case-sensitive regex: |
| 11 | |
| 12 | %s |
| 13 | |
| 14 | The Flag: stanza is regex matched and should describe whether your change is behind a flag or flags. |
Anushree Ganjam | dc48e5a | 2024-05-14 13:49:42 -0700 | [diff] [blame] | 15 | As a CL author, you'll have a consistent place to describe the risk of the proposed change by explicitly calling out the name of the flag. |
| 16 | For legacy flags use EXEMPT with your flag name. |
Anushree Ganjam | da9c624 | 2023-10-19 14:33:35 -0700 | [diff] [blame] | 17 | |
| 18 | Some examples below: |
| 19 | |
Anushree Ganjam | dc48e5a | 2024-05-14 13:49:42 -0700 | [diff] [blame] | 20 | Flag: NONE Repohook Update |
| 21 | Flag: TEST_ONLY |
| 22 | Flag: EXEMPT resource only update |
| 23 | Flag: EXEMPT bugfix |
| 24 | Flag: EXEMPT refactor |
| 25 | Flag: com.android.launcher3.enable_twoline_allapps |
| 26 | Flag: com.google.android.apps.nexuslauncher.zero_state_web_data_loader |
Anushree Ganjam | da9c624 | 2023-10-19 14:33:35 -0700 | [diff] [blame] | 27 | |
Anushree Ganjam | dc48e5a | 2024-05-14 13:49:42 -0700 | [diff] [blame] | 28 | Check the git history for more examples. It's a regex matched field. See go/android-flag-directive for more details on various formats. |
Anushree Ganjam | da9c624 | 2023-10-19 14:33:35 -0700 | [diff] [blame] | 29 | """ |
| 30 | |
| 31 | def main(): |
| 32 | """Check the commit message for a 'Flag:' line.""" |
| 33 | parser = argparse.ArgumentParser( |
| 34 | description='Check the commit message for a Flag: line.') |
| 35 | parser.add_argument('--msg', |
| 36 | metavar='msg', |
| 37 | type=str, |
| 38 | nargs='?', |
| 39 | default='HEAD', |
| 40 | help='commit message to process.') |
| 41 | parser.add_argument( |
| 42 | '--files', |
| 43 | metavar='files', |
| 44 | nargs='?', |
| 45 | default='', |
| 46 | help= |
| 47 | 'PREUPLOAD_FILES in repo upload to determine whether the check should run for the files.') |
| 48 | parser.add_argument( |
| 49 | '--project', |
| 50 | metavar='project', |
| 51 | type=str, |
| 52 | nargs='?', |
| 53 | default='', |
| 54 | help= |
Adrian Roos | 81b2c9b | 2024-05-17 12:04:16 +0200 | [diff] [blame] | 55 | 'REPO_PROJECT in repo upload to determine whether the check should run for this project.') |
Anushree Ganjam | da9c624 | 2023-10-19 14:33:35 -0700 | [diff] [blame] | 56 | |
| 57 | # Parse the arguments |
| 58 | args = parser.parse_args() |
| 59 | desc = args.msg |
| 60 | files = args.files |
| 61 | project = args.project |
| 62 | |
| 63 | if not should_run_path(project, files): |
| 64 | return |
| 65 | |
| 66 | field = 'Flag' |
Anushree Ganjam | dc48e5a | 2024-05-14 13:49:42 -0700 | [diff] [blame] | 67 | none = 'NONE' |
| 68 | testOnly = 'TEST_ONLY' |
| 69 | docsOnly = 'DOCS_ONLY' |
| 70 | exempt = 'EXEMPT' |
| 71 | justification = '<justification>' |
Anushree Ganjam | da9c624 | 2023-10-19 14:33:35 -0700 | [diff] [blame] | 72 | |
Anushree Ganjam | dc48e5a | 2024-05-14 13:49:42 -0700 | [diff] [blame] | 73 | # Aconfig Flag name format = <packageName>.<flagName> |
Anushree Ganjam | da9c624 | 2023-10-19 14:33:35 -0700 | [diff] [blame] | 74 | # package name - Contains only lowercase alphabets + digits + '.' - Ex: com.android.launcher3 |
Anushree Ganjam | dc48e5a | 2024-05-14 13:49:42 -0700 | [diff] [blame] | 75 | # For now alphabets, digits, "_", "." characters are allowed in flag name. |
| 76 | # Checks if there is "one dot" between packageName and flagName and not adding stricter format check |
Anushree Ganjam | da9c624 | 2023-10-19 14:33:35 -0700 | [diff] [blame] | 77 | #common_typos_disable |
Anushree Ganjam | dc48e5a | 2024-05-14 13:49:42 -0700 | [diff] [blame] | 78 | flagName = '([a-zA-Z0-9.]+)([.]+)([a-zA-Z0-9_.]+)' |
Anushree Ganjam | da9c624 | 2023-10-19 14:33:35 -0700 | [diff] [blame] | 79 | |
Anushree Ganjam | dc48e5a | 2024-05-14 13:49:42 -0700 | [diff] [blame] | 80 | # None and Exempt needs justification |
| 81 | exemptRegex = fr'{exempt}\s*[a-zA-Z]+' |
| 82 | noneRegex = fr'{none}\s*[a-zA-Z]+' |
Anushree Ganjam | da9c624 | 2023-10-19 14:33:35 -0700 | [diff] [blame] | 83 | #common_typos_enable |
| 84 | |
Anushree Ganjam | dc48e5a | 2024-05-14 13:49:42 -0700 | [diff] [blame] | 85 | readableRegexMsg = '\n\tFlag: '+none+' '+justification+'\n\tFlag: <packageName>.<flagName>\n\tFlag: ' +exempt+' '+justification+'\n\tFlag: '+testOnly+'\n\tFlag: '+docsOnly |
Anushree Ganjam | da9c624 | 2023-10-19 14:33:35 -0700 | [diff] [blame] | 86 | |
| 87 | flagRegex = fr'^{field}: .*$' |
| 88 | check_flag = re.compile(flagRegex) #Flag: |
| 89 | |
| 90 | # Ignore case for flag name format. |
Anushree Ganjam | dc48e5a | 2024-05-14 13:49:42 -0700 | [diff] [blame] | 91 | flagNameRegex = fr'(?i)^{field}:\s*({noneRegex}|{flagName}|{testOnly}|{docsOnly}|{exemptRegex})\s*' |
Anushree Ganjam | da9c624 | 2023-10-19 14:33:35 -0700 | [diff] [blame] | 92 | check_flagName = re.compile(flagNameRegex) #Flag: <flag name format> |
| 93 | |
| 94 | flagError = False |
| 95 | foundFlag = [] |
| 96 | # Check for multiple "Flag:" lines and all lines should match this format |
| 97 | for line in desc.splitlines(): |
| 98 | if check_flag.match(line): |
| 99 | if not check_flagName.match(line): |
| 100 | flagError = True |
| 101 | break |
| 102 | foundFlag.append(line) |
| 103 | |
| 104 | # Throw error if |
| 105 | # 1. No "Flag:" line is found |
| 106 | # 2. "Flag:" doesn't follow right format. |
| 107 | if (not foundFlag) or (flagError): |
| 108 | error = TEST_MSG % (readableRegexMsg) |
| 109 | print(error) |
| 110 | sys.exit(1) |
| 111 | |
| 112 | sys.exit(0) |
| 113 | |
| 114 | |
Adrian Roos | 81b2c9b | 2024-05-17 12:04:16 +0200 | [diff] [blame] | 115 | def should_run_path(project, files): |
Anushree Ganjam | da9c624 | 2023-10-19 14:33:35 -0700 | [diff] [blame] | 116 | """Returns a boolean if this check should run with these paths. |
| 117 | If you want to check for a particular subdirectory under the path, |
| 118 | add a check here, call should_run_files and check for a specific sub dir path in should_run_files. |
| 119 | """ |
Adrian Roos | 81b2c9b | 2024-05-17 12:04:16 +0200 | [diff] [blame] | 120 | if not project: |
Anushree Ganjam | da9c624 | 2023-10-19 14:33:35 -0700 | [diff] [blame] | 121 | return False |
Adrian Roos | 81b2c9b | 2024-05-17 12:04:16 +0200 | [diff] [blame] | 122 | if project == 'platform/frameworks/base': |
Anushree Ganjam | da9c624 | 2023-10-19 14:33:35 -0700 | [diff] [blame] | 123 | return should_run_files(files) |
Adrian Roos | 81b2c9b | 2024-05-17 12:04:16 +0200 | [diff] [blame] | 124 | # Default case, run for all other projects which calls this script. |
Anushree Ganjam | da9c624 | 2023-10-19 14:33:35 -0700 | [diff] [blame] | 125 | return True |
| 126 | |
| 127 | |
| 128 | def should_run_files(files): |
| 129 | """Returns a boolean if this check should run with these files.""" |
| 130 | if not files: |
| 131 | return False |
| 132 | if 'packages/SystemUI' in files: |
| 133 | return True |
| 134 | return False |
| 135 | |
| 136 | |
| 137 | if __name__ == '__main__': |
| 138 | main() |