Program.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Threading.Tasks;
  8. using System.Xml;
  9. using Microsoft.DotNet.VersionTools.Automation;
  10. using Microsoft.Extensions.Configuration;
  11. namespace Microsoft.Dotnet.Scripts
  12. {
  13. public static class Program
  14. {
  15. private static readonly Config _config = new Config();
  16. public static async Task Main(string[] args)
  17. {
  18. Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
  19. ParseArgs(args);
  20. await CreatePullRequest();
  21. }
  22. private static void ParseArgs(string[] args)
  23. {
  24. var builder = new ConfigurationBuilder().AddCommandLine(args);
  25. var configRoot = builder.Build();
  26. configRoot.Bind(_config);
  27. }
  28. private static async Task CreatePullRequest()
  29. {
  30. var gitHubAuth = new GitHubAuth(_config.GithubToken, _config.GithubUsername, _config.GithubEmail);
  31. var origin = new GitHubProject(_config.GithubProject, _config.GithubUsername);
  32. var upstreamBranch = new GitHubBranch(_config.GithubUpstreamBranch, new GitHubProject(_config.GithubProject, _config.GithubUpstreamOwner));
  33. var commitMessage = $"Updating external dependencies to '{ await GetOrchestratedBuildId() }'";
  34. var body = string.Empty;
  35. if (_config.GitHubPullRequestNotifications.Any())
  36. {
  37. body += PullRequestCreator.NotificationString(_config.GitHubPullRequestNotifications);
  38. }
  39. body += $"New versions:{Environment.NewLine}";
  40. foreach (var updatedVersion in _config.UpdatedVersionsList)
  41. {
  42. body += $" {updatedVersion}{Environment.NewLine}";
  43. }
  44. await new PullRequestCreator(gitHubAuth, origin, upstreamBranch)
  45. .CreateOrUpdateAsync(commitMessage, commitMessage + $" ({upstreamBranch.Name})", body);
  46. }
  47. private static async Task<string> GetOrchestratedBuildId()
  48. {
  49. var xmlUrl = _config.BuildXml;
  50. using (var client = new HttpClient())
  51. {
  52. var response = await client.GetAsync(xmlUrl);
  53. using (var bodyStream = await response.Content.ReadAsStreamAsync())
  54. {
  55. var xmlDoc = new XmlDocument();
  56. xmlDoc.Load(bodyStream);
  57. var orcBuilds = xmlDoc.GetElementsByTagName("OrchestratedBuild");
  58. if (orcBuilds.Count < 1)
  59. {
  60. throw new ArgumentException($"{xmlUrl} didn't have an 'OrchestratedBuild' element.");
  61. }
  62. var orcBuild = orcBuilds[0];
  63. return orcBuild.Attributes["BuildId"].Value;
  64. }
  65. }
  66. }
  67. }
  68. }