[FIX] update unresolved comment count when suggestions are applied (#2198)

unified-ui
Johannes Batzill 2024-07-10 23:05:22 +00:00 committed by Harness
parent dd143cc37e
commit 76682ad400
2 changed files with 26 additions and 2 deletions

View File

@ -66,12 +66,14 @@ func (c *Controller) Update(
func (c *Controller) sanitizeUpdateInput(in *UpdateInput) error {
parentRefAsID, err := strconv.ParseInt(in.SpaceRef, 10, 64)
if (err == nil && parentRefAsID <= 0) || (len(strings.TrimSpace(in.SpaceRef)) == 0) {
return ErrGitspaceRequiresParent
}
//nolint:revive
if err := check.Identifier(in.Identifier); err != nil {
return err
}
return nil
}

View File

@ -322,11 +322,16 @@ func (c *Controller) CommentApplySuggestions(
// update activities (use UpdateOptLock as it can have racing condition with comment migration)
resolved := ptr.Of(now.UnixMilli())
resolvedBy := &session.Principal.ID
resolvedActivities := map[int64]struct{}{}
for _, update := range activityUpdates {
_, err = c.activityStore.UpdateOptLock(ctx, update.act, func(act *types.PullReqActivity) error {
if update.resolve {
// only resolve where required (can happen in case of parallel resolution of activity)
if update.resolve && act.Resolved == nil {
act.Resolved = resolved
act.ResolvedBy = resolvedBy
resolvedActivities[act.ID] = struct{}{}
} else {
delete(resolvedActivities, act.ID)
}
if update.checksum != "" {
@ -345,6 +350,23 @@ func (c *Controller) CommentApplySuggestions(
}
}
// This is a best effort approach as in case of sqlite a transaction is likely to be blocked
// by parallel event-triggered db writes from the above commit.
// WARNING: This could cause the count to diverge (similar to create / delete).
// TODO: Use transaction once sqlite issue has been addressed.
pr, err = c.pullreqStore.UpdateOptLock(ctx, pr, func(pr *types.PullReq) error {
pr.UnresolvedCount -= len(resolvedActivities)
return nil
})
if err != nil {
return CommentApplySuggestionsOutput{}, nil,
fmt.Errorf("failed to update pull request's unresolved comment count: %w", err)
}
if err = c.sseStreamer.Publish(ctx, repo.ParentID, enum.SSETypePullRequestUpdated, pr); err != nil {
log.Ctx(ctx).Warn().Err(err).Msg("failed to publish PR changed event")
}
return CommentApplySuggestionsOutput{
CommitID: commitOut.CommitID.String(),
RuleViolations: violations,