private async Task <Dictionary <string, List <long> > > GetFileOwners(SophiaDbContext dbContext, long subscriptionId, IReadOnlyList <Octokit.PullRequestFile> pullRequestFiles)
        {
            var fileOwners = new Dictionary <string, List <long> >();

            foreach (var pullRequestFile in pullRequestFiles)
            {
                fileOwners[pullRequestFile.FileName] = new List <long>();

                var file = await GetFile(dbContext, subscriptionId, pullRequestFile);

                if (file == null)
                {
                    continue;
                }

                var contributorIds = file.Contributions.Select(q => q.ContributorId);

                foreach (var contributorId in contributorIds)
                {
                    var isActive = await IsContributorActive(dbContext, subscriptionId, contributorId);

                    if (!isActive)
                    {
                        continue;
                    }

                    fileOwners[pullRequestFile.FileName].Add(contributorId);
                }
            }

            return(fileOwners);
        }
Exemplo n.º 2
0
 public PullRequestEventHandler(SophiaDbContext SophiaDbContext,
                                GitHubRepositoryPullRequestService gitHubRepositoryPullRequestService,
                                ILogger <PullRequestEventHandler> logger)
 {
     _gitHubRepositoryPullRequestService = gitHubRepositoryPullRequestService;
     _SophiaDbContext = SophiaDbContext;
     _logger          = logger;
 }
Exemplo n.º 3
0
 public ApplyPullRequestsJob(SophiaDbContext SophiaDbContext
                             , IServiceProvider serviceProvider
                             , ILogger <ApplyPullRequestsJob> logger)
 {
     _logger          = logger;
     _serviceProvider = serviceProvider;
     _SophiaDbContext = SophiaDbContext;
 }
Exemplo n.º 4
0
        public CommitAnalyzer(Subscription subscription, Octokit.GitHubClient installationClient, Dictionary <string, Contributor> contributors = null, Dictionary <string, string> fileCanonicalMapper = null, SophiaDbContext dbContext = null)
        {
            _fileCanonicalMapper = fileCanonicalMapper ?? new Dictionary <string, string>();
            _contributors        = contributors ?? new Dictionary <string, Contributor>();

            _dbContext          = dbContext;
            _subscription       = subscription;
            _installationClient = installationClient;
        }
        private async Task <int> GetTotalActiveMonth(Candidate candidate, SophiaDbContext dbContext)
        {
            var lastYear = DateTimeOffset.Now.AddYears(-1);

            return(await _dbContext.Contributions
                   .Where(q => q.ContributorId == candidate.Contributor.Id && q.DateTime >= lastYear)
                   .Select(q => q.DateTime.Value.Month)
                   .Distinct()
                   .CountAsync());
        }
Exemplo n.º 6
0
 public ScanRepositoriesJob(SophiaDbContext SophiaDbContext,
                            IOptions <GitHubOption> gitHubOptions,
                            IServiceProvider serviceProvider,
                            ILogger <ScanRepositoriesJob> logger)
 {
     _serviceProvider = serviceProvider;
     _SophiaDbContext = SophiaDbContext;
     _gitHubOptions   = gitHubOptions;
     _logger          = logger;
 }
        private async Task <File> GetFile(SophiaDbContext dbContext, long subscriptionId, Octokit.PullRequestFile pullRequestFile)
        {
            var fileHistory = await dbContext.FileHistories.Where(q => q.Path == pullRequestFile.FileName && q.SubscriptionId == subscriptionId)
                              .Include(q => q.File)
                              .ThenInclude(q => q.Contributions)
                              .ThenInclude(q => q.Contributor)
                              .OrderByDescending(q => q.ContributionId)
                              .FirstOrDefaultAsync();

            return(fileHistory?.File);
        }
Exemplo n.º 8
0
        private async Task AnalyzePullRequests(long subscriptionId, SophiaDbContext dbContext, GitHubOption gitHubOption, GitHubRepositoryPullRequestService gitHubRepositoryPullRequestService)
        {
            try
            {
                var analyzer = new PullRequestAnalyzer(dbContext, gitHubOption, gitHubRepositoryPullRequestService);
                await analyzer.Analyze(subscriptionId);

                await dbContext.SaveChangesAsync();
            }
            catch (Exception e)
            {
                _logger.LogError("ApplyPullRequestsJob: Exception {exception} in {subscriptionId}", e.ToString(), subscriptionId);
                throw;
            }
        }
Exemplo n.º 9
0
        private async Task SaveCandidates(SophiaDbContext dbContext, IEnumerable <Recommending.Candidate> candidates, Octokit.PullRequest pullRequest, Data.Models.Subscription subscription)
        {
            var dateTime = DateTimeOffset.UtcNow;

            dbContext.AddRange(candidates.Select(q => new Data.Models.Candidate()
            {
                GitHubLogin       = q.Contributor.GitHubLogin,
                PullRequestNumber = pullRequest.Number,
                Rank               = q.Rank,
                RecommenderType    = RecommenderType.Chrev,
                SubscriptionId     = subscription.Id,
                SuggestionDateTime = dateTime
            }));

            await dbContext.SaveChangesAsync();
        }
Exemplo n.º 10
0
        private async Task GetCandidates(EventContext eventContext, SophiaDbContext dbContext, int issueNumber, long repositoryId, Data.Models.Subscription subscription, int topCandidatesLength)
        {
            var installationClient = eventContext.InstallationContext.Client;
            var recommender        = new CodeReviewerRecommender(RecommenderType.Chrev, dbContext);

            var pullRequest = await installationClient.PullRequest.Get(repositoryId, issueNumber);

            var pullRequestFiles = await installationClient.PullRequest.Files(repositoryId, issueNumber);

            var candidates = await recommender.Recommend(subscription.Id, pullRequest, pullRequestFiles, topCandidatesLength);

            await SaveCandidates(dbContext, candidates, pullRequest, subscription);

            var message = GenerateMessage(candidates, pullRequestFiles);

            await installationClient.Issue.Comment.Create(repositoryId, issueNumber, message);
        }
Exemplo n.º 11
0
        private async Task GetCandidates(EventContext eventContext, SophiaDbContext dbContext, int issueNumber, long repositoryId, Data.Models.Subscription subscription, int topCandidatesLength)
        {
            var installationClient = eventContext.InstallationContext.Client;
            var pullRequest        = await installationClient.PullRequest.Get(repositoryId, issueNumber);

            var pullRequestFiles = await installationClient.PullRequest.Files(repositoryId, issueNumber);

            var fileOwners = await GetFileOwners(dbContext, subscription.Id, pullRequestFiles);

            var expertCandidates = await GetExpertCandidates(dbContext, subscription.Id, pullRequest, pullRequestFiles, topCandidatesLength);

            var learnerCandidates = await GetLearnerCandidates(dbContext, subscription.Id, pullRequest, pullRequestFiles, topCandidatesLength);

            await SaveCandidates(dbContext, expertCandidates, learnerCandidates, pullRequest, subscription);

            var message = GenerateMessage(expertCandidates, learnerCandidates, pullRequestFiles, fileOwners);

            await installationClient.Issue.Comment.Create(repositoryId, issueNumber, message);
        }
Exemplo n.º 12
0
 public Task Execute(string action, string[] parts, string authorAssociation, EventContext eventContext, SophiaDbContext dbContext)
 {
     return(Task.CompletedTask);
 }
Exemplo n.º 13
0
 public PullRequestGatheringStep(SophiaDbContext dbContext, GitHubRepositoryPullRequestService gitHubRepositoryPullRequestService, GitHubOption gitHubOption)
     : base(dbContext)
 {
     _gitHubRepositoryPullRequestService = gitHubRepositoryPullRequestService;
     _gitHubOption = gitHubOption;
 }
Exemplo n.º 14
0
 public RepositoryCloningStep(SophiaDbContext dbContext, GitHubOption gitHubOption) : base(dbContext)
 {
     _gitHubOption = gitHubOption;
 }
Exemplo n.º 15
0
        private Task <bool> IsContributorActive(SophiaDbContext dbContext, long subscriptionId, long contributorId)
        {
            var lastCheck = DateTimeOffset.Now.Subtract(TimeSpan.FromDays(90));

            return(dbContext.Contributions.AnyAsync(q => q.SubscriptionId == subscriptionId && q.ContributorId == contributorId && q.DateTime > lastCheck));
        }
Exemplo n.º 16
0
        public async Task Execute(string action, string[] parts, string authorAssociation, EventContext eventContext, SophiaDbContext dbContext)
        {
            var issueNumber  = (int)eventContext.WebHookEvent.GetPayload().issue.number;
            var repositoryId = (long)eventContext.WebHookEvent.GetPayload().repository.id;
            var subscription = await dbContext.Subscriptions.SingleOrDefaultAsync(q => q.RepositoryId == repositoryId);

            var top = 10;

            if (parts.Length == 5)
            {
                top = int.Parse(parts[4]);
            }

            if (subscription == null)
            {
                var commentResponse = await eventContext.InstallationContext.Client.Issue.Comment
                                      .Create(repositoryId, issueNumber,
                                              "You have not registered the repository. First, you need to ask Sofia to scan it before asking for suggestions.");

                return;
            }

            if (subscription.ScanningStatus != SubscriptionStatus.Completed)
            {
                var commentResponse = await eventContext.InstallationContext.Client.Issue.Comment
                                      .Create(repositoryId, issueNumber,
                                              "Sofia has not yet finished scanning the repository. You can ask for suggestion once it is done.");

                return;
            }

            try
            {
                await GetCandidates(eventContext, dbContext, issueNumber, repositoryId, subscription, top);
            }
            catch (NotFoundException e)
            {
                await eventContext.InstallationContext.Client.Issue.Comment
                .Create(repositoryId, issueNumber,
                        "It's not a pull request. Sofia suggests reviewers for pull requests.");
            }
        }
Exemplo n.º 17
0
 public CommitGatheringStep(SophiaDbContext dbContext, GitHubOption gitHubOption) : base(dbContext)
 {
     _gitHubOption = gitHubOption;
 }
 public PersistBasedSpreadingRecommender(SophiaDbContext dbContext)
 {
     _dbContext = dbContext;
 }
Exemplo n.º 19
0
        private async Task SaveSubscription(int issueNumber, long repositoryId, string owner, string repository, string repositoryUrl, string branch, long?installationId, SophiaDbContext dbContext)
        {
            var subscription = new Subscription()
            {
                Branch               = branch,
                InstallationId       = installationId.Value,
                RepositoryId         = repositoryId,
                Owner                = owner,
                Repo                 = repository,
                IssueNumber          = issueNumber,
                SubscriptionDateTime = DateTime.Now,
                ScanningStatus       = SubscriptionStatus.NotScanned,
                GitHubRepositoryUrl  = repositoryUrl
            };

            dbContext.Add(subscription);
            await dbContext.SaveChangesAsync();
        }
Exemplo n.º 20
0
 private async Task <bool> AlreadyScanned(SophiaDbContext dbContext, long repositoryId)
 {
     return(await dbContext.Subscriptions.AnyAsync(q => q.RepositoryId == repositoryId));
 }
Exemplo n.º 21
0
        public async Task Execute(string action, string[] parts, string authorAssociation, EventContext eventContext, SophiaDbContext dbContext)
        {
            var branch         = parts[3];
            var issueNumber    = (int)eventContext.WebHookEvent.GetPayload().issue.number;
            var repositoryId   = (long)eventContext.WebHookEvent.GetPayload().repository.id;
            var owenerName     = (string)eventContext.WebHookEvent.GetPayload().repository.owner.login;
            var repositoryName = (string)eventContext.WebHookEvent.GetPayload().repository.name;
            var repositoryUrl  = (string)eventContext.WebHookEvent.GetPayload().repository.html_url;

            if (await AlreadyScanned(dbContext, repositoryId))
            {
                var commentResponse = await eventContext.InstallationContext.Client.Issue.Comment
                                      .Create(repositoryId, issueNumber, "Sofia has already scanned this branch and is monitoring it.");
            }
            else
            {
                var commentResponse = await eventContext.InstallationContext.Client.Issue.Comment.Create(repositoryId, issueNumber, "Sofia just started to scan your repository. After Completion you can ask for suggestions for code reviewers!");
                await SaveSubscription(issueNumber, repositoryId, owenerName, repositoryName, repositoryUrl, branch, eventContext.WebHookEvent.GetInstallationId(), dbContext);
            }
        }
Exemplo n.º 22
0
 public CodeReviewerRecommender(RecommenderType recommenderType, SophiaDbContext dbContext)
 {
     _dbContext       = dbContext;
     _recommenderType = recommenderType;
 }
Exemplo n.º 23
0
 public IssuesEventHandler(SophiaDbContext SophiaDbContext, ILogger <IssueCommentEventHandler> logger)
 {
     _logger          = logger;
     _SophiaDbContext = SophiaDbContext;
 }
Exemplo n.º 24
0
 public PushEventHandler(SophiaDbContext SophiaDbContext, ILogger <PushEventHandler> logger)
 {
     _logger          = logger;
     _SophiaDbContext = SophiaDbContext;
 }
Exemplo n.º 25
0
 public Step(SophiaDbContext dbContext)
 {
     DbContext = dbContext;
 }
Exemplo n.º 26
0
 public PullRequestAnalyzer(SophiaDbContext dbContext, GitHubOption gitHubOption, GitHubRepositoryPullRequestService gitHubRepositoryPullRequestService)
 {
     _dbContext    = dbContext;
     _gitHubOption = gitHubOption;
     _gitHubRepositoryPullRequestService = gitHubRepositoryPullRequestService;
 }
        private async Task <(int TotalReviews, int TotalCommts)> GetCandidateEffort(Candidate candidate, SophiaDbContext dbContext)
        {
            var lastYear = DateTimeOffset.Now.AddYears(-1);

            var totalReviews = await _dbContext.Contributions.Where(q => q.ContributorId == candidate.Contributor.Id &&
                                                                    q.ContributionType == ContributionType.Review &&
                                                                    q.DateTime >= lastYear)
                               .Select(q => q.ActivityId)
                               .Distinct()
                               .CountAsync();

            var totalCommits = await _dbContext.Contributions.Where(q => q.ContributorId == candidate.Contributor.Id &&
                                                                    q.ContributionType == ContributionType.Commit &&
                                                                    q.DateTime >= lastYear)
                               .Select(q => q.ActivityId)
                               .Distinct()
                               .CountAsync();

            return(totalReviews, totalCommits);
        }
Exemplo n.º 28
0
        private async Task <IEnumerable <Recommending.Candidate> > GetExpertCandidates(SophiaDbContext dbContext, long subscriptionId, Octokit.PullRequest pullRequest, IReadOnlyList <Octokit.PullRequestFile> pullRequestFiles, int topCandidatesLength)
        {
            var recommender = new CodeReviewerRecommender(RecommenderType.Chrev, dbContext);
            var candidates  = await recommender.Recommend(subscriptionId, pullRequest, pullRequestFiles, topCandidatesLength);

            return(candidates);
        }
Exemplo n.º 29
0
 public ChrevRecommender(SophiaDbContext dbContext)
 {
     _dbContext = dbContext;
 }
Exemplo n.º 30
0
 public PullRequestAnalyzingStep(SophiaDbContext dbContext) : base(dbContext)
 {
 }