Esempio n. 1
0
        public void ProcessTeams()
        {
            string file = $@"{AppDomain.CurrentDomain.BaseDirectory}appSettings.JSON";

            string     fileContents = GetFileContents(file);
            AppSetting settings     = JsonConvert.DeserializeObject <AppSetting>(fileContents);

            foreach (var team in settings.Teams)
            {
                Console.WriteLine($"Exporting team {team.Code}.");

                string _credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", team.Token)));
                string siteUrl      = $@"https://{team.TFSUrl}";
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(siteUrl);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);

                    //GetWorkItemsByWiql(client, team);
                    SprintResult sprintsFromSourceSystem = GetSprintsFromTeam(client, team);

                    HttpResponseMessage queryHttpResponseMessage = RunQueries(client, team);


                    if (queryHttpResponseMessage.IsSuccessStatusCode)
                    {
                        //bind the response content to the queryResult object
                        QueryResult queryResult = queryHttpResponseMessage.Content.ReadAsAsync <QueryResult>().Result;
                        string      queryId     = queryResult.id;

                        HttpResponseMessage httpResponseMessage = RunIDQuery(client, team, queryId);
                        if (httpResponseMessage.IsSuccessStatusCode)
                        {
                            WorkItemQueryResult workItemQueryResult = httpResponseMessage.Content.ReadAsAsync <WorkItemQueryResult>().Result;

                            //now that we have a bunch of work items, build a list of id's so we can get details
                            var builder = new System.Text.StringBuilder();
                            foreach (var item in workItemQueryResult.workItems)
                            {
                                builder.Append(item.id.ToString()).Append(",");
                            }

                            //clean up string of id's
                            string ids = builder.ToString().TrimEnd(new char[] { ',' });
                            //string[] idToQuery = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                            HttpResponseMessage getWorkItemsHttpResponse = RunWorkItemsQuery(client, team, ids, settings);

                            if (getWorkItemsHttpResponse.IsSuccessStatusCode)
                            {
                                var        result = getWorkItemsHttpResponse.Content.ReadAsStringAsync().Result;
                                RootObject ro     = JsonConvert.DeserializeObject <RootObject>(result);
                                ro.SourceSystemSprints = sprintsFromSourceSystem;
                                OutPutObject output      = ro.ConvertToOutput(team);
                                string       fileContent = JsonConvert.SerializeObject(output);
                                WriteFile(team.Code, settings.OutputDirectory, fileContent);
                            }
                        }
                    }
                }
            }
        }