mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-07-18 23:47:50 +00:00
Continuing the pattern from #9868, fixes another deadlock discovered in synthetic testing of #9785. This modifies the `milestone` table to have the `num_issues`, `num_closed_issues`, and `completeness` statistics be calculated asynchronously. An optional `updateTimestamp` field was added to the stats queue to support the conditional updating of the milestone's modification date, retaining existing functionality. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [x] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9916 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net> Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
116 lines
3 KiB
Go
116 lines
3 KiB
Go
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package issue
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"forgejo.org/models/db"
|
|
issues_model "forgejo.org/models/issues"
|
|
user_model "forgejo.org/models/user"
|
|
notify_service "forgejo.org/services/notify"
|
|
"forgejo.org/services/stats"
|
|
)
|
|
|
|
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 := stats.QueueRecalcMilestoneByIDWithDate(id, updatedUnix); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
if err := stats.QueueRecalcMilestoneByID(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 {
|
|
has, err := issues_model.HasMilestoneByRepoID(ctx, issue.RepoID, issue.MilestoneID)
|
|
if err != nil {
|
|
return fmt.Errorf("HasMilestoneByRepoID: %w", err)
|
|
}
|
|
if !has {
|
|
return errors.New("HasMilestoneByRepoID: issue doesn't exist")
|
|
}
|
|
}
|
|
|
|
if err := issues_model.UpdateIssueCols(ctx, issue, "milestone_id"); err != nil {
|
|
return err
|
|
}
|
|
|
|
if oldMilestoneID > 0 {
|
|
if err := updateMilestoneCounters(ctx, issue, oldMilestoneID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if issue.MilestoneID > 0 {
|
|
if err := updateMilestoneCounters(ctx, issue, issue.MilestoneID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if oldMilestoneID > 0 || issue.MilestoneID > 0 {
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
opts := &issues_model.CreateCommentOptions{
|
|
Type: issues_model.CommentTypeMilestone,
|
|
Doer: doer,
|
|
Repo: issue.Repo,
|
|
Issue: issue,
|
|
OldMilestoneID: oldMilestoneID,
|
|
MilestoneID: issue.MilestoneID,
|
|
}
|
|
if _, err := issues_model.CreateComment(ctx, opts); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if issue.MilestoneID == 0 {
|
|
issue.Milestone = nil
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ChangeMilestoneAssign changes assignment of milestone for issue.
|
|
func ChangeMilestoneAssign(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, oldMilestoneID int64) (err error) {
|
|
dbCtx, committer, err := db.TxContext(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer committer.Close()
|
|
|
|
if err = changeMilestoneAssign(dbCtx, doer, issue, oldMilestoneID); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = committer.Commit(); err != nil {
|
|
return fmt.Errorf("Commit: %w", err)
|
|
}
|
|
|
|
notify_service.IssueChangeMilestone(ctx, doer, issue, oldMilestoneID)
|
|
|
|
return nil
|
|
}
|