Пример #1
0
        public void CreateUpdateDeleteVSTSGroup()
        {
            // Get the client
            VssConnection   connection  = Context.Connection;
            GraphHttpClient graphClient = connection.GetClient <GraphHttpClient>();

            //
            // Part 1: create a group at the account level
            //
            ClientSampleHttpLogger.SetOperationName(this.Context, "CreateGroup");
            GraphGroupCreationContext createGroupContext = new GraphGroupVstsCreationContext
            {
                DisplayName = "Developers-" + Guid.NewGuid(),
                Description = "Group created via client library"
            };

            GraphGroup newGroup        = graphClient.CreateGroupAsync(createGroupContext).Result;
            string     groupDescriptor = newGroup.Descriptor;

            Context.Log("New group created! ID: {0}", groupDescriptor);

            //
            // Part 2: update the description attribute for the group
            //
            ClientSampleHttpLogger.SetOperationName(this.Context, "UpdateGroup");
            Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchDocument patchDocument = VssJsonPatchDocumentFactory.ConstructJsonPatchDocument(VisualStudio.Services.WebApi.Patch.Operation.Replace, Constants.GroupUpdateFields.Description, "Updated description");
            GraphGroup updatedGroup     = graphClient.UpdateGroupAsync(groupDescriptor, patchDocument).Result;
            string     groupDescription = updatedGroup.Description;

            Context.Log("Updated group description: {0}", groupDescription);

            //
            // Part 3: delete the group
            //

            ClientSampleHttpLogger.SetOperationName(this.Context, "DeleteGroup");
            graphClient.DeleteGroupAsync(groupDescriptor).SyncResult();

            // Try to get the deleted group (should result in an exception)
            try
            {
                ClientSampleHttpLogger.SetOperationName(this.Context, "GetDisabledGroup");
                newGroup = graphClient.GetGroupAsync(groupDescriptor).Result;
            }
            catch (Exception e)
            {
                Context.Log("Unable to get the deleted group:" + e.Message);
            }
        }
        private Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.AttachmentReference UploadImageToTarget(WorkItem wi, string filePath)
        {
            var httpClient = ((TfsConnection)Engine.Target.InternalCollection).GetClient <WorkItemTrackingHttpClient>();

            // uploads and creates the image attachment
            Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.AttachmentReference link = null;
            using (FileStream uploadStream = File.Open(filePath, FileMode.Open, FileAccess.Read))
            {
                link = httpClient.CreateAttachmentAsync(uploadStream, fileName: Path.GetFileName(filePath)).ConfigureAwait(false).GetAwaiter().GetResult();
            }

            if (link == null)
            {
                throw new Exception($"Problem uploading image [{filePath}] for Work Item [{wi.Id}].");
            }


            // Attaches it with dummy work item and removes it just to be able to make the image visible to all the users.
            // VS402330: Unauthorized Read access to the attachment under the areas
            var payload = new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchDocument();

            payload.Add(new Microsoft.VisualStudio.Services.WebApi.Patch.Json.JsonPatchOperation()
            {
                Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
                Path      = "/relations/-",
                Value     = new
                {
                    rel = "AttachedFile",
                    url = link.Url
                }
            });

            var dummyWi = GetDummyWorkItem(wi.Type);
            var wii     = httpClient.UpdateWorkItemAsync(payload, dummyWi.Id).GetAwaiter().GetResult();

            if (wii != null)
            {
                payload[0].Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Remove;
                payload[0].Path      = "/relations/" + (wii.Relations.Count - 1);
                payload[0].Value     = null;
                wii = httpClient.UpdateWorkItemAsync(payload, dummyWi.Id).GetAwaiter().GetResult();
            }
            else
            {
                throw new Exception($"Problem attaching the uploaded image [{filePath}] with dummy workitem [{dummyWi.Id}] to be able to use for Work Item [{wi.Id}].");
            }

            return(link);
        }