diff --git a/ExportPipelineDefinitions/Program.cs b/ExportPipelineDefinitions/Program.cs index cf9f958..53e4d51 100644 --- a/ExportPipelineDefinitions/Program.cs +++ b/ExportPipelineDefinitions/Program.cs @@ -1,4 +1,4 @@ -using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Linq; //using Json; using System; using System.IO; @@ -44,7 +44,7 @@ class Program static bool isYamlPipeline = false; static List yamlFileNames = new List(); // For holding duplicate template file names found in yamlFileNames. Allows avoiding file overwrites. - static HashSet yamlFileNameDuplicates = new HashSet(); + static HashSet yamlFileNameDuplicates = new HashSet(); public class BuildDef { @@ -337,7 +337,7 @@ public static async Task WriteDefinitionToFile(string project, string definition string filePathWithoutExtension = directory + "\\" + buildDef.name.Replace("?", "").Replace(":", "").Replace(@"\", "-").Replace("/", "-"); // Limit file path to 259 characters to avoid System.IO.PathTooLongException. - int maxLength = Math.Min(filePathWithoutExtension.Length, 254); + int maxLength = Math.Min(filePathWithoutExtension.Length, 254); string fullFilePath = filePathWithoutExtension.Substring(0, maxLength) + ".json"; System.IO.File.WriteAllText(fullFilePath, fileContent); @@ -512,31 +512,56 @@ private static bool DownloadFileFromGithub(string githubFileUrl, string outputFi githubFileUrl += $"?access_token={githubPersonalAccessToken}"; } - WebClient webClient = new WebClient(); - webClient.Headers.Add("user-agent", "Anything"); - try + using (var client = new HttpClient()) { - string contents = webClient.DownloadString(new Uri(githubFileUrl)); - File.WriteAllText(outputFilePath, contents); - Console.WriteLine($"{logIndent}* {fileName}"); - BuildCsvString(column, fileName); + client.DefaultRequestHeaders.Add("user-agent", "Anything"); - return true; - } - catch (Exception ex) - { - Console.WriteLine($"{logIndent}* {fileName}**"); - BuildCsvString(column, fileName + "**"); - // We couldn't get the file contents, so write the error message to the file instead. - string contents = $"* {ex.Message.ToString()}\r\n{githubFileUrl}"; - File.WriteAllText(outputFilePath, contents); - if (ex.Message.ToString().Contains("(404)")) + if (!githubFileUrl.Contains("githubusercontent")) + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", + Convert.ToBase64String( + Encoding.ASCII.GetBytes( + string.Format("{0}:{1}", "", azurePersonalAccessToken)))); + + try { - //Console.WriteLine($"Not Found (404): {githubFileUrl}"); + var response = client.GetAsync(githubFileUrl).Result; + + //Write content to file regardless of success + string contents = response.Content.ReadAsStringAsync().Result; + File.WriteAllText(outputFilePath, contents); + + if (response.IsSuccessStatusCode) + { + Console.WriteLine($"{logIndent}* {fileName}"); + BuildCsvString(column, fileName); + + return true; + } + else + { + Console.WriteLine($"{logIndent}* {fileName}**"); + BuildCsvString(column, fileName + "**"); + + Console.WriteLine($"* {response.StatusCode} ({(int)response.StatusCode}) {githubFileUrl}"); + + return false; + } } - else + catch (Exception ex) { - Console.WriteLine($"* {ex.Message.ToString()} {githubFileUrl}"); + Console.WriteLine($"{logIndent}* {fileName}**"); + BuildCsvString(column, fileName + "**"); + // We couldn't get the file contents, so write the error message to the file instead. + string contents = $"* {ex.Message}\r\n{githubFileUrl}"; + File.WriteAllText(outputFilePath, contents); + if (ex.Message.ToString().Contains("(404)")) + { + //Console.WriteLine($"Not Found (404): {githubFileUrl}"); + } + else + { + Console.WriteLine($"* {ex.Message} {githubFileUrl}"); + } } } return false;