mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-12 22:10:25 +00:00
`Option[T]` currently exposes a method `Value()` which is permitted to be called on an option that has a value, and an option that doesn't have a value. This API is awkward because the behaviour if the option doesn't have a value isn't clear to the caller, and, because almost all accesses end up being `.Has()?` then `OK, use .Value()`. `Get() (bool, T)` is added as a better replacement, which both returns whether the option has a value, and the value if present. Most call-sites are rewritten to this form. `ValueOrZeroValue()` is a direct replacement that has the same behaviour that `Value()` had, but describes the behaviour if the value is missing. In addition to the current API being awkward, the core reason for this change is that `Value()` conflicts with the `Value()` function from the `driver.Valuer` interface. If this interface was implemented, it would allow `Option[T]` to be used to represent a nullable field in an xorm bean struct (requires: https://code.forgejo.org/xorm/xorm/pulls/66). _Note:_ changes are extensive in this PR, but are almost all changes are easy, mechanical transitions from `.Has()` to `.Get()`. All of this work was performed by hand. ## 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... - [ ] 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 - [ ] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [x] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. *The decision if the pull request will be shown in the release notes is up to the mergers / release team.* The content of the `release-notes/<pull request number>.md` file will serve as the basis for the release notes. If the file does not exist, the title of the pull request will be used instead. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11218 Reviewed-by: Otto <otto@codeberg.org> Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net> Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
195 lines
5.4 KiB
Go
195 lines
5.4 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package issues
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"forgejo.org/models/db"
|
|
"forgejo.org/modules/optional"
|
|
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
// MilestoneList is a list of milestones offering additional functionality
|
|
type MilestoneList []*Milestone
|
|
|
|
func (milestones MilestoneList) getMilestoneIDs() []int64 {
|
|
ids := make([]int64, 0, len(milestones))
|
|
for _, ms := range milestones {
|
|
ids = append(ids, ms.ID)
|
|
}
|
|
return ids
|
|
}
|
|
|
|
// FindMilestoneOptions contain options to get milestones
|
|
type FindMilestoneOptions struct {
|
|
db.ListOptions
|
|
RepoID int64
|
|
IsClosed optional.Option[bool]
|
|
Name string
|
|
SortType string
|
|
RepoCond builder.Cond
|
|
RepoIDs []int64
|
|
}
|
|
|
|
func (opts FindMilestoneOptions) ToConds() builder.Cond {
|
|
cond := builder.NewCond()
|
|
if opts.RepoID != 0 {
|
|
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
|
|
}
|
|
if has, value := opts.IsClosed.Get(); has {
|
|
cond = cond.And(builder.Eq{"is_closed": value})
|
|
}
|
|
if opts.RepoCond != nil && opts.RepoCond.IsValid() {
|
|
cond = cond.And(builder.In("repo_id", builder.Select("id").From("repository").Where(opts.RepoCond)))
|
|
}
|
|
if len(opts.RepoIDs) > 0 {
|
|
cond = cond.And(builder.In("repo_id", opts.RepoIDs))
|
|
}
|
|
if len(opts.Name) != 0 {
|
|
cond = cond.And(db.BuildCaseInsensitiveLike("name", opts.Name))
|
|
}
|
|
|
|
return cond
|
|
}
|
|
|
|
func (opts FindMilestoneOptions) ToOrders() string {
|
|
switch opts.SortType {
|
|
case "furthestduedate":
|
|
return "deadline_unix DESC"
|
|
case "leastcomplete":
|
|
return "completeness ASC"
|
|
case "mostcomplete":
|
|
return "completeness DESC"
|
|
case "leastissues":
|
|
return "num_issues ASC"
|
|
case "mostissues":
|
|
return "num_issues DESC"
|
|
case "id":
|
|
return "id ASC"
|
|
case "name":
|
|
return "name DESC"
|
|
default:
|
|
return "deadline_unix ASC, name ASC"
|
|
}
|
|
}
|
|
|
|
// GetMilestoneIDsByNames returns a list of milestone ids by given names.
|
|
// It doesn't filter them by repo, so it could return milestones belonging to different repos.
|
|
// It's used for filtering issues via indexer, otherwise it would be useless.
|
|
// Since it could return milestones with the same name, so the length of returned ids could be more than the length of names.
|
|
func GetMilestoneIDsByNames(ctx context.Context, names []string) ([]int64, error) {
|
|
var ids []int64
|
|
return ids, db.GetEngine(ctx).Table("milestone").
|
|
Where(db.BuildCaseInsensitiveIn("name", names)).
|
|
Cols("id").
|
|
Find(&ids)
|
|
}
|
|
|
|
// LoadTotalTrackedTimes loads for every milestone in the list the TotalTrackedTime by a batch request
|
|
func (milestones MilestoneList) LoadTotalTrackedTimes(ctx context.Context) error {
|
|
type totalTimesByMilestone struct {
|
|
MilestoneID int64
|
|
Time int64
|
|
}
|
|
if len(milestones) == 0 {
|
|
return nil
|
|
}
|
|
trackedTimes := make(map[int64]int64, len(milestones))
|
|
|
|
// Get total tracked time by milestone_id
|
|
rows, err := db.GetEngine(ctx).Table("issue").
|
|
Join("INNER", "milestone", "issue.milestone_id = milestone.id").
|
|
Join("LEFT", "tracked_time", "tracked_time.issue_id = issue.id").
|
|
Where("tracked_time.deleted = ?", false).
|
|
Select("milestone_id, sum(time) as time").
|
|
In("milestone_id", milestones.getMilestoneIDs()).
|
|
GroupBy("milestone_id").
|
|
Rows(new(totalTimesByMilestone))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
var totalTime totalTimesByMilestone
|
|
err = rows.Scan(&totalTime)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
trackedTimes[totalTime.MilestoneID] = totalTime.Time
|
|
}
|
|
|
|
for _, milestone := range milestones {
|
|
milestone.TotalTrackedTime = trackedTimes[milestone.ID]
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CountMilestonesByRepoCondAndKw map from repo conditions and the keyword of milestones' name to number of milestones matching the options`
|
|
func CountMilestonesMap(ctx context.Context, opts FindMilestoneOptions) (map[int64]int64, error) {
|
|
sess := db.GetEngine(ctx).Where(opts.ToConds())
|
|
|
|
countsSlice := make([]*struct {
|
|
RepoID int64
|
|
Count int64
|
|
}, 0, 10)
|
|
if err := sess.GroupBy("repo_id").
|
|
Select("repo_id AS repo_id, COUNT(*) AS count").
|
|
Table("milestone").
|
|
Find(&countsSlice); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
countMap := make(map[int64]int64, len(countsSlice))
|
|
for _, c := range countsSlice {
|
|
countMap[c.RepoID] = c.Count
|
|
}
|
|
return countMap, nil
|
|
}
|
|
|
|
// MilestonesStats represents milestone statistic information.
|
|
type MilestonesStats struct {
|
|
OpenCount, ClosedCount int64
|
|
}
|
|
|
|
// Total returns the total counts of milestones
|
|
func (m MilestonesStats) Total() int64 {
|
|
return m.OpenCount + m.ClosedCount
|
|
}
|
|
|
|
// GetMilestonesStatsByRepoCondAndKw returns milestone statistic information for dashboard by given repo conditions and name keyword.
|
|
func GetMilestonesStatsByRepoCondAndKw(ctx context.Context, repoCond builder.Cond, keyword string) (*MilestonesStats, error) {
|
|
var err error
|
|
stats := &MilestonesStats{}
|
|
|
|
sess := db.GetEngine(ctx).Where("is_closed = ?", false)
|
|
if len(keyword) > 0 {
|
|
sess = sess.And(builder.Like{"UPPER(name)", strings.ToUpper(keyword)})
|
|
}
|
|
if repoCond.IsValid() {
|
|
sess.And(builder.In("repo_id", builder.Select("id").From("repository").Where(repoCond)))
|
|
}
|
|
stats.OpenCount, err = sess.Count(new(Milestone))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
sess = db.GetEngine(ctx).Where("is_closed = ?", true)
|
|
if len(keyword) > 0 {
|
|
sess = sess.And(builder.Like{"UPPER(name)", strings.ToUpper(keyword)})
|
|
}
|
|
if repoCond.IsValid() {
|
|
sess.And(builder.In("repo_id", builder.Select("id").From("repository").Where(repoCond)))
|
|
}
|
|
stats.ClosedCount, err = sess.Count(new(Milestone))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return stats, nil
|
|
}
|