Example #1
0
        private void ProcessBuildAddBuildTagCommand(IExecutionContext context, string data)
        {
            ArgUtil.NotNull(context, nameof(context));
            ArgUtil.NotNull(context.Endpoints, nameof(context.Endpoints));

            Guid projectId = context.Variables.System_TeamProjectId ?? Guid.Empty;

            ArgUtil.NotEmpty(projectId, nameof(projectId));

            int?buildId = context.Variables.Build_BuildId;

            ArgUtil.NotNull(buildId, nameof(buildId));

            if (!string.IsNullOrEmpty(data))
            {
                // queue async command task to associate artifact.
                context.Debug($"Add build tag: {data} to build: {buildId.Value} at backend.");
                var commandContext = HostContext.CreateService <IAsyncCommandContext>();
                commandContext.InitializeCommandContext(context, StringUtil.Loc("AddBuildTag"));
                commandContext.Task = AddBuildTagAsync(commandContext,
                                                       WorkerUtilies.GetVssConnection(context),
                                                       projectId,
                                                       buildId.Value,
                                                       data,
                                                       context.CancellationToken);
                context.AsyncCommands.Add(commandContext);
            }
            else
            {
                throw new Exception(StringUtil.Loc("BuildTagRequired"));
            }
        }
        private void ProcessPublishTestResultsCommand(IExecutionContext context, Dictionary <string, string> eventProperties, string data)
        {
            ArgUtil.NotNull(context, nameof(context));
            _executionContext = context;

            LoadPublishTestResultsInputs(eventProperties, data);

            string teamProject = context.Variables.System_TeamProject;
            string owner       = context.Variables.Build_RequestedFor;
            string buildUri    = context.Variables.Build_BuildUri;
            int    buildId     = context.Variables.Build_BuildId ?? 0;

            //Temporary fix to support publish in RM scenarios where there might not be a valid Build ID associated.
            //TODO: Make a cleaner fix after TCM User Story 401703 is completed.
            if (buildId == 0)
            {
                _platform = _configuration = null;
            }

            string releaseUri            = null;
            string releaseEnvironmentUri = null;

            // Check to identify if we are in the Release management flow; if not, then release fields will be kept null while publishing to TCM
            if (!string.IsNullOrWhiteSpace(context.Variables.Release_ReleaseUri))
            {
                releaseUri            = context.Variables.Release_ReleaseUri;
                releaseEnvironmentUri = context.Variables.Release_ReleaseEnvironmentUri;
            }

            IResultReader  resultReader = GetTestResultReader(_testRunner);
            TestRunContext runContext   = new TestRunContext(owner, _platform, _configuration, buildId, buildUri, releaseUri, releaseEnvironmentUri);

            Client.VssConnection connection = WorkerUtilies.GetVssConnection(_executionContext);

            var publisher = HostContext.GetService <ITestRunPublisher>();

            publisher.InitializePublisher(context, connection, teamProject, resultReader);

            var commandContext = HostContext.CreateService <IAsyncCommandContext>();

            commandContext.InitializeCommandContext(context, StringUtil.Loc("PublishTestResults"));

            if (_mergeResults)
            {
                commandContext.Task = PublishAllTestResultsToSingleTestRunAsync(_testResultFiles, publisher, buildId, runContext, resultReader.Name, context.CancellationToken);
            }
            else
            {
                commandContext.Task = PublishToNewTestRunPerTestResultFileAsync(_testResultFiles, publisher, runContext, resultReader.Name, context.CancellationToken);
            }
            _executionContext.AsyncCommands.Add(commandContext);
        }
        private void ProcessPublishCodeCoverageCommand(IExecutionContext context, Dictionary <string, string> eventProperties)
        {
            ArgUtil.NotNull(context, nameof(context));

            _buildId = context.Variables.Build_BuildId ?? -1;
            if (!IsHostTypeBuild(context) || _buildId < 0)
            {
                //In case the publishing codecoverage is not applicable for current Host type we continue without publishing
                context.Warning(StringUtil.Loc("CodeCoveragePublishIsValidOnlyForBuild"));
                return;
            }

            LoadPublishCodeCoverageInputs(eventProperties);

            string project = context.Variables.System_TeamProject;

            long?containerId = context.Variables.Build_ContainerId;

            ArgUtil.NotNull(containerId, nameof(containerId));

            Guid projectId = context.Variables.System_TeamProjectId ?? Guid.Empty;

            ArgUtil.NotEmpty(projectId, nameof(projectId));

            //step 1: read code coverage summary
            var reader = GetCodeCoverageSummaryReader(_codeCoverageTool);

            context.Output(StringUtil.Loc("ReadingCodeCoverageSummary", _summaryFileLocation));
            var coverageData = reader.GetCodeCoverageSummary(context, _summaryFileLocation);

            if (coverageData == null || coverageData.Count() == 0)
            {
                context.Warning(StringUtil.Loc("CodeCoverageDataIsNull"));
            }

            VssConnection connection            = WorkerUtilies.GetVssConnection(context);
            var           codeCoveragePublisher = HostContext.GetService <ICodeCoveragePublisher>();

            codeCoveragePublisher.InitializePublisher(_buildId, connection);

            var commandContext = HostContext.CreateService <IAsyncCommandContext>();

            commandContext.InitializeCommandContext(context, StringUtil.Loc("PublishCodeCoverage"));
            commandContext.Task = PublishCodeCoverageAsync(context, commandContext, codeCoveragePublisher, coverageData, project, projectId, containerId.Value, context.CancellationToken);
            context.AsyncCommands.Add(commandContext);
        }
        private void ProcessPublishTelemetryCommand(IExecutionContext context, Dictionary <string, string> eventProperties, string data)
        {
            ArgUtil.NotNull(context, nameof(context));

            LoadTelemetryInputs(eventProperties);

            var ciService     = HostContext.GetService <ICustomerIntelligenceServer>();
            var vssConnection = WorkerUtilies.GetVssConnection(context);

            ciService.Initialize(vssConnection);

            var commandContext = HostContext.CreateService <IAsyncCommandContext>();

            commandContext.InitializeCommandContext(context, StringUtil.Loc("Telemetry"));
            commandContext.Task = ciService.PublishEventsAsync(new CustomerIntelligenceEvent[] { PopulateCustomerIntelligenceData(context, data) });
            context.AsyncCommands.Add(commandContext);
        }
Example #5
0
        private void ProcessBuildUpdateBuildNumberCommand(IExecutionContext context, string data)
        {
            ArgUtil.NotNull(context, nameof(context));
            ArgUtil.NotNull(context.Endpoints, nameof(context.Endpoints));

            Guid projectId = context.Variables.System_TeamProjectId ?? Guid.Empty;

            ArgUtil.NotEmpty(projectId, nameof(projectId));

            int?buildId = context.Variables.Build_BuildId;

            ArgUtil.NotNull(buildId, nameof(buildId));

            if (!String.IsNullOrEmpty(data))
            {
                // update build number within Context.
                context.Variables.Set(WellKnownBuildVariables.BuildNumber, data);

                // queue async command task to update build number.
                context.Debug($"Update build number for build: {buildId.Value} to: {data} at backend.");
                var commandContext = HostContext.CreateService <IAsyncCommandContext>();
                commandContext.InitializeCommandContext(context, StringUtil.Loc("UpdateBuildNumber"));
                commandContext.Task = UpdateBuildNumberAsync(commandContext,
                                                             WorkerUtilies.GetVssConnection(context),
                                                             projectId,
                                                             buildId.Value,
                                                             data,
                                                             context.CancellationToken);

                context.AsyncCommands.Add(commandContext);
            }
            else
            {
                throw new Exception(StringUtil.Loc("BuildNumberRequired"));
            }
        }
Example #6
0
        private void ProcessArtifactAssociateCommand(IExecutionContext context, Dictionary <string, string> eventProperties, string data)
        {
            ArgUtil.NotNull(context, nameof(context));
            ArgUtil.NotNull(context.Endpoints, nameof(context.Endpoints));

            ServiceEndpoint systemConnection = context.Endpoints.FirstOrDefault(e => string.Equals(e.Name, ServiceEndpoints.SystemVssConnection, StringComparison.OrdinalIgnoreCase));

            ArgUtil.NotNull(systemConnection, nameof(systemConnection));
            ArgUtil.NotNull(systemConnection.Url, nameof(systemConnection.Url));

            Uri            projectUrl        = systemConnection.Url;
            VssCredentials projectCredential = ApiUtil.GetVssCredential(systemConnection);

            Guid projectId = context.Variables.System_TeamProjectId ?? Guid.Empty;

            ArgUtil.NotEmpty(projectId, nameof(projectId));

            int?buildId = context.Variables.Build_BuildId;

            ArgUtil.NotNull(buildId, nameof(buildId));

            string artifactName;

            if (!eventProperties.TryGetValue(ArtifactAssociateEventProperties.ArtifactName, out artifactName) ||
                string.IsNullOrEmpty(artifactName))
            {
                throw new Exception(StringUtil.Loc("ArtifactNameRequired"));
            }

            string artifactLocation = data;

            if (string.IsNullOrEmpty(artifactLocation))
            {
                throw new Exception(StringUtil.Loc("ArtifactLocationRequired"));
            }

            string artifactType;

            if (!eventProperties.TryGetValue(ArtifactAssociateEventProperties.ArtifactType, out artifactType))
            {
                artifactType = InferArtifactResourceType(context, artifactLocation);
            }

            if (string.IsNullOrEmpty(artifactType))
            {
                throw new Exception(StringUtil.Loc("ArtifactTypeRequired"));
            }

            if (!IsUncSharePath(context, artifactLocation) && (context.Variables.System_HostType != HostTypes.Build))
            {
                throw new Exception(StringUtil.Loc("AssociateArtifactCommandNotSupported", context.Variables.System_HostType));
            }

            var propertyDictionary = ExtractArtifactProperties(eventProperties);

            string artifactData = "";

            if (IsContainerPath(artifactLocation) ||
                IsValidServerPath(artifactLocation))
            {
                //if artifactlocation is a file container path or a tfvc server path
                artifactData = artifactLocation;
            }
            else if (IsUncSharePath(context, artifactLocation))
            {
                //if artifactlocation is a UNC share path
                artifactData = new Uri(artifactLocation).LocalPath;
            }
            else
            {
                throw new Exception(StringUtil.Loc("ArtifactLocationNotSupport", artifactLocation));
            }

            // queue async command task to associate artifact.
            context.Debug($"Associate artifact: {artifactName} with build: {buildId.Value} at backend.");
            var commandContext = HostContext.CreateService <IAsyncCommandContext>();

            commandContext.InitializeCommandContext(context, StringUtil.Loc("AssociateArtifact"));
            commandContext.Task = AssociateArtifactAsync(commandContext,
                                                         WorkerUtilies.GetVssConnection(context),
                                                         projectId,
                                                         buildId.Value,
                                                         artifactName,
                                                         artifactType,
                                                         artifactData,
                                                         propertyDictionary,
                                                         context.CancellationToken);
            context.AsyncCommands.Add(commandContext);
        }
Example #7
0
        private void ProcessArtifactUploadCommand(IExecutionContext context, Dictionary <string, string> eventProperties, string data)
        {
            ArgUtil.NotNull(context, nameof(context));
            ArgUtil.NotNull(context.Endpoints, nameof(context.Endpoints));

            Guid projectId = context.Variables.System_TeamProjectId ?? Guid.Empty;

            ArgUtil.NotEmpty(projectId, nameof(projectId));

            int?buildId = context.Variables.Build_BuildId;

            ArgUtil.NotNull(buildId, nameof(buildId));

            long?containerId = context.Variables.Build_ContainerId;

            ArgUtil.NotNull(containerId, nameof(containerId));

            string artifactName;

            if (!eventProperties.TryGetValue(ArtifactAssociateEventProperties.ArtifactName, out artifactName) ||
                string.IsNullOrEmpty(artifactName))
            {
                throw new Exception(StringUtil.Loc("ArtifactNameRequired"));
            }

            string containerFolder;

            if (!eventProperties.TryGetValue(ArtifactUploadEventProperties.ContainerFolder, out containerFolder) ||
                string.IsNullOrEmpty(containerFolder))
            {
                containerFolder = artifactName;
            }

            var propertyDictionary = ExtractArtifactProperties(eventProperties);

            string localPath = data;

            if (string.IsNullOrEmpty(localPath))
            {
                throw new Exception(StringUtil.Loc("ArtifactLocationRequired"));
            }

            if (!IsUncSharePath(context, localPath) && (context.Variables.System_HostType != HostTypes.Build))
            {
                throw new Exception(StringUtil.Loc("UploadArtifactCommandNotSupported", context.Variables.System_HostType));
            }

            string fullPath = Path.GetFullPath(localPath);

            if (!File.Exists(fullPath) && !Directory.Exists(fullPath))
            {
                // if localPath is not a file or folder on disk
                throw new FileNotFoundException(StringUtil.Loc("PathNotExist", localPath));
            }
            else if (Directory.Exists(fullPath) && Directory.EnumerateFiles(fullPath, "*", SearchOption.AllDirectories).FirstOrDefault() == null)
            {
                // if localPath is a folder but the folder contains nothing
                context.Warning(StringUtil.Loc("DirectoryIsEmptyForArtifact", fullPath, artifactName));
                return;
            }

            // queue async command task to associate artifact.
            context.Debug($"Upload artifact: {fullPath} to server for build: {buildId.Value} at backend.");
            var commandContext = HostContext.CreateService <IAsyncCommandContext>();

            commandContext.InitializeCommandContext(context, StringUtil.Loc("UploadArtifact"));
            commandContext.Task = UploadArtifactAsync(commandContext,
                                                      WorkerUtilies.GetVssConnection(context),
                                                      projectId,
                                                      containerId.Value,
                                                      containerFolder,
                                                      buildId.Value,
                                                      artifactName,
                                                      propertyDictionary,
                                                      fullPath,
                                                      context.CancellationToken);
            context.AsyncCommands.Add(commandContext);
        }