public async Task IssueComment(DateTimeOffset eventDate, IssueCommentPayload payload)
        {
            var updater = new DataUpdater(_contextFactory, _mapper);

            // Ensure the issue that owns this comment exists locally before we add the comment.
            await updater.UpdateRepositories(eventDate, new[] { payload.Repository });

            await updater.UpdateIssues(payload.Repository.Id, eventDate, new[] { payload.Issue });

            switch (payload.Action)
            {
            case "created":
                await updater.UpdateIssueComments(payload.Repository.Id, eventDate, new[] { payload.Comment });

                break;

            case "edited":
                // GitHub doesn't send the new comment body. We have to look it up ourselves.
                var repoActor = _grainFactory.GetGrain <IRepositoryActor>(payload.Repository.Id);
                await repoActor.RefreshIssueComment(payload.Comment.Id);

                break;

            case "deleted":
                await updater.DeleteIssueComment(payload.Comment.Id, null);

                break;

            default:
                throw new NotImplementedException($"Action '{payload.Action}' is not valid for event {nameof(IssueComment)}.");
            }

            await updater.Changes.Submit(_queueClient);
        }
Exemple #2
0
        private async Task UpdateIssueCommentReactions(IGitHubActor ghc, DataUpdater updater, ISet <long> knownIssueCommentIds)
        {
            // TODO: Use knownIssueCommentIds and response date to prune deleted comments in one go.

            IDictionary <long, GitHubMetadata> commentReactionMetadata;

            using (var context = _contextFactory.CreateInstance()) {
                commentReactionMetadata = await context.IssueComments
                                          .AsNoTracking()
                                          .Where(x => x.IssueId == _issueId)
                                          .ToDictionaryAsync(x => x.Id, x => x.ReactionMetadata);
            }

            if (commentReactionMetadata.Any())
            {
                // Now, find the ones that need updating.
                var commentReactionRequests = new Dictionary <long, Task <GitHubResponse <IEnumerable <gm.Reaction> > > >();
                foreach (var reactionMetadata in commentReactionMetadata)
                {
                    if (reactionMetadata.Value.IsExpired())
                    {
                        commentReactionRequests.Add(reactionMetadata.Key, ghc.IssueCommentReactions(_repoFullName, reactionMetadata.Key, reactionMetadata.Value, RequestPriority.Interactive));
                    }
                }

                if (commentReactionRequests.Any())
                {
                    await Task.WhenAll(commentReactionRequests.Values);

                    foreach (var commentReactionsResponse in commentReactionRequests)
                    {
                        var resp = await commentReactionsResponse.Value;
                        switch (resp.Status)
                        {
                        case HttpStatusCode.NotModified:
                            break;

                        case HttpStatusCode.NotFound:
                            if (!knownIssueCommentIds.Contains(commentReactionsResponse.Key))
                            {
                                await updater.DeleteIssueComment(commentReactionsResponse.Key, resp.Date);
                            }
                            break;

                        default:
                            await updater.UpdateIssueCommentReactions(_repoId, resp.Date, commentReactionsResponse.Key, resp.Result);

                            break;
                        }
                        using (var context = _contextFactory.CreateInstance()) {
                            await context.UpdateMetadata("Comments", "ReactionMetadataJson", commentReactionsResponse.Key, resp);
                        }
                    }
                }
            }
        }