diff --git a/api/plugins/action/deploy_target_config.py b/api/plugins/action/deploy_target_config.py index 19207db..a1e1b79 100644 --- a/api/plugins/action/deploy_target_config.py +++ b/api/plugins/action/deploy_target_config.py @@ -90,35 +90,42 @@ def run(self, tmp=None, task_vars=None): return result def determine_required_updates(existing_configs, desired_configs): - addition_required = [] - deletion_required = [ - config['id'] - for config in existing_configs - if not any( - config['branches'] == desired['branches'] - for desired in desired_configs - ) - ] - + addition_required = [] + deletion_required = [] + + grouped_configs = {} + for config in existing_configs: + key = (config['branches'], config['pullrequests'], str(config['deployTarget']['id']), str(config['weight'])) + if key not in grouped_configs: + grouped_configs[key] = [] + grouped_configs[key].append(config) + + #Identify duplicates and mark older ones for deletion + for key, configs in grouped_configs.items(): + if len(configs) > 1: + sorted_configs = sorted(configs, key=lambda x: x['id'], reverse=True) + newest_config = sorted_configs[0] + for config in sorted_configs[1:]: + deletion_required.append(config['id']) + grouped_configs[key] = [newest_config] + + # Adjusted logic for handling additions for desired in desired_configs: - found = False - uptodate = True - for existing_config in existing_configs: - if existing_config['branches'] != desired['branches']: - continue - - desired['_existing_id'] = existing_config['id'] - found = True - - # Mark for update (or in this context, addition) if there are discrepancies in any key property - if (existing_config['pullrequests'] != desired['pullrequests'] or - str(existing_config['deployTarget']['id']) != str(desired['deployTarget']) or - str(existing_config['weight']) != str(desired['weight'])): - desired['_existing_id'] = existing_config['id'] - uptodate = False - break - - if not found or not uptodate: + key = (desired['branches'], desired['pullrequests'], str(desired['deployTarget']), str(desired['weight'])) + if key not in grouped_configs: addition_required.append(desired) - - return addition_required, deletion_required \ No newline at end of file + + + # checking existing configurations for deletions + for configs in grouped_configs.values(): + for config in configs: + if not any( + config['branches'] == desired['branches'] and + str(config['deployTarget']['id']) == str(desired['deployTarget']) and + config['pullrequests'] == desired['pullrequests'] and + str(config['weight']) == str(desired['weight']) + for desired in desired_configs): + deletion_required.append(config['id']) + + + return addition_required, deletion_required diff --git a/api/tests/unit/plugins/action/test_determine_updates_required.py b/api/tests/unit/plugins/action/test_determine_updates_required.py index 43b00ab..43c2a3a 100644 --- a/api/tests/unit/plugins/action/test_determine_updates_required.py +++ b/api/tests/unit/plugins/action/test_determine_updates_required.py @@ -15,6 +15,7 @@ def test_update_required(self): 'branches': '^(main)$', 'deployTarget': 1, 'pullrequests': 'false', + 'weight': 1 } ] @@ -72,7 +73,7 @@ def test_update_required_weight(self): assert len(addition_required) == 1, "Expected one addition required due to weight change" assert addition_required[0]['weight'] == 2, "Expected weight to be updated to 2" - assert len(deletion_required) == 0, "Expected no deletions required" + assert len(deletion_required) == 1, "Expected no deletions required" def test_update_required_cluster(self): existing_configs = [ @@ -97,7 +98,7 @@ def test_update_required_cluster(self): assert len(addition_required) == 1, "Expected one addition required due to deployTarget change" assert addition_required[0]['deployTarget'] == 2, "Expected deployTarget to be updated to 2" - assert len(deletion_required) == 0, "Expected no deletions required" + assert len(deletion_required) == 1, "Expected no deletions required" def test_orphan_existing(self): existing_configs = [ @@ -136,4 +137,49 @@ def test_orphan_existing(self): assert len(addition_required) == 2, "Expected two additions required due to changes" assert addition_required[0]['deployTarget'] == 1, "Expected the first addition to have deployTarget 1" assert addition_required[1]['deployTarget'] == 2, "Expected the second addition to have deployTarget 2" - assert len(deletion_required) == 2, "Expecting 2 deletion of orphan deploytarget config" \ No newline at end of file + assert len(deletion_required) == 2, "Expecting 2 deletion of orphan deploytarget config" + + + def test_duplicate_target_config(self): + existing_configs = [ + { + 'branches': '^(main)$', + 'deployTarget': {'id': 1, 'name': 'cluster.io'}, + 'id': 1, + 'pullrequests': 'false', + 'weight': 1 + }, + { + 'branches': '^(main)$', + 'deployTarget': {'id': 1, 'name': 'cluster.io'}, + 'id': 2, + 'pullrequests': 'false', + 'weight': 1 + }, + { + 'branches': '^(develop)$', + 'deployTarget': {'id': 2, 'name': 'cluster.io'}, + 'id': 2, + 'pullrequests': 'true', + 'weight': 1 + } + ] + desired_configs = [ + { + 'branches': '^(main)$', + 'deployTarget': 1, + 'pullrequests': 'false', + 'weight': 1 + }, + { + 'branches': '^(develop)$', + 'deployTarget': 2, + 'pullrequests': 'true', + 'weight': 1 + } + ] + + addition_required, deletion_required = determine_required_updates(existing_configs, desired_configs) + + assert len(addition_required) == 0, "Expected no additions changes" + assert len(deletion_required) == 1, "Expected one deletion of duplicate deploytarget config" \ No newline at end of file