2019-05-07 04:12:51 +03:00
// Copyright 2019 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2019-05-07 04:12:51 +03:00
package models
2020-01-23 20:28:15 +03:00
import (
2022-01-17 21:31:58 +03:00
"context"
2021-09-19 14:49:59 +03:00
"code.gitea.io/gitea/models/db"
2022-04-08 12:11:15 +03:00
issues_model "code.gitea.io/gitea/models/issues"
2022-08-25 05:31:57 +03:00
repo_model "code.gitea.io/gitea/models/repo"
2022-10-12 08:18:26 +03:00
"code.gitea.io/gitea/modules/container"
2020-01-23 20:28:15 +03:00
"code.gitea.io/gitea/modules/structs"
)
2019-05-07 04:12:51 +03:00
2019-06-29 16:38:22 +03:00
// InsertMilestones creates milestones of repository.
2022-04-08 12:11:15 +03:00
func InsertMilestones ( ms ... * issues_model . Milestone ) ( err error ) {
2019-06-29 16:38:22 +03:00
if len ( ms ) == 0 {
return nil
}
2022-11-12 23:18:50 +03:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 18:41:00 +03:00
if err != nil {
2019-05-07 04:12:51 +03:00
return err
}
2021-11-21 18:41:00 +03:00
defer committer . Close ( )
sess := db . GetEngine ( ctx )
2019-05-07 04:12:51 +03:00
2019-06-29 16:38:22 +03:00
// to return the id, so we should not use batch insert
for _ , m := range ms {
if _ , err = sess . NoAutoTime ( ) . Insert ( m ) ; err != nil {
return err
}
}
2021-11-21 18:41:00 +03:00
if _ , err = db . Exec ( ctx , "UPDATE `repository` SET num_milestones = num_milestones + ? WHERE id = ?" , len ( ms ) , ms [ 0 ] . RepoID ) ; err != nil {
2019-05-07 04:12:51 +03:00
return err
}
2021-11-21 18:41:00 +03:00
return committer . Commit ( )
2019-05-07 04:12:51 +03:00
}
2019-06-29 16:38:22 +03:00
// InsertIssues insert issues to database
2022-06-13 12:37:59 +03:00
func InsertIssues ( issues ... * issues_model . Issue ) error {
2022-11-12 23:18:50 +03:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 18:41:00 +03:00
if err != nil {
2019-06-29 16:38:22 +03:00
return err
}
2021-11-21 18:41:00 +03:00
defer committer . Close ( )
2019-06-29 16:38:22 +03:00
for _ , issue := range issues {
2022-01-17 21:31:58 +03:00
if err := insertIssue ( ctx , issue ) ; err != nil {
2019-05-07 04:12:51 +03:00
return err
}
}
2021-11-21 18:41:00 +03:00
return committer . Commit ( )
2019-06-29 16:38:22 +03:00
}
2022-06-13 12:37:59 +03:00
func insertIssue ( ctx context . Context , issue * issues_model . Issue ) error {
2022-01-17 21:31:58 +03:00
sess := db . GetEngine ( ctx )
2019-05-07 04:12:51 +03:00
if _ , err := sess . NoAutoTime ( ) . Insert ( issue ) ; err != nil {
return err
}
2022-06-13 12:37:59 +03:00
issueLabels := make ( [ ] issues_model . IssueLabel , 0 , len ( issue . Labels ) )
2019-06-29 16:38:22 +03:00
for _ , label := range issue . Labels {
2022-06-13 12:37:59 +03:00
issueLabels = append ( issueLabels , issues_model . IssueLabel {
2019-05-07 04:12:51 +03:00
IssueID : issue . ID ,
2019-06-29 16:38:22 +03:00
LabelID : label . ID ,
2019-05-07 04:12:51 +03:00
} )
}
2020-04-17 20:42:57 +03:00
if len ( issueLabels ) > 0 {
if _ , err := sess . Insert ( issueLabels ) ; err != nil {
return err
}
2019-05-07 04:12:51 +03:00
}
2019-08-03 21:38:42 +03:00
2020-01-15 14:14:07 +03:00
for _ , reaction := range issue . Reactions {
reaction . IssueID = issue . ID
}
2020-04-17 20:42:57 +03:00
if len ( issue . Reactions ) > 0 {
if _ , err := sess . Insert ( issue . Reactions ) ; err != nil {
return err
}
2020-01-15 14:14:07 +03:00
}
2019-05-07 04:12:51 +03:00
return nil
}
2019-06-29 16:38:22 +03:00
// InsertIssueComments inserts many comments of issues.
2022-06-13 12:37:59 +03:00
func InsertIssueComments ( comments [ ] * issues_model . Comment ) error {
2019-06-29 16:38:22 +03:00
if len ( comments ) == 0 {
return nil
}
2022-10-12 08:18:26 +03:00
issueIDs := make ( container . Set [ int64 ] )
2019-06-29 16:38:22 +03:00
for _ , comment := range comments {
2022-10-12 08:18:26 +03:00
issueIDs . Add ( comment . IssueID )
2019-06-29 16:38:22 +03:00
}
2022-11-12 23:18:50 +03:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 18:41:00 +03:00
if err != nil {
2019-05-07 04:12:51 +03:00
return err
}
2021-11-21 18:41:00 +03:00
defer committer . Close ( )
2020-01-15 14:14:07 +03:00
for _ , comment := range comments {
2021-11-21 18:41:00 +03:00
if _ , err := db . GetEngine ( ctx ) . NoAutoTime ( ) . Insert ( comment ) ; err != nil {
2020-01-15 14:14:07 +03:00
return err
}
for _ , reaction := range comment . Reactions {
reaction . IssueID = comment . IssueID
reaction . CommentID = comment . ID
}
2020-04-17 20:42:57 +03:00
if len ( comment . Reactions ) > 0 {
2021-11-21 18:41:00 +03:00
if err := db . Insert ( ctx , comment . Reactions ) ; err != nil {
2020-04-17 20:42:57 +03:00
return err
}
2020-01-15 14:14:07 +03:00
}
2019-05-07 04:12:51 +03:00
}
2020-01-15 14:14:07 +03:00
2019-06-29 16:38:22 +03:00
for issueID := range issueIDs {
2022-06-13 12:37:59 +03:00
if _ , err := db . Exec ( ctx , "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ? AND `type`=?) WHERE id = ?" ,
issueID , issues_model . CommentTypeComment , issueID ) ; err != nil {
2019-06-29 16:38:22 +03:00
return err
}
2019-05-07 04:12:51 +03:00
}
2021-11-21 18:41:00 +03:00
return committer . Commit ( )
2019-05-07 04:12:51 +03:00
}
2019-06-29 16:38:22 +03:00
// InsertPullRequests inserted pull requests
2022-06-13 12:37:59 +03:00
func InsertPullRequests ( prs ... * issues_model . PullRequest ) error {
2022-11-12 23:18:50 +03:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 18:41:00 +03:00
if err != nil {
2019-05-07 04:12:51 +03:00
return err
}
2021-11-21 18:41:00 +03:00
defer committer . Close ( )
sess := db . GetEngine ( ctx )
2019-06-29 16:38:22 +03:00
for _ , pr := range prs {
2022-01-17 21:31:58 +03:00
if err := insertIssue ( ctx , pr . Issue ) ; err != nil {
2019-06-29 16:38:22 +03:00
return err
}
pr . IssueID = pr . Issue . ID
if _ , err := sess . NoAutoTime ( ) . Insert ( pr ) ; err != nil {
return err
}
2019-05-07 04:12:51 +03:00
}
2021-11-21 18:41:00 +03:00
return committer . Commit ( )
2019-05-07 04:12:51 +03:00
}
2019-06-29 16:38:22 +03:00
// InsertReleases migrates release
2022-08-25 05:31:57 +03:00
func InsertReleases ( rels ... * repo_model . Release ) error {
2022-11-12 23:18:50 +03:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 18:41:00 +03:00
if err != nil {
2019-05-07 04:12:51 +03:00
return err
}
2021-11-21 18:41:00 +03:00
defer committer . Close ( )
sess := db . GetEngine ( ctx )
2019-05-07 04:12:51 +03:00
2019-06-29 16:38:22 +03:00
for _ , rel := range rels {
2019-05-07 04:12:51 +03:00
if _ , err := sess . NoAutoTime ( ) . Insert ( rel ) ; err != nil {
return err
}
2020-04-17 20:42:57 +03:00
if len ( rel . Attachments ) > 0 {
for i := range rel . Attachments {
rel . Attachments [ i ] . ReleaseID = rel . ID
}
2019-05-07 04:12:51 +03:00
2020-04-17 20:42:57 +03:00
if _ , err := sess . NoAutoTime ( ) . Insert ( rel . Attachments ) ; err != nil {
return err
}
2019-06-29 16:38:22 +03:00
}
2019-05-07 04:12:51 +03:00
}
2021-11-21 18:41:00 +03:00
return committer . Commit ( )
2019-05-07 04:12:51 +03:00
}
2020-01-23 20:28:15 +03:00
2021-11-28 17:11:58 +03:00
// UpdateMigrationsByType updates all migrated repositories' posterid from gitServiceType to replace originalAuthorID to posterID
func UpdateMigrationsByType ( tp structs . GitServiceType , externalUserID string , userID int64 ) error {
2022-06-13 12:37:59 +03:00
if err := issues_model . UpdateIssuesMigrationsByType ( tp , externalUserID , userID ) ; err != nil {
2021-11-28 17:11:58 +03:00
return err
}
2022-06-13 12:37:59 +03:00
if err := issues_model . UpdateCommentsMigrationsByType ( tp , externalUserID , userID ) ; err != nil {
2021-11-28 17:11:58 +03:00
return err
}
2022-08-25 05:31:57 +03:00
if err := repo_model . UpdateReleasesMigrationsByType ( tp , externalUserID , userID ) ; err != nil {
2021-11-28 17:11:58 +03:00
return err
}
2022-06-13 12:37:59 +03:00
if err := issues_model . UpdateReactionsMigrationsByType ( tp , externalUserID , userID ) ; err != nil {
2021-11-28 17:11:58 +03:00
return err
}
2022-06-13 12:37:59 +03:00
return issues_model . UpdateReviewsMigrationsByType ( tp , externalUserID , userID )
2021-11-28 17:11:58 +03:00
}