2021-11-21 14:41:48 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-07-30 07:29:10 +05:30
|
|
|
|
2021-11-21 14:41:48 +05:30
|
|
|
package issue
|
2019-07-30 07:29:10 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-12-07 21:22:36 +05:30
|
|
|
"html"
|
2021-11-16 23:48:25 +05:30
|
|
|
"net/url"
|
2020-09-04 21:07:37 +05:30
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2020-06-19 12:49:56 +05:30
|
|
|
"strings"
|
2020-09-04 21:07:37 +05:30
|
|
|
"time"
|
2019-07-30 07:29:10 +05:30
|
|
|
|
2022-04-28 17:18:48 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 15:07:59 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2022-05-11 15:39:36 +05:30
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-10-12 10:48:26 +05:30
|
|
|
"code.gitea.io/gitea/modules/container"
|
2022-08-25 08:01:57 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-12-07 21:22:36 +05:30
|
|
|
"code.gitea.io/gitea/modules/references"
|
2020-01-10 15:04:21 +05:30
|
|
|
"code.gitea.io/gitea/modules/repository"
|
2019-07-30 07:29:10 +05:30
|
|
|
)
|
|
|
|
|
2020-09-04 21:07:37 +05:30
|
|
|
const (
|
|
|
|
secondsByMinute = float64(time.Minute / time.Second) // seconds in a minute
|
|
|
|
secondsByHour = 60 * secondsByMinute // seconds in an hour
|
|
|
|
secondsByDay = 8 * secondsByHour // seconds in a day
|
|
|
|
secondsByWeek = 5 * secondsByDay // seconds in a week
|
|
|
|
secondsByMonth = 4 * secondsByWeek // seconds in a month
|
|
|
|
)
|
|
|
|
|
|
|
|
var reDuration = regexp.MustCompile(`(?i)^(?:(\d+([\.,]\d+)?)(?:mo))?(?:(\d+([\.,]\d+)?)(?:w))?(?:(\d+([\.,]\d+)?)(?:d))?(?:(\d+([\.,]\d+)?)(?:h))?(?:(\d+([\.,]\d+)?)(?:m))?$`)
|
|
|
|
|
|
|
|
// timeLogToAmount parses time log string and returns amount in seconds
|
|
|
|
func timeLogToAmount(str string) int64 {
|
|
|
|
matches := reDuration.FindAllStringSubmatch(str, -1)
|
|
|
|
if len(matches) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
match := matches[0]
|
|
|
|
|
|
|
|
var a int64
|
|
|
|
|
|
|
|
// months
|
|
|
|
if len(match[1]) > 0 {
|
|
|
|
mo, _ := strconv.ParseFloat(strings.Replace(match[1], ",", ".", 1), 64)
|
|
|
|
a += int64(mo * secondsByMonth)
|
|
|
|
}
|
|
|
|
|
|
|
|
// weeks
|
|
|
|
if len(match[3]) > 0 {
|
|
|
|
w, _ := strconv.ParseFloat(strings.Replace(match[3], ",", ".", 1), 64)
|
|
|
|
a += int64(w * secondsByWeek)
|
|
|
|
}
|
|
|
|
|
|
|
|
// days
|
|
|
|
if len(match[5]) > 0 {
|
|
|
|
d, _ := strconv.ParseFloat(strings.Replace(match[5], ",", ".", 1), 64)
|
|
|
|
a += int64(d * secondsByDay)
|
|
|
|
}
|
|
|
|
|
|
|
|
// hours
|
|
|
|
if len(match[7]) > 0 {
|
|
|
|
h, _ := strconv.ParseFloat(strings.Replace(match[7], ",", ".", 1), 64)
|
|
|
|
a += int64(h * secondsByHour)
|
|
|
|
}
|
|
|
|
|
|
|
|
// minutes
|
|
|
|
if len(match[9]) > 0 {
|
|
|
|
d, _ := strconv.ParseFloat(strings.Replace(match[9], ",", ".", 1), 64)
|
|
|
|
a += int64(d * secondsByMinute)
|
|
|
|
}
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
func issueAddTime(issue *issues_model.Issue, doer *user_model.User, time time.Time, timeLog string) error {
|
2020-09-04 21:07:37 +05:30
|
|
|
amount := timeLogToAmount(timeLog)
|
|
|
|
if amount == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
_, err := issues_model.AddTime(doer, issue, amount, time)
|
2020-09-04 21:07:37 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 14:41:48 +05:30
|
|
|
// getIssueFromRef returns the issue referenced by a ref. Returns a nil *Issue
|
|
|
|
// if the provided ref references a non-existent issue.
|
2022-06-13 15:07:59 +05:30
|
|
|
func getIssueFromRef(repo *repo_model.Repository, index int64) (*issues_model.Issue, error) {
|
|
|
|
issue, err := issues_model.GetIssueByIndex(repo.ID, index)
|
2019-12-16 03:27:34 +05:30
|
|
|
if err != nil {
|
2022-06-13 15:07:59 +05:30
|
|
|
if issues_model.IsErrIssueNotExist(err) {
|
2021-11-21 14:41:48 +05:30
|
|
|
return nil, nil
|
2019-12-07 21:22:36 +05:30
|
|
|
}
|
2021-11-21 14:41:48 +05:30
|
|
|
return nil, err
|
2019-12-07 21:22:36 +05:30
|
|
|
}
|
2021-11-21 14:41:48 +05:30
|
|
|
return issue, nil
|
2019-12-07 21:22:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateIssuesCommit checks if issues are manipulated by commit message.
|
2021-12-10 06:57:50 +05:30
|
|
|
func UpdateIssuesCommit(doer *user_model.User, repo *repo_model.Repository, commits []*repository.PushCommit, branchName string) error {
|
2019-12-07 21:22:36 +05:30
|
|
|
// Commits are appended in the reverse order.
|
|
|
|
for i := len(commits) - 1; i >= 0; i-- {
|
|
|
|
c := commits[i]
|
|
|
|
|
|
|
|
type markKey struct {
|
|
|
|
ID int64
|
|
|
|
Action references.XRefAction
|
|
|
|
}
|
|
|
|
|
2022-10-12 10:48:26 +05:30
|
|
|
refMarked := make(container.Set[markKey])
|
2021-12-10 06:57:50 +05:30
|
|
|
var refRepo *repo_model.Repository
|
2022-06-13 15:07:59 +05:30
|
|
|
var refIssue *issues_model.Issue
|
2019-12-07 21:22:36 +05:30
|
|
|
var err error
|
|
|
|
for _, ref := range references.FindAllIssueReferences(c.Message) {
|
|
|
|
|
|
|
|
// issue is from another repo
|
|
|
|
if len(ref.Owner) > 0 && len(ref.Name) > 0 {
|
2022-12-03 08:18:26 +05:30
|
|
|
refRepo, err = repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, ref.Owner, ref.Name)
|
2019-12-07 21:22:36 +05:30
|
|
|
if err != nil {
|
2022-08-25 08:01:57 +05:30
|
|
|
if repo_model.IsErrRepoNotExist(err) {
|
|
|
|
log.Warn("Repository referenced in commit but does not exist: %v", err)
|
|
|
|
} else {
|
|
|
|
log.Error("repo_model.GetRepositoryByOwnerAndName: %v", err)
|
|
|
|
}
|
2019-12-07 21:22:36 +05:30
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
refRepo = repo
|
|
|
|
}
|
|
|
|
if refIssue, err = getIssueFromRef(refRepo, ref.Index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if refIssue == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-11 15:39:36 +05:30
|
|
|
perm, err := access_model.GetUserRepoPermission(db.DefaultContext, refRepo, doer)
|
2019-12-07 21:22:36 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
key := markKey{ID: refIssue.ID, Action: ref.Action}
|
2022-10-12 10:48:26 +05:30
|
|
|
if !refMarked.Add(key) {
|
2019-12-07 21:22:36 +05:30
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: this kind of condition is all over the code, it should be consolidated in a single place
|
2020-01-20 17:30:32 +05:30
|
|
|
canclose := perm.IsAdmin() || perm.IsOwner() || perm.CanWriteIssuesOrPulls(refIssue.IsPull) || refIssue.PosterID == doer.ID
|
|
|
|
cancomment := canclose || perm.CanReadIssuesOrPulls(refIssue.IsPull)
|
2019-12-07 21:22:36 +05:30
|
|
|
|
|
|
|
// Don't proceed if the user can't comment
|
|
|
|
if !cancomment {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-16 23:48:25 +05:30
|
|
|
message := fmt.Sprintf(`<a href="%s/commit/%s">%s</a>`, html.EscapeString(repo.Link()), html.EscapeString(url.PathEscape(c.Sha1)), html.EscapeString(strings.SplitN(c.Message, "\n", 2)[0]))
|
2022-06-13 15:07:59 +05:30
|
|
|
if err = issues_model.CreateRefComment(doer, refRepo, refIssue, message, c.Sha1); err != nil {
|
2019-12-07 21:22:36 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only issues can be closed/reopened this way, and user needs the correct permissions
|
|
|
|
if refIssue.IsPull || !canclose {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only process closing/reopening keywords
|
|
|
|
if ref.Action != references.XRefActionCloses && ref.Action != references.XRefActionReopens {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !repo.CloseIssuesViaCommitInAnyBranch {
|
|
|
|
// If the issue was specified to be in a particular branch, don't allow commits in other branches to close it
|
|
|
|
if refIssue.Ref != "" {
|
|
|
|
if branchName != refIssue.Ref {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Otherwise, only process commits to the default branch
|
|
|
|
} else if branchName != repo.DefaultBranch {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2021-04-09 13:10:34 +05:30
|
|
|
close := ref.Action == references.XRefActionCloses
|
2020-09-04 21:07:37 +05:30
|
|
|
if close && len(ref.TimeLog) > 0 {
|
|
|
|
if err := issueAddTime(refIssue, doer, c.Timestamp, ref.TimeLog); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-01-11 06:50:11 +05:30
|
|
|
if close != refIssue.IsClosed {
|
2021-11-21 14:41:48 +05:30
|
|
|
refIssue.Repo = refRepo
|
|
|
|
if err := ChangeStatus(refIssue, doer, close); err != nil {
|
2020-01-11 06:50:11 +05:30
|
|
|
return err
|
|
|
|
}
|
2019-12-07 21:22:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|