Add a SetIssueUpdateDate() function in services/issue.go

That function is used by some API calls to set the NoAutoDate and
UpdatedUnix fields of an Issue if an updated_at date is provided.
This commit is contained in:
fluzz 2023-05-30 18:53:56 +02:00
parent c524d33402
commit f061caa655
2 changed files with 43 additions and 25 deletions

View file

@ -774,31 +774,10 @@ func EditIssue(ctx *context.APIContext) {
return return
} }
// In order to be set a specific update time, the DB will be updated err = issue_service.SetIssueUpdateDate(ctx, issue, form.Updated, ctx.Doer)
// with NoAutoTime. The 'noAutoTime' bool will be propagated down to the if err != nil {
// DB update calls to apply autoupdate or not. ctx.Error(http.StatusForbidden, "SetIssueUpdateDate", err)
issue.NoAutoTime = false return
if form.Updated != nil {
// Check if the poster is allowed to set an update date
perm, err := access_model.GetUserRepoPermission(ctx, issue.Repo, ctx.Doer)
if err != nil {
ctx.Status(http.StatusForbidden)
return
}
if !perm.IsAdmin() && !perm.IsOwner() {
ctx.Error(http.StatusUnauthorized, "EditIssue", "user needs to have admin or owner right")
return
}
// A simple guard against potential inconsistent calls
updatedUnix := timeutil.TimeStamp(form.Updated.Unix())
if updatedUnix < issue.CreatedUnix || updatedUnix > timeutil.TimeStampNow() {
ctx.Error(http.StatusForbidden, "EditIssue", "unallowed update date")
return
}
issue.UpdatedUnix = updatedUnix
issue.NoAutoTime = true
} }
oldTitle := issue.Title oldTitle := issue.Title

View file

@ -6,6 +6,7 @@ package issue
import ( import (
"context" "context"
"fmt" "fmt"
"time"
activities_model "code.gitea.io/gitea/models/activities" activities_model "code.gitea.io/gitea/models/activities"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
@ -18,6 +19,7 @@ import (
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/storage" "code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/timeutil"
) )
// NewIssue creates new issue with labels for repository. // NewIssue creates new issue with labels for repository.
@ -304,3 +306,40 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue) error {
return committer.Commit() return committer.Commit()
} }
// Set the UpdatedUnix date and the NoAutoTime field of an Issue if a non
// nil 'updated' time is provided
//
// In order to set a specific update time, the DB will be updated with
// NoAutoTime(). A 'NoAutoTime' boolean field in the Issue struct is used to
// propagate down to the DB update calls the will to apply autoupdate or not.
func SetIssueUpdateDate(ctx context.Context, issue *issues_model.Issue, updated *time.Time, doer *user_model.User) error {
issue.NoAutoTime = false
if updated == nil {
return nil
}
if err := issue.LoadRepo(ctx); err != nil {
return err
}
// Check if the poster is allowed to set an update date
perm, err := access_model.GetUserRepoPermission(ctx, issue.Repo, doer)
if err != nil {
return err
}
if !perm.IsAdmin() && !perm.IsOwner() {
return fmt.Errorf("user needs to have admin or owner right")
}
// A simple guard against potential inconsistent calls
updatedUnix := timeutil.TimeStamp(updated.Unix())
if updatedUnix < issue.CreatedUnix || updatedUnix > timeutil.TimeStampNow() {
return fmt.Errorf("unallowed update date")
}
issue.UpdatedUnix = updatedUnix
issue.NoAutoTime = true
return nil
}