public static async Task CreateIteration(IConfiguration configuration)
        {
            Util.WriteLog("BaseUrl", ConsoleColor.Cyan);
            Util.WriteLog(TfsVariables.BaseUrl.ToString());
            Util.WriteLog("Collection ", ConsoleColor.Cyan);
            Util.WriteLog(TfsVariables.Collection);
            Util.WriteLog("Personal Access Token", ConsoleColor.Cyan);
            Util.WriteLog(TfsVariables.PersonalAccessToken);
            Util.WriteLog("Azure DevOps project ", ConsoleColor.Cyan);
            Util.WriteLog(TfsVariables.Project);
            Util.WriteLog("Adding iterations to this teams", ConsoleColor.Cyan);
            TfsVariables.Teams.ForEach(o => Util.WriteLog("-" + o.Value));
            Util.WriteLog("Iterations to add to teams:", ConsoleColor.Cyan);
            TfsVariables.IterationToAddToTeams.ForEach(c => Util.WriteLog("-" + c.Key));


            Util.WriteLog("Press enter when ready to change iterations? If not ready press ctrl-c and start again.", ConsoleColor.Green);
            Console.ReadLine();


            var iterationIdentifier = new List <string>();
            var a = new Attributes();

            TfsVariables.IterationToAddToTeams.ForEach(iterationAppSettings =>
            {
                var iteration = TfsApi.GetIteration(TfsVariables.Project, iterationAppSettings.Key).GetAwaiter().GetResult();

                if (iteration == null)
                {
                    Util.WriteLog($"Creating iteration {iterationAppSettings.Key}");

                    IEnumerable <IConfigurationSection> section = Program.Configuration.GetSection(iterationAppSettings.Path + ":attributes").GetChildren();

                    foreach (var attr in section)
                    {
                        switch (attr.Key)
                        {
                        case "startDate":
                            //a.startDate = DateTime.ParseExact(attr.Value, "yyyy-mm-dd", null);
                            a.startDate = attr.Value + "T00:00:00Z";
                            Console.WriteLine($"Creating iteration startdate {a.startDate}");
                            break;

                        case "finishDate":
                            a.finishDate = attr.Value + "T00:00:00Z";
                            Console.WriteLine($"Creating iteration startdate {a.finishDate}");
                            break;
                        }
                    }

                    var o = new CreateIterationBody
                    {
                        name       = iterationAppSettings.Key,
                        attributes = a
                    };

                    var createdIteration = TfsApi.CreateIteration(TfsVariables.Project, o).GetAwaiter().GetResult();
                    iterationIdentifier.Add(createdIteration.identifier);
                }
                else
                {
                    Console.WriteLine($"Iteration {iterationAppSettings.Key} already exists");
                    iterationIdentifier.Add(iteration.identifier);
                }
            });


            HttpStatusCode statusCode;

            foreach (var configTeams in TfsVariables.Teams)
            {
                Console.WriteLine($"Adding iteration to team {configTeams.Value}");
                foreach (var identifier in iterationIdentifier)
                {
                    statusCode = TfsApi.TeamDefaultIterationPath(configTeams.Value).GetAwaiter().GetResult();
                    if (statusCode != HttpStatusCode.NotFound)
                    {
                        var addIteration = await TfsApi.AddIterationToTeam(configTeams.Value, identifier);
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
示例#2
0
        public static async Task <GetIterationResponse> CreateIteration(string project, CreateIterationBody body)
        {
            var newIterationPath             = String.Empty;
            var iterationPath                = body.name.Split('/');
            GetIterationResponse objResponse = null;

            Util.WriteLog("Starting create of iteration " + body.name);

            foreach (var path in iterationPath)
            {
                body.name = path;
                var bodyContent   = JsonConvert.SerializeObject(body);
                var stringContent = new StringContent(bodyContent, Encoding.UTF8, "application/json");
                using (HttpResponseMessage response = await Client.PostAsync(
                           $"/tfs/{TfsVariables.Collection}/{project}/_apis/wit/classificationnodes/iterations/{newIterationPath}?api-version=4.1", stringContent))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        Util.WriteLog($"Created iteration path {newIterationPath}");
                        string content = await response.Content.ReadAsStringAsync();

                        objResponse       = JsonConvert.DeserializeObject <GetIterationResponse>(content);
                        newIterationPath += path + "/";
                    }
                    else if (response.StatusCode == HttpStatusCode.Conflict)
                    {
                        Util.WriteLog($"Iteration path already exists {newIterationPath}. Skip create iteration and move on to next. ");
                        //Path exist - but we need to see if subpath exists
                        newIterationPath += path + "/";
                    }
                }
            }
            return(objResponse);
        }