Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New script to generate the command docs #4664

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Shellyber
Copy link
Contributor

@Shellyber Shellyber commented Nov 12, 2024

Related Issues

fixes: https://jira-dc.paloaltonetworks.com/browse/CIAC-12077
https://jira-dc.paloaltonetworks.com/browse/CIAC-12078

Description

  • Script to generate the command description and the options if the command setup has been changed.
  • Pre-commit hook to run the script.

Copy link
Contributor

@ShahafBenYakir ShahafBenYakir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

  1. Why did we create two Python files?
  2. I want us to run multiple tests on this i.e make all sorts of changes - delete, add modified etc as creative as you can bee and fix issues.
  3. Do we have infra for unit tests?

Comment on lines +10 to +29
def get_current_branch():
"""Returns the current Git branch name."""
result = subprocess.run(["git", "rev-parse", "--abbrev-ref", "HEAD"], capture_output=True, text=True)
return result.stdout.strip()


def get_modified_files():
"""Returns a list of files modified in the current commit."""
result = subprocess.run(["git", "diff", "--cached", "--name-only"], capture_output=True, text=True)
return result.stdout.splitlines()


def extract_changed_commands(modified_files):
"""Extract command names from modified _setup.py files."""
changed_commands = []
for file in modified_files:
if file.endswith("_setup.py"):
command_name = Path(file).stem.replace("_setup", "")
changed_commands.append(command_name)
return changed_commands
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add return argument and return value docstring as well as a return type: (-> str and so on)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True for all functions

# Check if the branch should be excluded
current_branch = get_current_branch()
if re.match(EXCLUDED_BRANCHES_REGEX, current_branch):
print(f"Pre-commit hook skipped on branch '{current_branch}'")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print(f"Pre-commit hook skipped on branch '{current_branch}'")
print(f"Generate docs pre-commit hook skipped on branch '{current_branch}'")

Comment on lines +44 to +46
if not changed_commands:
print("No modified _setup.py files found. Skipping documentation generation.")
sys.exit(0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does it make sense tho? Per the hook config (files: ^.*_setup\.py$) the hook only runs when the setup file change no?


# Run the documentation generation script with all changed commands
print(f"Generating documentation for modified commands: {changed_commands}")
subprocess.run([sys.executable, "generate_docs.py", *changed_commands])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we using a subprocess here? why not just import and run the relevant python module?


# Receive the list of modified files from command-line arguments
modified_files = sys.argv[1:]
changed_commands = extract_changed_commands(modified_files)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused, you called this script with changed commands (not changed files) - subprocess.run([sys.executable, "generate_docs.py", *changed_commands])
Why are we trying to re-extract the command names?

options_text += (
f"- **{param_name}**: {param.help or 'No description provided'}\n"
)
if param.default is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is not None:? Isn't if param.default enough? if for example it is'' you still don't want the condition to apply right?


def get_command_description(command_name: str) -> str:
"""Retrieve the description (docstring) for the command."""
command = get_sdk_command(command_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the response is "No README found for command..."?


def generate_docs_for_command(command_name: str):
"""Generate documentation for a specific command."""
description = get_command_description(command_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we ok with the description being 'No description provided'? Where is it going be added to?

command_doc_path = Path("demisto_sdk/commands") / command_name / "README.md"

if not command_doc_path.exists():
print(f"README.md not found for command: {command_name}") # noqa: T201
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not generate a new one?

"""Generate documentation for a specific command."""
description = get_command_description(command_name)
options = get_command_options(command_name)
update_readme(command_name, description, options)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it enough to write the file? what about committing etc?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants