Skip to content

Changed the unify process to add a constant of the pack version to each Python file. #4844

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

Merged
merged 13 commits into from
Mar 23, 2025
4 changes: 4 additions & 0 deletions .changelog/4844.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- description: Changed the unify process to add a constant of the pack version to each Python file.
type: internal
pr_number: 4844
22 changes: 13 additions & 9 deletions demisto_sdk/commands/prepare_content/integration_script_unifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,11 @@ def insert_script_to_yml(
)
pack_metadata = get_pack_metadata(file_path=str(package_path))
pack_version = pack_metadata.get("currentVersion", "")
pack_name = pack_metadata.get("name", "")
if pack_name and pack_version:
pack_id = package_path.parts[package_path.parts.index("Packs") + 1]

if pack_id and pack_version:
script_code = IntegrationScriptUnifier.insert_pack_version(
script_type, script_code, pack_version, pack_name
script_type, script_code, pack_version, pack_id
)

if script_type == ".py":
Expand Down Expand Up @@ -521,7 +522,7 @@ def insert_module_code(

@staticmethod
def insert_pack_version(
script_type: str, script_code: str, pack_version: str, pack_name: str
script_type: str, script_code: str, pack_version: str, pack_id: str
) -> str:
"""
Inserts the pack version to the script so it will be easy to know what was the contribution original pack version.
Expand All @@ -530,7 +531,7 @@ def insert_pack_version(
script_type (str): The type of the script (e.g., ".js", ".ps1", ".py").
script_code (str): The integration code.
pack_version (str): The pack version.
pack_name (str): The pack name.
pack_id (str): The pack id.

Returns:
str: The integration script with the pack version appended if needed, otherwise returns the original script.
Expand All @@ -551,17 +552,20 @@ def insert_pack_version(
existing_pack_info_debug_log_re = (
r"demisto\.debug\('pack name = .*?, pack version = .*?'\)"
)
pack_name_escaped = pack_name.replace("'", "\\'")
pack_info_debug_statement = f"demisto.debug('pack name = {pack_name_escaped}, pack version = {pack_version}')"
global_name = f"{pack_id.upper()}_PACK_VERSION"
pack_version_statement = (
f"{global_name} = '{pack_version}'"
f"\ndemisto.debug(f'pack id = {pack_id}, pack version = {{{global_name}}}')"
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't it be only 2?

Suggested change
f"\ndemisto.debug(f'pack id = {pack_id}, pack version = {{{global_name}}}')"
f"\ndemisto.debug(f'pack id = {pack_id}, pack version = {{global_name}}')"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, with two, will it print "pack id = the_pack_id, pack version = {global_name}"

Copy link
Contributor

Choose a reason for hiding this comment

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

r u sure?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Screenshot 2025-03-10 at 13 07 15

)

if re.search(existing_pack_info_debug_log_re, script_code):
script_code = re.sub(
existing_pack_info_debug_log_re,
pack_info_debug_statement,
pack_version_statement,
script_code,
) # replace existing (edge case)
else: # common case
script_code = f"{pack_info_debug_statement}\n{script_code}"
script_code = f"{pack_version_statement}\n{script_code}"
return script_code
return script_code

Expand Down
20 changes: 7 additions & 13 deletions demisto_sdk/commands/prepare_content/tests/yml_unifier_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,27 +602,21 @@
"""
def main():
""",
["test pack", "1.0.3"],
"demisto.debug('pack name = test pack, pack version = 1.0.3')",
["testPack", "1.0.3"],
"TESTPACK_PACK_VERSION = '1.0.3'"
"\ndemisto.debug(f'pack id = testPack, pack version = {TESTPACK_PACK_VERSION}')",
id="script without version",
),
pytest.param(
"""
demisto.debug('pack name = test pack, pack version = 1.0.3')
demisto.debug('pack id = test pack, pack version = 1.0.3')
def main():
""",
["test pack", "1.0.4"],
"demisto.debug('pack name = test pack, pack version = 1.0.4')",
["testPack", "1.0.4"],
"TESTPACK_PACK_VERSION = '1.0.4'"
"\ndemisto.debug(f'pack id = testPack, pack version = {TESTPACK_PACK_VERSION}')",
id="script with version",
),
pytest.param(
"""
def main():
""",
["here's a pack", "1.0.5"],
"demisto.debug('pack name = here\\'s a pack, pack version = 1.0.5')",
id="pack name with apostrophe",
),
],
)
def test_insert_pack_version_and_script_to_yml_python_script(
Expand Down Expand Up @@ -719,7 +713,7 @@

test_yml_unified = copy.deepcopy(test_yml_data)

yml_unified, script_path = IntegrationScriptUnifier.insert_script_to_yml(

Check failure on line 716 in demisto_sdk/commands/prepare_content/tests/yml_unifier_test.py

View workflow job for this annotation

GitHub Actions / Unit Tests / Python 3.9

test_insert_script_to_yml[/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/VulnDB/-Integrations-/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/VulnDB/VulnDB] ValueError: tuple.index(x): x not in tuple

Check failure on line 716 in demisto_sdk/commands/prepare_content/tests/yml_unifier_test.py

View workflow job for this annotation

GitHub Actions / Unit Tests / Python 3.9

test_insert_script_to_yml[/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/CalculateGeoDistance/-Scripts-/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/CalculateGeoDistance/CalculateGeoDistance] ValueError: tuple.index(x): x not in tuple

Check failure on line 716 in demisto_sdk/commands/prepare_content/tests/yml_unifier_test.py

View workflow job for this annotation

GitHub Actions / Unit Tests / Python 3.10

test_insert_script_to_yml[/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/VulnDB/-Integrations-/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/VulnDB/VulnDB] ValueError: tuple.index(x): x not in tuple

Check failure on line 716 in demisto_sdk/commands/prepare_content/tests/yml_unifier_test.py

View workflow job for this annotation

GitHub Actions / Unit Tests / Python 3.10

test_insert_script_to_yml[/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/CalculateGeoDistance/-Scripts-/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/CalculateGeoDistance/CalculateGeoDistance] ValueError: tuple.index(x): x not in tuple

Check failure on line 716 in demisto_sdk/commands/prepare_content/tests/yml_unifier_test.py

View workflow job for this annotation

GitHub Actions / Unit Tests / Python 3.11

test_insert_script_to_yml[/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/VulnDB/-Integrations-/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/VulnDB/VulnDB] ValueError: tuple.index(x): x not in tuple

Check failure on line 716 in demisto_sdk/commands/prepare_content/tests/yml_unifier_test.py

View workflow job for this annotation

GitHub Actions / Unit Tests / Python 3.11

test_insert_script_to_yml[/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/CalculateGeoDistance/-Scripts-/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/CalculateGeoDistance/CalculateGeoDistance] ValueError: tuple.index(x): x not in tuple

Check failure on line 716 in demisto_sdk/commands/prepare_content/tests/yml_unifier_test.py

View workflow job for this annotation

GitHub Actions / Unit Tests / Python 3.12

test_insert_script_to_yml[/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/VulnDB/-Integrations-/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/VulnDB/VulnDB] ValueError: tuple.index(x): x not in tuple

Check failure on line 716 in demisto_sdk/commands/prepare_content/tests/yml_unifier_test.py

View workflow job for this annotation

GitHub Actions / Unit Tests / Python 3.12

test_insert_script_to_yml[/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/CalculateGeoDistance/-Scripts-/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/CalculateGeoDistance/CalculateGeoDistance] ValueError: tuple.index(x): x not in tuple
Path(package_path), ".py", test_yml_unified, test_yml_data, is_script_package
)

Expand Down Expand Up @@ -765,7 +759,7 @@
else:
test_yml_data["script"]["script"] = "blah"

IntegrationScriptUnifier.insert_script_to_yml(

Check failure on line 762 in demisto_sdk/commands/prepare_content/tests/yml_unifier_test.py

View workflow job for this annotation

GitHub Actions / Unit Tests / Python 3.9

test_insert_script_to_yml_exceptions[/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/VulnDB/-Integrations-/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/VulnDB/VulnDB] ValueError: tuple.index(x): x not in tuple

Check failure on line 762 in demisto_sdk/commands/prepare_content/tests/yml_unifier_test.py

View workflow job for this annotation

GitHub Actions / Unit Tests / Python 3.10

test_insert_script_to_yml_exceptions[/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/VulnDB/-Integrations-/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/VulnDB/VulnDB] ValueError: tuple.index(x): x not in tuple

Check failure on line 762 in demisto_sdk/commands/prepare_content/tests/yml_unifier_test.py

View workflow job for this annotation

GitHub Actions / Unit Tests / Python 3.11

test_insert_script_to_yml_exceptions[/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/VulnDB/-Integrations-/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/VulnDB/VulnDB] ValueError: tuple.index(x): x not in tuple

Check failure on line 762 in demisto_sdk/commands/prepare_content/tests/yml_unifier_test.py

View workflow job for this annotation

GitHub Actions / Unit Tests / Python 3.12

test_insert_script_to_yml_exceptions[/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/VulnDB/-Integrations-/home/runner/work/demisto-sdk/demisto-sdk/demisto_sdk/tests/test_files/VulnDB/VulnDB] ValueError: tuple.index(x): x not in tuple
Path(package_path), ".py", {"script": {}}, test_yml_data, is_script_package
)

Expand Down
Loading