mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-29 10:57:44 +09:00 
			
		
		
		
	Use db.WithTx/WithTx2 instead of TxContext when possible (#35130)
This commit is contained in:
		| @@ -282,12 +282,7 @@ func CancelPreviousJobs(ctx context.Context, repoID int64, ref, workflowID strin | ||||
| // InsertRun inserts a run | ||||
| // The title will be cut off at 255 characters if it's longer than 255 characters. | ||||
| func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWorkflow) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		index, err := db.GetNextResourceIndex(ctx, "action_run_index", run.RepoID) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| @@ -351,8 +346,8 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func GetRunByRepoAndID(ctx context.Context, repoID, runID int64) (*ActionRun, error) { | ||||
|   | ||||
| @@ -56,18 +56,12 @@ func CreateScheduleTask(ctx context.Context, rows []*ActionSchedule) error { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	// Begin transaction | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		// Loop through each schedule row | ||||
| 		for _, row := range rows { | ||||
| 			row.Title = util.EllipsisDisplayString(row.Title, 255) | ||||
| 			// Create new schedule row | ||||
| 		if err = db.Insert(ctx, row); err != nil { | ||||
| 			if err := db.Insert(ctx, row); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
|  | ||||
| @@ -94,18 +88,12 @@ func CreateScheduleTask(ctx context.Context, rows []*ActionSchedule) error { | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	// Commit transaction | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func DeleteScheduleTaskByRepo(ctx context.Context, id int64) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if _, err := db.GetEngine(ctx).Delete(&ActionSchedule{RepoID: id}); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -114,7 +102,8 @@ func DeleteScheduleTaskByRepo(ctx context.Context, id int64) error { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func CleanRepoScheduleTasks(ctx context.Context, repo *repo_model.Repository) ([]*ActionRunJob, error) { | ||||
|   | ||||
| @@ -352,12 +352,7 @@ func UpdateTaskByState(ctx context.Context, runnerID int64, state *runnerv1.Task | ||||
| 		stepStates[v.Id] = v | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*ActionTask, error) { | ||||
| 		e := db.GetEngine(ctx) | ||||
|  | ||||
| 		task := &ActionTask{} | ||||
| @@ -419,11 +414,8 @@ func UpdateTaskByState(ctx context.Context, runnerID int64, state *runnerv1.Task | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	if err := committer.Commit(); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 		return task, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func StopTask(ctx context.Context, taskID int64, status Status) error { | ||||
|   | ||||
| @@ -73,12 +73,7 @@ func increaseTasksVersionByScope(ctx context.Context, ownerID, repoID int64) err | ||||
| } | ||||
|  | ||||
| func IncreaseTaskVersion(ctx context.Context, ownerID, repoID int64) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		// 1. increase global | ||||
| 		if err := increaseTasksVersionByScope(ctx, 0, 0); err != nil { | ||||
| 			log.Error("IncreaseTasksVersionByScope(Global): %v", err) | ||||
| @@ -101,5 +96,6 @@ func IncreaseTaskVersion(ctx context.Context, ownerID, repoID int64) error { | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|   | ||||
| @@ -70,17 +70,9 @@ func (opts FindNotificationOptions) ToOrders() string { | ||||
| // for each watcher, or updates it if already exists | ||||
| // receiverID > 0 just send to receiver, else send to all watcher | ||||
| func CreateOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, notificationAuthorID, receiverID int64) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	if err := createOrUpdateIssueNotifications(ctx, issueID, commentID, notificationAuthorID, receiverID); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		return createOrUpdateIssueNotifications(ctx, issueID, commentID, notificationAuthorID, receiverID) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, notificationAuthorID, receiverID int64) error { | ||||
|   | ||||
| @@ -228,17 +228,10 @@ func DeleteGPGKey(ctx context.Context, doer *user_model.User, id int64) (err err | ||||
| 		return fmt.Errorf("GetPublicKeyByID: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		_, err = deleteGPGKey(ctx, key.KeyID) | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	if _, err = deleteGPGKey(ctx, key.KeyID); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func FindGPGKeyWithSubKeys(ctx context.Context, keyID string) ([]*GPGKey, error) { | ||||
|   | ||||
| @@ -14,29 +14,11 @@ import ( | ||||
| 	"code.gitea.io/gitea/modules/log" | ||||
| ) | ||||
|  | ||||
| //   __________________  ________   ____  __. | ||||
| //  /  _____/\______   \/  _____/  |    |/ _|____ ___.__. | ||||
| // /   \  ___ |     ___/   \  ___  |      <_/ __ <   |  | | ||||
| // \    \_\  \|    |   \    \_\  \ |    |  \  ___/\___  | | ||||
| //  \______  /|____|    \______  / |____|__ \___  > ____| | ||||
| //         \/                  \/          \/   \/\/ | ||||
| // ____   ____           .__  _____ | ||||
| // \   \ /   /___________|__|/ ____\__.__. | ||||
| //  \   Y   // __ \_  __ \  \   __<   |  | | ||||
| //   \     /\  ___/|  | \/  ||  |  \___  | | ||||
| //    \___/  \___  >__|  |__||__|  / ____| | ||||
| //               \/                \/ | ||||
|  | ||||
| // This file provides functions relating verifying gpg keys | ||||
|  | ||||
| // VerifyGPGKey marks a GPG key as verified | ||||
| func VerifyGPGKey(ctx context.Context, ownerID int64, keyID, token, signature string) (string, error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (string, error) { | ||||
| 		key := new(GPGKey) | ||||
|  | ||||
| 		has, err := db.GetEngine(ctx).Where("owner_id = ? AND key_id = ?", ownerID, keyID).Get(key) | ||||
| @@ -100,11 +82,8 @@ func VerifyGPGKey(ctx context.Context, ownerID int64, keyID, token, signature st | ||||
| 			return "", err | ||||
| 		} | ||||
|  | ||||
| 	if err := committer.Commit(); err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
|  | ||||
| 		return key.KeyID, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // VerificationToken returns token for the user that will be valid in minutes (time) | ||||
|   | ||||
| @@ -99,12 +99,7 @@ func AddPublicKey(ctx context.Context, ownerID int64, name, content string, auth | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*PublicKey, error) { | ||||
| 		if err := checkKeyFingerprint(ctx, fingerprint); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| @@ -132,7 +127,8 @@ func AddPublicKey(ctx context.Context, ownerID int64, name, content string, auth | ||||
| 			return nil, fmt.Errorf("addKey: %w", err) | ||||
| 		} | ||||
|  | ||||
| 	return key, committer.Commit() | ||||
| 		return key, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // GetPublicKeyByID returns public key by given ID. | ||||
| @@ -288,13 +284,7 @@ func PublicKeyIsExternallyManaged(ctx context.Context, id int64) (bool, error) { | ||||
|  | ||||
| // deleteKeysMarkedForDeletion returns true if ssh keys needs update | ||||
| func deleteKeysMarkedForDeletion(ctx context.Context, keys []string) (bool, error) { | ||||
| 	// Start session | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return false, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (bool, error) { | ||||
| 		// Delete keys marked for deletion | ||||
| 		var sshKeysNeedUpdate bool | ||||
| 		for _, KeyToDelete := range keys { | ||||
| @@ -310,11 +300,8 @@ func deleteKeysMarkedForDeletion(ctx context.Context, keys []string) (bool, erro | ||||
| 			sshKeysNeedUpdate = true | ||||
| 		} | ||||
|  | ||||
| 	if err := committer.Commit(); err != nil { | ||||
| 		return false, err | ||||
| 	} | ||||
|  | ||||
| 		return sshKeysNeedUpdate, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // AddPublicKeysBySource add a users public keys. Returns true if there are changes. | ||||
|   | ||||
| @@ -125,12 +125,7 @@ func AddDeployKey(ctx context.Context, repoID int64, name, content string, readO | ||||
| 		accessMode = perm.AccessModeWrite | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*DeployKey, error) { | ||||
| 		pkey, exist, err := db.Get[PublicKey](ctx, builder.Eq{"fingerprint": fingerprint}) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| @@ -157,7 +152,8 @@ func AddDeployKey(ctx context.Context, repoID int64, name, content string, readO | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| 	return key, committer.Commit() | ||||
| 		return key, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // GetDeployKeyByID returns deploy key by given ID. | ||||
|   | ||||
| @@ -15,12 +15,7 @@ import ( | ||||
|  | ||||
| // VerifySSHKey marks a SSH key as verified | ||||
| func VerifySSHKey(ctx context.Context, ownerID int64, fingerprint, token, signature string) (string, error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (string, error) { | ||||
| 		key := new(PublicKey) | ||||
|  | ||||
| 		has, err := db.GetEngine(ctx).Where("owner_id = ? AND fingerprint = ?", ownerID, fingerprint).Get(key) | ||||
| @@ -47,9 +42,6 @@ func VerifySSHKey(ctx context.Context, ownerID int64, fingerprint, token, signat | ||||
| 			return "", err | ||||
| 		} | ||||
|  | ||||
| 	if err := committer.Commit(); err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
|  | ||||
| 		return key.Fingerprint, nil | ||||
| 	}) | ||||
| } | ||||
|   | ||||
| @@ -289,12 +289,7 @@ type UpdateOAuth2ApplicationOptions struct { | ||||
|  | ||||
| // UpdateOAuth2Application updates an oauth2 application | ||||
| func UpdateOAuth2Application(ctx context.Context, opts UpdateOAuth2ApplicationOptions) (*OAuth2Application, error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*OAuth2Application, error) { | ||||
| 		app, err := GetOAuth2ApplicationByID(ctx, opts.ID) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| @@ -317,7 +312,8 @@ func UpdateOAuth2Application(ctx context.Context, opts UpdateOAuth2ApplicationOp | ||||
| 		} | ||||
| 		app.ClientSecret = "" | ||||
|  | ||||
| 	return app, committer.Commit() | ||||
| 		return app, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func updateOAuth2Application(ctx context.Context, app *OAuth2Application) error { | ||||
| @@ -358,11 +354,7 @@ func deleteOAuth2Application(ctx context.Context, id, userid int64) error { | ||||
|  | ||||
| // DeleteOAuth2Application deletes the application with the given id and the grants and auth codes related to it. It checks if the userid was the creator of the app. | ||||
| func DeleteOAuth2Application(ctx context.Context, id, userid int64) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		app, err := GetOAuth2ApplicationByID(ctx, id) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| @@ -371,10 +363,8 @@ func DeleteOAuth2Application(ctx context.Context, id, userid int64) error { | ||||
| 		if _, builtin := builtinApps[app.ClientID]; builtin { | ||||
| 			return fmt.Errorf("failed to delete OAuth2 application: application is locked: %s", app.ClientID) | ||||
| 		} | ||||
| 	if err := deleteOAuth2Application(ctx, id, userid); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	return committer.Commit() | ||||
| 		return deleteOAuth2Application(ctx, id, userid) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| ////////////////////////////////////////////////////// | ||||
|   | ||||
| @@ -35,12 +35,7 @@ func UpdateSession(ctx context.Context, key string, data []byte) error { | ||||
|  | ||||
| // ReadSession reads the data for the provided session | ||||
| func ReadSession(ctx context.Context, key string) (*Session, error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*Session, error) { | ||||
| 		session, exist, err := db.Get[Session](ctx, builder.Eq{"`key`": key}) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| @@ -54,7 +49,8 @@ func ReadSession(ctx context.Context, key string) (*Session, error) { | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	return session, committer.Commit() | ||||
| 		return session, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // ExistSession checks if a session exists | ||||
| @@ -72,12 +68,7 @@ func DestroySession(ctx context.Context, key string) error { | ||||
|  | ||||
| // RegenerateSession regenerates a session from the old id | ||||
| func RegenerateSession(ctx context.Context, oldKey, newKey string) (*Session, error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*Session, error) { | ||||
| 		if has, err := db.Exist[Session](ctx, builder.Eq{"`key`": newKey}); err != nil { | ||||
| 			return nil, err | ||||
| 		} else if has { | ||||
| @@ -105,7 +96,8 @@ func RegenerateSession(ctx context.Context, oldKey, newKey string) (*Session, er | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| 	return s, committer.Commit() | ||||
| 		return s, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // CountSessions returns the number of sessions | ||||
|   | ||||
| @@ -470,12 +470,7 @@ func NewCommitStatus(ctx context.Context, opts NewCommitStatusOptions) error { | ||||
| 		return fmt.Errorf("NewCommitStatus[%s, %s]: no user specified", opts.Repo.FullName(), opts.SHA) | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %w", opts.Repo.ID, opts.Creator.ID, opts.SHA, err) | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		// Get the next Status Index | ||||
| 		idx, err := GetNextCommitStatusIndex(ctx, opts.Repo.ID, opts.SHA.String()) | ||||
| 		if err != nil { | ||||
| @@ -494,11 +489,12 @@ func NewCommitStatus(ctx context.Context, opts NewCommitStatusOptions) error { | ||||
| 		opts.CommitStatus.ContextHash = hashCommitStatusContext(opts.CommitStatus.Context) | ||||
|  | ||||
| 		// Insert new CommitStatus | ||||
| 	if _, err = db.GetEngine(ctx).Insert(opts.CommitStatus); err != nil { | ||||
| 		if err = db.Insert(ctx, opts.CommitStatus); err != nil { | ||||
| 			return fmt.Errorf("insert CommitStatus[%s, %s]: %w", opts.Repo.FullName(), opts.SHA, err) | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // SignCommitWithStatuses represents a commit with validation of signature and status state. | ||||
|   | ||||
| @@ -135,25 +135,18 @@ var ErrLFSObjectNotExist = db.ErrNotExist{Resource: "LFS Meta object"} | ||||
| // NewLFSMetaObject stores a given populated LFSMetaObject structure in the database | ||||
| // if it is not already present. | ||||
| func NewLFSMetaObject(ctx context.Context, repoID int64, p lfs.Pointer) (*LFSMetaObject, error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	m, exist, err := db.Get[LFSMetaObject](ctx, builder.Eq{"repository_id": repoID, "oid": p.Oid}) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} else if exist { | ||||
| 		return m, committer.Commit() | ||||
| 		return m, nil | ||||
| 	} | ||||
|  | ||||
| 	m = &LFSMetaObject{Pointer: p, RepositoryID: repoID} | ||||
| 	if err = db.Insert(ctx, m); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	return m, committer.Commit() | ||||
| 	return m, nil | ||||
| } | ||||
|  | ||||
| // GetLFSMetaObjectByOid selects a LFSMetaObject entry from database by its OID. | ||||
|   | ||||
| @@ -70,20 +70,15 @@ func (l *LFSLock) LoadOwner(ctx context.Context) error { | ||||
|  | ||||
| // CreateLFSLock creates a new lock. | ||||
| func CreateLFSLock(ctx context.Context, repo *repo_model.Repository, lock *LFSLock) (*LFSLock, error) { | ||||
| 	dbCtx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	if err := CheckLFSAccessForRepo(dbCtx, lock.OwnerID, repo, perm.AccessModeWrite); err != nil { | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*LFSLock, error) { | ||||
| 		if err := CheckLFSAccessForRepo(ctx, lock.OwnerID, repo, perm.AccessModeWrite); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| 		lock.Path = util.PathJoinRel(lock.Path) | ||||
| 		lock.RepoID = repo.ID | ||||
|  | ||||
| 	l, err := GetLFSLock(dbCtx, repo, lock.Path) | ||||
| 		l, err := GetLFSLock(ctx, repo, lock.Path) | ||||
| 		if err == nil { | ||||
| 			return l, ErrLFSLockAlreadyExist{lock.RepoID, lock.Path} | ||||
| 		} | ||||
| @@ -91,11 +86,12 @@ func CreateLFSLock(ctx context.Context, repo *repo_model.Repository, lock *LFSLo | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| 	if err := db.Insert(dbCtx, lock); err != nil { | ||||
| 		if err := db.Insert(ctx, lock); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| 	return lock, committer.Commit() | ||||
| 		return lock, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // GetLFSLock returns release by given path. | ||||
| @@ -163,18 +159,13 @@ func CountLFSLockByRepoID(ctx context.Context, repoID int64) (int64, error) { | ||||
|  | ||||
| // DeleteLFSLockByID deletes a lock by given ID. | ||||
| func DeleteLFSLockByID(ctx context.Context, id int64, repo *repo_model.Repository, u *user_model.User, force bool) (*LFSLock, error) { | ||||
| 	dbCtx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	lock, err := GetLFSLockByID(dbCtx, id) | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*LFSLock, error) { | ||||
| 		lock, err := GetLFSLockByID(ctx, id) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| 	if err := CheckLFSAccessForRepo(dbCtx, u.ID, repo, perm.AccessModeWrite); err != nil { | ||||
| 		if err := CheckLFSAccessForRepo(ctx, u.ID, repo, perm.AccessModeWrite); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| @@ -182,11 +173,12 @@ func DeleteLFSLockByID(ctx context.Context, id int64, repo *repo_model.Repositor | ||||
| 			return nil, errors.New("user doesn't own lock and force flag is not set") | ||||
| 		} | ||||
|  | ||||
| 	if _, err := db.GetEngine(dbCtx).ID(id).Delete(new(LFSLock)); err != nil { | ||||
| 		if _, err := db.GetEngine(ctx).ID(id).Delete(new(LFSLock)); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| 	return lock, committer.Commit() | ||||
| 		return lock, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // CheckLFSAccessForRepo check needed access mode base on action | ||||
|   | ||||
| @@ -766,13 +766,7 @@ func (c *Comment) CodeCommentLink(ctx context.Context) string { | ||||
|  | ||||
| // CreateComment creates comment with context | ||||
| func CreateComment(ctx context.Context, opts *CreateCommentOptions) (_ *Comment, err error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	e := db.GetEngine(ctx) | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*Comment, error) { | ||||
| 		var LabelID int64 | ||||
| 		if opts.Label != nil { | ||||
| 			LabelID = opts.Label.ID | ||||
| @@ -822,7 +816,7 @@ func CreateComment(ctx context.Context, opts *CreateCommentOptions) (_ *Comment, | ||||
| 			Invalidated:      opts.Invalidated, | ||||
| 			CommentMetaData:  commentMetaData, | ||||
| 		} | ||||
| 	if _, err = e.Insert(comment); err != nil { | ||||
| 		if err = db.Insert(ctx, comment); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| @@ -837,10 +831,8 @@ func CreateComment(ctx context.Context, opts *CreateCommentOptions) (_ *Comment, | ||||
| 		if err = comment.AddCrossReferences(ctx, opts.Doer, false); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 	if err = committer.Commit(); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 		return comment, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment *Comment) (err error) { | ||||
| @@ -1092,16 +1084,10 @@ func UpdateCommentInvalidate(ctx context.Context, c *Comment) error { | ||||
|  | ||||
| // UpdateComment updates information of comment. | ||||
| func UpdateComment(ctx context.Context, c *Comment, contentVersion int, doer *user_model.User) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	sess := db.GetEngine(ctx) | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		c.ContentVersion = contentVersion + 1 | ||||
|  | ||||
| 	affected, err := sess.ID(c.ID).AllCols().Where("content_version = ?", contentVersion).Update(c) | ||||
| 		affected, err := db.GetEngine(ctx).ID(c.ID).AllCols().Where("content_version = ?", contentVersion).Update(c) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -1111,14 +1097,8 @@ func UpdateComment(ctx context.Context, c *Comment, contentVersion int, doer *us | ||||
| 		if err := c.LoadIssue(ctx); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	if err := c.AddCrossReferences(ctx, doer, true); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	if err := committer.Commit(); err != nil { | ||||
| 		return fmt.Errorf("Commit: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| 		return c.AddCrossReferences(ctx, doer, true) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // DeleteComment deletes the comment | ||||
| @@ -1277,11 +1257,7 @@ func InsertIssueComments(ctx context.Context, comments []*Comment) error { | ||||
| 		return comment.IssueID, true | ||||
| 	}) | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		for _, comment := range comments { | ||||
| 			if _, err := db.GetEngine(ctx).NoAutoTime().Insert(comment); err != nil { | ||||
| 				return err | ||||
| @@ -1303,5 +1279,6 @@ func InsertIssueComments(ctx context.Context, comments []*Comment) error { | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|   | ||||
| @@ -128,12 +128,7 @@ const ( | ||||
|  | ||||
| // CreateIssueDependency creates a new dependency for an issue | ||||
| func CreateIssueDependency(ctx context.Context, user *user_model.User, issue, dep *Issue) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		// Check if it already exists | ||||
| 		exists, err := issueDepExists(ctx, issue.ID, dep.ID) | ||||
| 		if err != nil { | ||||
| @@ -160,21 +155,13 @@ func CreateIssueDependency(ctx context.Context, user *user_model.User, issue, de | ||||
| 		} | ||||
|  | ||||
| 		// Add comment referencing the new dependency | ||||
| 	if err = createIssueDependencyComment(ctx, user, issue, dep, true); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return createIssueDependencyComment(ctx, user, issue, dep, true) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // RemoveIssueDependency removes a dependency from an issue | ||||
| func RemoveIssueDependency(ctx context.Context, user *user_model.User, issue, dep *Issue, depType DependencyType) (err error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		var issueDepToDelete IssueDependency | ||||
|  | ||||
| 		switch depType { | ||||
| @@ -197,10 +184,8 @@ func RemoveIssueDependency(ctx context.Context, user *user_model.User, issue, de | ||||
| 		} | ||||
|  | ||||
| 		// Add comment referencing the removed dependency | ||||
| 	if err = createIssueDependencyComment(ctx, user, issue, dep, false); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	return committer.Commit() | ||||
| 		return createIssueDependencyComment(ctx, user, issue, dep, false) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // Check if the dependency already exists | ||||
|   | ||||
| @@ -755,18 +755,14 @@ func (issue *Issue) HasOriginalAuthor() bool { | ||||
|  | ||||
| // InsertIssues insert issues to database | ||||
| func InsertIssues(ctx context.Context, issues ...*Issue) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		for _, issue := range issues { | ||||
| 			if err := insertIssue(ctx, issue); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func insertIssue(ctx context.Context, issue *Issue) error { | ||||
|   | ||||
| @@ -12,20 +12,12 @@ import ( | ||||
| // RecalculateIssueIndexForRepo create issue_index for repo if not exist and | ||||
| // update it based on highest index of existing issues assigned to a repo | ||||
| func RecalculateIssueIndexForRepo(ctx context.Context, repoID int64) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		var maxIndex int64 | ||||
| 	if _, err = db.GetEngine(ctx).Select(" MAX(`index`)").Table("issue").Where("repo_id=?", repoID).Get(&maxIndex); err != nil { | ||||
| 		if _, err := db.GetEngine(ctx).Select(" MAX(`index`)").Table("issue").Where("repo_id=?", repoID).Get(&maxIndex); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 	if err = db.SyncMaxResourceIndex(ctx, "issue_index", repoID, maxIndex); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return db.SyncMaxResourceIndex(ctx, "issue_index", repoID, maxIndex) | ||||
| 	}) | ||||
| } | ||||
|   | ||||
| @@ -88,12 +88,7 @@ func NewIssueLabel(ctx context.Context, issue *Issue, label *Label, doer *user_m | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if err = issue.LoadRepo(ctx); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -113,11 +108,8 @@ func NewIssueLabel(ctx context.Context, issue *Issue, label *Label, doer *user_m | ||||
|  | ||||
| 		issue.isLabelsLoaded = false | ||||
| 		issue.Labels = nil | ||||
| 	if err = issue.LoadLabels(ctx); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return issue.LoadLabels(ctx) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // newIssueLabels add labels to an issue. It will check if the labels are valid for the issue | ||||
| @@ -151,12 +143,7 @@ func newIssueLabels(ctx context.Context, issue *Issue, labels []*Label, doer *us | ||||
|  | ||||
| // NewIssueLabels creates a list of issue-label relations. | ||||
| func NewIssueLabels(ctx context.Context, issue *Issue, labels []*Label, doer *user_model.User) (err error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if err = newIssueLabels(ctx, issue, labels, doer); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -164,11 +151,8 @@ func NewIssueLabels(ctx context.Context, issue *Issue, labels []*Label, doer *us | ||||
| 		// reload all labels | ||||
| 		issue.isLabelsLoaded = false | ||||
| 		issue.Labels = nil | ||||
| 	if err = issue.LoadLabels(ctx); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return issue.LoadLabels(ctx) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func deleteIssueLabel(ctx context.Context, issue *Issue, label *Label, doer *user_model.User) (err error) { | ||||
| @@ -365,12 +349,7 @@ func clearIssueLabels(ctx context.Context, issue *Issue, doer *user_model.User) | ||||
| // ClearIssueLabels removes all issue labels as the given user. | ||||
| // Triggers appropriate WebHooks, if any. | ||||
| func ClearIssueLabels(ctx context.Context, issue *Issue, doer *user_model.User) (err error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if err := issue.LoadRepo(ctx); err != nil { | ||||
| 			return err | ||||
| 		} else if err = issue.LoadPullRequest(ctx); err != nil { | ||||
| @@ -385,15 +364,8 @@ func ClearIssueLabels(ctx context.Context, issue *Issue, doer *user_model.User) | ||||
| 			return ErrRepoLabelNotExist{} | ||||
| 		} | ||||
|  | ||||
| 	if err = clearIssueLabels(ctx, issue, doer); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if err = committer.Commit(); err != nil { | ||||
| 		return fmt.Errorf("Commit: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| 		return clearIssueLabels(ctx, issue, doer) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| type labelSorter []*Label | ||||
| @@ -438,12 +410,7 @@ func RemoveDuplicateExclusiveLabels(labels []*Label) []*Label { | ||||
| // ReplaceIssueLabels removes all current labels and add new labels to the issue. | ||||
| // Triggers appropriate WebHooks, if any. | ||||
| func ReplaceIssueLabels(ctx context.Context, issue *Issue, labels []*Label, doer *user_model.User) (err error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if err = issue.LoadRepo(ctx); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -498,9 +465,6 @@ func ReplaceIssueLabels(ctx context.Context, issue *Issue, labels []*Label, doer | ||||
| 		} | ||||
|  | ||||
| 		issue.Labels = nil | ||||
| 	if err = issue.LoadLabels(ctx); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return issue.LoadLabels(ctx) | ||||
| 	}) | ||||
| } | ||||
|   | ||||
| @@ -47,12 +47,7 @@ func updateIssueLock(ctx context.Context, opts *IssueLockOptions, lock bool) err | ||||
| 		commentType = CommentTypeUnlock | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if err := UpdateIssueCols(ctx, opts.Issue, "is_locked"); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -64,9 +59,7 @@ func updateIssueLock(ctx context.Context, opts *IssueLockOptions, lock bool) err | ||||
| 			Type:    commentType, | ||||
| 			Content: opts.Reason, | ||||
| 		} | ||||
| 	if _, err := CreateComment(ctx, opt); err != nil { | ||||
| 		_, err := CreateComment(ctx, opt) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 	}) | ||||
| } | ||||
|   | ||||
| @@ -167,20 +167,9 @@ func CloseIssue(ctx context.Context, issue *Issue, doer *user_model.User) (*Comm | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	comment, err := SetIssueAsClosed(ctx, issue, doer, false) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	if err := committer.Commit(); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	return comment, nil | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*Comment, error) { | ||||
| 		return SetIssueAsClosed(ctx, issue, doer, false) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // ReopenIssue changes issue status to open. | ||||
| @@ -192,30 +181,14 @@ func ReopenIssue(ctx context.Context, issue *Issue, doer *user_model.User) (*Com | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	comment, err := setIssueAsReopen(ctx, issue, doer) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	if err := committer.Commit(); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	return comment, nil | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*Comment, error) { | ||||
| 		return setIssueAsReopen(ctx, issue, doer) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // ChangeIssueTitle changes the title of this issue, as the given user. | ||||
| func ChangeIssueTitle(ctx context.Context, issue *Issue, doer *user_model.User, oldTitle string) (err error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		issue.Title = util.EllipsisDisplayString(issue.Title, 255) | ||||
| 		if err = UpdateIssueCols(ctx, issue, "name"); err != nil { | ||||
| 			return fmt.Errorf("updateIssueCols: %w", err) | ||||
| @@ -236,21 +209,13 @@ func ChangeIssueTitle(ctx context.Context, issue *Issue, doer *user_model.User, | ||||
| 		if _, err = CreateComment(ctx, opts); err != nil { | ||||
| 			return fmt.Errorf("createComment: %w", err) | ||||
| 		} | ||||
| 	if err = issue.AddCrossReferences(ctx, doer, true); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return issue.AddCrossReferences(ctx, doer, true) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // ChangeIssueRef changes the branch of this issue, as the given user. | ||||
| func ChangeIssueRef(ctx context.Context, issue *Issue, doer *user_model.User, oldRef string) (err error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if err = UpdateIssueCols(ctx, issue, "ref"); err != nil { | ||||
| 			return fmt.Errorf("updateIssueCols: %w", err) | ||||
| 		} | ||||
| @@ -272,8 +237,8 @@ func ChangeIssueRef(ctx context.Context, issue *Issue, doer *user_model.User, ol | ||||
| 		if _, err = CreateComment(ctx, opts); err != nil { | ||||
| 			return fmt.Errorf("createComment: %w", err) | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // AddDeletePRBranchComment adds delete branch comment for pull request issue | ||||
| @@ -295,11 +260,7 @@ func AddDeletePRBranchComment(ctx context.Context, doer *user_model.User, repo * | ||||
|  | ||||
| // UpdateIssueAttachments update attachments by UUIDs for the issue | ||||
| func UpdateIssueAttachments(ctx context.Context, issueID int64, uuids []string) (err error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, uuids) | ||||
| 		if err != nil { | ||||
| 			return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %w", uuids, err) | ||||
| @@ -310,17 +271,13 @@ func UpdateIssueAttachments(ctx context.Context, issueID int64, uuids []string) | ||||
| 				return fmt.Errorf("update attachment [id: %d]: %w", attachments[i].ID, err) | ||||
| 			} | ||||
| 		} | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // ChangeIssueContent changes issue content, as the given user. | ||||
| func ChangeIssueContent(ctx context.Context, issue *Issue, doer *user_model.User, content string, contentVersion int) (err error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		hasContentHistory, err := HasIssueContentHistory(ctx, issue.ID, 0) | ||||
| 		if err != nil { | ||||
| 			return fmt.Errorf("HasIssueContentHistory: %w", err) | ||||
| @@ -351,8 +308,8 @@ func ChangeIssueContent(ctx context.Context, issue *Issue, doer *user_model.User | ||||
| 		if err = issue.AddCrossReferences(ctx, doer, true); err != nil { | ||||
| 			return fmt.Errorf("addCrossReferences: %w", err) | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // NewIssueOptions represents the options of a new issue. | ||||
| @@ -512,12 +469,8 @@ func UpdateIssueDeadline(ctx context.Context, issue *Issue, deadlineUnix timeuti | ||||
| 	if issue.DeadlineUnix == deadlineUnix { | ||||
| 		return nil | ||||
| 	} | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		// Update the deadline | ||||
| 		if err = UpdateIssueCols(ctx, &Issue{ID: issue.ID, DeadlineUnix: deadlineUnix}, "deadline_unix"); err != nil { | ||||
| 			return err | ||||
| @@ -527,8 +480,8 @@ func UpdateIssueDeadline(ctx context.Context, issue *Issue, deadlineUnix timeuti | ||||
| 		if _, err = createDeadlineComment(ctx, doer, issue, deadlineUnix); err != nil { | ||||
| 			return fmt.Errorf("createRemovedDueDateComment: %w", err) | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // FindAndUpdateIssueMentions finds users mentioned in the given content string, and saves them in the database. | ||||
|   | ||||
| @@ -209,12 +209,7 @@ func NewLabel(ctx context.Context, l *Label) error { | ||||
|  | ||||
| // NewLabels creates new labels | ||||
| func NewLabels(ctx context.Context, labels ...*Label) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		for _, l := range labels { | ||||
| 			color, err := label.NormalizeColor(l.Color) | ||||
| 			if err != nil { | ||||
| @@ -226,7 +221,8 @@ func NewLabels(ctx context.Context, labels ...*Label) error { | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // UpdateLabel updates label information. | ||||
| @@ -250,14 +246,7 @@ func DeleteLabel(ctx context.Context, id, labelID int64) error { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	sess := db.GetEngine(ctx) | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if l.BelongsToOrg() && l.OrgID != id { | ||||
| 			return nil | ||||
| 		} | ||||
| @@ -267,18 +256,16 @@ func DeleteLabel(ctx context.Context, id, labelID int64) error { | ||||
|  | ||||
| 		if _, err = db.DeleteByID[Label](ctx, labelID); err != nil { | ||||
| 			return err | ||||
| 	} else if _, err = sess. | ||||
| 		} else if _, err = db.GetEngine(ctx). | ||||
| 			Where("label_id = ?", labelID). | ||||
| 			Delete(new(IssueLabel)); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 		// delete comments about now deleted label_id | ||||
| 	if _, err = sess.Where("label_id = ?", labelID).Cols("label_id").Delete(&Comment{}); err != nil { | ||||
| 		_, err = db.GetEngine(ctx).Where("label_id = ?", labelID).Cols("label_id").Delete(&Comment{}) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // GetLabelByID returns a label by given ID. | ||||
|   | ||||
| @@ -105,22 +105,16 @@ func (m *Milestone) State() api.StateType { | ||||
|  | ||||
| // NewMilestone creates new milestone of repository. | ||||
| func NewMilestone(ctx context.Context, m *Milestone) (err error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		m.Name = strings.TrimSpace(m.Name) | ||||
|  | ||||
| 		if err = db.Insert(ctx, m); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 	if _, err = db.Exec(ctx, "UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?", m.RepoID); err != nil { | ||||
| 		_, err = db.Exec(ctx, "UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?", m.RepoID) | ||||
| 		return err | ||||
| 	} | ||||
| 	return committer.Commit() | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // HasMilestoneByRepoID returns if the milestone exists in the repository. | ||||
| @@ -155,12 +149,7 @@ func GetMilestoneByRepoIDANDName(ctx context.Context, repoID int64, name string) | ||||
|  | ||||
| // UpdateMilestone updates information of given milestone. | ||||
| func UpdateMilestone(ctx context.Context, m *Milestone, oldIsClosed bool) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if m.IsClosed && !oldIsClosed { | ||||
| 			m.ClosedDateUnix = timeutil.TimeStampNow() | ||||
| 		} | ||||
| @@ -175,8 +164,8 @@ func UpdateMilestone(ctx context.Context, m *Milestone, oldIsClosed bool) error | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func updateMilestone(ctx context.Context, m *Milestone) error { | ||||
| @@ -213,12 +202,7 @@ func UpdateMilestoneCounters(ctx context.Context, id int64) error { | ||||
|  | ||||
| // ChangeMilestoneStatusByRepoIDAndID changes a milestone open/closed status if the milestone ID is in the repo. | ||||
| func ChangeMilestoneStatusByRepoIDAndID(ctx context.Context, repoID, milestoneID int64, isClosed bool) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		m := &Milestone{ | ||||
| 			ID:     milestoneID, | ||||
| 			RepoID: repoID, | ||||
| @@ -231,26 +215,15 @@ func ChangeMilestoneStatusByRepoIDAndID(ctx context.Context, repoID, milestoneID | ||||
| 			return ErrMilestoneNotExist{ID: milestoneID, RepoID: repoID} | ||||
| 		} | ||||
|  | ||||
| 	if err := changeMilestoneStatus(ctx, m, isClosed); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return changeMilestoneStatus(ctx, m, isClosed) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // ChangeMilestoneStatus changes the milestone open/closed status. | ||||
| func ChangeMilestoneStatus(ctx context.Context, m *Milestone, isClosed bool) (err error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	if err := changeMilestoneStatus(ctx, m, isClosed); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		return changeMilestoneStatus(ctx, m, isClosed) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func changeMilestoneStatus(ctx context.Context, m *Milestone, isClosed bool) error { | ||||
| @@ -284,12 +257,7 @@ func DeleteMilestoneByRepoID(ctx context.Context, repoID, id int64) error { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if _, err = db.DeleteByID[Milestone](ctx, m.ID); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -314,10 +282,9 @@ func DeleteMilestoneByRepoID(ctx context.Context, repoID, id int64) error { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 	if _, err = db.Exec(ctx, "UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil { | ||||
| 		_, err = db.Exec(ctx, "UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?", m.ID) | ||||
| 		return err | ||||
| 	} | ||||
| 	return committer.Commit() | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func updateRepoMilestoneNum(ctx context.Context, repoID int64) error { | ||||
| @@ -360,22 +327,15 @@ func InsertMilestones(ctx context.Context, ms ...*Milestone) (err error) { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	sess := db.GetEngine(ctx) | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		// to return the id, so we should not use batch insert | ||||
| 		for _, m := range ms { | ||||
| 		if _, err = sess.NoAutoTime().Insert(m); err != nil { | ||||
| 			if _, err = db.GetEngine(ctx).NoAutoTime().Insert(m); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	if _, err = db.Exec(ctx, "UPDATE `repository` SET num_milestones = num_milestones + ? WHERE id = ?", len(ms), ms[0].RepoID); err != nil { | ||||
| 		_, err = db.Exec(ctx, "UPDATE `repository` SET num_milestones = num_milestones + ? WHERE id = ?", len(ms), ms[0].RepoID) | ||||
| 		return err | ||||
| 	} | ||||
| 	return committer.Commit() | ||||
| 	}) | ||||
| } | ||||
|   | ||||
| @@ -364,17 +364,10 @@ func (pr *PullRequest) GetApprovers(ctx context.Context) string { | ||||
|  | ||||
| func (pr *PullRequest) getReviewedByLines(ctx context.Context, writer io.Writer) error { | ||||
| 	maxReviewers := setting.Repository.PullRequest.DefaultMergeMessageMaxApprovers | ||||
|  | ||||
| 	if maxReviewers == 0 { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	// Note: This doesn't page as we only expect a very limited number of reviews | ||||
| 	reviews, err := FindLatestReviews(ctx, FindReviewOptions{ | ||||
| 		Types:        []ReviewType{ReviewTypeApprove}, | ||||
| @@ -410,7 +403,7 @@ func (pr *PullRequest) getReviewedByLines(ctx context.Context, writer io.Writer) | ||||
| 		} | ||||
| 		reviewersWritten++ | ||||
| 	} | ||||
| 	return committer.Commit() | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // GetGitHeadRefName returns git ref for hidden pull request branch | ||||
| @@ -464,12 +457,7 @@ func (pr *PullRequest) IsFromFork() bool { | ||||
|  | ||||
| // NewPullRequest creates new pull request with labels for repository. | ||||
| func NewPullRequest(ctx context.Context, repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		idx, err := db.GetNextResourceIndex(ctx, "issue_index", repo.ID) | ||||
| 		if err != nil { | ||||
| 			return fmt.Errorf("generate pull request index failed: %w", err) | ||||
| @@ -497,12 +485,8 @@ func NewPullRequest(ctx context.Context, repo *repo_model.Repository, issue *Iss | ||||
| 		if err = db.Insert(ctx, pr); err != nil { | ||||
| 			return fmt.Errorf("insert pull repo: %w", err) | ||||
| 		} | ||||
|  | ||||
| 	if err = committer.Commit(); err != nil { | ||||
| 		return fmt.Errorf("Commit: %w", err) | ||||
| 	} | ||||
|  | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // ErrUserMustCollaborator represents an error that the user must be a collaborator to a given repo. | ||||
| @@ -977,22 +961,18 @@ func TokenizeCodeOwnersLine(line string) []string { | ||||
|  | ||||
| // InsertPullRequests inserted pull requests | ||||
| func InsertPullRequests(ctx context.Context, prs ...*PullRequest) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	sess := db.GetEngine(ctx) | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		for _, pr := range prs { | ||||
| 			if err := insertIssue(ctx, pr.Issue); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			pr.IssueID = pr.Issue.ID | ||||
| 		if _, err := sess.NoAutoTime().Insert(pr); err != nil { | ||||
| 			if _, err := db.GetEngine(ctx).NoAutoTime().Insert(pr); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // GetPullRequestByMergedCommit returns a merged pull request by the given commit | ||||
|   | ||||
| @@ -224,21 +224,9 @@ func CreateReaction(ctx context.Context, opts *ReactionOptions) (*Reaction, erro | ||||
| 		return nil, ErrForbiddenIssueReaction{opts.Type} | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	reaction, err := createReaction(ctx, opts) | ||||
| 	if err != nil { | ||||
| 		return reaction, err | ||||
| 	} | ||||
|  | ||||
| 	if err := committer.Commit(); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	return reaction, nil | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*Reaction, error) { | ||||
| 		return createReaction(ctx, opts) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // DeleteReaction deletes reaction for issue or comment. | ||||
|   | ||||
| @@ -334,11 +334,7 @@ func IsOfficialReviewerTeam(ctx context.Context, issue *Issue, team *organizatio | ||||
|  | ||||
| // CreateReview creates a new review based on opts | ||||
| func CreateReview(ctx context.Context, opts CreateReviewOptions) (*Review, error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*Review, error) { | ||||
| 		sess := db.GetEngine(ctx) | ||||
|  | ||||
| 		review := &Review{ | ||||
| @@ -381,7 +377,8 @@ func CreateReview(ctx context.Context, opts CreateReviewOptions) (*Review, error | ||||
| 		if _, err := sess.Insert(review); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 	return review, committer.Commit() | ||||
| 		return review, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // GetCurrentReview returns the current pending review of reviewer for given issue | ||||
| @@ -605,11 +602,7 @@ func DismissReview(ctx context.Context, review *Review, isDismiss bool) (err err | ||||
|  | ||||
| // InsertReviews inserts review and review comments | ||||
| func InsertReviews(ctx context.Context, reviews []*Review) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		sess := db.GetEngine(ctx) | ||||
|  | ||||
| 		for _, review := range reviews { | ||||
| @@ -645,17 +638,13 @@ func InsertReviews(ctx context.Context, reviews []*Review) error { | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // AddReviewRequest add a review request from one reviewer | ||||
| func AddReviewRequest(ctx context.Context, issue *Issue, reviewer, doer *user_model.User) (*Comment, error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*Comment, error) { | ||||
| 		sess := db.GetEngine(ctx) | ||||
|  | ||||
| 		review, err := GetReviewByIssueIDAndUserID(ctx, issue.ID, reviewer.ID) | ||||
| @@ -666,7 +655,7 @@ func AddReviewRequest(ctx context.Context, issue *Issue, reviewer, doer *user_mo | ||||
| 		if review != nil { | ||||
| 			// skip it when reviewer has been request to review | ||||
| 			if review.Type == ReviewTypeRequest { | ||||
| 			return nil, committer.Commit() // still commit the transaction, or committer.Close() will rollback it, even if it's a reused transaction. | ||||
| 				return nil, nil // still commit the transaction, or committer.Close() will rollback it, even if it's a reused transaction. | ||||
| 			} | ||||
|  | ||||
| 			if issue.IsClosed { | ||||
| @@ -721,17 +710,13 @@ func AddReviewRequest(ctx context.Context, issue *Issue, reviewer, doer *user_mo | ||||
| 		// func caller use the created comment to retrieve created review too. | ||||
| 		comment.Review = review | ||||
|  | ||||
| 	return comment, committer.Commit() | ||||
| 		return comment, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // RemoveReviewRequest remove a review request from one reviewer | ||||
| func RemoveReviewRequest(ctx context.Context, issue *Issue, reviewer, doer *user_model.User) (*Comment, error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*Comment, error) { | ||||
| 		review, err := GetReviewByIssueIDAndUserID(ctx, issue.ID, reviewer.ID) | ||||
| 		if err != nil && !IsErrReviewNotExist(err) { | ||||
| 			return nil, err | ||||
| @@ -754,7 +739,7 @@ func RemoveReviewRequest(ctx context.Context, issue *Issue, reviewer, doer *user | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	comment, err := CreateComment(ctx, &CreateCommentOptions{ | ||||
| 		return CreateComment(ctx, &CreateCommentOptions{ | ||||
| 			Type:            CommentTypeReviewRequest, | ||||
| 			Doer:            doer, | ||||
| 			Repo:            issue.Repo, | ||||
| @@ -762,11 +747,7 @@ func RemoveReviewRequest(ctx context.Context, issue *Issue, reviewer, doer *user | ||||
| 			RemovedAssignee: true,        // Use RemovedAssignee as !isRequest | ||||
| 			AssigneeID:      reviewer.ID, // Use AssigneeID as reviewer ID | ||||
| 		}) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	return comment, committer.Commit() | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // Recalculate the latest official review for reviewer | ||||
| @@ -787,12 +768,7 @@ func restoreLatestOfficialReview(ctx context.Context, issueID, reviewerID int64) | ||||
|  | ||||
| // AddTeamReviewRequest add a review request from one team | ||||
| func AddTeamReviewRequest(ctx context.Context, issue *Issue, reviewer *organization.Team, doer *user_model.User) (*Comment, error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*Comment, error) { | ||||
| 		review, err := GetTeamReviewerByIssueIDAndTeamID(ctx, issue.ID, reviewer.ID) | ||||
| 		if err != nil && !IsErrReviewNotExist(err) { | ||||
| 			return nil, err | ||||
| @@ -841,17 +817,13 @@ func AddTeamReviewRequest(ctx context.Context, issue *Issue, reviewer *organizat | ||||
| 			return nil, fmt.Errorf("CreateComment(): %w", err) | ||||
| 		} | ||||
|  | ||||
| 	return comment, committer.Commit() | ||||
| 		return comment, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // RemoveTeamReviewRequest remove a review request from one team | ||||
| func RemoveTeamReviewRequest(ctx context.Context, issue *Issue, reviewer *organization.Team, doer *user_model.User) (*Comment, error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*Comment, error) { | ||||
| 		review, err := GetTeamReviewerByIssueIDAndTeamID(ctx, issue.ID, reviewer.ID) | ||||
| 		if err != nil && !IsErrReviewNotExist(err) { | ||||
| 			return nil, err | ||||
| @@ -885,7 +857,7 @@ func RemoveTeamReviewRequest(ctx context.Context, issue *Issue, reviewer *organi | ||||
| 		} | ||||
|  | ||||
| 		if doer == nil { | ||||
| 		return nil, committer.Commit() | ||||
| 			return nil, nil | ||||
| 		} | ||||
|  | ||||
| 		comment, err := CreateComment(ctx, &CreateCommentOptions{ | ||||
| @@ -900,7 +872,8 @@ func RemoveTeamReviewRequest(ctx context.Context, issue *Issue, reviewer *organi | ||||
| 			return nil, fmt.Errorf("CreateComment(): %w", err) | ||||
| 		} | ||||
|  | ||||
| 	return comment, committer.Commit() | ||||
| 		return comment, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // MarkConversation Add or remove Conversation mark for a code comment | ||||
| @@ -966,12 +939,7 @@ func CanMarkConversation(ctx context.Context, issue *Issue, doer *user_model.Use | ||||
|  | ||||
| // DeleteReview delete a review and it's code comments | ||||
| func DeleteReview(ctx context.Context, r *Review) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if r.ID == 0 { | ||||
| 			return errors.New("review is not allowed to be 0") | ||||
| 		} | ||||
| @@ -1019,8 +987,8 @@ func DeleteReview(ctx context.Context, r *Review) error { | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // GetCodeCommentsCount return count of CodeComments a Review has | ||||
|   | ||||
| @@ -168,12 +168,7 @@ func GetTrackedSeconds(ctx context.Context, opts FindTrackedTimesOptions) (track | ||||
|  | ||||
| // AddTime will add the given time (in seconds) to the issue | ||||
| func AddTime(ctx context.Context, user *user_model.User, issue *Issue, amount int64, created time.Time) (*TrackedTime, error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*TrackedTime, error) { | ||||
| 		t, err := addTime(ctx, user, issue, amount, created) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| @@ -196,7 +191,8 @@ func AddTime(ctx context.Context, user *user_model.User, issue *Issue, amount in | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| 	return t, committer.Commit() | ||||
| 		return t, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func addTime(ctx context.Context, user *user_model.User, issue *Issue, amount int64, created time.Time) (*TrackedTime, error) { | ||||
| @@ -241,12 +237,7 @@ func TotalTimesForEachUser(ctx context.Context, options *FindTrackedTimesOptions | ||||
|  | ||||
| // DeleteIssueUserTimes deletes times for issue | ||||
| func DeleteIssueUserTimes(ctx context.Context, issue *Issue, user *user_model.User) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		opts := FindTrackedTimesOptions{ | ||||
| 			IssueID: issue.ID, | ||||
| 			UserID:  user.ID, | ||||
| @@ -263,7 +254,7 @@ func DeleteIssueUserTimes(ctx context.Context, issue *Issue, user *user_model.Us | ||||
| 		if err := issue.LoadRepo(ctx); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	if _, err := CreateComment(ctx, &CreateCommentOptions{ | ||||
| 		_, err = CreateComment(ctx, &CreateCommentOptions{ | ||||
| 			Issue: issue, | ||||
| 			Repo:  issue.Repo, | ||||
| 			Doer:  user, | ||||
| @@ -271,21 +262,14 @@ func DeleteIssueUserTimes(ctx context.Context, issue *Issue, user *user_model.Us | ||||
| 			// so use "|" as delimiter to mark the new format | ||||
| 			Content: fmt.Sprintf("|%d", removedTime), | ||||
| 			Type:    CommentTypeDeleteTimeManual, | ||||
| 	}); err != nil { | ||||
| 		}) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // DeleteTime delete a specific Time | ||||
| func DeleteTime(ctx context.Context, t *TrackedTime) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if err := t.LoadAttributes(ctx); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -294,7 +278,7 @@ func DeleteTime(ctx context.Context, t *TrackedTime) error { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 	if _, err := CreateComment(ctx, &CreateCommentOptions{ | ||||
| 		_, err := CreateComment(ctx, &CreateCommentOptions{ | ||||
| 			Issue: t.Issue, | ||||
| 			Repo:  t.Issue.Repo, | ||||
| 			Doer:  t.User, | ||||
| @@ -302,11 +286,9 @@ func DeleteTime(ctx context.Context, t *TrackedTime) error { | ||||
| 			// so use "|" as delimiter to mark the new format | ||||
| 			Content: fmt.Sprintf("|%d", t.Time), | ||||
| 			Type:    CommentTypeDeleteTimeManual, | ||||
| 	}); err != nil { | ||||
| 		}) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func deleteTimes(ctx context.Context, opts FindTrackedTimesOptions) (removedTime int64, err error) { | ||||
|   | ||||
| @@ -310,12 +310,7 @@ func CreateOrganization(ctx context.Context, org *Organization, owner *user_mode | ||||
| 	org.NumMembers = 1 | ||||
| 	org.Type = user_model.UserTypeOrganization | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if err = user_model.DeleteUserRedirect(ctx, org.Name); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -376,8 +371,8 @@ func CreateOrganization(ctx context.Context, org *Organization, owner *user_mode | ||||
| 		}); err != nil { | ||||
| 			return fmt.Errorf("insert team-user relation: %w", err) | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // GetOrgByName returns organization by given name. | ||||
| @@ -499,12 +494,7 @@ func AddOrgUser(ctx context.Context, orgID, uid int64) error { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		// check in transaction | ||||
| 		isAlreadyMember, err = IsOrganizationMember(ctx, orgID, uid) | ||||
| 		if err != nil || isAlreadyMember { | ||||
| @@ -522,8 +512,8 @@ func AddOrgUser(ctx context.Context, orgID, uid int64) error { | ||||
| 		} else if _, err = db.Exec(ctx, "UPDATE `user` SET num_members = num_members + 1 WHERE id = ?", orgID); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // GetOrgByID returns the user object by given ID if exists. | ||||
|   | ||||
| @@ -31,12 +31,7 @@ func getUnitsByTeamID(ctx context.Context, teamID int64) (units []*TeamUnit, err | ||||
|  | ||||
| // UpdateTeamUnits updates a teams's units | ||||
| func UpdateTeamUnits(ctx context.Context, team *Team, units []TeamUnit) (err error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if _, err = db.GetEngine(ctx).Where("team_id = ?", team.ID).Delete(new(TeamUnit)); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -46,6 +41,6 @@ func UpdateTeamUnits(ctx context.Context, team *Team, units []TeamUnit) (err err | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|   | ||||
| @@ -359,12 +359,7 @@ func updateRepositoryProjectCount(ctx context.Context, repoID int64) error { | ||||
|  | ||||
| // ChangeProjectStatusByRepoIDAndID toggles a project between opened and closed | ||||
| func ChangeProjectStatusByRepoIDAndID(ctx context.Context, repoID, projectID int64, isClosed bool) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		p := new(Project) | ||||
|  | ||||
| 		has, err := db.GetEngine(ctx).ID(projectID).Where("repo_id = ?", repoID).Get(p) | ||||
| @@ -374,26 +369,15 @@ func ChangeProjectStatusByRepoIDAndID(ctx context.Context, repoID, projectID int | ||||
| 			return ErrProjectNotExist{ID: projectID, RepoID: repoID} | ||||
| 		} | ||||
|  | ||||
| 	if err := changeProjectStatus(ctx, p, isClosed); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return changeProjectStatus(ctx, p, isClosed) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // ChangeProjectStatus toggle a project between opened and closed | ||||
| func ChangeProjectStatus(ctx context.Context, p *Project, isClosed bool) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	if err := changeProjectStatus(ctx, p, isClosed); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		return changeProjectStatus(ctx, p, isClosed) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func changeProjectStatus(ctx context.Context, p *Project, isClosed bool) error { | ||||
|   | ||||
| @@ -290,19 +290,14 @@ func UpdateRepoStats(ctx context.Context, id int64) error { | ||||
| } | ||||
|  | ||||
| func updateUserStarNumbers(ctx context.Context, users []user_model.User) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		for _, user := range users { | ||||
| 		if _, err = db.Exec(ctx, "UPDATE `user` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE uid=?) WHERE id=?", user.ID, user.ID); err != nil { | ||||
| 			if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE uid=?) WHERE id=?", user.ID, user.ID); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // DoctorUserStarNum recalculate Stars number for all user | ||||
|   | ||||
| @@ -141,11 +141,7 @@ func GetTopLanguageStats(ctx context.Context, repo *Repository, limit int) (Lang | ||||
|  | ||||
| // UpdateLanguageStats updates the language statistics for repository | ||||
| func UpdateLanguageStats(ctx context.Context, repo *Repository, commitID string, stats map[string]int64) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		sess := db.GetEngine(ctx) | ||||
|  | ||||
| 		oldstats, err := GetLanguageStats(ctx, repo) | ||||
| @@ -203,21 +199,13 @@ func UpdateLanguageStats(ctx context.Context, repo *Repository, commitID string, | ||||
| 		} | ||||
|  | ||||
| 		// Update indexer status | ||||
| 	if err = UpdateIndexerStatus(ctx, repo, RepoIndexerTypeStats, commitID); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return UpdateIndexerStatus(ctx, repo, RepoIndexerTypeStats, commitID) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // CopyLanguageStat Copy originalRepo language stat information to destRepo (use for forked repo) | ||||
| func CopyLanguageStat(ctx context.Context, originalRepo, destRepo *Repository) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		RepoLang := make(LanguageStatList, 0, 6) | ||||
| 		if err := db.GetEngine(ctx).Where("`repo_id` = ?", originalRepo.ID).Desc("`size`").Find(&RepoLang); err != nil { | ||||
| 			return err | ||||
| @@ -237,5 +225,6 @@ func CopyLanguageStat(ctx context.Context, originalRepo, destRepo *Repository) e | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|   | ||||
| @@ -472,15 +472,9 @@ func (r *Release) GetExternalID() int64 { return r.OriginalAuthorID } | ||||
|  | ||||
| // InsertReleases migrates release | ||||
| func InsertReleases(ctx context.Context, rels ...*Release) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	sess := db.GetEngine(ctx) | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		for _, rel := range rels { | ||||
| 		if _, err := sess.NoAutoTime().Insert(rel); err != nil { | ||||
| 			if _, err := db.GetEngine(ctx).NoAutoTime().Insert(rel); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
|  | ||||
| @@ -489,13 +483,13 @@ func InsertReleases(ctx context.Context, rels ...*Release) error { | ||||
| 					rel.Attachments[i].ReleaseID = rel.ID | ||||
| 				} | ||||
|  | ||||
| 			if _, err := sess.NoAutoTime().Insert(rel.Attachments); err != nil { | ||||
| 				if _, err := db.GetEngine(ctx).NoAutoTime().Insert(rel.Attachments); err != nil { | ||||
| 					return err | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func FindTagsByCommitIDs(ctx context.Context, repoID int64, commitIDs ...string) (map[string][]*Release, error) { | ||||
|   | ||||
| @@ -25,11 +25,7 @@ func init() { | ||||
|  | ||||
| // StarRepo or unstar repository. | ||||
| func StarRepo(ctx context.Context, doer *user_model.User, repo *Repository, star bool) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		staring := IsStaring(ctx, doer.ID, repo.ID) | ||||
|  | ||||
| 		if star { | ||||
| @@ -66,7 +62,8 @@ func StarRepo(ctx context.Context, doer *user_model.User, repo *Repository, star | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // IsStaring checks if user has starred given repository. | ||||
|   | ||||
| @@ -227,13 +227,7 @@ func GetRepoTopicByName(ctx context.Context, repoID int64, topicName string) (*T | ||||
|  | ||||
| // AddTopic adds a topic name to a repository (if it does not already have it) | ||||
| func AddTopic(ctx context.Context, repoID int64, topicName string) (*Topic, error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	sess := db.GetEngine(ctx) | ||||
|  | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*Topic, error) { | ||||
| 		topic, err := GetRepoTopicByName(ctx, repoID, topicName) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| @@ -248,11 +242,11 @@ func AddTopic(ctx context.Context, repoID int64, topicName string) (*Topic, erro | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| 	if err = syncTopicsInRepository(sess, repoID); err != nil { | ||||
| 		if err = syncTopicsInRepository(ctx, repoID); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| 	return topic, committer.Commit() | ||||
| 		return topic, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // DeleteTopic removes a topic name from a repository (if it has it) | ||||
| @@ -266,14 +260,15 @@ func DeleteTopic(ctx context.Context, repoID int64, topicName string) (*Topic, e | ||||
| 		return nil, nil | ||||
| 	} | ||||
|  | ||||
| 	err = removeTopicFromRepo(ctx, repoID, topic) | ||||
| 	if err != nil { | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (*Topic, error) { | ||||
| 		if err = removeTopicFromRepo(ctx, repoID, topic); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| 	err = syncTopicsInRepository(db.GetEngine(ctx), repoID) | ||||
|  | ||||
| 	return topic, err | ||||
| 		if err = syncTopicsInRepository(ctx, repoID); err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 		return topic, nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // SaveTopics save topics to a repository | ||||
| @@ -285,13 +280,7 @@ func SaveTopics(ctx context.Context, repoID int64, topicNames ...string) error { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	sess := db.GetEngine(ctx) | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		var addedTopicNames []string | ||||
| 		for _, topicName := range topicNames { | ||||
| 			if strings.TrimSpace(topicName) == "" { | ||||
| @@ -338,11 +327,8 @@ func SaveTopics(ctx context.Context, repoID int64, topicNames ...string) error { | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	if err := syncTopicsInRepository(sess, repoID); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return syncTopicsInRepository(ctx, repoID) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // GenerateTopics generates topics from a template repository | ||||
| @@ -353,19 +339,19 @@ func GenerateTopics(ctx context.Context, templateRepo, generateRepo *Repository) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return syncTopicsInRepository(db.GetEngine(ctx), generateRepo.ID) | ||||
| 	return syncTopicsInRepository(ctx, generateRepo.ID) | ||||
| } | ||||
|  | ||||
| // syncTopicsInRepository makes sure topics in the topics table are copied into the topics field of the repository | ||||
| func syncTopicsInRepository(sess db.Engine, repoID int64) error { | ||||
| func syncTopicsInRepository(ctx context.Context, repoID int64) error { | ||||
| 	topicNames := make([]string, 0, 25) | ||||
| 	if err := sess.Table("topic").Cols("name"). | ||||
| 	if err := db.GetEngine(ctx).Table("topic").Cols("name"). | ||||
| 		Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id"). | ||||
| 		Where("repo_topic.repo_id = ?", repoID).Asc("topic.name").Find(&topicNames); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if _, err := sess.ID(repoID).Cols("topics").Update(&Repository{ | ||||
| 	if _, err := db.GetEngine(ctx).ID(repoID).Cols("topics").Update(&Repository{ | ||||
| 		Topics: topicNames, | ||||
| 	}); err != nil { | ||||
| 		return err | ||||
|   | ||||
| @@ -19,11 +19,6 @@ func UpdateRepositoryOwnerNames(ctx context.Context, ownerID int64, ownerName st | ||||
| 	if ownerID == 0 { | ||||
| 		return nil | ||||
| 	} | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	if _, err := db.GetEngine(ctx).Where("owner_id = ?", ownerID).Cols("owner_name").NoAutoTime().Update(&Repository{ | ||||
| 		OwnerName: ownerName, | ||||
| @@ -31,7 +26,7 @@ func UpdateRepositoryOwnerNames(ctx context.Context, ownerID int64, ownerName st | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // UpdateRepositoryUpdatedTime updates a repository's updated time | ||||
|   | ||||
| @@ -117,12 +117,6 @@ func DeleteUploads(ctx context.Context, uploads ...*Upload) (err error) { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	ids := make([]int64, len(uploads)) | ||||
| 	for i := range uploads { | ||||
| 		ids[i] = uploads[i].ID | ||||
| @@ -131,10 +125,6 @@ func DeleteUploads(ctx context.Context, uploads ...*Upload) (err error) { | ||||
| 		return fmt.Errorf("delete uploads: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	if err = committer.Commit(); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	for _, upload := range uploads { | ||||
| 		localPath := upload.LocalPath() | ||||
| 		isFile, err := util.IsFile(localPath) | ||||
|   | ||||
| @@ -256,15 +256,9 @@ func IsEmailUsed(ctx context.Context, email string) (bool, error) { | ||||
|  | ||||
| // ActivateEmail activates the email address to given user. | ||||
| func ActivateEmail(ctx context.Context, email *EmailAddress) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	if err := updateActivation(ctx, email, true); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	return committer.Commit() | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		return updateActivation(ctx, email, true) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func updateActivation(ctx context.Context, email *EmailAddress, activate bool) error { | ||||
| @@ -305,21 +299,17 @@ func makeEmailPrimaryInternal(ctx context.Context, emailID int64, isActive bool) | ||||
| 		return ErrUserNotExist{UID: email.UID} | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		sess := db.GetEngine(ctx) | ||||
|  | ||||
| 		// 1. Update user table | ||||
| 		user.Email = email.Email | ||||
| 	if _, err = sess.ID(user.ID).Cols("email").Update(user); err != nil { | ||||
| 		if _, err := sess.ID(user.ID).Cols("email").Update(user); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 		// 2. Update old primary email | ||||
| 	if _, err = sess.Where("uid=? AND is_primary=?", email.UID, true).Cols("is_primary").Update(&EmailAddress{ | ||||
| 		if _, err := sess.Where("uid=? AND is_primary=?", email.UID, true).Cols("is_primary").Update(&EmailAddress{ | ||||
| 			IsPrimary: false, | ||||
| 		}); err != nil { | ||||
| 			return err | ||||
| @@ -327,11 +317,12 @@ func makeEmailPrimaryInternal(ctx context.Context, emailID int64, isActive bool) | ||||
|  | ||||
| 		// 3. update new primary email | ||||
| 		email.IsPrimary = true | ||||
| 	if _, err = sess.ID(email.ID).Cols("is_primary").Update(email); err != nil { | ||||
| 		if _, err := sess.ID(email.ID).Cols("is_primary").Update(email); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // ChangeInactivePrimaryEmail replaces the inactive primary email of a given user | ||||
|   | ||||
| @@ -38,12 +38,7 @@ func FollowUser(ctx context.Context, user, follow *User) (err error) { | ||||
| 		return ErrBlockedUser | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if err = db.Insert(ctx, &Follow{UserID: user.ID, FollowID: follow.ID}); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -55,7 +50,8 @@ func FollowUser(ctx context.Context, user, follow *User) (err error) { | ||||
| 		if _, err = db.Exec(ctx, "UPDATE `user` SET num_following = num_following + 1 WHERE id = ?", user.ID); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // UnfollowUser unmarks someone as another's follower. | ||||
| @@ -64,12 +60,7 @@ func UnfollowUser(ctx context.Context, userID, followID int64) (err error) { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if _, err = db.DeleteByBean(ctx, &Follow{UserID: userID, FollowID: followID}); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -81,5 +72,6 @@ func UnfollowUser(ctx context.Context, userID, followID int64) (err error) { | ||||
| 		if _, err = db.Exec(ctx, "UPDATE `user` SET num_following = num_following - 1 WHERE id = ?", userID); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|   | ||||
| @@ -319,12 +319,7 @@ func UpdateWebhookLastStatus(ctx context.Context, w *Webhook) error { | ||||
| // DeleteWebhookByID uses argument bean as query condition, | ||||
| // ID must be specified and do not assign unnecessary fields. | ||||
| func DeleteWebhookByID(ctx context.Context, id int64) (err error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if count, err := db.DeleteByID[Webhook](ctx, id); err != nil { | ||||
| 			return err | ||||
| 		} else if count == 0 { | ||||
| @@ -332,8 +327,8 @@ func DeleteWebhookByID(ctx context.Context, id int64) (err error) { | ||||
| 		} else if _, err = db.DeleteByBean(ctx, &HookTask{HookID: id}); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // DeleteWebhookByRepoID deletes webhook of repository by given ID. | ||||
|   | ||||
| @@ -49,12 +49,7 @@ func deleteDeployKeyFromDB(ctx context.Context, key *asymkey_model.DeployKey) er | ||||
| // DeleteDeployKey deletes deploy key from its repository authorized_keys file if needed. | ||||
| // Permissions check should be done outside. | ||||
| func DeleteDeployKey(ctx context.Context, repo *repo_model.Repository, id int64) error { | ||||
| 	dbCtx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	if err := db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		key, err := asymkey_model.GetDeployKeyByID(ctx, id) | ||||
| 		if err != nil { | ||||
| 			if asymkey_model.IsErrDeployKeyNotExist(err) { | ||||
| @@ -67,10 +62,8 @@ func DeleteDeployKey(ctx context.Context, repo *repo_model.Repository, id int64) | ||||
| 			return fmt.Errorf("deploy key %d does not belong to repository %d", id, repo.ID) | ||||
| 		} | ||||
|  | ||||
| 	if err := deleteDeployKeyFromDB(dbCtx, key); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	if err := committer.Commit(); err != nil { | ||||
| 		return deleteDeployKeyFromDB(ctx, key) | ||||
| 	}); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -27,20 +27,9 @@ func DeletePublicKey(ctx context.Context, doer *user_model.User, id int64) (err | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	dbCtx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 	if _, err = db.DeleteByID[asymkey_model.PublicKey](ctx, id); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	if _, err = db.DeleteByID[asymkey_model.PublicKey](dbCtx, id); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if err = committer.Commit(); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	committer.Close() | ||||
|  | ||||
| 	if key.Type == asymkey_model.KeyTypePrincipal { | ||||
| 		return RewriteAllPrincipalKeys(ctx) | ||||
|   | ||||
| @@ -14,24 +14,6 @@ import ( | ||||
|  | ||||
| // AddPrincipalKey adds new principal to database and authorized_principals file. | ||||
| func AddPrincipalKey(ctx context.Context, ownerID int64, content string, authSourceID int64) (*asymkey_model.PublicKey, error) { | ||||
| 	dbCtx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	// Principals cannot be duplicated. | ||||
| 	has, err := db.GetEngine(dbCtx). | ||||
| 		Where("content = ? AND type = ?", content, asymkey_model.KeyTypePrincipal). | ||||
| 		Get(new(asymkey_model.PublicKey)) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} else if has { | ||||
| 		return nil, asymkey_model.ErrKeyAlreadyExist{ | ||||
| 			Content: content, | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	key := &asymkey_model.PublicKey{ | ||||
| 		OwnerID:       ownerID, | ||||
| 		Name:          content, | ||||
| @@ -40,15 +22,27 @@ func AddPrincipalKey(ctx context.Context, ownerID int64, content string, authSou | ||||
| 		Type:          asymkey_model.KeyTypePrincipal, | ||||
| 		LoginSourceID: authSourceID, | ||||
| 	} | ||||
| 	if err = db.Insert(dbCtx, key); err != nil { | ||||
| 		return nil, fmt.Errorf("addKey: %w", err) | ||||
|  | ||||
| 	if err := db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		// Principals cannot be duplicated. | ||||
| 		has, err := db.GetEngine(ctx). | ||||
| 			Where("content = ? AND type = ?", content, asymkey_model.KeyTypePrincipal). | ||||
| 			Get(new(asymkey_model.PublicKey)) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} else if has { | ||||
| 			return asymkey_model.ErrKeyAlreadyExist{ | ||||
| 				Content: content, | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	if err = committer.Commit(); err != nil { | ||||
| 		if err = db.Insert(ctx, key); err != nil { | ||||
| 			return fmt.Errorf("addKey: %w", err) | ||||
| 		} | ||||
| 		return nil | ||||
| 	}); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	committer.Close() | ||||
|  | ||||
| 	return key, RewriteAllPrincipalKeys(ctx) | ||||
| } | ||||
|   | ||||
| @@ -46,17 +46,12 @@ func AddLabels(ctx context.Context, issue *issues_model.Issue, doer *user_model. | ||||
|  | ||||
| // RemoveLabel removes a label from issue by given ID. | ||||
| func RemoveLabel(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, label *issues_model.Label) error { | ||||
| 	dbCtx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	if err := issue.LoadRepo(dbCtx); err != nil { | ||||
| 	if err := db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if err := issue.LoadRepo(ctx); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 	perm, err := access_model.GetUserRepoPermission(dbCtx, issue.Repo, doer) | ||||
| 		perm, err := access_model.GetUserRepoPermission(ctx, issue.Repo, doer) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -67,11 +62,8 @@ func RemoveLabel(ctx context.Context, issue *issues_model.Issue, doer *user_mode | ||||
| 			return issues_model.ErrRepoLabelNotExist{} | ||||
| 		} | ||||
|  | ||||
| 	if err := issues_model.DeleteIssueLabel(dbCtx, issue, label, doer); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if err := committer.Commit(); err != nil { | ||||
| 		return issues_model.DeleteIssueLabel(ctx, issue, label, doer) | ||||
| 	}); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -69,21 +69,12 @@ func changeMilestoneAssign(ctx context.Context, doer *user_model.User, issue *is | ||||
|  | ||||
| // ChangeMilestoneAssign changes assignment of milestone for issue. | ||||
| func ChangeMilestoneAssign(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, oldMilestoneID int64) (err error) { | ||||
| 	dbCtx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 	if err := db.WithTx(ctx, func(dbCtx context.Context) error { | ||||
| 		return changeMilestoneAssign(dbCtx, doer, issue, oldMilestoneID) | ||||
| 	}); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	if err = changeMilestoneAssign(dbCtx, doer, issue, oldMilestoneID); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if err = committer.Commit(); err != nil { | ||||
| 		return fmt.Errorf("Commit: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	notify_service.IssueChangeMilestone(ctx, doer, issue, oldMilestoneID) | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
| @@ -15,31 +15,25 @@ import ( | ||||
|  | ||||
| // CloseIssue close an issue. | ||||
| func CloseIssue(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, commitID string) error { | ||||
| 	dbCtx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	comment, err := issues_model.CloseIssue(dbCtx, issue, doer) | ||||
| 	var comment *issues_model.Comment | ||||
| 	if err := db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		var err error | ||||
| 		comment, err = issues_model.CloseIssue(ctx, issue, doer) | ||||
| 		if err != nil { | ||||
| 			if issues_model.IsErrDependenciesLeft(err) { | ||||
| 			if _, err := issues_model.FinishIssueStopwatch(dbCtx, doer, issue); err != nil { | ||||
| 				if _, err := issues_model.FinishIssueStopwatch(ctx, doer, issue); err != nil { | ||||
| 					log.Error("Unable to stop stopwatch for issue[%d]#%d: %v", issue.ID, issue.Index, err) | ||||
| 				} | ||||
| 			} | ||||
| 			return err | ||||
| 		} | ||||
|  | ||||
| 	if _, err := issues_model.FinishIssueStopwatch(dbCtx, doer, issue); err != nil { | ||||
| 		_, err = issues_model.FinishIssueStopwatch(ctx, doer, issue) | ||||
| 		return err | ||||
| 	}); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if err := committer.Commit(); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	committer.Close() | ||||
|  | ||||
| 	notify_service.IssueChangeStatus(ctx, doer, commitID, issue, comment, true) | ||||
|  | ||||
| 	return nil | ||||
|   | ||||
| @@ -54,12 +54,7 @@ func NewTeam(ctx context.Context, t *organization.Team) (err error) { | ||||
| 		return organization.ErrTeamAlreadyExist{OrgID: t.OrgID, Name: t.LowerName} | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if err = db.Insert(ctx, t); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -83,10 +78,9 @@ func NewTeam(ctx context.Context, t *organization.Team) (err error) { | ||||
| 		} | ||||
|  | ||||
| 		// Update organization number of teams. | ||||
| 	if _, err = db.Exec(ctx, "UPDATE `user` SET num_teams=num_teams+1 WHERE id = ?", t.OrgID); err != nil { | ||||
| 		_, err = db.Exec(ctx, "UPDATE `user` SET num_teams=num_teams+1 WHERE id = ?", t.OrgID) | ||||
| 		return err | ||||
| 	} | ||||
| 	return committer.Commit() | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // UpdateTeam updates information of team. | ||||
| @@ -99,12 +93,7 @@ func UpdateTeam(ctx context.Context, t *organization.Team, authChanged, includeA | ||||
| 		t.Description = t.Description[:255] | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		t.LowerName = strings.ToLower(t.Name) | ||||
| 		has, err := db.Exist[organization.Team](ctx, builder.Eq{ | ||||
| 			"org_id":     t.OrgID, | ||||
| @@ -163,18 +152,14 @@ func UpdateTeam(ctx context.Context, t *organization.Team, authChanged, includeA | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // DeleteTeam deletes given team. | ||||
| // It's caller's responsibility to assign organization ID. | ||||
| func DeleteTeam(ctx context.Context, t *organization.Team) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if err := t.LoadMembers(ctx); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -216,11 +201,9 @@ func DeleteTeam(ctx context.Context, t *organization.Team) error { | ||||
| 		} | ||||
|  | ||||
| 		// Update organization number of teams. | ||||
| 	if _, err := db.Exec(ctx, "UPDATE `user` SET num_teams=num_teams-1 WHERE id=?", t.OrgID); err != nil { | ||||
| 		_, err := db.Exec(ctx, "UPDATE `user` SET num_teams=num_teams-1 WHERE id=?", t.OrgID) | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // AddTeamMember adds new membership of given team to given organization, | ||||
| @@ -363,13 +346,7 @@ func removeInvalidOrgUser(ctx context.Context, orgID int64, user *user_model.Use | ||||
|  | ||||
| // RemoveTeamMember removes member from given team of given organization. | ||||
| func RemoveTeamMember(ctx context.Context, team *organization.Team, user *user_model.User) error { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
| 	if err := removeTeamMember(ctx, team, user); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	return committer.Commit() | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		return removeTeamMember(ctx, team, user) | ||||
| 	}) | ||||
| } | ||||
|   | ||||
| @@ -164,13 +164,9 @@ func ExecuteCleanupRules(ctx context.Context) error { | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func CleanupExpiredData(outerCtx context.Context, olderThan time.Duration) error { | ||||
| 	ctx, committer, err := db.TxContext(outerCtx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| func CleanupExpiredData(ctx context.Context, olderThan time.Duration) error { | ||||
| 	pbs := make([]*packages_model.PackageBlob, 0, 100) | ||||
| 	if err := db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if err := container_service.Cleanup(ctx, olderThan); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -188,7 +184,7 @@ func CleanupExpiredData(outerCtx context.Context, olderThan time.Duration) error | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	pbs, err := packages_model.FindExpiredUnreferencedBlobs(ctx, olderThan) | ||||
| 		pbs, err = packages_model.FindExpiredUnreferencedBlobs(ctx, olderThan) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -198,8 +194,8 @@ func CleanupExpiredData(outerCtx context.Context, olderThan time.Duration) error | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	if err := committer.Commit(); err != nil { | ||||
| 		return nil | ||||
| 	}); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -469,24 +469,15 @@ func RemovePackageVersionByNameAndVersion(ctx context.Context, doer *user_model. | ||||
|  | ||||
| // RemovePackageVersion deletes the package version and all associated files | ||||
| func RemovePackageVersion(ctx context.Context, doer *user_model.User, pv *packages_model.PackageVersion) error { | ||||
| 	dbCtx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	pd, err := packages_model.GetPackageDescriptor(dbCtx, pv) | ||||
| 	pd, err := packages_model.GetPackageDescriptor(ctx, pv) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if err := db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		log.Trace("Deleting package: %v", pv.ID) | ||||
|  | ||||
| 	if err := DeletePackageVersionAndReferences(dbCtx, pv); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if err := committer.Commit(); err != nil { | ||||
| 		return DeletePackageVersionAndReferences(ctx, pv) | ||||
| 	}); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -687,12 +687,7 @@ func SetMerged(ctx context.Context, pr *issues_model.PullRequest, mergedCommitID | ||||
| 		return false, fmt.Errorf("unable to merge PullRequest[%d], some required fields are empty", pr.Index) | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return false, err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx2(ctx, func(ctx context.Context) (bool, error) { | ||||
| 		pr.Issue = nil | ||||
| 		if err := pr.LoadIssue(ctx); err != nil { | ||||
| 			return false, err | ||||
| @@ -726,9 +721,6 @@ func SetMerged(ctx context.Context, pr *issues_model.PullRequest, mergedCommitID | ||||
| 			return false, issues_model.ErrIssueAlreadyChanged | ||||
| 		} | ||||
|  | ||||
| 	if err := committer.Commit(); err != nil { | ||||
| 		return false, err | ||||
| 	} | ||||
|  | ||||
| 		return true, nil | ||||
| 	}) | ||||
| } | ||||
|   | ||||
| @@ -29,12 +29,7 @@ func UploadAvatar(ctx context.Context, repo *repo_model.Repository, data []byte) | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		oldAvatarPath := repo.CustomAvatarRelativePath() | ||||
|  | ||||
| 		// Users can upload the same image to other repo - prefix it with ID | ||||
| @@ -56,8 +51,8 @@ func UploadAvatar(ctx context.Context, repo *repo_model.Repository, data []byte) | ||||
| 				return fmt.Errorf("UploadAvatar: Failed to remove old repo avatar %s: %w", oldAvatarPath, err) | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // DeleteAvatar deletes the repos's custom avatar. | ||||
| @@ -70,12 +65,7 @@ func DeleteAvatar(ctx context.Context, repo *repo_model.Repository) error { | ||||
| 	avatarPath := repo.CustomAvatarRelativePath() | ||||
| 	log.Trace("DeleteAvatar[%d]: %s", repo.ID, avatarPath) | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		repo.Avatar = "" | ||||
| 		if err := repo_model.UpdateRepositoryColsNoAutoTime(ctx, repo, "avatar"); err != nil { | ||||
| 			return fmt.Errorf("DeleteAvatar: Update repository avatar: %w", err) | ||||
| @@ -84,8 +74,8 @@ func DeleteAvatar(ctx context.Context, repo *repo_model.Repository) error { | ||||
| 		if err := storage.RepoAvatars.Delete(avatarPath); err != nil { | ||||
| 			return fmt.Errorf("DeleteAvatar: Failed to remove %s: %w", avatarPath, err) | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // RemoveRandomAvatars removes the randomly generated avatars that were created for repositories | ||||
|   | ||||
| @@ -71,16 +71,11 @@ func DeleteCollaboration(ctx context.Context, repo *repo_model.Repository, colla | ||||
| 		UserID: collaborator.ID, | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		if has, err := db.GetEngine(ctx).Delete(collaboration); err != nil { | ||||
| 			return err | ||||
| 		} else if has == 0 { | ||||
| 		return committer.Commit() | ||||
| 			return nil | ||||
| 		} | ||||
|  | ||||
| 		if err := repo.LoadOwner(ctx); err != nil { | ||||
| @@ -100,11 +95,8 @@ func DeleteCollaboration(ctx context.Context, repo *repo_model.Repository, colla | ||||
| 		} | ||||
|  | ||||
| 		// Unassign a user from any issue (s)he has been assigned to in the repository | ||||
| 	if err := ReconsiderRepoIssuesAssignee(ctx, repo, collaborator); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return ReconsiderRepoIssuesAssignee(ctx, repo, collaborator) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func ReconsiderRepoIssuesAssignee(ctx context.Context, repo *repo_model.Repository, user *user_model.User) error { | ||||
|   | ||||
| @@ -86,17 +86,9 @@ func RemoveAllRepositoriesFromTeam(ctx context.Context, t *organization.Team) (e | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	if err = removeAllRepositoriesFromTeam(ctx, t); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		return removeAllRepositoriesFromTeam(ctx, t) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // removeAllRepositoriesFromTeam removes all repositories from team and recalculates access | ||||
| @@ -167,17 +159,9 @@ func RemoveRepositoryFromTeam(ctx context.Context, t *organization.Team, repoID | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	if err = removeRepositoryFromTeam(ctx, t, repo, true); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		return removeRepositoryFromTeam(ctx, t, repo, true) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // removeRepositoryFromTeam removes a repository from a team and recalculates access | ||||
|   | ||||
| @@ -16,12 +16,7 @@ import ( | ||||
|  | ||||
| // UpdateRepositoryUnits updates a repository's units | ||||
| func UpdateRepositoryUnits(ctx context.Context, repo *repo_model.Repository, units []repo_model.RepoUnit, deleteUnitTypes []unit.Type) (err error) { | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		// Delete existing settings of units before adding again | ||||
| 		for _, u := range units { | ||||
| 			deleteUnitTypes = append(deleteUnitTypes, u.Type) | ||||
| @@ -52,5 +47,6 @@ func UpdateRepositoryUnits(ctx context.Context, repo *repo_model.Repository, uni | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|   | ||||
| @@ -24,12 +24,7 @@ func UploadAvatar(ctx context.Context, u *user_model.User, data []byte) error { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	ctx, committer, err := db.TxContext(ctx) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer committer.Close() | ||||
|  | ||||
| 	return db.WithTx(ctx, func(ctx context.Context) error { | ||||
| 		u.UseCustomAvatar = true | ||||
| 		u.Avatar = avatar.HashAvatar(u.ID, data) | ||||
| 		if err = user_model.UpdateUserCols(ctx, u, "use_custom_avatar", "avatar"); err != nil { | ||||
| @@ -43,7 +38,8 @@ func UploadAvatar(ctx context.Context, u *user_model.User, data []byte) error { | ||||
| 			return fmt.Errorf("Failed to create dir %s: %w", u.CustomAvatarRelativePath(), err) | ||||
| 		} | ||||
|  | ||||
| 	return committer.Commit() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // DeleteAvatar deletes the user's custom avatar. | ||||
|   | ||||
		Reference in New Issue
	
	Block a user