示例#1
0
        public async Task Execute(long sourceBuildId, string newNameSuffix, bool simulate)
        {
            Log.InfoFormat("Deep Clone Build Config. sourceBuildId: {0}, newNameSuffix: {1}", sourceBuildId, newNameSuffix);

            var sourceBuild = await _client.Builds.ById(sourceBuildId);

            _buildConfigXmlClient.Simulate = simulate;

            _simulate          = simulate;
            _sourceBuildConfig = await _client.BuildConfigs.GetByConfigurationId(sourceBuild.BuildTypeId);

            _newNameSuffix = newNameSuffix;
            _newBranchName = VcsRootHelper.ToValidGitBranchName(_newNameSuffix);
            _buildChainId  = Guid.NewGuid().ToString();
            _buildChain    = new BuildChain(_client.Builds, sourceBuild);

            var buildConfigsToClone = await GetBuildsToClone();

            foreach (var b in buildConfigsToClone)
            {
                Log.Info($"==== Branch {b.HistoricBuild.BuildTypeId} from Build #{b.HistoricBuild.Number} (id: {b.HistoricBuild.Id}) ====");
                if (!_simulate)
                {
                    await _vcsRootHelper.BranchUsingGitLabApi(b.HistoricBuild.Id, _newBranchName);
                }
            }

            var cloneBuildConfigCommands = GetCloneBuildConfigsCommands(buildConfigsToClone.ToList());

            foreach (var c in cloneBuildConfigCommands)
            {
                Log.Info($"==== {c} ====");
                if (!_simulate)
                {
                    c.Execute();
                }
            }

            var swapDependencyCommands = GetSwapDependenciesCommands(buildConfigsToClone);

            foreach (var c in swapDependencyCommands)
            {
                Log.Info($"==== {c} ====");
                if (!_simulate)
                {
                    c.Execute();
                }
            }

            if (!_simulate)
            {
                _buildConfigXmlClient.Push();
            }
        }
        public async Task Execute(long sourceBuildId, string newNameSuffix, bool simulate)
        {
            Log.InfoFormat("Clone Root Build Config. sourceBuildId: {0}, newNameSuffix: {1}", sourceBuildId, newNameSuffix);

            var sourceBuild = await _client.Builds.ById(sourceBuildId);

            await EnsureUniqueSuffixProvided(sourceBuild, newNameSuffix);

            var newBranchName = VcsRootHelper.ToValidGitBranchName(newNameSuffix);

            Log.InfoFormat("==== Branch {2} from Build #{1} (id: {0}) ====", sourceBuild.Id, sourceBuild.Number, sourceBuild.BuildConfig.Id);
            if (!simulate)
            {
                await _vcsRootHelper.BranchUsingLocalGit(sourceBuildId, newBranchName);
            }

            Log.InfoFormat("==== Clone {2} from Build #{1} (id: {0}) ====", sourceBuild.Id, sourceBuild.Number, sourceBuild.BuildConfig.Id);
            if (!simulate)
            {
                await CopyBuildConfigurationFromBuild(sourceBuild, newNameSuffix, newBranchName);
            }
        }
        private async Task Init(string sourceBuildConfigId, string targetRootBuildConfigId, bool simulate)
        {
            _simulate          = simulate;
            _sourceBuildConfig = await _client.BuildConfigs.GetByConfigurationId(sourceBuildConfigId);

            _targetRootBuildConfig = await _client.BuildConfigs.GetByConfigurationId(targetRootBuildConfigId);

            if (_targetRootBuildConfig.Parameters[ParameterName.ClonedFromBuildId] == null)
            {
                throw new Exception(
                          $"Target root Build Config doesn't appear to be cloned. It is missing the \"{ParameterName.ClonedFromBuildId}\" parameter.");
            }

            _targetBuildChainId = _targetRootBuildConfig.Parameters[ParameterName.BuildConfigChainId].Value;
            _newNameSuffix      = _targetRootBuildConfig.Parameters[ParameterName.CloneNameSuffix].Value;
            _newBranchName      = VcsRootHelper.ToValidGitBranchName(_newNameSuffix);
            _dependencyChain    = new DependencyChain(_client, _targetRootBuildConfig);

            if (!_dependencyChain.Contains(_sourceBuildConfig.Id))
            {
                throw new Exception(
                          $"Cannot clone Build Config, because requested source Build Config ({_sourceBuildConfig.Id}) " +
                          $"is not found in the current Build Config chain for target Build Config ({targetRootBuildConfigId}). " +
                          $"Make sure target Build Config depends on source Build Config." + Environment.NewLine +
                          $"Currently discovered Build Config chain is: " + Environment.NewLine + "{_dependencyChain}");
            }

            if (_sourceBuildConfig.Parameters[ParameterName.BuildConfigChainId].Value == _targetBuildChainId)
            {
                throw new Exception(
                          $"It appears that Build Config \"{_sourceBuildConfig.Id}\" is already a cloned for target " +
                          $"Build Config \"{_targetRootBuildConfig.Id}\", because \"{ParameterName.BuildConfigChainId}\" " +
                          $"parameter is the same \"{_sourceBuildConfig.Parameters[ParameterName.BuildConfigChainId]}\" . " +
                          $"Create a new clone of root Build Config first");
            }
        }