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

Add firstCreated Field #4707

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changelog/4707.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- description: Fixed an issue where the created date would conflict with the new XSOAR system field with the same name, by introducing a new field named `firstCreated`.
type: fix
pr_number: 4707
2 changes: 2 additions & 0 deletions TestSuite/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import shutil
from pathlib import Path
from typing import List, Optional
from unittest.mock import MagicMock

from demisto_sdk.commands.common.constants import DEMISTO_GIT_PRIMARY_BRANCH
from demisto_sdk.commands.common.git_util import GitUtil
Expand Down Expand Up @@ -320,6 +321,7 @@ def create_graph(self, output_path: Path = None):
return self.graph_interface

def create_pack(self, name: Optional[str] = None):
GitUtil.get_file_creation_date = MagicMock(return_value="2024-12-19T11:49:45Z") # type: ignore[assignment]
if name is None:
name = f"pack_{len(self.packs)}"
pack = Pack(self._packs_path, name, repo=self)
Expand Down
10 changes: 9 additions & 1 deletion demisto_sdk/commands/common/git_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import re
from datetime import datetime
from functools import lru_cache
from pathlib import Path
from typing import List, Optional, Sequence, Set, Tuple, Union
Expand All @@ -18,6 +19,7 @@
from demisto_sdk.commands.common.constants import (
DEMISTO_GIT_PRIMARY_BRANCH,
DEMISTO_GIT_UPSTREAM,
ISO_TIMESTAMP_FORMAT,
PACKS_FOLDER,
)
from demisto_sdk.commands.common.logger import logger
Expand Down Expand Up @@ -1123,7 +1125,7 @@ def fetch_all(self):

def commit_files(self, commit_message: str, files: Union[List, str] = "."):
self.repo.git.add(files)
self.repo.index.commit(commit_message)
return self.repo.index.commit(commit_message)
Copy link
Contributor

Choose a reason for hiding this comment

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

why its needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the test_get_file_creation_date test, in order to check that the commit time is the same as we return.


def has_file_permissions_changed(
self, file_path: str, ci: bool = False
Expand Down Expand Up @@ -1194,3 +1196,9 @@ def stage_file(self, file_path: Union[Path, str]):
logger.debug(f"Staged file '{file_path}'")
else:
logger.error(f"File '{file_path}' doesn't exist. Not adding.")

def get_file_creation_date(self, file_path: Path) -> str:
if commits := list(self.repo.iter_commits(paths=file_path)):
first_commit = commits[-1]
return first_commit.authored_datetime.strftime(ISO_TIMESTAMP_FORMAT)
return datetime.now().strftime(ISO_TIMESTAMP_FORMAT)
21 changes: 21 additions & 0 deletions demisto_sdk/commands/common/tests/git_util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from git import Blob

from demisto_sdk.commands.common.constants import ISO_TIMESTAMP_FORMAT
from TestSuite.repo import Repo


Expand Down Expand Up @@ -144,3 +145,23 @@
git_util = GitUtil(repo)
assert git_util.repo is not None
assert git_util.repo.working_dir == repo.working_dir


def test_get_file_creation_date(git_repo: Repo):
"""
Given:
- A git repo and a file in it.

When:
- Getting the creation time of the given file.

Then:
- The correct creation time of the file is returned.
"""
file = Path("pack_metadata.json")
git_repo.make_file(str(file), "{}")
commit = git_repo.git_util.commit_files(f"added {file}", str(file))

file_creation_date = git_repo.git_util.get_file_creation_date(file)

assert file_creation_date == commit.authored_datetime.strftime(ISO_TIMESTAMP_FORMAT)

Check failure on line 167 in demisto_sdk/commands/common/tests/git_util_test.py

View workflow job for this annotation

GitHub Actions / Unit Tests / Python 3.9

test_get_file_creation_date AssertionError: assert '2024-12-19T11:49:45Z' == '2024-12-31T12:25:08Z' - 2024-12-31T12:25:08Z + 2024-12-19T11:49:45Z

Check failure on line 167 in demisto_sdk/commands/common/tests/git_util_test.py

View workflow job for this annotation

GitHub Actions / Unit Tests / Python 3.10

test_get_file_creation_date AssertionError: assert '2024-12-19T11:49:45Z' == '2024-12-31T12:25:08Z' - 2024-12-31T12:25:08Z + 2024-12-19T11:49:45Z

Check failure on line 167 in demisto_sdk/commands/common/tests/git_util_test.py

View workflow job for this annotation

GitHub Actions / Unit Tests / Python 3.11

test_get_file_creation_date AssertionError: assert '2024-12-19T11:49:45Z' == '2024-12-31T12:25:02Z' - 2024-12-31T12:25:02Z + 2024-12-19T11:49:45Z

Check failure on line 167 in demisto_sdk/commands/common/tests/git_util_test.py

View workflow job for this annotation

GitHub Actions / Unit Tests / Python 3.12

test_get_file_creation_date AssertionError: assert '2024-12-19T11:49:45Z' == '2024-12-31T12:25:34Z' - 2024-12-31T12:25:34Z + 2024-12-19T11:49:45Z
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PackMetadata(BaseModel):
name: str
display_name: str
description: Optional[str]
created: Optional[str]
created: Optional[str] = Field(alias="firstCreated")
updated: Optional[str] = Field("")
legacy: Optional[bool]
support: str = Field("")
Expand Down
6 changes: 4 additions & 2 deletions demisto_sdk/commands/content_graph/parsers/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ def __init__(self, path: Path, metadata: Dict[str, Any]) -> None:
self.display_name: str = metadata.get("name", "")
self.description: str = metadata.get("description", "")
self.support: str = metadata.get("support", "")
self.created: str = metadata.get("created") or NOW
self.created = metadata.get("firstCreated") or metadata.get("created")
if not self.created:
self.created = GitUtil().get_file_creation_date(file_path=path)
self.updated: str = metadata.get("updated") or NOW
self.legacy: bool = metadata.get(
"legacy", metadata.get("partnerId") is None
Expand Down Expand Up @@ -377,7 +379,7 @@ def field_mapping(self):
return {
"name": "name",
"description": "description",
"created": "created",
"created": "firstCreated",
"support": "support",
"email": "email",
"price": "price",
Expand Down
4 changes: 4 additions & 0 deletions demisto_sdk/commands/init/initiator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import re
import shutil
from datetime import datetime
from distutils.dir_util import copy_tree
from pathlib import Path
from typing import Dict, List, Optional, Set, Tuple
Expand Down Expand Up @@ -29,6 +30,7 @@
INTEGRATION_CATEGORIES,
INTEGRATIONS_DIR,
INTEGRATIONS_DIR_REGEX,
ISO_TIMESTAMP_FORMAT,
JOBS_DIR,
LAYOUTS_DIR,
MARKETPLACE_LIVE_DISCUSSIONS,
Expand Down Expand Up @@ -68,6 +70,7 @@
from demisto_sdk.commands.secrets.secrets import SecretsValidator

ANALYTICS_AND_SIEM_CATEGORY = "Analytics & SIEM"
NOW = datetime.now().strftime(ISO_TIMESTAMP_FORMAT)


def extract_values_from_nested_dict_to_a_set(given_dictionary: dict, return_set: set):
Expand Down Expand Up @@ -683,6 +686,7 @@ def create_metadata(fill_manually: bool, data: Dict = None) -> Dict:
"useCases": [],
"keywords": [],
"marketplaces": MARKETPLACES,
"firstCreated": NOW,
}

if data:
Expand Down
11 changes: 10 additions & 1 deletion demisto_sdk/commands/init/tests/initiator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
from demisto_sdk.commands.common.handlers import DEFAULT_JSON_HANDLER as json
from demisto_sdk.commands.common.handlers import DEFAULT_YAML_HANDLER as yaml
from demisto_sdk.commands.common.tools import get_file
from demisto_sdk.commands.init.initiator import ANALYTICS_AND_SIEM_CATEGORY, Initiator
from demisto_sdk.commands.init.initiator import (
ANALYTICS_AND_SIEM_CATEGORY,
NOW,
Initiator,
)
from TestSuite.test_tools import ChangeCWD

DIR_NAME = "DirName"
Expand Down Expand Up @@ -129,6 +133,7 @@ def test_create_metadata_non_filled_manually(self, initiator):
"useCases": [],
"keywords": [],
"marketplaces": MARKETPLACES,
"firstCreated": NOW,
}

def test_create_metadata_non_filled_manually_with_data(self, initiator):
Expand Down Expand Up @@ -162,6 +167,7 @@ def test_create_metadata_non_filled_manually_with_data(self, initiator):
"useCases": [],
"keywords": [],
"marketplaces": MARKETPLACES,
"firstCreated": NOW,
}

def test_create_metadata_partner(self, monkeypatch, initiator):
Expand Down Expand Up @@ -214,6 +220,7 @@ def test_create_metadata_partner(self, monkeypatch, initiator):
"useCases": [],
"githubUser": [],
"marketplaces": MARKETPLACES,
"firstCreated": NOW,
}

def test_create_metadata_partner_wrong_url(self, monkeypatch, initiator):
Expand Down Expand Up @@ -267,6 +274,7 @@ def test_create_metadata_partner_wrong_url(self, monkeypatch, initiator):
"useCases": [],
"githubUser": [],
"marketplaces": ["xsoar"],
"firstCreated": NOW,
}

def test_create_metadata_community(self, monkeypatch, initiator):
Expand Down Expand Up @@ -317,6 +325,7 @@ def test_create_metadata_community(self, monkeypatch, initiator):
"useCases": [],
"githubUser": [],
"marketplaces": ["marketplacev2"],
"firstCreated": NOW,
}


Expand Down
10 changes: 7 additions & 3 deletions demisto_sdk/commands/prepare_content/tests/yml_unifier_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,7 @@ def test_unify_default_output_script(self, mocker):
"tags": [],
"useCases": [],
"keywords": [],
"created": "2020-06-15T11:49:45Z",
}
)
PACK_METADATA_PARTNER_EMAIL_LIST = json.dumps(
Expand All @@ -1203,6 +1204,7 @@ def test_unify_default_output_script(self, mocker):
"tags": [],
"useCases": [],
"keywords": [],
"created": "2020-06-15T11:49:45Z",
}
)
PACK_METADATA_STRINGS_EMAIL_LIST = json.dumps(
Expand All @@ -1218,6 +1220,7 @@ def test_unify_default_output_script(self, mocker):
"tags": [],
"useCases": [],
"keywords": [],
"created": "2020-06-15T11:49:45Z",
}
)
PACK_METADATA_PARTNER_NO_EMAIL = json.dumps(
Expand All @@ -1233,6 +1236,7 @@ def test_unify_default_output_script(self, mocker):
"tags": [],
"useCases": [],
"keywords": [],
"created": "2020-06-15T11:49:45Z",
}
)
PACK_METADATA_PARTNER_NO_URL = json.dumps(
Expand All @@ -1248,6 +1252,7 @@ def test_unify_default_output_script(self, mocker):
"tags": [],
"useCases": [],
"keywords": [],
"created": "2020-06-15T11:49:45Z",
}
)
PACK_METADATA_XSOAR = json.dumps(
Expand All @@ -1263,6 +1268,7 @@ def test_unify_default_output_script(self, mocker):
"tags": [],
"useCases": [],
"keywords": [],
"created": "2020-06-15T11:49:45Z",
}
)

Expand All @@ -1279,6 +1285,7 @@ def test_unify_default_output_script(self, mocker):
"tags": [],
"useCases": [],
"keywords": [],
"created": "2020-06-15T11:49:45Z",
}
)

Expand Down Expand Up @@ -1331,7 +1338,6 @@ def test_unify_partner_contributed_pack(mocker, repo):
Then
- Ensure unify create unified file with partner support notes.
"""

pack = repo.create_pack("PackName")
integration = pack.create_integration("integration", "bla", INTEGRATION_YAML)
pack.pack_metadata.write_json(PACK_METADATA_PARTNER)
Expand Down Expand Up @@ -1495,7 +1501,6 @@ def test_unify_partner_contributed_pack_no_url(mocker, repo):
Then
- Ensure unify create unified file with partner support notes.
"""

pack = repo.create_pack("PackName")
integration = pack.create_integration("integration", "bla", INTEGRATION_YAML)
pack.pack_metadata.write_json(PACK_METADATA_PARTNER_NO_URL)
Expand Down Expand Up @@ -1604,7 +1609,6 @@ def test_unify_community_contributed(mocker, repo):
Then
- Ensure unify create unified file with community detailed description.
"""

pack = repo.create_pack("PackName")
integration = pack.create_integration("integration", "bla", INTEGRATION_YAML)
pack.pack_metadata.write_json(PACK_METADATA_COMMUNITY)
Expand Down
4 changes: 3 additions & 1 deletion demisto_sdk/tests/integration_tests/prepare_content_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def test_unify_integration__detailed_description_partner_collector(
"""
name = "test"
description = f"this is an integration {name}"
pack.pack_metadata.update({"support": "partner"})
pack.pack_metadata.update(
{"support": "partner", "created": "2023-10-24T11:49:45Z"}
)
yml = {
"commonfields": {"id": name, "version": -1},
"name": name,
Expand Down
4 changes: 4 additions & 0 deletions demisto_sdk/tests/integration_tests/unify_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def test_add_custom_section_flag_integration(self, mocker, repo, flag):
- make sure the nativeImage was key was added with the native-images.
"""
pack = repo.create_pack("PackName")
pack.pack_metadata.update({"created": "2023-10-24T11:49:45Z"})
integration = pack.create_integration("dummy-integration")
integration.create_default_integration()

Expand Down Expand Up @@ -242,6 +243,7 @@ def test_add_custom_section_flag(self, repo):
- make sure the nativeimage was key was added with the native-images.
"""
pack = repo.create_pack("PackName")
pack.pack_metadata.update({"created": "2023-10-24T11:49:45Z"})
script = pack.create_script("dummy-script")
script.create_default_script()

Expand All @@ -267,6 +269,7 @@ def test_ignore_native_image_integration(self, monkeypatch, repo):
- make sure the nativeimage key is not added to the integration unified yml.
"""
pack = repo.create_pack("PackName")
pack.pack_metadata.update({"created": "2023-10-24T11:49:45Z"})
integration = pack.create_integration("dummy-integration")
integration.create_default_integration()

Expand Down Expand Up @@ -295,6 +298,7 @@ def test_ignore_native_image_script(self, repo):
- make sure the nativeImage key is not added to the script unified yml.
"""
pack = repo.create_pack("PackName")
pack.pack_metadata.update({"created": "2023-10-24T11:49:45Z"})
script = pack.create_script("dummy-script")
script.create_default_script()

Expand Down
Loading