Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/npm_and_yarn/mocha-11.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
gherceg authored Dec 10, 2024
2 parents bed277a + 155b20c commit c783280
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
24 changes: 24 additions & 0 deletions corehq/apps/app_manager/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
from corehq.blobs.mixin import CODES, BlobMixin
from corehq.const import USER_DATE_FORMAT, USER_TIME_FORMAT
from corehq.util import bitly, view_utils
from corehq.util.metrics import metrics_counter
from corehq.util.quickcache import quickcache
from corehq.util.timer import TimingContext, time_method
from corehq.util.timezones.conversions import ServerTime
Expand Down Expand Up @@ -4467,8 +4468,31 @@ def make_build(self, comment=None, user_id=None):
assert copy._id
prune_auto_generated_builds.delay(self.domain, self._id)

self.check_build_dependencies(new_build=copy)

return copy

def check_build_dependencies(self, new_build):
"""
Reports whether the app dependencies have been added or removed.
"""

def has_dependencies(build):
return bool(
build.profile.get('features', {}).get('dependencies')
)

new_build_has_dependencies = has_dependencies(new_build)

last_build = get_latest_build_doc(self.domain, self.id)
last_build = self.__class__.wrap(last_build) if last_build else None
last_build_has_dependencies = has_dependencies(last_build) if last_build else False

if not last_build_has_dependencies and new_build_has_dependencies:
metrics_counter('commcare.app_build.dependencies_added')
elif last_build_has_dependencies and not new_build_has_dependencies:
metrics_counter('commcare.app_build.dependencies_removed')

def convert_app_to_build(self, copy_of, user_id, comment=None):
self.copy_of = copy_of
built_on = datetime.datetime.utcnow()
Expand Down
45 changes: 45 additions & 0 deletions corehq/apps/app_manager/tests/test_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,48 @@ def test_get_latest_enabled_build_with_loc_flag(self):
location_flag_enabled=True,
)
self.assertIsNone(build)

def test_dependencies_feature_added(self):
factory = AppFactory(build_version='2.40.0')
m0, f0 = factory.new_basic_module('register', 'case')
f0.source = get_simple_form(xmlns=f0.unique_id)
factory.app.profile = {'features': {'dependencies': ['coffee']}}
factory.app.save()
self.addCleanup(factory.app.delete)

with patch("corehq.apps.app_manager.models.metrics_counter") as metric_counter_mock:
factory.app.make_build()
metric_counter_mock.assert_called_with('commcare.app_build.dependencies_added')

def test_dependencies_feature_removed(self):
factory = AppFactory(build_version='2.40.0')
m0, f0 = factory.new_basic_module('register', 'case')
f0.source = get_simple_form(xmlns=f0.unique_id)
factory.app.profile = {'features': {'dependencies': ['coffee']}}
factory.app.save()
self.addCleanup(factory.app.delete)
build1 = factory.app.make_build()
build1.save()

factory.app.profile = {'features': {'dependencies': []}}
factory.app.save()

with patch("corehq.apps.app_manager.models.metrics_counter") as metric_counter_mock:
factory.app.make_build()
metric_counter_mock.assert_called_with('commcare.app_build.dependencies_removed')

def test_dependencies_feature_metrics_not_triggerd(self):
factory = AppFactory(build_version='2.40.0')
m0, f0 = factory.new_basic_module('register', 'case')
f0.source = get_simple_form(xmlns=f0.unique_id)
factory.app.profile = {'features': {'dependencies': []}}
factory.app.save()
self.addCleanup(factory.app.delete)
build1 = factory.app.make_build()
build1.save()

factory.app.save()

with patch("corehq.apps.app_manager.models.metrics_counter") as metric_counter_mock:
factory.app.make_build()
metric_counter_mock.assert_not_called()

0 comments on commit c783280

Please sign in to comment.