예제 #1
0
        private void ProcessArtifactUploadCommand(IExecutionContext context, IArtifactCommands artifactCommands, 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));

            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"));
            }

            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;
            }

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

            commandContext.InitializeCommandContext(context, StringUtil.Loc("UploadArtifact"));
            commandContext.Task = artifactCommands.UploadArtifactAsync(context,
                                                                       commandContext,
                                                                       WorkerUtilies.GetVssConnection(context),
                                                                       projectId,
                                                                       containerFolder,
                                                                       artifactName,
                                                                       propertyDictionary,
                                                                       fullPath,
                                                                       context.CancellationToken);
            context.AsyncCommands.Add(commandContext);
        }
예제 #2
0
        private void ProcessArtifactAssociateCommand(IExecutionContext context, IArtifactCommands artifactCommands, 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));

            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"));
            }

            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.
            var commandContext = HostContext.CreateService <IAsyncCommandContext>();

            commandContext.InitializeCommandContext(context, StringUtil.Loc("AssociateArtifact"));
            commandContext.Task = artifactCommands.AssociateArtifactAsync(context,
                                                                          commandContext,
                                                                          WorkerUtilies.GetVssConnection(context),
                                                                          projectId,
                                                                          artifactName,
                                                                          artifactType,
                                                                          artifactData,
                                                                          propertyDictionary,
                                                                          context.CancellationToken);
            context.AsyncCommands.Add(commandContext);
        }