public async void ShouldSaveCommentsBeforeRetrieveDiff() { await _mainWindowVm.RetrieveDiffs(); _commentsPersist.ClearReceivedCalls(); var pr = PullRequestLocator.FromUrl("https://github.com/owner/newrepo1/pull/122"); _mainWindowVm.PullRequestLocator = pr; var expectedCommentsContainer = CommentsContainer.From(_mainWindowVm.Diffs, _mainWindowVm.GeneralComments); try { await _mainWindowVm.RetrieveDiffs(); } catch { // ignored } _commentsPersist.Received(1) .Save(_pullRequestLocator, Arg.Is <CommentsContainer>(x => x.Equals(expectedCommentsContainer))) .IgnoreAsyncWarning(); }
public async void WhenRetrieveDiffs_ShouldAdjustRepoPositionInHistory_SoItWillBeTheDefaultRepo() { var container = new RepoHistoryContainer() { Owners = new List <string> { "owner1", "owner2" }, Repositories = new List <string> { "repo1", "repo2" }, Urls = new List <string> { "https://github.com/owner/repo1/pull/122", "https://github.com/owner/repo2/pull/121" }, }; var persist = Substitute.For <IRepoHistoryPersist>(); var repoHistory = new RecentRepo(); repoHistory.From(container); var prInfo = new PullRequestLocator(); prInfo.UpdateWith(container.Urls.First()); await repoHistory.Save(prInfo, persist); persist.Received(1).Save(Arg.Is <RepoHistoryContainer>(x => x.Urls.First() == prInfo.ToUrl())).IgnoreAsyncWarning(); }
private static string GetCommentsFilePath(PullRequestLocator prInfo) { var dir = FileContentPersist.GetPullRequestDir(prInfo); var commentsFile = Path.Combine(dir, "comments.xml"); return(commentsFile); }
public async Task Save(PullRequestLocator prInfo, IRepoHistoryPersist persist) { if (!prInfo.IsValid()) { return; } var newPrInfo = new PullRequestLocator() { Owner = prInfo.Owner, Repository = prInfo.Repository, PullRequestNumber = prInfo.PullRequestNumber, }; if (PullRequests.Contains(prInfo)) { PullRequests.Remove(prInfo); } PullRequests.Insert(0, newPrInfo); if (!Owners.Contains(prInfo.Owner)) { Owners.Insert(0, prInfo.Owner); } if (!Repositories.Contains(prInfo.Repository)) { Repositories.Insert(0, prInfo.Repository); } await persist.Save(ToContainer()); }
public void GivenAUrlWithNoProtocol_ShouldParseCorretly() { var locator = PullRequestLocator.FromUrl(@"github.com/EbenZhang/EZPlayer/pull/183"); Assert.That(locator.Owner, Is.EqualTo("EbenZhang")); Assert.That(locator.Repository, Is.EqualTo("EZPlayer")); Assert.That(locator.PullRequestNumber, Is.EqualTo(183)); }
public void CanProcessHttpAsWellAsHttps() { var locator = PullRequestLocator.FromUrl(@"Http://github.com/EbenZhang/EZPlayer/pull/183"); Assert.That(locator.Owner, Is.EqualTo("EbenZhang")); Assert.That(locator.Repository, Is.EqualTo("EZPlayer")); Assert.That(locator.PullRequestNumber, Is.EqualTo(183)); }
private static void RetrieveCommits(IPullRequestsClient prClient, PullRequestLocator prLocator, CommitsCombiner combiner) { var commits = prClient.Commits(prLocator.Owner, prLocator.Repository, prLocator.PullRequestNumber).Result; DispatcherHelper.RunAsync(() => { combiner.Add(commits); }); }
public void CanConvertToUrl() { var locator = new PullRequestLocator() { Owner = "EbenZhang", PullRequestNumber = 100, Repository = "PReviewer", }; var url = locator.ToUrl(); Assert.That(url, Is.EqualTo("https://github.com/EbenZhang/PReviewer/pull/100")); }
public async void ShouldUpdatePRUrlAccordingly() { var prInfo = new PullRequestLocator { Owner = "1stOwner", Repository = "1stRepo", PullRequestNumber = 1 }; var persist = Substitute.For <IRepoHistoryPersist>(); var repoHistory = new RecentRepo(); await repoHistory.Save(prInfo, persist); Assert.That(repoHistory.PullRequests.Select(r => r.ToUrl()), Contains.Item(prInfo.ToUrl())); }
public async Task Delete(PullRequestLocator pullRequestLocator) { var commentsFile = GetCommentsFilePath(pullRequestLocator); await Task.Run(() => { if (!File.Exists(commentsFile)) { return; } File.Delete(commentsFile); }); }
public void GivenAValidUrl_ShouldParseCorrectly() { var locator = PullRequestLocator.FromUrl(@"https://github.com/EbenZhang/EZPlayer/pull/183"); Assert.That(locator.Owner, Is.EqualTo("EbenZhang")); Assert.That(locator.Repository, Is.EqualTo("EZPlayer")); Assert.That(locator.PullRequestNumber, Is.EqualTo(183)); locator = PullRequestLocator.FromUrl(@"https://github.com/EbenZhang1/EZPlayer1/pull/184"); Assert.That(locator.Owner, Is.EqualTo("EbenZhang1")); Assert.That(locator.Repository, Is.EqualTo("EZPlayer1")); Assert.That(locator.PullRequestNumber, Is.EqualTo(184)); }
public void CanProcessProtocolInACaseInsensitiveManner() { var locator = PullRequestLocator.FromUrl(@"Https://github.com/EbenZhang/EZPlayer/pull/183"); Assert.That(locator.Owner, Is.EqualTo("EbenZhang")); Assert.That(locator.Repository, Is.EqualTo("EZPlayer")); Assert.That(locator.PullRequestNumber, Is.EqualTo(183)); locator = PullRequestLocator.FromUrl(@"HtTps://github.com/EbenZhang1/EZPlayer1/pull/184"); Assert.That(locator.Owner, Is.EqualTo("EbenZhang1")); Assert.That(locator.Repository, Is.EqualTo("EZPlayer1")); Assert.That(locator.PullRequestNumber, Is.EqualTo(184)); }
public async void ShouldSaveEitherOwnersOrRepositoriesChanged() { var prInfo = new PullRequestLocator { Owner = "1stOwner", Repository = "1stRepo", PullRequestNumber = 1 }; var persist = Substitute.For <IRepoHistoryPersist>(); var repoHistory = new RecentRepo(); await repoHistory.Save(prInfo, persist); Assert.That(repoHistory.Owners.Count, Is.EqualTo(1)); Assert.That(repoHistory.Repositories.Count, Is.EqualTo(1)); Assert.That(repoHistory.PullRequests.Count, Is.EqualTo(1)); persist.Received(1).Save(Arg.Is <RepoHistoryContainer>(r => r.Equals(repoHistory.ToContainer()))).IgnoreAsyncWarning(); persist.ClearReceivedCalls(); prInfo.Owner += "changed owner"; await repoHistory.Save(prInfo, persist); Assert.That(repoHistory.Owners.Count, Is.EqualTo(2)); Assert.That(repoHistory.Repositories.Count, Is.EqualTo(1)); Assert.That(repoHistory.PullRequests.Count, Is.EqualTo(2)); persist.Received(1).Save(Arg.Is <RepoHistoryContainer>(r => r.Equals(repoHistory.ToContainer()))).IgnoreAsyncWarning(); persist.ClearReceivedCalls(); prInfo.Repository += "changed repo"; await repoHistory.Save(prInfo, persist); Assert.That(repoHistory.Owners.Count, Is.EqualTo(2)); Assert.That(repoHistory.Repositories.Count, Is.EqualTo(2)); Assert.That(repoHistory.PullRequests.Count, Is.EqualTo(3)); persist.Received(1).Save(Arg.Is <RepoHistoryContainer>(r => r.Equals(repoHistory.ToContainer()))).IgnoreAsyncWarning(); persist.ClearReceivedCalls(); prInfo.PullRequestNumber++; await repoHistory.Save(prInfo, persist); Assert.That(repoHistory.Owners.Count, Is.EqualTo(2)); Assert.That(repoHistory.Repositories.Count, Is.EqualTo(2)); Assert.That(repoHistory.PullRequests.Count, Is.EqualTo(4)); persist.Received(1).Save(Arg.Is <RepoHistoryContainer>(r => r.Equals(repoHistory.ToContainer()))).IgnoreAsyncWarning(); persist.ClearReceivedCalls(); }
public async Task Save(PullRequestLocator prInfo, CommentsContainer container) { var commentsFile = GetCommentsFilePath(prInfo); await Task.Run(() => { if (File.Exists(commentsFile)) { File.Delete(commentsFile); } using (var stream = File.OpenWrite(commentsFile)) { new XmlSerializer(typeof(CommentsContainer)) .Serialize(stream, container); } }); }
public async Task <CommentsContainer> Load(PullRequestLocator prInfo) { var commentsFile = GetCommentsFilePath(prInfo); return(await Task.Run(() => { if (!File.Exists(commentsFile)) { return new CommentsContainer(); } using (var stream = File.OpenRead(commentsFile)) { var container = new XmlSerializer(typeof(CommentsContainer)).Deserialize(stream) as CommentsContainer; return container; } })); }
private async Task SaveCommentsWithoutChangeBusyStatus(PullRequestLocator request) { if (request == null || !request.IsValid()) { return; } var commentsContainer = _loadedComments; if (_loadedComments != null) { commentsContainer.AddComments(Diffs.ToList(), GeneralComments); } else { commentsContainer = CommentsContainer.From(Diffs, GeneralComments); } await _commentsPersist.Save(request, commentsContainer); }
public async Task LoadRepoHistory() { using (new ScopeGuard(() => IsProcessing = true, () => IsProcessing = false)) { var historyContainer = await _repoHistoryPersist.Load(); RecentRepoes.From(historyContainer); PullRequestLocator = RecentRepoes.PullRequests.FirstOrDefault(); if (PullRequestLocator != null) { PullRequestUrl = PullRequestLocator.ToUrl(); } else { PullRequestLocator = PullRequestLocator.Empty; } } }
private void CoercePullRequestUrlAndLocator() { _prePullRequestLocator = PullRequestLocator; if (IsUrlMode) { try { PullRequestLocator.UpdateWith(PullRequestUrl); } catch (Exception ex) { throw new UriFormatException(ex.ToString()); } } else { PullRequestUrl = PullRequestLocator.ToUrl(); } }
public async void ShouldExcludeEmptyOwners() { var prInfo = new PullRequestLocator { Owner = "", Repository = "", PullRequestNumber = 1 }; var persist = Substitute.For <IRepoHistoryPersist>(); var repoHistory = new RecentRepo(); await repoHistory.Save(prInfo, persist); Assert.That(repoHistory.Owners.Count, Is.EqualTo(0)); Assert.That(repoHistory.Repositories.Count, Is.EqualTo(0)); persist.DidNotReceiveWithAnyArgs().Save(null).IgnoreAsyncWarning(); prInfo.Owner += "changed owner"; await repoHistory.Save(prInfo, persist); Assert.That(repoHistory.Owners.Count, Is.EqualTo(0)); Assert.That(repoHistory.Repositories.Count, Is.EqualTo(0)); persist.DidNotReceiveWithAnyArgs().Save(null).IgnoreAsyncWarning(); persist.ClearReceivedCalls(); prInfo.Owner = null; prInfo.Repository += "changed repo"; await repoHistory.Save(prInfo, persist); Assert.That(repoHistory.Owners.Count, Is.EqualTo(0)); Assert.That(repoHistory.Repositories.Count, Is.EqualTo(0)); persist.DidNotReceiveWithAnyArgs().Save(null).IgnoreAsyncWarning(); persist.ClearReceivedCalls(); prInfo.Owner = "owner"; prInfo.Repository = "repo"; prInfo.PullRequestNumber = 0; await repoHistory.Save(prInfo, persist); Assert.That(repoHistory.Owners.Count, Is.EqualTo(0)); Assert.That(repoHistory.Repositories.Count, Is.EqualTo(0)); persist.DidNotReceiveWithAnyArgs().Save(null).IgnoreAsyncWarning(); persist.ClearReceivedCalls(); }
public async void ShouldInsertMostRecentRepoToTheBegining() { var container = new RepoHistoryContainer(); for (var i = 1; i <= 4; ++i) { container.Urls.Add("https://github.com/gitext/gitextensions/pull/" + i); } var repoHistory = new RecentRepo(); repoHistory.From(container); const string newPrUrl = "https://github.com/ebenzhang/ezplayer/pull/21000"; var newPrInfo = PullRequestLocator.FromUrl(newPrUrl); var persist = Substitute.For <IRepoHistoryPersist>(); await repoHistory.Save(newPrInfo, persist); repoHistory.PullRequests.First().ShouldBe(newPrInfo); repoHistory.Owners.First().ShouldBe("ebenzhang"); repoHistory.Repositories.First().ShouldBe("ezplayer"); }
public async Task <string> GetContent(PullRequestLocator pr, string filePath) { var contents = await _client.Repository.Content.GetAllContents(pr.Owner, pr.Repository, filePath); return(contents.First().Content); }
private static void ProcessMessage(Request req) { var file = req.file_path; var toolPath = req.difftool; var prUrl = req.pull_request; var token = req.token; var questionMarkPos = prUrl.IndexOf("?", StringComparison.InvariantCulture); if (questionMarkPos > 0) { prUrl = prUrl.Substring(0, questionMarkPos); } var prNum = 0; string owner = null; string repo = null; string commit = null; var matchPrCommit = Regex.Match(prUrl, @"(https|http)://.*?/(?<owner>.*?)/(?<repo>.*?)/pull/(?<pr>\d+)/commits/(?<commit>.*)"); if (matchPrCommit.Success) { prNum = int.Parse(matchPrCommit.Groups["pr"].Value); owner = matchPrCommit.Groups["owner"].Value; repo = matchPrCommit.Groups["repo"].Value; commit = matchPrCommit.Groups["commit"].Value; } var match = Regex.Match(prUrl, @"(https|http)://.*?/(?<owner>.*?)/(?<repo>.*?)/pull/(?<pr>\d+).*"); if (match.Success) { prNum = int.Parse(match.Groups["pr"].Value); owner = match.Groups["owner"].Value; repo = match.Groups["repo"].Value; } var client = new GitHubClient(new ProductHeaderValue("GitHubBuddy")) { Credentials = new Credentials(token) }; var pr = client.PullRequest.Get(owner, repo, prNum).Result; var mergeCommit = client.Repository.Commit.Get(owner, repo, $"refs/pull/{prNum}/merge").Result; var headCommit = commit ?? mergeCommit?.Sha ?? pr.Head.Sha; var parents = commit != null? client.Repository.Commit.Get(owner, repo, commit).Result.Parents.Select(x => x.Sha) : new List <string> { pr.Base.Sha }; var pullRequestLocator = new PullRequestLocator() { PullRequestNumber = prNum, Owner = owner, Repository = repo }; foreach (var baseCommit in parents) { var compareResults = client.Repository.Commit.Compare(owner, repo, baseCommit, headCommit).Result; var githubFile = compareResults.Files.FirstOrDefault(x => x.Filename == file); var fetcher = new DiffContentFetcher(pullRequestLocator, new FileContentPersist(), client, new PatchService()); var files = fetcher.FetchDiffContent(githubFile, headCommit, baseCommit).Result; try { var arguments = $"\"{files.Item1}\" \"{files.Item2}\""; if (!string.IsNullOrWhiteSpace(req.arguments)) { arguments = req.arguments.Replace("$BASE", files.Item1).Replace("$HEAD", files.Item2); } using (var proc = Process.Start(toolPath, arguments)) { proc.WaitForExit(); Write("Result", "OK"); } } catch (Exception ex) { WriteError($"Failed to launch the difftool\r\n\r\n{ex}"); return; } } }