public List <TeamMemberCapacityIdentityRef> ReplaceTeamMemberCapacities() { VssConnection connection = Context.Connection; WorkHttpClient workClient = connection.GetClient <WorkHttpClient>(); Guid projectId = ClientSampleHelpers.FindAnyProject(this.Context).Id; Guid teamId = ClientSampleHelpers.FindAnyTeam(this.Context, projectId).Id; TeamContext teamContext = new TeamContext(projectId, teamId); List <TeamSettingsIteration> result = workClient.GetTeamIterationsAsync(teamContext, "current").Result; Guid iterationId = result[0].Id; List <TeamMemberCapacityIdentityRef> capacity = workClient.GetCapacitiesWithIdentityRefAsync(teamContext, iterationId).Result; var updatedCapacity = capacity.Select(teamMemberCapacity => { return(new TeamMemberCapacityIdentityRef() { TeamMember = teamMemberCapacity.TeamMember, Activities = teamMemberCapacity.Activities.Select(a => { return new Activity() { Name = a.Name, CapacityPerDay = a.CapacityPerDay }; }), DaysOff = teamMemberCapacity.DaysOff }); }); var updatedCapacityResult = workClient.ReplaceCapacitiesWithIdentityRefAsync(updatedCapacity, teamContext, iterationId).Result; return(updatedCapacityResult); }
private void MigrateCapacities(WorkHttpClient sourceHttpClient, WorkHttpClient targetHttpClient, TeamFoundationTeam sourceTeam, TeamFoundationTeam targetTeam, Dictionary <string, string> iterationMap) { if (!_Options.MigrateTeamCapacities) { return; } Log.LogInformation("Migrating team capacities.."); try { var sourceTeamContext = new TeamContext(Source.TfsProject.Guid, sourceTeam.Identity.TeamFoundationId); var sourceIterations = sourceHttpClient.GetTeamIterationsAsync(sourceTeamContext).ConfigureAwait(false).GetAwaiter().GetResult(); var targetTeamContext = new TeamContext(Target.TfsProject.Guid, targetTeam.Identity.TeamFoundationId); var targetIterations = targetHttpClient.GetTeamIterationsAsync(targetTeamContext).ConfigureAwait(false).GetAwaiter().GetResult(); foreach (var sourceIteration in sourceIterations) { try { var targetIterationPath = iterationMap[sourceIteration.Path]; var targetIteration = targetIterations.FirstOrDefault(i => i.Path == targetIterationPath); if (targetIteration == null) { continue; } var targetCapacities = new List <TeamMemberCapacityIdentityRef>(); var sourceCapacities = sourceHttpClient.GetCapacitiesWithIdentityRefAsync(sourceTeamContext, sourceIteration.Id).ConfigureAwait(false).GetAwaiter().GetResult(); foreach (var sourceCapacity in sourceCapacities) { var sourceDisplayName = sourceCapacity.TeamMember.DisplayName; var index = sourceDisplayName.IndexOf("<"); if (index > 0) { sourceDisplayName = sourceDisplayName.Substring(0, index).Trim(); } var targetTeamFoundatationIdentity = _targetTeamFoundationIdentitiesLazyCache.Value.FirstOrDefault(i => i.DisplayName == sourceDisplayName); if (targetTeamFoundatationIdentity != null) { targetCapacities.Add(new TeamMemberCapacityIdentityRef { Activities = sourceCapacity.Activities, DaysOff = sourceCapacity.DaysOff, TeamMember = new IdentityRef { Id = targetTeamFoundatationIdentity.TeamFoundationId.ToString() } }); } else { Log.LogWarning("[SKIP] Team Member {member} was not found on target when replacing capacities on iteration {iteration}.", sourceCapacity.TeamMember.DisplayName, targetIteration.Path); } } if (targetCapacities.Count > 0) { targetHttpClient.ReplaceCapacitiesWithIdentityRefAsync(targetCapacities, targetTeamContext, targetIteration.Id).ConfigureAwait(false).GetAwaiter().GetResult(); Log.LogDebug("Team {team} capacities for iteration {iteration} migrated.", targetTeam.Name, targetIteration.Path); } } catch (Exception ex) { Log.LogError(ex, "[SKIP] Problem migrating team capacities for iteration {iteration}.", sourceIteration.Path); } } } catch (Exception ex) { Log.LogError(ex, "[SKIP] Problem migrating team capacities."); } Log.LogInformation("Team capacities migration done.."); }