[F3] refactor issue insertion to preserve the creation date

issues_model.NewIssue() is convenient but will override the creation
date. Update the database instead.
This commit is contained in:
Earl Warren 2023-09-17 08:23:51 +02:00
parent f1b2f82e7e
commit 729f4be1e4
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00

View file

@ -182,17 +182,37 @@ func (o *IssueProvider) Get(ctx context.Context, user *User, project *Project, e
func (o *IssueProvider) Put(ctx context.Context, user *User, project *Project, issue, existing *Issue) *Issue {
i := issue.Issue
i.RepoID = project.GetID()
labels := make([]int64, 0, len(i.Labels))
makeLabels := func(issueID int64) []issues_model.IssueLabel {
labels := make([]issues_model.IssueLabel, 0, len(i.Labels))
for _, label := range i.Labels {
labels = append(labels, label.ID)
o.g.GetLogger().Trace("%d with label %d", issueID, label.ID)
labels = append(labels, issues_model.IssueLabel{
IssueID: issueID,
LabelID: label.ID,
})
}
return labels
}
var result *Issue
sess := db.GetEngine(ctx)
if existing == nil || existing.IsNil() {
if err := issues_model.NewIssue(&project.Repository, &i, labels, []string{}); err != nil {
idx, err := db.GetNextResourceIndex(ctx, "issue_index", project.Repository.ID)
if err != nil {
panic(fmt.Errorf("generate issue index failed: %w", err))
}
i.Index = idx
if _, err = sess.NoAutoTime().Insert(&i); err != nil {
panic(err)
}
labels := makeLabels(i.ID)
if len(labels) > 0 {
if _, err := sess.Insert(labels); err != nil {
panic(err)
}
}
result = IssueConverter(&i)
} else {
e := existing.Issue
@ -209,7 +229,7 @@ func (o *IssueProvider) Put(ctx context.Context, user *User, project *Project, i
}
if len(cols) > 0 {
if _, err := db.GetEngine(ctx).ID(existing.ID).Cols(cols...).Update(u); err != nil {
if _, err := sess.ID(existing.ID).Cols(cols...).Update(u); err != nil {
panic(err)
}
}