From 8f22ea182e6b49e933dc6534040160dd739ff18a Mon Sep 17 00:00:00 2001 From: fluzz Date: Thu, 10 Aug 2023 19:35:32 +0200 Subject: [PATCH] Issue edition: Keep the max of the milestone and issue update dates. When editing an issue via the API, an updated_at date can be provided. If the EditIssue call changes the issue's milestone, the milestone's update date is to be changed accordingly, but only with a greater value. This ensures that a milestone's update date is the max of all issue's update dates. --- services/issue/milestone.go | 46 ++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/services/issue/milestone.go b/services/issue/milestone.go index 6de3ce2d8b..55e9cb69c1 100644 --- a/services/issue/milestone.go +++ b/services/issue/milestone.go @@ -13,6 +13,32 @@ import ( "code.gitea.io/gitea/modules/notification" ) +func updateMilestoneCounters(ctx context.Context, issue *issues_model.Issue, id int64) error { + if issue.NoAutoTime { + // We set the milestone's update date to the max of the + // milestone and issue update dates. + // Note: we can not call UpdateMilestoneCounters() if the + // milestone's update date is to be kept, because that function + // auto-updates the dates. + milestone, err := issues_model.GetMilestoneByRepoID(ctx, issue.RepoID, id) + if err != nil { + return fmt.Errorf("GetMilestoneByRepoID: %w", err) + } + updatedUnix := milestone.UpdatedUnix + if issue.UpdatedUnix > updatedUnix { + updatedUnix = issue.UpdatedUnix + } + if err := issues_model.UpdateMilestoneCountersWithDate(ctx, id, updatedUnix); err != nil { + return err + } + } else { + if err := issues_model.UpdateMilestoneCounters(ctx, id); err != nil { + return err + } + } + return nil +} + func changeMilestoneAssign(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) error { // Only check if milestone exists if we don't remove it. if issue.MilestoneID > 0 { @@ -30,26 +56,14 @@ func changeMilestoneAssign(ctx context.Context, doer *user_model.User, issue *is } if oldMilestoneID > 0 { - if issue.NoAutoTime { - if err := issues_model.UpdateMilestoneCountersWithDate(ctx, oldMilestoneID, issue.UpdatedUnix); err != nil { - return err - } - } else { - if err := issues_model.UpdateMilestoneCounters(ctx, oldMilestoneID); err != nil { - return err - } + if err := updateMilestoneCounters(ctx, issue, oldMilestoneID); err != nil { + return err } } if issue.MilestoneID > 0 { - if issue.NoAutoTime { - if err := issues_model.UpdateMilestoneCountersWithDate(ctx, issue.MilestoneID, issue.UpdatedUnix); err != nil { - return err - } - } else { - if err := issues_model.UpdateMilestoneCounters(ctx, issue.MilestoneID); err != nil { - return err - } + if err := updateMilestoneCounters(ctx, issue, issue.MilestoneID); err != nil { + return err } }