Example #1
0
 private NewChanges GetChangesToApply(RepositoryInfo targetRepo, string branch)
 {
     _listCommits = GetCommitsToMirror(targetRepo, branch);
     if (_listCommits.Count() != 0)
     {
         s_logger.Info($"Commits to mirror for {targetRepo}/{branch}");
         var result = new NewChanges(targetRepo);
         foreach (var commit in _listCommits)
         {
             string key = commit.Properties["SourceRepo"].StringValue;
             if (result.changes.ContainsKey(key))
             {
                 result.changes[key].Add(commit.RowKey);
             }
             else
             {
                 result.changes[key] = new List <string>()
                 {
                     commit.RowKey
                 }
             };
         }
         return(result);
     }
     s_logger.Info($"No new commits to mirror for {targetRepo}/{branch}");
     return(null);
 }
Example #2
0
        private async Task SubmitPRForNewChangesAsync(NewChanges newChanges, string branch, string prBranch)
        {
            using (var repository = new Repository(newChanges.TargetRepository.Path))
            {
                s_logger.Debug($"Pushing {branch} to {newChanges.TargetRepository} to update {prBranch}");
                var origin = repository.Network.Remotes["origin"];
                repository.Network.Push(origin, new[] { "refs/heads/" + branch }, new PushOptions
                {
                    CredentialsProvider = (url, usernameFromUrl, types) =>
                                          new UsernamePasswordCredentials
                    {
                        Username = newChanges.TargetRepository.Configuration.UserName,
                        Password = newChanges.TargetRepository.Configuration.Password,
                    }
                });
            }
            var targetRepo = newChanges.TargetRepository;
            var newPr      = new NewPullRequest($"Mirror changes from { targetRepo.UpstreamOwner }/{string.Join(",", newChanges.changes.Keys)}", $"{ targetRepo.Owner}:{branch}", prBranch)
            {
                Body = $"This PR contains mirrored changes from { targetRepo.UpstreamOwner }/{string.Join(",", newChanges.changes.Keys)}\n\n\n**Please REBASE this PR when merging**"
            };

            s_logger.Debug($"Creating pull request in {newChanges.TargetRepository.UpstreamOwner}");
            var pr = await Client.PullRequest.Create(targetRepo.UpstreamOwner, targetRepo.Name, newPr);

            s_logger.Debug($"Adding the commits");
            var commits = await Client.Repository.PullRequest.Commits(targetRepo.UpstreamOwner, targetRepo.Name, pr.Number);

            s_logger.Debug($"Getting Assignees");
            var additionalAssignees = await Task.WhenAll(commits.Select(c => GetAuthorAsync(targetRepo, c.Sha)).Distinct());

            try
            {
                var update = new PullRequestUpdate()
                {
                    Body = pr.Body + "\n\n cc " + string.Join(" ", additionalAssignees.Select(a => "@" + a).Distinct())
                };
                await Client.PullRequest.Update(targetRepo.UpstreamOwner, targetRepo.Name, pr.Number, update);
            }
            catch (Exception)
            {
            }
            targetRepo.PendingPRs[prBranch] = new PullRequestInfo
            {
                Number = pr.Number,
            };
            s_logger.Info($"Pull request #{pr.Number} created for {prBranch}");
            UpdateEntities(_listCommits, pr.Url.ToString());
            s_logger.Info($"Commit Entries modififed to show mirrored in the azure table");
            ConfigFile.Save(targetRepo.Configuration);
        }
Example #3
0
        private string CreateBranchForNewChanges(NewChanges newChanges, string prBranch)
        {
            string OriginalSha;
            var    targetRepo = newChanges.TargetRepository;
            var    branchName =
                $"mirror-merge-{(long)DateTime.Now.Subtract(new DateTime(2000, 1, 1, 0, 0, 0)).TotalMinutes}";

            s_logger.Info($"Creating branch {prBranch} in {targetRepo} to merge changes into {prBranch}");
            using (var repo = new Repository(targetRepo.Path))
            {
                var branch = repo.CreateBranch(branchName);
                s_logger.Info("Checking out PR branch");
                Commands.Checkout(repo, branch);
                OriginalSha = branch.Tip.ToString();
            }

            foreach (var source in newChanges.changes.Keys)
            {
                var sourceRepository = targetRepo.Configuration.Repos.Where(t => t.Name == source).First();
                using (var repo = new Repository(sourceRepository.Path))
                {
                    foreach (var change in newChanges.changes[sourceRepository.Name])
                    {
                        var commit = repo.Lookup <Commit>(change);
                        if (!IsMirrorCommit(commit.Message, targetRepo.Configuration.MirrorSignatureUser) &&
                            commit.Parents.Count() == 1
                            )
                        {
                            s_logger.Info($"Applying {change}");
                            var patch = FormatPatch(sourceRepository, change);
                            if (string.IsNullOrWhiteSpace(patch))
                            {
                                continue;
                            }
                            s_logger.Debug($"Patch:\n{patch}");
                            ApplyPatch(sourceRepository, newChanges.TargetRepository, patch, commit);
                        }
                    }
                }
            }
            using (var repo = new Repository(targetRepo.Path))
            {
                if (repo.Head.Tip.ToString() == OriginalSha)
                {
                    s_logger.Info($"No new commits To add into this branch");
                    return(null);
                }
            }

            return(branchName);
        }