﻿using System.Linq;
using System.Threading.Tasks;
using TeamCityApi.Helpers;
using TeamCityApi.Logging;

namespace TeamCityApi.UseCases
{
    public class DeleteClonedBuildChainUseCase
    {
        private static readonly ILog Log = LogProvider.GetLogger(typeof(DeleteClonedBuildChainUseCase));

        private readonly ITeamCityClient _client;

        public DeleteClonedBuildChainUseCase(ITeamCityClient client)
        {
            _client = client;
        }

        public async Task Execute(string buildConfigId, bool simulate = false)
        {
            Log.InfoFormat("Delete Cloned Build Chain.");

            var buildConfig = await _client.BuildConfigs.GetByConfigurationId(buildConfigId);
            var buildChainId = buildConfig.Parameters[ParameterName.BuildConfigChainId].Value;
            var buildConfigChain = new BuildConfigChain(_client.BuildConfigs, buildConfig);
            await DeleteClonedBuildChain(buildConfigChain, buildChainId, simulate);
        }

        private async Task DeleteClonedBuildChain(BuildConfigChain buildConfigChain, string buildChainId, bool simulate)
        {
            foreach (var node in buildConfigChain.Nodes.Where(node => node.Value.Parameters[ParameterName.BuildConfigChainId].Value == buildChainId))
            {
                Log.InfoFormat("Deleting buildConfigId: {0}", node.Value.Id);
                if (!simulate)
                {
                    await _client.BuildConfigs.DeleteBuildConfig(node.Value.Id);
                }
            }
        }
    }
}
﻿using System.Linq;
using System.Threading.Tasks;
using TeamCityApi.Helpers;
using TeamCityApi.Logging;

namespace TeamCityApi.UseCases
{
    public class DeleteClonedBuildChainUseCase
    {
        private static readonly ILog Log = LogProvider.GetLogger(typeof(DeleteClonedBuildChainUseCase));

        private readonly ITeamCityClient _client;

        public DeleteClonedBuildChainUseCase(ITeamCityClient client)
        {
            _client = client;
        }

        public async Task Execute(string buildConfigId, bool simulate = false)
        {
            Log.InfoFormat("Delete Cloned Build Chain.");

            var buildConfig = await _client.BuildConfigs.GetByConfigurationId(buildConfigId);
            var buildChainId = buildConfig.Parameters[ParameterName.BuildConfigChainId].Value;
            var buildConfigChain = new BuildConfigChain(_client.BuildConfigs, buildConfig);
            await DeleteClonedBuildChain(buildConfigChain, buildChainId, simulate);
        }

        private async Task DeleteClonedBuildChain(BuildConfigChain buildConfigChain, string buildChainId, bool simulate)
        {
            var buildConfigIdsToDelete = buildConfigChain.Nodes
                .Where(node => node.Value.Parameters[ParameterName.BuildConfigChainId].Value == buildChainId)
                .Select(n => n.Value.Id)
                .ToList();

            buildConfigIdsToDelete.Reverse(); //to start deletion from leafs

            foreach (var buildConfigId in buildConfigIdsToDelete)
            {
                Log.InfoFormat("Deleting buildConfigId: {0}", buildConfigId);
                if (!simulate)
                {
                    await _client.BuildConfigs.DeleteBuildConfig(buildConfigId);
                }
            }
        }
    }
}
Tweak delete cloned build chain routine.
Tweak delete cloned build chain routine.

Change to delete leafs first (so it will be easier to restart
delete from root of something fails in the middle)

Also collect all build config ids to separate list before deletion,
to make it easier to inspect and debug what is about to be deleted
