Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions models/actions/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
25 changes: 7 additions & 18 deletions models/actions/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand All @@ -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) {
Expand Down
12 changes: 2 additions & 10 deletions models/actions/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 3 additions & 7 deletions models/actions/tasks_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -101,5 +96,6 @@ func IncreaseTaskVersion(ctx context.Context, ownerID, repoID int64) error {
}
}

return committer.Commit()
return nil
})
}
14 changes: 3 additions & 11 deletions models/activities/notification_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 3 additions & 10 deletions models/asymkey/gpg_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 err
}
defer committer.Close()

if _, err = deleteGPGKey(ctx, key.KeyID); err != nil {
return db.WithTx(ctx, func(ctx context.Context) error {
_, err = deleteGPGKey(ctx, key.KeyID)
return err
}

return committer.Commit()
})
}

func FindGPGKeyWithSubKeys(ctx context.Context, keyID string) ([]*GPGKey, error) {
Expand Down
25 changes: 2 additions & 23 deletions models/asymkey/gpg_key_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 5 additions & 18 deletions models/asymkey/ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand Down
10 changes: 3 additions & 7 deletions models/asymkey/ssh_key_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
12 changes: 2 additions & 10 deletions models/asymkey/ssh_key_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
})
}
22 changes: 6 additions & 16 deletions models/auth/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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)
})
}

//////////////////////////////////////////////////////
Expand Down
Loading