Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="diffFile"></param>
        /// <returns>basePath and headPath</returns>
        private async Task <Tuple <string, string> > FetchDiffContent(ICommitFile diffFile)
        {
            var fetcher = new DiffContentFetcher(PullRequestLocator, _fileContentPersist,
                                                 _patchService, new FileContentRetriever(_client));

            return(await fetcher.FetchDiffContent(diffFile, HeadCommit, BaseCommit));
        }
        public async void SaveToTempDir_WhenFileContentRecieved()
        {
            var baseContent = MockFile1PersistFor("baseContent", _pullRequest.Base.Sha);

            var headContent = MockFile1PersistFor("headContent", _pullRequest.Head.Sha);

            _mainWindowVm.SelectedDiffFile = new CommitFileVm(_compareResults.File1);

            await _mainWindowVm.RetrieveDiffs();

            await _mainWindowVm.PrepareDiffContent();

            _fileContentPersist.Received(1).SaveContent(_pullRequestLocator,
                                                        DiffContentFetcher.BuildHeadFileName(_pullRequest.Head.Sha, _headFileName),
                                                        headContent.Content).IgnoreAsyncWarning();
            _fileContentPersist.Received(1).SaveContent(_pullRequestLocator,
                                                        DiffContentFetcher.BuildBaseFileName(_pullRequest.Base.Sha, _baseFileName),
                                                        baseContent.Content).IgnoreAsyncWarning();
        }
        public async void GivenARenamedFile_ShouldCallPatchToGetBaseFile()
        {
            _patchService.RevertViaPatch(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>())
            .Returns(Task.FromResult(""));

            var headContent = MockFile1PersistFor("headContent", _pullRequest.Head.Sha);

            const string basePath = "basePath";

            _fileContentPersist.GetCachedFilePath(_pullRequestLocator,
                                                  DiffContentFetcher.BuildBaseFileName(_pullRequest.Base.Sha, _baseFileName)).Returns(basePath);

            const string headPath = "headpath";

            _fileContentPersist.SaveContent(Arg.Any <PullRequestLocator>(),
                                            Arg.Any <string>(),
                                            headContent.Content).Returns(Task.FromResult(headPath));

            _fileContentPersist.ReadContent(headPath).Returns(Task.FromResult(headContent.Content));

            _compareResults.File1.Status   = GitFileStatus.Renamed;
            _mainWindowVm.SelectedDiffFile = new CommitFileVm(_compareResults.File1);

            await _mainWindowVm.RetrieveDiffs();

            await _mainWindowVm.PrepareDiffContent();

            _patchService.Received(1)
            .RevertViaPatch(headContent.Content, _compareResults.File1.Patch, basePath)
            .IgnoreAsyncWarning();

            _fileContentPersist.DidNotReceive().SaveContent(Arg.Any <PullRequestLocator>(),
                                                            _baseFileName,
                                                            Arg.Any <string>()).IgnoreAsyncWarning();

            _contentsClient.DidNotReceive().GetAllContents(_pullRequestLocator.Owner,
                                                           _pullRequestLocator.Repository, _baseFileName).IgnoreAsyncWarning();
            _diffTool.Received(1).Open(basePath, headPath);
        }
Beispiel #4
0
        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;
                }
            }
        }