public void ExportPullRequests(string project) { try { using (var client = new HttpClient()) { client.BaseAddress = new Uri(_sourceConfig.UriString); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _sourceCredentials); HttpResponseMessage response = client.GetAsync(_sourceConfig.UriString + string.Format("/DefaultCollection/{0}/_apis/git/repositories?api-version=1.0", project)).Result; if (response.IsSuccessStatusCode) { RepositoryResponse.Repository repository = Newtonsoft.Json.JsonConvert.DeserializeObject <RepositoryResponse.Repository>(response.Content.ReadAsStringAsync().Result.ToString()); if (repository.count > 0) { foreach (var value in repository.value) { using (var clinetOne = new HttpClient()) { clinetOne.BaseAddress = new Uri(_sourceConfig.UriString); clinetOne.DefaultRequestHeaders.Accept.Clear(); clinetOne.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); clinetOne.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _sourceCredentials); HttpResponseMessage pullRequestResponse = clinetOne.GetAsync(_sourceConfig.UriString + string.Format("/DefaultCollection/_apis/git/repositories/{0}/pullRequests?api-version=3.0", value.id)).Result; if (pullRequestResponse.IsSuccessStatusCode) { PullRequestResponse.PullRequest pullRequests = JsonConvert.DeserializeObject <PullRequestResponse.PullRequest>(pullRequestResponse.Content.ReadAsStringAsync().Result.ToString()); int count = 1; if (pullRequests.count > 0) { foreach (var pullReq in pullRequests.value) { if (!Directory.Exists(@"Templates\PullRequests")) { Directory.CreateDirectory(@"Templates\PullRequests"); } foreach (var reviewer in pullReq.reviewers) { reviewer.id = "$reviewer$"; } string pullRequestJSON = JsonConvert.SerializeObject(pullReq, Formatting.Indented); string path = string.Format(@"Templates\PullRequests\{0}", value.name); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } System.IO.File.WriteAllText(path + @"\PullRequest" + count + ".json", pullRequestJSON); count = count + 1; using (var clientTwo = new HttpClient()) { clientTwo.BaseAddress = new Uri(_sourceConfig.UriString); clientTwo.DefaultRequestHeaders.Accept.Clear(); clientTwo.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); clientTwo.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _sourceCredentials); HttpResponseMessage pullRequestComments = clientTwo.GetAsync(_sourceConfig.UriString + string.Format("/DefaultCollection/_apis/git/repositories/{0}/pullRequests/{1}/threads?api-version=3.0", value.id, pullReq.pullRequestId)).Result; if (pullRequestComments.IsSuccessStatusCode) { if (!Directory.Exists(@"Templates\PullRequestComments")) { Directory.CreateDirectory(@"Templates\PullRequestComments"); } PullRequestCommentResponse.Comments pullReqComments = JsonConvert.DeserializeObject <PullRequestCommentResponse.Comments>(pullRequestComments.Content.ReadAsStringAsync().Result.ToString()); if (pullReqComments.count > 0) { string commentJSON = JsonConvert.SerializeObject(pullReqComments.value, Formatting.Indented); string commentPath = string.Format(@"Templates\PullRequestComments\{0}", pullReq.title); if (!Directory.Exists(commentPath)) { Directory.CreateDirectory(commentPath); } System.IO.File.WriteAllText(commentPath + @"\Comment.json", commentJSON); } } } } } } } } } } } } catch (Exception ex) { } }
/// <summary> /// method to export source code url jsons /// </summary> /// <param name="project"></param> public void ExportSourceCode(string project) { try { using (var client = new HttpClient()) { client.BaseAddress = new Uri(_sourceConfig.UriString); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _sourceCredentials); HttpResponseMessage response = client.GetAsync(_sourceConfig.UriString + string.Format("/DefaultCollection/{0}/_apis/git/repositories?api-version=1.0", project)).Result; if (response.IsSuccessStatusCode) { RepositoryResponse.Repository repository = Newtonsoft.Json.JsonConvert.DeserializeObject <RepositoryResponse.Repository>(response.Content.ReadAsStringAsync().Result.ToString()); if (repository.count > 0) { int count = 1; foreach (RepositoryResponse.Value repo in repository.value) { using (var client1 = new HttpClient()) { client1.BaseAddress = new Uri(_sourceConfig.UriString); client1.DefaultRequestHeaders.Accept.Clear(); client1.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client1.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _sourceCredentials); HttpResponseMessage responseCode = client1.GetAsync(_sourceConfig.UriString + string.Format("/DefaultCollection/{0}/_apis/git/repositories/{1}/importRequests?includeAbandoned=true&api-version=3.0-preview", project, repo.id)).Result; if (responseCode.IsSuccessStatusCode) { SourceCodeResponse.Code code = Newtonsoft.Json.JsonConvert.DeserializeObject <SourceCodeResponse.Code>(responseCode.Content.ReadAsStringAsync().Result.ToString()); if (code.value != null) { if (!Directory.Exists(@"Templates\sourceCode")) { Directory.CreateDirectory(@"Templates\sourceCode"); } string fetchedSourceCodeJSON = JsonConvert.SerializeObject(code.value, Formatting.Indented); File.WriteAllText(@"Templates\sourceCode\importSourceCode" + count + ".json", fetchedSourceCodeJSON); count = count + 1; } } } using (var client2 = new HttpClient()) { client2.BaseAddress = new Uri(_sourceConfig.UriString); client2.DefaultRequestHeaders.Accept.Clear(); client2.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client2.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _sourceCredentials); HttpResponseMessage responsePullRequest = client2.GetAsync(_sourceConfig.UriString + string.Format("/DefaultCollection/_apis/git/repositories/{0}/pullRequests?api-version=3.0", repo.id)).Result; if (responsePullRequest.IsSuccessStatusCode) { PullRequestResponse.PullRequest pullRequests = JsonConvert.DeserializeObject <PullRequestResponse.PullRequest>(responsePullRequest.Content.ReadAsStringAsync().Result.ToString()); if (pullRequests.count > 0) { foreach (PullRequestResponse.Value request in pullRequests.value) { if (!Directory.Exists(@"Templates\PullRequests\" + repo.name)) { Directory.CreateDirectory(@"Templates\PullRequests\" + repo.name); } string fetchedPullRequestJSON = JsonConvert.SerializeObject(request, Formatting.Indented); string path = string.Format(@"Templates\PullRequests\{0}\{1}.json", repo.name, request.title); File.WriteAllText(path, fetchedPullRequestJSON); } } } } } } } } } catch (Exception ex) { Console.WriteLine("Error occured while generating export sourcecode template: " + ex.Message); Console.WriteLine(""); } }