Example #1
0
        private async Task <IEnumerable <Wit> > InternalGetWitsByIdChunked(List <int> ids, string[] fields, DateTime?asOf,
                                                                           WorkItemExpand?expand, WorkItemErrorPolicy?errorPolicy, CancellationToken cancellationToken)
        {
            const int nSize     = 150;
            var       workItems = new List <Wit>();

            if (errorPolicy == null)
            {
                errorPolicy = ErrorPolicy;
            }

            // Chunk the retrieval.
            for (int i = 0; i < ids.Count; i += nSize)
            {
                var range = ids.GetRange(i, Math.Min(nSize, ids.Count - i));

                using var mc = log?.Enter(LogLevel.RawApis, new object[] { range, fields, asOf, expand, errorPolicy, null, cancellationToken }, "GetWorkItemsAsync");
                {
                    var results = await WorkItemClient.GetWorkItemsAsync(range, fields, asOf, expand, errorPolicy, userState : null, cancellationToken)
                                  .ConfigureAwait(false);

                    workItems.AddRange(results);
                }
            }

            return(workItems);
        }
Example #2
0
        private async Task <IEnumerable <Wit> > InternalGetWorkItemsAsync(string query, string[] fields, int?top, bool?timePrecision,
                                                                          WorkItemExpand?expand, WorkItemErrorPolicy?errorPolicy,
                                                                          CancellationToken cancellationToken)
        {
            log?.WriteLine(LogLevel.Query, query);

            Wiql wiql = new Wiql {
                Query = query
            };

            if (errorPolicy == null)
            {
                errorPolicy = ErrorPolicy;
            }

            using var mc = log?.Enter(LogLevel.RawApis, new object[] { wiql, timePrecision, top, null, cancellationToken }, "QueryByWiqlAsync");

            // Return a list of URLs + Ids for matching workItems.
            WorkItemQueryResult queryResult = await WorkItemClient.QueryByWiqlAsync(wiql, timePrecision, top, userState : null, cancellationToken)
                                              .ConfigureAwait(false);

            if (queryResult.WorkItems?.Any() == true)
            {
                var ids = queryResult.WorkItems.Select(wi => wi.Id).ToList();

                // Get the actual work items from the IDs; chunked.
                return(await InternalGetWitsByIdChunked(ids, fields, queryResult.AsOf, expand, errorPolicy, cancellationToken)
                       .ConfigureAwait(false));
            }

            return(Enumerable.Empty <Wit>());
        }
        /// <summary>
        /// Gets the work items for the provided <paramref name="ids"/>.
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="ids">The work item identifiers.</param>
        /// <param name="fields">The fields.</param>
        /// <param name="asOf">As of.</param>
        /// <param name="expand">The expand.</param>
        /// <param name="errorPolicy">The error policy.</param>
        /// <param name="userState">State of the user.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">client</exception>
        public static IObservable <WorkItem> GetWorkItems(this WorkItemTrackingHttpClient client, IEnumerable <int> ids, IEnumerable <string> fields = null,
                                                          DateTime?asOf = null, WorkItemExpand?expand = null,
                                                          WorkItemErrorPolicy?errorPolicy = null,
                                                          object userState = null)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            if (ids == null)
            {
                return(Observable.Empty <WorkItem>());
            }

            // else
            return(Observable.FromAsync(
                       token => client.GetWorkItemsAsync(
                           ids,
                           fields,
                           asOf,
                           expand,
                           errorPolicy,
                           userState,
                           token))
                   .SelectMany(workItems => workItems));
        }
 public Task <WorkItem> GetWorkItemAsync(
     string project,
     int id,
     IEnumerable <string> fields = null,
     DateTime?asOf         = null,
     WorkItemExpand?expand = null,
     object userState      = null,
     CancellationToken cancellationToken = default)
 {
     return(this.workItemTrackingHttpClient.GetWorkItemAsync(project, id, fields, asOf, expand, userState, cancellationToken));
 }
 public Task <WorkItem> UpdateWorkItemAsync(
     JsonPatchDocument document,
     int id,
     bool?validateOnly                   = null,
     bool?bypassRules                    = null,
     bool?suppressNotifications          = null,
     WorkItemExpand?expand               = null,
     object userState                    = null,
     CancellationToken cancellationToken = default)
 {
     return(this.workItemTrackingHttpClient.UpdateWorkItemAsync(document, id, validateOnly, bypassRules, suppressNotifications, expand, userState, cancellationToken));
 }
        /// <summary>
        /// Gets the <see cref="WorkItem" /> for the specified <paramref name="id" />.
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="id">The work item identifier.</param>
        /// <param name="fields">The fields.</param>
        /// <param name="asOf">As of.</param>
        /// <param name="expand">The expand.</param>
        /// <param name="userState">State of the user.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">client</exception>
        /// <exception cref="ArgumentOutOfRangeException">id</exception>
        public static IObservable <WorkItem> GetWorkItem(this WorkItemTrackingHttpClient client, int id, IEnumerable <string> fields = null,
                                                         DateTime?asOf = null, WorkItemExpand?expand = null, object userState = null)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            return(id <= 0
                ? Observable.Empty <WorkItem>()
                : Observable.FromAsync(token => client.GetWorkItemAsync(id, fields, asOf, expand, userState, token)));
        }
Example #7
0
        private async Task <Wit> UpdateAsync(int id, JsonPatchDocument patchDocument, bool?validateOnly, bool?bypassRules,
                                             bool?supressNotifications, WorkItemExpand?expand, CancellationToken cancellationToken)
        {
            if (this.ValidateOnly)
            {
                validateOnly = true;
            }

            using var mc = log?.Enter(LogLevel.RawApis, new object[] { patchDocument, id, validateOnly, bypassRules, supressNotifications, expand, null, cancellationToken }, "UpdateWorkItemAsync");
            log?.Dump(patchDocument);

            return(await WorkItemClient.UpdateWorkItemAsync(patchDocument, id, validateOnly, bypassRules, supressNotifications, expand, userState : null, cancellationToken)
                   .ConfigureAwait(false));
        }
        /// <summary>
        /// Gets the <see cref="WorkItem"/> for the specified <paramref name="id"/>.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="fields">The fields.</param>
        /// <param name="asOf">As of.</param>
        /// <param name="expand">The expand.</param>
        /// <param name="userState">State of the user.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException">id</exception>
        public virtual IObservable <WorkItem> GetWorkItem(int id, IEnumerable <string> fields = null, DateTime?asOf = null,
                                                          WorkItemExpand?expand = null, object userState            = null)
        {
            if (id <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(id));
            }

            return(Observable.Create <WorkItem>(observer =>
            {
                var workItemTrackingHttpClient = VisualStudioServicesConnection.GetClient <WorkItemTrackingHttpClient>();

                var retrievalObservable = workItemTrackingHttpClient
                                          .GetWorkItem(id, fields, asOf, expand, userState)
                                          .Subscribe(observer);

                return new CompositeDisposable(workItemTrackingHttpClient, retrievalObservable);
            }));
        }
        public override Task <WorkItem> GetWorkItemAsync(int id, IEnumerable <string> fields = null, DateTime?asOf = null, WorkItemExpand?expand = null, object userState = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Debug.WriteLine($"FakeWorkItemTrackingHttpClient.GetWorkItemAsync({id})");
            if (expand == null)
            {
                throw new ArgumentNullException(nameof(expand));
            }
            if (expand != WorkItemExpand.All)
            {
                throw new ArgumentException("Must be WorkItemExpand.All", nameof(expand));
            }

            if (workItemFactory.ContainsKey(id))
            {
                var t = new Task <WorkItem>(workItemFactory[id]);
                t.RunSynchronously();
                return(t);
            }
            else
            {
                return(null);
            }
        }
        public override Task <List <WorkItem> > GetWorkItemsAsync(IEnumerable <int> ids, IEnumerable <string> fields = null, DateTime?asOf = null, WorkItemExpand?expand = null, WorkItemErrorPolicy?errorPolicy = null, object userState = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            string sid = ids.Aggregate(string.Empty, (s, i) => s + "," + i.ToString());

            Debug.WriteLine($"FakeWorkItemTrackingHttpClient.GetWorkItemsAsync({sid})");
            if (expand == null)
            {
                throw new ArgumentNullException(nameof(expand));
            }
            if (expand != WorkItemExpand.All)
            {
                throw new ArgumentException("Must be WorkItemExpand.All", nameof(expand));
            }

            var t = new Task <List <WorkItem> >(() => {
                var result = new List <WorkItem>();
                foreach (var id in ids)
                {
                    if (workItemFactory.ContainsKey(id))
                    {
                        var wi = workItemFactory[id]();
                        result.Add(wi);
                    }
                }
                return(result);
            });

            t.RunSynchronously();
            return(t);
        }
Example #11
0
 /// <summary>
 /// Populates batchContext.WorkItems
 /// </summary>
 /// <param name="migrationContext"></param>
 /// <param name="workItemIds"></param>
 /// <param name="batchContext"></param>
 /// <param name="expand"></param>
 /// <returns></returns>
 public static async Task ReadSourceWorkItems(IMigrationContext migrationContext, IEnumerable <int> workItemIds, IBatchMigrationContext batchContext, WorkItemExpand?expand = WorkItemExpand.All)
 {
     batchContext.SourceWorkItems = await WorkItemTrackingHelpers.GetWorkItemsAsync(migrationContext.SourceClient.WorkItemTrackingHttpClient, workItemIds, expand : expand);
 }
Example #12
0
        /// <summary>
        /// Given an int array, get the list of workitems. Retries 5 times for connection timeouts etc.
        /// </summary>
        /// <param name="client"></param>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async static Task <IList <WorkItem> > GetWorkItemsAsync(WorkItemTrackingHttpClient client, IEnumerable <int> ids, IEnumerable <string> fields = null, WorkItemExpand?expand = null)
        {
            Logger.LogDebug(LogDestination.File, $"Getting work items for {client.BaseAddress.Host}");

            if (ids == null)
            {
                throw new ArgumentNullException(nameof(ids));
            }

            return(await RetryHelper.RetryAsync(async() =>
            {
                return await client.GetWorkItemsAsync(ids, fields: fields, expand: expand);
            }, 5));
        }
Example #13
0
        public override Task <WorkItem> GetWorkItemAsync(int id, IEnumerable <string> fields = null, DateTime?asOf = null, WorkItemExpand?expand = null, object userState = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (expand == null)
            {
                throw new ArgumentNullException(nameof(expand));
            }
            if (expand != WorkItemExpand.All)
            {
                throw new ArgumentException("Must be WorkItemExpand.All", nameof(expand));
            }
            if (userState == null)
            {
                throw new ArgumentNullException(nameof(userState));
            }
            if (!(userState is RequestContextBase))
            {
                throw new ArgumentException("Must derive from {nameof(RequestContextBase)}", nameof(userState));
            }
            var context = userState as RequestContextBase;

            var t = new Task <WorkItem>(() => new WorkItem()
            {
                Id     = id,
                Fields = new Dictionary <string, object>()
                {
                    { "System.WorkItemType", "Bug" },
                    { "System.State", "Open" },
                    { "System.TeamProject", "MyProject" }
                },
                Rev       = 99,
                Relations = new List <WorkItemRelation>()
                {
                    new WorkItemRelation
                    {
                        Rel = "System.LinkTypes.Hierarchy-Reverse",
                        Url = $"https://example.visualstudio.com/{context.ProjectName}/_apis/wit/workItems/33"
                    },
                    new WorkItemRelation
                    {
                        Rel = "System.LinkTypes.Hierarchy-Forward",
                        Url = $"https://example.visualstudio.com/{context.ProjectName}/_apis/wit/workItems/77"
                    },
                    new WorkItemRelation
                    {
                        Rel = "System.LinkTypes.Hierarchy-Forward",
                        Url = $"https://example.visualstudio.com/{context.ProjectName}/_apis/wit/workItems/78"
                    }
                }
            });

            t.RunSynchronously();
            return(t);
        }
        /// <summary>
        /// Gets the work items for the provided <paramref name="ids" />.
        /// </summary>
        /// <param name="ids">The work item identifiers.</param>
        /// <param name="fields">The fields.</param>
        /// <param name="asOf">As of.</param>
        /// <param name="expand">The expand.</param>
        /// <param name="errorPolicy">The error policy.</param>
        /// <param name="userState">State of the user.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">client</exception>
        public virtual IObservable <WorkItem> GetWorkItemsAsync(IEnumerable <int> ids, IEnumerable <string> fields = null,
                                                                DateTime?asOf = null, WorkItemExpand?expand = null,
                                                                WorkItemErrorPolicy?errorPolicy = null,
                                                                object userState = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            return(Observable.Create <WorkItem>(observer =>
            {
                var workItemTrackingHttpClient = VisualStudioServicesConnection.GetClient <WorkItemTrackingHttpClient>();

                var retrievalObservable = workItemTrackingHttpClient
                                          .GetWorkItems(ids, fields, asOf, expand,
                                                        errorPolicy,
                                                        userState)
                                          .Subscribe(observer);

                return new CompositeDisposable(workItemTrackingHttpClient, retrievalObservable);
            }));
        }