From acdd27ab8c48ce1f531060cc1b97e2cb3172f44a Mon Sep 17 00:00:00 2001 From: IITI-tushar <019saxenatushar@gmail.com> Date: Sun, 29 Dec 2024 17:45:52 +0530 Subject: [PATCH 1/4] done --- .github/workflows/css_check.py | 119 +++++++++++++++++++++++++++++ .github/workflows/pull-request.yml | 6 ++ 2 files changed, 125 insertions(+) create mode 100644 .github/workflows/css_check.py diff --git a/.github/workflows/css_check.py b/.github/workflows/css_check.py new file mode 100644 index 0000000000..c65711f06d --- /dev/null +++ b/.github/workflows/css_check.py @@ -0,0 +1,119 @@ +"""Check TypeScript files for CSS violations and embedded CSS.""" + +import argparse +import os +import re +import sys + + +def check_embedded_css(content: str) -> list: + """ + Check for embedded CSS in the content. + + Args: + content: The content of the file to check. + + Returns: + A list of embedded CSS violations found. + """ + embedded_css_pattern = r'#([0-9a-fA-F]{3}){1,2}' + return re.findall(embedded_css_pattern, content) + + +def check_files(directory: str, exclude_files: list, exclude_directories: list) -> tuple: + """ + Check TypeScript files for CSS violations and print correct CSS imports. + + Args: + directory: The directory to check. + exclude_files: List of files to exclude from analysis. + exclude_directories: List of directories to exclude from analysis. + + Returns: + A tuple containing lists of violations, correct CSS imports, and embedded CSS violations. + """ + violations = [] + correct_css_imports = [] + embedded_css_violations = [] + + # Normalize exclude paths + exclude_files = set(os.path.abspath(file) for file in exclude_files) + exclude_directories = set(os.path.abspath(dir) for dir in exclude_directories) + + for root, _, files in os.walk(directory): + # Skip excluded directories + if any(root.startswith(exclude_dir) for exclude_dir in exclude_directories): + continue + + for file in files: + file_path = os.path.abspath(os.path.join(root, file)) + + # Skip excluded files + if file_path in exclude_files: + continue + + # Process TypeScript files + if file.endswith((".ts", ".tsx")) and "test" not in root: + with open(file_path, "r", encoding="utf-8") as f: + content = f.read() + + # Check for CSS imports with an improved regex pattern + css_imports = re.findall(r'import\s+.*?["\'](.*?\.css)["\'];', content) + for css_file in css_imports: + # Check if the CSS import ends with /app.module.css + if css_file.endswith("/app.module.css"): + correct_css_imports.append(f"Correct CSS import ({css_file}) in {file_path}") + else: + violations.append(f"Invalid CSS import ({css_file}) in {file_path}") + + # Check for embedded CSS + embedded_css = check_embedded_css(content) + if embedded_css: + embedded_css_violations.append(f"Embedded CSS found in {file_path}: {', '.join(embedded_css)}") + + return violations, correct_css_imports, embedded_css_violations + + +def main(): + """Run the CSS check script.""" + parser = argparse.ArgumentParser(description="Check for CSS violations in TypeScript files.") + parser.add_argument("--directory", required=True, help="Directory to check.") + parser.add_argument( + "--exclude_files", nargs="*", default=[], + help="Specific files to exclude from analysis." + ) + parser.add_argument( + "--exclude_directories", nargs="*", default=[], + help="Directories to exclude from analysis." + ) + args = parser.parse_args() + + violations, correct_css_imports, embedded_css_violations = check_files( + directory=args.directory, + exclude_files=args.exclude_files, + exclude_directories=args.exclude_directories + ) + + if violations: + print("\nCSS Import Violations:") + print("\n".join(violations)) + + if embedded_css_violations: + print("\nEmbedded CSS Violations:") + print("\n".join(embedded_css_violations)) + + if correct_css_imports: + print("\nCorrect CSS Imports:") + print("\n".join(correct_css_imports)) + else: + print("\nNo correct CSS imports found.") + + if violations or embedded_css_violations: + sys.exit(1) # Exit with error code if violations found + else: + print("\nNo CSS violations found.") + sys.exit(0) # Exit with success code + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 4708e39869..06875cea79 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -39,6 +39,12 @@ jobs: run: | chmod +x ./.github/workflows/countline.py ./.github/workflows/countline.py --lines 600 --exclude_files src/screens/LoginPage/LoginPage.tsx src/GraphQl/Queries/Queries.ts src/screens/OrgList/OrgList.tsx src/GraphQl/Mutations/mutations.ts src/components/EventListCard/EventListCardModals.tsx src/components/TagActions/TagActionsMocks.ts src/utils/interfaces.ts src/screens/MemberDetail/MemberDetail.tsx + + # Run the CSS import check script + - name: Check for CSS violations and print correct imports + run: | + chmod +x ./.github/workflows/css_check.py + ./.github/workflows/css_check.py --directory . - name: Get changed TypeScript files id: changed-files From adab7234a27163492de7b43f81255064222a04c1 Mon Sep 17 00:00:00 2001 From: IITI-tushar <165766280+IITI-tushar@users.noreply.github.com> Date: Sun, 29 Dec 2024 18:13:08 +0530 Subject: [PATCH 2/4] Update css_check.py --- .github/workflows/css_check.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/css_check.py b/.github/workflows/css_check.py index c65711f06d..1d6db9e9a1 100644 --- a/.github/workflows/css_check.py +++ b/.github/workflows/css_check.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 +# -*- coding: UTF-8 -*- """Check TypeScript files for CSS violations and embedded CSS.""" import argparse @@ -116,4 +118,4 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + main() From 2cdc7e0f95852f580700afedd5f2db852db0d362 Mon Sep 17 00:00:00 2001 From: IITI-tushar <165766280+IITI-tushar@users.noreply.github.com> Date: Tue, 7 Jan 2025 15:35:50 +0530 Subject: [PATCH 3/4] Update css_check.py --- .github/workflows/css_check.py | 77 +++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 24 deletions(-) diff --git a/.github/workflows/css_check.py b/.github/workflows/css_check.py index 1d6db9e9a1..4c6aef06d2 100644 --- a/.github/workflows/css_check.py +++ b/.github/workflows/css_check.py @@ -18,11 +18,13 @@ def check_embedded_css(content: str) -> list: Returns: A list of embedded CSS violations found. """ - embedded_css_pattern = r'#([0-9a-fA-F]{3}){1,2}' + embedded_css_pattern = r"#([0-9a-fA-F]{3}){1,2}" # Matches CSS color codes return re.findall(embedded_css_pattern, content) -def check_files(directory: str, exclude_files: list, exclude_directories: list) -> tuple: +def check_files( + directory: str, exclude_files: list, exclude_directories: list, allowed_css_patterns: list +) -> tuple: """ Check TypeScript files for CSS violations and print correct CSS imports. @@ -30,6 +32,7 @@ def check_files(directory: str, exclude_files: list, exclude_directories: list) directory: The directory to check. exclude_files: List of files to exclude from analysis. exclude_directories: List of directories to exclude from analysis. + allowed_css_patterns: List of allowed CSS file patterns. Returns: A tuple containing lists of violations, correct CSS imports, and embedded CSS violations. @@ -56,44 +59,69 @@ def check_files(directory: str, exclude_files: list, exclude_directories: list) # Process TypeScript files if file.endswith((".ts", ".tsx")) and "test" not in root: - with open(file_path, "r", encoding="utf-8") as f: - content = f.read() - - # Check for CSS imports with an improved regex pattern - css_imports = re.findall(r'import\s+.*?["\'](.*?\.css)["\'];', content) - for css_file in css_imports: - # Check if the CSS import ends with /app.module.css - if css_file.endswith("/app.module.css"): - correct_css_imports.append(f"Correct CSS import ({css_file}) in {file_path}") - else: - violations.append(f"Invalid CSS import ({css_file}) in {file_path}") - - # Check for embedded CSS - embedded_css = check_embedded_css(content) - if embedded_css: - embedded_css_violations.append(f"Embedded CSS found in {file_path}: {', '.join(embedded_css)}") + try: + with open(file_path, "r", encoding="utf-8") as f: + content = f.read() + except (IOError, UnicodeDecodeError) as e: + print(f"Error reading file {file_path}: {e}") + continue + + # Check for CSS imports with an improved regex pattern + css_imports = re.findall( + r'import\s+.*?["\'](.*?\.css)["\'];', content + ) + for css_file in css_imports: + # Check if the CSS import matches the allowed patterns + if any(css_file.endswith(pattern) for pattern in allowed_css_patterns): + correct_css_imports.append( + f"Correct CSS import ({css_file}) in {file_path}" + ) + else: + violations.append( + f"Invalid CSS import ({css_file}) in {file_path}" + ) + + # Check for embedded CSS + embedded_css = check_embedded_css(content) + if embedded_css: + embedded_css_violations.append( + f"Embedded CSS found in {file_path}: {', '.join(embedded_css)}" + ) return violations, correct_css_imports, embedded_css_violations def main(): """Run the CSS check script.""" - parser = argparse.ArgumentParser(description="Check for CSS violations in TypeScript files.") + parser = argparse.ArgumentParser( + description="Check for CSS violations in TypeScript files." + ) parser.add_argument("--directory", required=True, help="Directory to check.") parser.add_argument( - "--exclude_files", nargs="*", default=[], - help="Specific files to exclude from analysis." + "--exclude_files", + nargs="*", + default=[], + help="Specific files to exclude from analysis.", + ) + parser.add_argument( + "--exclude_directories", + nargs="*", + default=[], + help="Directories to exclude from analysis.", ) parser.add_argument( - "--exclude_directories", nargs="*", default=[], - help="Directories to exclude from analysis." + "--allowed_css_patterns", + nargs="*", + default=["app.module.css"], + help="Allowed CSS file patterns.", ) args = parser.parse_args() violations, correct_css_imports, embedded_css_violations = check_files( directory=args.directory, exclude_files=args.exclude_files, - exclude_directories=args.exclude_directories + exclude_directories=args.exclude_directories, + allowed_css_patterns=args.allowed_css_patterns, ) if violations: @@ -119,3 +147,4 @@ def main(): if __name__ == "__main__": main() + From 2218e9f825a1d283509c0fc28c298f981ece8367 Mon Sep 17 00:00:00 2001 From: IITI-tushar <165766280+IITI-tushar@users.noreply.github.com> Date: Tue, 7 Jan 2025 15:38:46 +0530 Subject: [PATCH 4/4] Update pull-request.yml --- .github/workflows/pull-request.yml | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index d486d367cc..20c283c797 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -39,14 +39,21 @@ jobs: run: | chmod +x ./.github/workflows/scripts/countline.py ./.github/workflows/scripts/countline.py --lines 600 --exclude_files src/screens/LoginPage/LoginPage.tsx src/GraphQl/Queries/Queries.ts src/screens/OrgList/OrgList.tsx src/GraphQl/Mutations/mutations.ts src/components/EventListCard/EventListCardModals.tsx src/components/TagActions/TagActionsMocks.ts src/utils/interfaces.ts src/screens/MemberDetail/MemberDetail.tsx - - # Run the CSS import check script + + # Run the CSS import check script - name: Check for CSS violations and print correct imports run: | - chmod +x ./.github/workflows/css_check.py - ./.github/workflows/css_check.py --directory . - - - name: Get changed TypeScript files + if [ ! -f ./.github/workflows/css_check.py ]; then + echo "Error: CSS check script not found" + exit 1 + fi + chmod +x ./.github/workflows/css_check.py + ./.github/workflows/css_check.py --directory . || { + echo "Error: CSS check failed" + exit 1 + } + + - name: Get changed TypeScript files id: changed-files uses: tj-actions/changed-files@v45 - name: Check formatting