Skip to content

Commit 7dbd8c6

Browse files
wuliang229copybara-github
authored andcommitted
chore: add file contents check in a GitHub action
PiperOrigin-RevId: 767710673
1 parent b7ebb69 commit 7dbd8c6

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: "Check file contents"
16+
17+
on:
18+
pull_request:
19+
paths:
20+
- '**.py'
21+
22+
jobs:
23+
check-file-contents:
24+
runs-on: ubuntu-latest
25+
steps:
26+
- name: Checkout Code
27+
uses: actions/checkout@v4
28+
with:
29+
fetch-depth: 2
30+
31+
- name: Check for logger pattern in all changed Python files
32+
run: |
33+
git fetch origin ${{ github.base_ref }}
34+
CHANGED_FILES=$(git diff --diff-filter=ACMR --name-only origin/${{ github.base_ref }}...HEAD | grep -E '\.py$' || true)
35+
if [ -n "$CHANGED_FILES" ]; then
36+
echo "Changed Python files to check:"
37+
echo "$CHANGED_FILES"
38+
echo ""
39+
40+
# Check for 'logger = logging.getLogger(__name__)' in changed .py files.
41+
# The grep command will exit with a non-zero status code if the pattern is not found.
42+
# We invert the exit code with ! so the step succeeds if the pattern is NOT found.
43+
set +e
44+
FILES_WITH_FORBIDDEN_LOGGER=$(grep -lE 'logger = logging\.getLogger\(__name__\)' $CHANGED_FILES)
45+
GREP_EXIT_CODE=$?
46+
set -e
47+
48+
# grep exits with 0 if matches are found, 1 if no matches are found.
49+
# A non-zero exit code other than 1 indicates an error.
50+
if [ $GREP_EXIT_CODE -eq 0 ]; then
51+
echo "❌ Found forbidden use of 'logger = logging.getLogger(__name__)'. Please use 'logger = logging.getLogger('google_adk.' + __name__)' instead."
52+
echo "The following files contain the forbidden pattern:"
53+
echo "$FILES_WITH_FORBIDDEN_LOGGER"
54+
exit 1
55+
elif [ $GREP_EXIT_CODE -eq 1 ]; then
56+
echo "✅ No instances of 'logger = logging.getLogger(__name__)' found in changed Python files."
57+
fi
58+
else
59+
echo "✅ No relevant Python files found."
60+
fi
61+
62+
- name: Check for import pattern in certain changed Python files
63+
run: |
64+
git fetch origin ${{ github.base_ref }}
65+
CHANGED_FILES=$(git diff --diff-filter=ACMR --name-only origin/${{ github.base_ref }}...HEAD | grep -E '\.py$' | grep -v -E '__init__.py$|version.py$|tests/.*|contributing/samples/' || true)
66+
if [ -n "$CHANGED_FILES" ]; then
67+
echo "Changed Python files to check:"
68+
echo "$CHANGED_FILES"
69+
echo ""
70+
71+
# Use grep -L to find files that DO NOT contain the pattern.
72+
# This command will output a list of non-compliant files.
73+
FILES_MISSING_IMPORT=$(grep -L 'from __future__ import annotations' $CHANGED_FILES)
74+
75+
# Check if the list of non-compliant files is empty
76+
if [ -z "$FILES_MISSING_IMPORT" ]; then
77+
echo "✅ All modified Python files include 'from __future__ import annotations'."
78+
exit 0
79+
else
80+
echo "❌ The following files are missing 'from __future__ import annotations':"
81+
echo "$FILES_MISSING_IMPORT"
82+
echo "This import is required to allow forward references in type annotations without quotes."
83+
exit 1
84+
fi
85+
else
86+
echo "✅ No relevant Python files found."
87+
fi
88+
89+
- name: Check for import from cli package in certain changed Python files
90+
run: |
91+
git fetch origin ${{ github.base_ref }}
92+
CHANGED_FILES=$(git diff --diff-filter=ACMR --name-only origin/${{ github.base_ref }}...HEAD | grep -E '\.py$' | grep -v -E 'cli/.*|tests/.*|contributing/samples/' || true)
93+
if [ -n "$CHANGED_FILES" ]; then
94+
echo "Changed Python files to check:"
95+
echo "$CHANGED_FILES"
96+
echo ""
97+
98+
set +e
99+
FILES_WITH_FORBIDDEN_IMPORT=$(grep -lE '^from.*cli.*import.*$' $CHANGED_FILES)
100+
GREP_EXIT_CODE=$?
101+
set -e
102+
103+
if [[ $GREP_EXIT_CODE -eq 0 ]]; then
104+
echo "❌ Do not import from the cli package outside of the cli package. If you need to reuse the code elsewhere, please move the code outside of the cli package."
105+
echo "The following files contain the forbidden pattern:"
106+
echo "$FILES_WITH_FORBIDDEN_IMPORT"
107+
exit 1
108+
else
109+
echo "✅ No instances of importing from the cli package found in relevant changed Python files."
110+
fi
111+
else
112+
echo "✅ No relevant Python files found."
113+
fi

0 commit comments

Comments
 (0)