From 3c2027be187b937e3b6620278b9ad982e2c0e07f Mon Sep 17 00:00:00 2001 From: Devedse Date: Mon, 10 Aug 2020 21:43:56 +0200 Subject: [PATCH] Now return data from the optimization --- .../GitHubRepositoryOptimizer.cs | 138 ++++++++++-------- WebOptimizationProject/WopResult.cs | 12 ++ 2 files changed, 89 insertions(+), 61 deletions(-) create mode 100644 WebOptimizationProject/WopResult.cs diff --git a/WebOptimizationProject/GitHubRepositoryOptimizer.cs b/WebOptimizationProject/GitHubRepositoryOptimizer.cs index 4979b4b..5d6ae94 100644 --- a/WebOptimizationProject/GitHubRepositoryOptimizer.cs +++ b/WebOptimizationProject/GitHubRepositoryOptimizer.cs @@ -53,7 +53,7 @@ public async Task GoOptimize(bool cleanupAfterwards, string repositoryOwner, str await GoOptimize(cleanupAfterwards, repositoryInfo, branchName); } - public async Task GoOptimize(bool cleanupAfterwards, Repository repository, string branchName = null) + public async Task GoOptimize(bool cleanupAfterwards, Repository repository, string branchName = null) { var repositoryOwner = repository.Owner.Login; var repositoryName = repository.Name; @@ -76,93 +76,109 @@ public async Task GoOptimize(bool cleanupAfterwards, Repository repository, stri var clonedRepo = await _git.GitClone(dirOfClonedRepos, repositoryOwner, repositoryName); Directory.SetCurrentDirectory(clonedRepo); - var repositoryInfo = await _gitOctoKitHandler.GitHubClient.Repository.Get(repositoryOwner, repositoryName); - - if (branchName == null) + try { - branchName = repositoryInfo.DefaultBranch; + var repositoryInfo = await _gitOctoKitHandler.GitHubClient.Repository.Get(repositoryOwner, repositoryName); + if (branchName == null) { - throw new Exception("ERROR, couldn't determine branchname"); + branchName = repositoryInfo.DefaultBranch; + if (branchName == null) + { + throw new Exception("ERROR, couldn't determine branchname"); + } } - } - await _git.RunHubCommand("fork"); + await _git.RunHubCommand("fork"); - await _git.RunHubCommand($"remote set-url {_wopConfig.GitHubUserName} https://github.com/{_wopConfig.GitHubUserName}/{repositoryName}.git"); + await _git.RunHubCommand($"remote set-url {_wopConfig.GitHubUserName} https://github.com/{_wopConfig.GitHubUserName}/{repositoryName}.git"); - //Fetch everything in my repository - await _git.RunHubCommand("fetch --all"); + //Fetch everything in my repository + await _git.RunHubCommand("fetch --all"); - //Go to master - await _git.RunHubCommand($"checkout {_wopConfig.GitHubUserName}/{branchName}"); - await _git.RunHubCommand($"merge --strategy-option=theirs origin/{branchName}"); - await _git.RunHubCommand($"push {_wopConfig.GitHubUserName} HEAD:{branchName}"); + //Go to master + await _git.RunHubCommand($"checkout {_wopConfig.GitHubUserName}/{branchName}"); + await _git.RunHubCommand($"merge --strategy-option=theirs origin/{branchName}"); + await _git.RunHubCommand($"push {_wopConfig.GitHubUserName} HEAD:{branchName}"); - var wasAbleToAddTrackedBranch = await _git.RunHubCommand($"checkout --track -b {Constants.FeatureName} {_wopConfig.GitHubUserName}/{Constants.FeatureName}"); + var wasAbleToAddTrackedBranch = await _git.RunHubCommand($"checkout --track -b {Constants.FeatureName} {_wopConfig.GitHubUserName}/{Constants.FeatureName}"); - if (wasAbleToAddTrackedBranch.ExitCode == 0) - { - await _git.RunHubCommand($"merge --strategy-option=theirs {_wopConfig.GitHubUserName}/{branchName}"); - await _git.RunHubCommand($"push {_wopConfig.GitHubUserName} {Constants.FeatureName} -u"); - } - else - { - var createdNewBranch = await _git.RunHubCommand($"checkout -b {Constants.FeatureName}"); - if (createdNewBranch.ExitCode == 0) + if (wasAbleToAddTrackedBranch.ExitCode == 0) { + await _git.RunHubCommand($"merge --strategy-option=theirs {_wopConfig.GitHubUserName}/{branchName}"); + await _git.RunHubCommand($"push {_wopConfig.GitHubUserName} {Constants.FeatureName} -u"); } else { - await _git.RunHubCommand($"checkout {Constants.FeatureName}"); - await _git.RunHubCommand($"merge --strategy-option=theirs {_wopConfig.GitHubUserName}/{branchName}"); + var createdNewBranch = await _git.RunHubCommand($"checkout -b {Constants.FeatureName}"); + if (createdNewBranch.ExitCode == 0) + { + } + else + { + await _git.RunHubCommand($"checkout {Constants.FeatureName}"); + await _git.RunHubCommand($"merge --strategy-option=theirs {_wopConfig.GitHubUserName}/{branchName}"); + } + await _git.RunHubCommand($"push {_wopConfig.GitHubUserName} {Constants.FeatureName} -u"); } - await _git.RunHubCommand($"push {_wopConfig.GitHubUserName} {Constants.FeatureName} -u"); - } - var optimizedFileResults = await GoOptimize(clonedRepo, _wopConfig); - //var optimizedFileResults = await GoOptimizeStub(clonedRepo, config); + var optimizedFileResults = await GoOptimize(clonedRepo, _wopConfig); + //var optimizedFileResults = await GoOptimizeStub(clonedRepo, config); - await _git.RunHubCommand("add ."); + await _git.RunHubCommand("add ."); - var descriptionForCommit = TemplatesHandler.GetDescriptionForCommit(); - await _git.Commit("Wop optimized this repository", descriptionForCommit); - await _git.RunHubCommand($"push"); + var descriptionForCommit = TemplatesHandler.GetDescriptionForCommit(); + await _git.Commit("Wop optimized this repository", descriptionForCommit); + await _git.RunHubCommand($"push"); - var descriptionForPullRequest = TemplatesHandler.GetDescriptionForPullRequest(); + var descriptionForPullRequest = TemplatesHandler.GetDescriptionForPullRequest(); - //Only create pull request if there were actually any successful optimizations - if (optimizedFileResults.Any(t => t.OptimizationResult == OptimizationResult.Success && t.OriginalSize > t.OptimizedSize)) - { - PullRequest obtainedPullRequest = await _gitOctoKitHandler.GetPullRequest(repositoryOwner, repositoryName); + var successfulOptimizations = optimizedFileResults.Any(t => t.OptimizationResult == OptimizationResult.Success && t.OriginalSize > t.OptimizedSize); + + var retval = new WopResult() + { + OptimizedFiles = optimizedFileResults + }; - if (obtainedPullRequest == null) + //Only create pull request if there were actually any successful optimizations + if (successfulOptimizations) { - var pr = new NewPullRequest("The Web Optimization Project has optimized your repository!", $"{_wopConfig.GitHubUserName}:{Constants.FeatureName}", branchName) + PullRequest obtainedPullRequest = await _gitOctoKitHandler.GetPullRequest(repositoryOwner, repositoryName); + + if (obtainedPullRequest == null) { - Body = descriptionForPullRequest - }; - obtainedPullRequest = await _gitOctoKitHandler.GitHubClient.PullRequest.Create(repositoryOwner, repositoryName, pr); + var pr = new NewPullRequest("The Web Optimization Project has optimized your repository!", $"{_wopConfig.GitHubUserName}:{Constants.FeatureName}", branchName) + { + Body = descriptionForPullRequest + }; + obtainedPullRequest = await _gitOctoKitHandler.GitHubClient.PullRequest.Create(repositoryOwner, repositoryName, pr); + } + Console.WriteLine($"Using PR: {obtainedPullRequest.Url}"); + + var descriptionForCommitInPr = TemplatesHandler.GetCommitDescriptionForPullRequest(clonedRepo, branchName, optimizedFileResults, DateTime.UtcNow.ToString()); + Console.WriteLine($"Creating comment on pr with length {descriptionForCommitInPr.Length}..."); + var createdComment = await _gitOctoKitHandler.GitHubClient.Issue.Comment.Create(repositoryOwner, repositoryName, obtainedPullRequest.Number, descriptionForCommitInPr); + Console.WriteLine($"Comment created: {createdComment.Url}"); + + retval.CreatedPullRequest = obtainedPullRequest; } - Console.WriteLine($"Using PR: {obtainedPullRequest.Url}"); - - var descriptionForCommitInPr = TemplatesHandler.GetCommitDescriptionForPullRequest(clonedRepo, branchName, optimizedFileResults, DateTime.UtcNow.ToString()); - Console.WriteLine($"Creating comment on pr with length {descriptionForCommitInPr.Length}..."); - var createdComment = await _gitOctoKitHandler.GitHubClient.Issue.Comment.Create(repositoryOwner, repositoryName, obtainedPullRequest.Number, descriptionForCommitInPr); - Console.WriteLine($"Comment created: {createdComment.Url}"); - } - Console.WriteLine(); - Console.WriteLine($"{repositoryOwner}/{repositoryName} is optimized :)"); - Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine($"{repositoryOwner}/{repositoryName} is optimized :)"); + Console.WriteLine(); - if (cleanupAfterwards) + return retval; + } + finally { - Console.WriteLine($"Cleaning up local files '{clonedRepo}'..."); - Directory.SetCurrentDirectory(dirOfClonedRepos); - CleanupRecursively(clonedRepo); - //Directory.Delete(clonedRepo, true); - Console.WriteLine($"Directory {clonedRepo} removed."); + if (cleanupAfterwards) + { + Console.WriteLine($"Cleaning up local files '{clonedRepo}'..."); + Directory.SetCurrentDirectory(dirOfClonedRepos); + CleanupRecursively(clonedRepo); + //Directory.Delete(clonedRepo, true); + Console.WriteLine($"Directory {clonedRepo} removed."); + } } } diff --git a/WebOptimizationProject/WopResult.cs b/WebOptimizationProject/WopResult.cs new file mode 100644 index 0000000..6e716de --- /dev/null +++ b/WebOptimizationProject/WopResult.cs @@ -0,0 +1,12 @@ +using DeveImageOptimizer.State; +using Octokit; +using System.Collections.Generic; + +namespace WebOptimizationProject +{ + public class WopResult + { + public PullRequest CreatedPullRequest { get; set; } + public IEnumerable OptimizedFiles { get; set; } + } +}