Esempio n. 1
0
        public async Task<object> Get(string id)
        {
            using (var ctx = new ReviewContext())
            {
                var review = await ctx.Reviews.FindAsync(id);
                if (review == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                List<Revision> revisions;
                var commits = new HashSet<Commit>();
                using (var repo = new Repository(GitReviewApplication.RepositoryPath))
                {
                    revisions = repo.GetRevisions(review.RefPrefix).ToList();
                    var mergeBases = new Dictionary<Revision, HashSet<Commit>>(revisions.Count);
                    var ignored = new Dictionary<Revision, HashSet<Commit>>(revisions.Count);

                    foreach (var rev in revisions)
                    {
                        var revReachable = new HashSet<Commit>();
                        var revMergeBases = new HashSet<Commit>();
                        var revIgnored = new HashSet<Commit>();

                        var sourceCommit = (Commit)rev.Source.Target;
                        var destinationCommit = (Commit)rev.Destination.Target;
                        GetMergeDetails(repo, sourceCommit, destinationCommit, out revReachable, out revMergeBases, out revIgnored);

                        ignored[rev] = revIgnored;
                        mergeBases[rev] = revMergeBases;
                        commits.UnionWith(revReachable);
                    }

                    return new
                    {
                        Review = new
                        {
                            Id = review.Id,
                            Revisions = revisions.Select(r => review.Id + ":" + r.Id)
                        },
                        Revisions = revisions.Select(r => new
                        {
                            Id = review.Id + ":" + r.Id,
                            Source = r.Source.TargetIdentifier,
                            Destination = r.Destination.TargetIdentifier,
                            MergeBases = mergeBases[r].Select(b => b.Sha).ToList(),
                            Ignored = ignored[r].Select(i => i.Sha).ToList(),
                        }).ToList(),
                        Commits = commits.Select(c => new
                        {
                            Id = c.Sha,
                            Author = c.Author.Email,
                            AuthoredAt = c.Author.When,
                            Committer = c.Committer.Email,
                            CommittedAt = c.Committer.When,
                            Message = c.Message,
                            Parents = c.Parents.Select(p => p.Sha).ToList(),
                        }).ToList(),
                    };
                }
            }
        }