public static bool FindAnyTeam(ClientSampleContext context, Guid?projectId, out WebApiTeamRef team)
        {
            if (!projectId.HasValue)
            {
                TeamProjectReference project;
                if (FindAnyProject(context, out project))
                {
                    projectId = project.Id;
                }
            }

            // Check if we already have a team that has been cached for this project
            if (!context.TryGetValue <WebApiTeamRef>("$" + projectId + "Team", out team))
            {
                TeamHttpClient teamClient = context.Connection.GetClient <TeamHttpClient>();

                using (new ClientSampleHttpLoggerOutputSuppression())
                {
                    team = teamClient.GetTeamsAsync(projectId.ToString(), top: 1).Result.FirstOrDefault();
                }

                if (team != null)
                {
                    context.SetValue <WebApiTeamRef>("$" + projectId + "Team", team);
                }
                else
                {
                    // create a team?
                    throw new Exception("No team available for running this sample.");
                }
            }

            return(team != null);
        }
        public static bool FindAnyProject(ClientSampleContext context, out TeamProjectReference project)
        {
            // Check if we already have a default project loaded
            if (!context.TryGetValue <TeamProjectReference>("$defautProject", out project))
            {
                VssConnection     connection    = context.Connection;
                ProjectHttpClient projectClient = connection.GetClient <ProjectHttpClient>();

                using (new ClientSampleHttpLoggerOutputSuppression())
                {
                    // Check if an ID was already set (this could have been provided by the caller)
                    Guid projectId;
                    if (!context.TryGetValue <Guid>("projectId", out projectId))
                    {
                        // Get the first project
                        project = projectClient.GetProjects(null, top: 1).Result.FirstOrDefault();
                    }
                    else
                    {
                        // Get the details for this project
                        project = projectClient.GetProject(projectId.ToString()).Result;
                    }
                }

                if (project != null)
                {
                    context.SetValue <TeamProjectReference>("$defautProject", project);
                }
                else
                {
                    // create a project here?
                    throw new Exception("No projects available for running the sample.");
                }
            }

            return(project != null);
        }
Beispiel #3
0
        public static void RunClientSampleMethods(Uri connectionUrl, VssCredentials credentials, string area = null, string resource = null, DirectoryInfo outputPath = null)
        {
            if (area == "*")
            {
                area = null;
            }

            if (resource == "*")
            {
                resource = null;
            }

            Dictionary <ClientSample, IEnumerable <RunnableClientSampleMethod> > runnableMethodsBySample = GetRunnableClientSampleMethods(area, resource);

            if (!runnableMethodsBySample.Any())
            {
                Console.WriteLine("No samples found to run.");
            }
            else
            {
                ClientSampleContext context = new ClientSampleContext(connectionUrl, credentials);

                Console.WriteLine("Start running client samples...");
                Console.WriteLine("");
                Console.WriteLine("  URL     : {0}", connectionUrl);
                Console.WriteLine("  Area    : {0}", (area == null ? "(all)" : area));
                Console.WriteLine("  Resource: {0}", (resource == null ? "(all)" : resource));
                Console.WriteLine("  Output  : {0}", (outputPath == null ? "(disabled)" : outputPath.FullName));
                Console.WriteLine("");

                // Make sure we can connect before running the samples
                context.Connection.ConnectAsync().SyncResult();

                context.SetValue <DirectoryInfo>(ClientSampleHttpLogger.PropertyOutputFilePath, outputPath);

                foreach (var item in runnableMethodsBySample)
                {
                    ClientSample clientSample = item.Key;
                    clientSample.Context = context;

                    foreach (var runnableMethod in item.Value)
                    {
                        try
                        {
                            context.Log("+------------------------------------------------------------------------------+");
                            context.Log("| {0} |", String.Format("{0}/{1}", runnableMethod.MethodBase.Name, runnableMethod.MethodBase.DeclaringType.Name).PadRight(76));
                            context.Log("|                                                                              |");
                            context.Log("| API: {0} |", String.Format("{0}/{1}", runnableMethod.Area, runnableMethod.Resource).PadRight(71));
                            context.Log("+------------------------------------------------------------------------------+");
                            context.Log("");

                            // Set these so the HTTP logger has access to them when it needs to write the output
                            ClientSampleContext.CurrentRunnableMethod = runnableMethod;
                            ClientSampleContext.CurrentContext        = context;

                            // Reset suppression and operation name (don't want these values carrying over to the next sample method call)
                            ClientSampleHttpLogger.SetSuppressOutput(context, false);
                            ClientSampleHttpLogger.ResetOperationName(context);

                            object result = runnableMethod.MethodBase.Invoke(clientSample, null);
                            // if the runnable method is async, then wait for the completion
                            if (result is Task resultTask)
                            {
                                resultTask.SyncResult();
                            }
                        }
                        catch (Exception ex)
                        {
                            //the innermost exception is the interesting one
                            while (ex.InnerException != null)
                            {
                                ex = ex.InnerException;
                            }
                            Console.WriteLine("FAILED! With exception: " + ex.Message);
                        }
                        finally
                        {
                            context.Log("");
                        }
                    }
                }
            }
        }
 public static void SetOperationName(ClientSampleContext context, string name)
 {
     context.SetValue <string>(PropertyOperationName, name);
 }
 public static void SetSuppressOutput(ClientSampleContext context, bool suppress)
 {
     context.SetValue <bool>(PropertySuppressOutput, suppress);
 }