Skip to content

Commit

Permalink
Add Update Release Notes command (demisto#338)
Browse files Browse the repository at this point in the history
* Add *.md files from ReleaseNotes to Artifacts

* Add *.md files from ReleaseNotes to Artifacts

* Linting

* Fix imports

* Remove unnecessary prints

* Ran isort

* Revert: Ran isort (f4d53e1)

* Revert: Revert: Ran isort (f4d53e1) (ace6c7b)

* Import mess

* Import mess

* ugh

* ugh

* ughhhh

* Move constants to common constants

* begin add tests

* begin add tests

* checking tests

* checking tests

* trying to squeak over the test coverage threshold

* eof

* cover this

* Changes per Design Review

* Changes per Design Review

* Fix tests

* Testing gpg

* Testing gpg

* Remove unnecessary line

* Updates per CR

* Improve unit testing, add ability to iterate over list of packs

* Preserve pack_metadata while testing

* Add check for errors, update init template

* flake

* Fix mocker import

* Coverage...

* Please coverall gods, don't fail this build needlessly

* Changes per CR

* Changes per CR

* Changes per CR

* Fix tests

* Fixed bug

* Changes per CR

* Add more unit tests

* Add even more unit tests
  • Loading branch information
amshamah419 authored Apr 30, 2020
1 parent 818426f commit db6e858
Show file tree
Hide file tree
Showing 21 changed files with 747 additions and 11 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
omit =
**/tests/**
**/__init__.py
demisto_sdk/__main__.py

[html]
directory = coverage_html_report
96 changes: 95 additions & 1 deletion demisto_sdk/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Site packages
import os
import re
import sys

from pkg_resources import get_distribution
Expand All @@ -10,7 +11,9 @@
# Common tools
from demisto_sdk.commands.common.tools import (find_type,
get_last_remote_release_version,
print_error, print_warning)
get_pack_name,
pack_name_to_path, print_error,
print_warning)
from demisto_sdk.commands.create_artifacts.content_creator import \
ContentCreator
from demisto_sdk.commands.create_id_set.create_id_set import IDSetCreator
Expand All @@ -36,6 +39,7 @@
from demisto_sdk.commands.secrets.secrets import SecretsValidator
from demisto_sdk.commands.split_yml.extractor import Extractor
from demisto_sdk.commands.unify.unifier import Unifier
from demisto_sdk.commands.update_release_notes.update_rn import UpdateRN
from demisto_sdk.commands.upload.uploader import Uploader
from demisto_sdk.commands.validate.file_validator import FilesValidator

Expand All @@ -49,6 +53,28 @@ def __init__(self):
self.configuration = None


class RNInputValidation(click.ParamType):
name = 'update_type'

def validate_rn_input(self, value, param, ctx):
if value:
if re.match(r'(?i)(?<=|^)major(?= |$)', value):
update_type = 'major'
elif re.match(r'(?i)(?<=|^)minor(?= |$)', value):
update_type = 'minor'
elif re.match(r'(?i)(?<=|^)revision(?= |$)', value):
update_type = 'revision'
else:
self.fail(
f'{value} is not a valid option. Please select: major, minor, revision',
param,
ctx,
)
else:
update_type = 'revision'
return update_type


pass_config = click.make_pass_decorator(DemistoSDK, ensure=True)


Expand Down Expand Up @@ -684,6 +710,74 @@ def id_set_command(**kwargs):
id_set_creator.create_id_set()


# ====================== update-release-notes =================== #
@main.command(name="update-release-notes",
short_help='''Auto-increment pack version and generate release notes template.''')
@click.help_option(
'-h', '--help'
)
@click.option(
"-p", "--pack", help="Name of the pack."
)
@click.option(
'-u', '--update_type', help="The type of update being done. [major, minor, revision]",
type=RNInputValidation()
)
@click.option(
'--all', help="Update all changed packs", is_flag=True
)
@click.option(
"--pre_release", help="Indicates that this change should be designated a pre-release version.",
is_flag=True)
def update_pack_releasenotes(**kwargs):
_pack = kwargs.get('pack')
update_type = kwargs.get('update_type')
pre_release = kwargs.get('pre_release')
is_all = kwargs.get('all')
modified, added, old, _packs = FilesValidator(use_git=True).get_modified_and_added_files()
packs_existing_rn = set()
for pf in added:
if 'ReleaseNotes' in pf:
pack_with_existing_rn = get_pack_name(pf)
packs_existing_rn.add(pack_with_existing_rn)
if len(packs_existing_rn):
existing_rns = ''.join(f"{p}, " for p in packs_existing_rn)
print_warning(f"Found existing release notes for the following packs: {existing_rns.rstrip(', ')}")
if len(_packs) > 1:
pack_list = ''.join(f"{p}, " for p in _packs)
if not is_all:
if _pack:
pass
else:
print_error(f"Detected changes in the following packs: {pack_list.rstrip(', ')}\n"
f"To update release notes in a specific pack, please use the -p parameter "
f"along with the pack name.")
sys.exit(0)
if len(modified) < 1:
print_warning('No changes were detected.')
sys.exit(0)
if is_all and not _pack:
packs = list(_packs - packs_existing_rn)
packs_list = ''.join(f"{p}, " for p in packs)
print_warning(f"Adding release notes to the following packs: {packs_list.rstrip(', ')}")
for pack in packs:
update_pack_rn = UpdateRN(pack=pack, update_type=update_type, pack_files=modified,
pre_release=pre_release)
update_pack_rn.execute_update()
elif is_all and _pack:
print_error(f"Please remove the --all flag when specifying only one pack.")
sys.exit(0)
else:
if _pack:
if _pack in packs_existing_rn:
print_error(f"New release notes file already found for {_pack}. "
f"Please update manually or delete {pack_name_to_path(_pack)}")
else:
update_pack_rn = UpdateRN(pack=_pack, update_type=update_type, pack_files=modified,
pre_release=pre_release)
update_pack_rn.execute_update()


# ====================== find-dependencies ====================== #
@main.command(name="find-dependencies",
short_help='''Find pack dependencies and update pack metadata.''')
Expand Down
12 changes: 12 additions & 0 deletions demisto_sdk/commands/common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def found_hidden_param(parameter_name):
BETA_INTEGRATIONS_DIR = 'Beta_Integrations'
PACKS_DIR = 'Packs'
TOOLS_DIR = 'Tools'
RELEASE_NOTES_DIR = 'ReleaseNotes'
TESTS_DIR = 'Tests'

SCRIPT = 'script'
Expand Down Expand Up @@ -555,6 +556,7 @@ def found_hidden_param(parameter_name):
PACKS_WIDGETS_REGEX = r'{}{}/([^/]+)/{}/([^.]+)\.json'.format(CAN_START_WITH_DOT_SLASH, PACKS_DIR, WIDGETS_DIR)
PACKS_REPORTS_REGEX = r'{}/([^/]+)/{}/([^.]+)\.json'.format(PACKS_DIR, REPORTS_DIR)
PACKS_CHANGELOG_REGEX = r'{}{}/([^/]+)/CHANGELOG\.md$'.format(CAN_START_WITH_DOT_SLASH, PACKS_DIR)
PACKS_RELEASE_NOTES_REGEX = r'{}{}/([^/]+)/{}/([^/]+)\.md$'.format(CAN_START_WITH_DOT_SLASH, PACKS_DIR, RELEASE_NOTES_DIR)
PACKS_README_REGEX = r'{}{}/([^/]+)/README\.md'.format(CAN_START_WITH_DOT_SLASH, PACKS_DIR)
PACKS_README_REGEX_INNER = r'{}{}/([^/]+)/([^/]+)/([^/]+)/README\.md'.format(CAN_START_WITH_DOT_SLASH, PACKS_DIR)

Expand Down Expand Up @@ -846,6 +848,16 @@ def found_hidden_param(parameter_name):
PACKS_README_REGEX,
PACKS_README_REGEX_INNER,
INTEGRATION_OLD_README_REGEX,
# Pack Misc
PACKS_CLASSIFIERS_REGEX,
PACKS_DASHBOARDS_REGEX,
PACKS_INCIDENT_TYPES_REGEX,
PACKS_INCIDENT_FIELDS_REGEX,
PACKS_INDICATOR_FIELDS_REGEX,
PACKS_LAYOUTS_REGEX,
PACKS_WIDGETS_REGEX,
PACKS_REPORTS_REGEX,
PACKS_RELEASE_NOTES_REGEX
]

CHECKED_TYPES_NO_REGEX = [item.replace(CAN_START_WITH_DOT_SLASH, "").replace(NOT_TEST, "") for item in
Expand Down
27 changes: 27 additions & 0 deletions demisto_sdk/commands/create_artifacts/content_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
INTEGRATIONS_DIR,
LAYOUTS_DIR, MISC_DIR,
PACKS_DIR, PLAYBOOKS_DIR,
RELEASE_NOTES_DIR,
REPORTS_DIR, SCRIPTS_DIR,
TEST_PLAYBOOKS_DIR,
TOOLS_DIR, WIDGETS_DIR)
Expand Down Expand Up @@ -244,6 +245,30 @@ def copy_dir_json(self, dir_path, bundle):

shutil.copyfile(path, os.path.join(bundle, dpath))

def copy_dir_md(self, dir_path, bundle):
"""
Copy the md files inside a directory to a bundle.
:param dir_path: source directory
:param bundle: destination bundle
:return: None
"""
# handle *.md files
dir_name = os.path.basename(dir_path)
scan_files = glob.glob(os.path.join(dir_path, '*.md'))
for path in scan_files:
new_path = os.path.basename(path)
if dir_name == RELEASE_NOTES_DIR:
if os.path.isfile(os.path.join(bundle, new_path)):
raise NameError(
f'Failed while trying to create {os.path.join(bundle, new_path)}. File already exists.'
)

if len(new_path) >= self.file_name_max_size:
self.long_file_names.append(os.path.basename(new_path))

shutil.copyfile(path, os.path.join(bundle, new_path))

def copy_dir_files(self, *args):
"""
Copy the yml and json files from inside a directory to a bundle.
Expand All @@ -255,6 +280,8 @@ def copy_dir_files(self, *args):
self.copy_dir_json(*args)
# handle *.yml files
self.copy_dir_yml(*args)
# handle *.md files
self.copy_dir_md(*args)

def copy_test_files(self, test_playbooks_dir=TEST_PLAYBOOKS_DIR):
"""
Expand Down
6 changes: 3 additions & 3 deletions demisto_sdk/commands/init/initiator.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ def create_metadata(fill_manually: bool) -> Dict:
metadata = {
'name': '## FILL OUT MANUALLY ##',
'description': '## FILL OUT MANUALLY ##',
'support': 'demisto',
'support': 'xsoar',
'currentVersion': PACK_INITIAL_VERSION,
'author': 'demisto',
'url': 'https://www.demisto.com',
'author': 'Cortex XSOAR',
'url': 'https://www.paloaltonetworks.com/cortex',
'email': '',
'categories': [],
'tags': [],
Expand Down
6 changes: 3 additions & 3 deletions demisto_sdk/commands/init/tests/initiator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ def test_create_metadata(monkeypatch, initiator):
assert pack_metadata == {
'name': '## FILL OUT MANUALLY ##',
'description': '## FILL OUT MANUALLY ##',
'support': 'demisto',
'support': 'xsoar',
'currentVersion': PACK_INITIAL_VERSION,
'author': 'demisto',
'url': 'https://www.demisto.com',
'author': 'Cortex XSOAR',
'url': 'https://www.paloaltonetworks.com/cortex',
'email': '',
'categories': [],
'tags': [],
Expand Down
Empty file.
Loading

0 comments on commit db6e858

Please sign in to comment.