static void Main(string[] args) { var argumentOptions = new ArgumentOptions(); if (CommandLine.Parser.Default.ParseArguments(args, argumentOptions)) { if (argumentOptions.ListPRs) { ListPullRequests(); } else if (argumentOptions.CreatePR && !string.IsNullOrWhiteSpace(argumentOptions.BranchName)) { CreatePullRequest(argumentOptions, Git.AppConfig("MasterBranch")); } else if (argumentOptions.OpenWeb && argumentOptions.PullRequestId.HasValue) { OpenPrWeb(argumentOptions.PullRequestId.Value); } else if (argumentOptions.Sha && argumentOptions.PullRequestId.HasValue) { PrintPrSha(argumentOptions.PullRequestId.Value); } else { Console.WriteLine(argumentOptions.GetUsage()); } } }
private static void CreatePullRequest(ArgumentOptions argumentOptions, string mainBranch) { Console.WriteLine("Creating PR for branch {0} into {1}", argumentOptions.BranchName, mainBranch); var executablePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); executablePath = new Uri(executablePath).LocalPath; var notepadPath = Path.Combine(executablePath, "notepad2.exe"); var prMessageFilePath = Path.Combine(executablePath, "pull-request-text.txt"); string prMessageCreatorTemplate = string.Format( "{0}" + "# The first line is the title of the PR, all other lines are the body{0}" + "# Lines starting with # and blank lines are ignored.{0}" + "# PR branch: {1}.\n" + "# Main branch: {2}", Environment.NewLine, argumentOptions.BranchName, mainBranch); File.WriteAllText(prMessageFilePath, prMessageCreatorTemplate); Process.Start(notepadPath, prMessageFilePath).WaitForExit(); var prMessageCreatorLines = File.ReadAllLines(prMessageFilePath); var validLines = prMessageCreatorLines.Where(line => !line.StartsWith("#") && !string.IsNullOrWhiteSpace(line)); if (validLines.Count() >= 2) { string title = validLines.ElementAt(0); string body = string.Join(Environment.NewLine, validLines.Skip(1)); var response = GitHub.CreatePullRequest(title, argumentOptions.BranchName, mainBranch, body); if (response.StatusCode != HttpStatusCode.Created) { Console.WriteLine("Failed to create PR: \n" + JsonConvert.SerializeObject(JsonConvert.DeserializeObject(response.RawText), Formatting.Indented)); } else { dynamic pr = JsonConvert.DeserializeObject(response.RawText); Console.WriteLine("Created PR: " + pr.html_url); } } else { Console.WriteLine("PR cancelled - At least 2 lines are expected from the user - Title and body"); } //GitHub.CreatePullRequest("From API!", argumentOptions.BranchName, MainBranch, "This is some body\nHi!"); }