diff --git a/docs/content/doc/advanced/repo-mirror.en-us.md b/docs/content/doc/advanced/repo-mirror.en-us.md index c1d794762a..e95dfa0d63 100644 --- a/docs/content/doc/advanced/repo-mirror.en-us.md +++ b/docs/content/doc/advanced/repo-mirror.en-us.md @@ -41,13 +41,15 @@ For an existing remote repository, you can set up pull mirroring as follows: The repository now gets mirrored periodically from the remote repository. You can force a sync by selecting **Synchronize Now** in the repository settings. +:exclamation::exclamation: **NOTE:** You can only set up pull mirroring for repos that don't exist yet on your instance. Once the repo is created, you can't convert it into a pull mirror anymore. :exclamation::exclamation: + ## Pushing to a remote repository For an existing repository, you can set up push mirroring as follows: 1. In your repository, go to **Settings** > **Repository**, and then the **Mirror Settings** section. 2. Enter a repository URL. -3. If the repository needs authentication expand the **Authorization** section and fill in your authentication information. +3. If the repository needs authentication expand the **Authorization** section and fill in your authentication information. Note that the requested **password** can also be your access token. 4. Select **Add Push Mirror** to save the configuration. The repository now gets mirrored periodically to the remote repository. You can force a sync by selecting **Synchronize Now**. In case of an error a message displayed to help you resolve it. @@ -59,9 +61,11 @@ The repository now gets mirrored periodically to the remote repository. You can To set up a mirror from Gitea to GitHub, you need to follow these steps: 1. Create a [GitHub personal access token](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) with the *public_repo* box checked. -2. Fill in the **Git Remote Repository URL**: `https://github.com//.git`. -3. Fill in the **Authorization** fields with your GitHub username and the personal access token. -4. Select **Add Push Mirror** to save the configuration. +2. Create a repository with that name on GitHub. Unlike Gitea, GitHub does not support creating repositories by pushing to the remote. You can also use an existing remote repo if it has the same commit history as your Gitea repo. +3. In the settings of your Gitea repo, fill in the **Git Remote Repository URL**: `https://github.com//.git`. +4. Fill in the **Authorization** fields with your GitHub username and the personal access token as **Password**. +5. (Optional, available on Gitea 1.18+) Select `Sync when new commits are pushed` so that the mirror will be updated as well as soon as there are changes. You can also disable the periodic sync if you like. +6. Select **Add Push Mirror** to save the configuration. The repository pushes shortly thereafter. To force a push, select the **Synchronize Now** button. diff --git a/docs/content/doc/translation/guidelines.de-de.md b/docs/content/doc/translation/guidelines.de-de.md index eee031a54e..3fa7185562 100644 --- a/docs/content/doc/translation/guidelines.de-de.md +++ b/docs/content/doc/translation/guidelines.de-de.md @@ -45,15 +45,15 @@ Beispiel: Wenn der Button mit `löschen` beschriftet ist, sollte im Modal die Fr ## Artikeldefinitionen für Anglizismen * _Der_ Commit (m.) -* _Der_ Branch (m.) +* _Der_ Branch (m.), plural: die Branches * _Das_ Issue (n.) * _Der_ Fork (m.) -* _Das_ Repository (n.) +* _Das_ Repository (n.), plural: die Repositories * _Der_ Pull-Request (m.) -* _Der_ Token (m.) +* _Der_ Token (m.), plural: die Token * _Das_ Review (n.) * _Der_ Key (m.) -* _Der_ Committer (m.) +* _Der_ Committer (m.), plural: die Committer ## Weiterführende Links diff --git a/go.mod b/go.mod index 408249880c..cf1397c468 100644 --- a/go.mod +++ b/go.mod @@ -94,11 +94,11 @@ require ( github.com/yuin/goldmark-meta v1.1.0 go.jolheiser.com/hcaptcha v0.0.4 go.jolheiser.com/pwn v0.0.3 - golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be - golang.org/x/net v0.0.0-20220927171203-f486391704dc + golang.org/x/crypto v0.2.1-0.20221112162523-6fad3dfc1891 + golang.org/x/net v0.2.0 golang.org/x/oauth2 v0.0.0-20220909003341-f21342109be1 - golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec - golang.org/x/text v0.3.8 + golang.org/x/sys v0.2.0 + golang.org/x/text v0.4.0 golang.org/x/tools v0.1.12 gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df gopkg.in/ini.v1 v1.67.0 diff --git a/go.sum b/go.sum index 65841b8ec3..d6748eae34 100644 --- a/go.sum +++ b/go.sum @@ -1608,8 +1608,8 @@ golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0 golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be h1:fmw3UbQh+nxngCAHrDCCztao/kbYFnWjoqop8dHx05A= -golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.2.1-0.20221112162523-6fad3dfc1891 h1:WhEPFM1Ck5gaKybeSWvzI7Y/cd8K9K5tJGRxXMACOBA= +golang.org/x/crypto v0.2.1-0.20221112162523-6fad3dfc1891/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -1721,8 +1721,8 @@ golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su golang.org/x/net v0.0.0-20220607020251-c690dde0001d/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.0.0-20220630215102-69896b714898/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= -golang.org/x/net v0.0.0-20220927171203-f486391704dc h1:FxpXZdoBqT8RjqTy6i1E8nXHhW21wK7ptQ/EPIGxzPQ= -golang.org/x/net v0.0.0-20220927171203-f486391704dc/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU= +golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -1876,13 +1876,13 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec h1:BkDtF2Ih9xZ7le9ndzTA7KJow28VbQW3odyk/8drmuI= -golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= +golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc= golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.2.0 h1:z85xZCsEl7bi/KwbNADeBYoOP0++7W1ipu+aGnpwzRM= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -1892,8 +1892,8 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= -golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= +golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/models/activities/action.go b/models/activities/action.go index cad3263c2d..5c3419c5ec 100644 --- a/models/activities/action.go +++ b/models/activities/action.go @@ -572,7 +572,7 @@ func NotifyWatchers(actions ...*Action) error { // NotifyWatchersActions creates batch of actions for every watcher. func NotifyWatchersActions(acts []*Action) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/activities/notification.go b/models/activities/notification.go index 5748b807a0..28adc8cc4e 100644 --- a/models/activities/notification.go +++ b/models/activities/notification.go @@ -142,7 +142,7 @@ func CountNotifications(opts *FindNotificationOptions) (int64, error) { // CreateRepoTransferNotification creates notification for the user a repository was transferred to func CreateRepoTransferNotification(doer, newOwner *user_model.User, repo *repo_model.Repository) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -185,7 +185,7 @@ func CreateRepoTransferNotification(doer, newOwner *user_model.User, repo *repo_ // for each watcher, or updates it if already exists // receiverID > 0 just send to receiver, else send to all watcher func CreateOrUpdateIssueNotifications(issueID, commentID, notificationAuthorID, receiverID int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/asymkey/gpg_key.go b/models/asymkey/gpg_key.go index 83774533aa..30f5ecc949 100644 --- a/models/asymkey/gpg_key.go +++ b/models/asymkey/gpg_key.go @@ -234,7 +234,7 @@ func DeleteGPGKey(doer *user_model.User, id int64) (err error) { return ErrGPGKeyAccessDenied{doer.ID, key.ID} } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/asymkey/gpg_key_add.go b/models/asymkey/gpg_key_add.go index d01f2deb03..e7be19073a 100644 --- a/models/asymkey/gpg_key_add.go +++ b/models/asymkey/gpg_key_add.go @@ -73,7 +73,7 @@ func AddGPGKey(ownerID int64, content, token, signature string) ([]*GPGKey, erro return nil, err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } diff --git a/models/asymkey/gpg_key_verify.go b/models/asymkey/gpg_key_verify.go index 152765cc3a..4e9d04f576 100644 --- a/models/asymkey/gpg_key_verify.go +++ b/models/asymkey/gpg_key_verify.go @@ -31,7 +31,7 @@ import ( // VerifyGPGKey marks a GPG key as verified func VerifyGPGKey(ownerID int64, keyID, token, signature string) (string, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return "", err } diff --git a/models/asymkey/ssh_key.go b/models/asymkey/ssh_key.go index 7ed4ad6b3f..811f563c8d 100644 --- a/models/asymkey/ssh_key.go +++ b/models/asymkey/ssh_key.go @@ -100,7 +100,7 @@ func AddPublicKey(ownerID int64, name, content string, authSourceID int64) (*Pub return nil, err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -321,7 +321,7 @@ func PublicKeyIsExternallyManaged(id int64) (bool, error) { // deleteKeysMarkedForDeletion returns true if ssh keys needs update func deleteKeysMarkedForDeletion(keys []string) (bool, error) { // Start session - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return false, err } diff --git a/models/asymkey/ssh_key_deploy.go b/models/asymkey/ssh_key_deploy.go index d5c981da47..601e5c32e5 100644 --- a/models/asymkey/ssh_key_deploy.go +++ b/models/asymkey/ssh_key_deploy.go @@ -126,7 +126,7 @@ func AddDeployKey(repoID int64, name, content string, readOnly bool) (*DeployKey accessMode = perm.AccessModeWrite } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } diff --git a/models/asymkey/ssh_key_principals.go b/models/asymkey/ssh_key_principals.go index e0d407af35..7a769fd84d 100644 --- a/models/asymkey/ssh_key_principals.go +++ b/models/asymkey/ssh_key_principals.go @@ -26,7 +26,7 @@ import ( // AddPrincipalKey adds new principal to database and authorized_principals file. func AddPrincipalKey(ownerID int64, content string, authSourceID int64) (*PublicKey, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } diff --git a/models/asymkey/ssh_key_verify.go b/models/asymkey/ssh_key_verify.go index 710cb3af00..6931df9e88 100644 --- a/models/asymkey/ssh_key_verify.go +++ b/models/asymkey/ssh_key_verify.go @@ -15,7 +15,7 @@ import ( // VerifySSHKey marks a SSH key as verified func VerifySSHKey(ownerID int64, fingerprint, token, signature string) (string, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return "", err } diff --git a/models/auth/oauth2.go b/models/auth/oauth2.go index ccd9336f65..2ff1066288 100644 --- a/models/auth/oauth2.go +++ b/models/auth/oauth2.go @@ -201,7 +201,7 @@ type UpdateOAuth2ApplicationOptions struct { // UpdateOAuth2Application updates an oauth2 application func UpdateOAuth2Application(opts UpdateOAuth2ApplicationOptions) (*OAuth2Application, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -265,7 +265,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(id, userid int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/auth/session.go b/models/auth/session.go index 5b130c64b6..b5e4712bde 100644 --- a/models/auth/session.go +++ b/models/auth/session.go @@ -37,7 +37,7 @@ func ReadSession(key string) (*Session, error) { Key: key, } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -73,7 +73,7 @@ func DestroySession(key string) error { // RegenerateSession regenerates a session from the old id func RegenerateSession(oldKey, newKey string) (*Session, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } diff --git a/models/avatars/avatar.go b/models/avatars/avatar.go index ec3b611312..61f0ac19c3 100644 --- a/models/avatars/avatar.go +++ b/models/avatars/avatar.go @@ -97,7 +97,7 @@ func saveEmailHash(email string) string { Hash: emailHash, } // OK we're going to open a session just because I think that that might hide away any problems with postgres reporting errors - if err := db.WithTx(func(ctx context.Context) error { + if err := db.WithTx(db.DefaultContext, func(ctx context.Context) error { has, err := db.GetEngine(ctx).Where("email = ? AND hash = ?", emailHash.Email, emailHash.Hash).Get(new(EmailHash)) if has || err != nil { // Seriously we don't care about any DB problems just return the lowerEmail - we expect the transaction to fail most of the time diff --git a/models/db/context.go b/models/db/context.go index e90780e4e9..8c81469ebc 100644 --- a/models/db/context.go +++ b/models/db/context.go @@ -8,6 +8,7 @@ import ( "context" "database/sql" + "xorm.io/xorm" "xorm.io/xorm/schemas" ) @@ -86,7 +87,11 @@ type Committer interface { } // TxContext represents a transaction Context -func TxContext() (*Context, Committer, error) { +func TxContext(parentCtx context.Context) (*Context, Committer, error) { + if InTransaction(parentCtx) { + return nil, nil, ErrAlreadyInTransaction + } + sess := x.NewSession() if err := sess.Begin(); err != nil { sess.Close() @@ -97,14 +102,24 @@ func TxContext() (*Context, Committer, error) { } // WithTx represents executing database operations on a transaction -// you can optionally change the context to a parent one -func WithTx(f func(ctx context.Context) error, stdCtx ...context.Context) error { - parentCtx := DefaultContext - if len(stdCtx) != 0 && stdCtx[0] != nil { - // TODO: make sure parent context has no open session - parentCtx = stdCtx[0] +// This function will always open a new transaction, if a transaction exist in parentCtx return an error. +func WithTx(parentCtx context.Context, f func(ctx context.Context) error) error { + if InTransaction(parentCtx) { + return ErrAlreadyInTransaction } + return txWithNoCheck(parentCtx, f) +} +// AutoTx represents executing database operations on a transaction, if the transaction exist, +// this function will reuse it otherwise will create a new one and close it when finished. +func AutoTx(parentCtx context.Context, f func(ctx context.Context) error) error { + if InTransaction(parentCtx) { + return f(newContext(parentCtx, GetEngine(parentCtx), true)) + } + return txWithNoCheck(parentCtx, f) +} + +func txWithNoCheck(parentCtx context.Context, f func(ctx context.Context) error) error { sess := x.NewSession() defer sess.Close() if err := sess.Begin(); err != nil { @@ -180,3 +195,28 @@ func EstimateCount(ctx context.Context, bean interface{}) (int64, error) { } return rows, err } + +// InTransaction returns true if the engine is in a transaction otherwise return false +func InTransaction(ctx context.Context) bool { + var e Engine + if engined, ok := ctx.(Engined); ok { + e = engined.Engine() + } else { + enginedInterface := ctx.Value(enginedContextKey) + if enginedInterface != nil { + e = enginedInterface.(Engined).Engine() + } + } + if e == nil { + return false + } + + switch t := e.(type) { + case *xorm.Engine: + return false + case *xorm.Session: + return t.IsInTx() + default: + return false + } +} diff --git a/models/db/context_test.go b/models/db/context_test.go new file mode 100644 index 0000000000..f00e7834ed --- /dev/null +++ b/models/db/context_test.go @@ -0,0 +1,33 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package db_test + +import ( + "context" + "testing" + + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" + + "github.com/stretchr/testify/assert" +) + +func TestInTransaction(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + assert.False(t, db.InTransaction(db.DefaultContext)) + assert.NoError(t, db.WithTx(db.DefaultContext, func(ctx context.Context) error { + assert.True(t, db.InTransaction(ctx)) + return nil + })) + + ctx, committer, err := db.TxContext(db.DefaultContext) + assert.NoError(t, err) + defer committer.Close() + assert.True(t, db.InTransaction(ctx)) + assert.Error(t, db.WithTx(ctx, func(ctx context.Context) error { + assert.True(t, db.InTransaction(ctx)) + return nil + })) +} diff --git a/models/db/error.go b/models/db/error.go index 9577fa55db..85cba5c4e9 100644 --- a/models/db/error.go +++ b/models/db/error.go @@ -5,11 +5,14 @@ package db import ( + "errors" "fmt" "code.gitea.io/gitea/modules/util" ) +var ErrAlreadyInTransaction = errors.New("database connection has already been in a transaction") + // ErrCancelled represents an error due to context cancellation type ErrCancelled struct { Message string diff --git a/models/db/index_test.go b/models/db/index_test.go index 1ea30e2b60..cf777b9d38 100644 --- a/models/db/index_test.go +++ b/models/db/index_test.go @@ -59,7 +59,7 @@ func TestSyncMaxResourceIndex(t *testing.T) { assert.EqualValues(t, 62, maxIndex) // commit transaction - err = db.WithTx(func(ctx context.Context) error { + err = db.WithTx(db.DefaultContext, func(ctx context.Context) error { err = db.SyncMaxResourceIndex(ctx, "test_index", 10, 73) assert.NoError(t, err) maxIndex, err = getCurrentResourceIndex(ctx, "test_index", 10) @@ -73,7 +73,7 @@ func TestSyncMaxResourceIndex(t *testing.T) { assert.EqualValues(t, 73, maxIndex) // rollback transaction - err = db.WithTx(func(ctx context.Context) error { + err = db.WithTx(db.DefaultContext, func(ctx context.Context) error { err = db.SyncMaxResourceIndex(ctx, "test_index", 10, 84) maxIndex, err = getCurrentResourceIndex(ctx, "test_index", 10) assert.NoError(t, err) @@ -102,7 +102,7 @@ func TestGetNextResourceIndex(t *testing.T) { assert.EqualValues(t, 2, maxIndex) // commit transaction - err = db.WithTx(func(ctx context.Context) error { + err = db.WithTx(db.DefaultContext, func(ctx context.Context) error { maxIndex, err = db.GetNextResourceIndex(ctx, "test_index", 20) assert.NoError(t, err) assert.EqualValues(t, 3, maxIndex) @@ -114,7 +114,7 @@ func TestGetNextResourceIndex(t *testing.T) { assert.EqualValues(t, 3, maxIndex) // rollback transaction - err = db.WithTx(func(ctx context.Context) error { + err = db.WithTx(db.DefaultContext, func(ctx context.Context) error { maxIndex, err = db.GetNextResourceIndex(ctx, "test_index", 20) assert.NoError(t, err) assert.EqualValues(t, 4, maxIndex) diff --git a/models/git/branches.go b/models/git/branches.go index b17d762dbe..328f6f0f0d 100644 --- a/models/git/branches.go +++ b/models/git/branches.go @@ -544,7 +544,7 @@ func FindRenamedBranch(repoID int64, from string) (branch *RenamedBranch, exist // RenameBranch rename a branch func RenameBranch(repo *repo_model.Repository, from, to string, gitAction func(isDefault bool) error) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/git/branches_test.go b/models/git/branches_test.go index 58c4ad027b..754c5a4e66 100644 --- a/models/git/branches_test.go +++ b/models/git/branches_test.go @@ -102,7 +102,7 @@ func TestRenameBranch(t *testing.T) { repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) _isDefault := false - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) defer committer.Close() assert.NoError(t, err) assert.NoError(t, git_model.UpdateProtectBranch(ctx, repo1, &git_model.ProtectedBranch{ diff --git a/models/git/commit_status.go b/models/git/commit_status.go index 620baa036c..df6f5ae7ea 100644 --- a/models/git/commit_status.go +++ b/models/git/commit_status.go @@ -94,7 +94,7 @@ func GetNextCommitStatusIndex(repoID int64, sha string) (int64, error) { // getNextCommitStatusIndex return the next index func getNextCommitStatusIndex(repoID int64, sha string) (int64, error) { - ctx, commiter, err := db.TxContext() + ctx, commiter, err := db.TxContext(db.DefaultContext) if err != nil { return 0, err } @@ -297,7 +297,7 @@ func NewCommitStatus(opts NewCommitStatusOptions) error { return fmt.Errorf("generate commit status index failed: %w", err) } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) 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) } diff --git a/models/git/lfs.go b/models/git/lfs.go index 58042edfdb..87e07d7a5e 100644 --- a/models/git/lfs.go +++ b/models/git/lfs.go @@ -137,7 +137,7 @@ var ErrLFSObjectNotExist = db.ErrNotExist{Resource: "LFS Meta object"} func NewLFSMetaObject(m *LFSMetaObject) (*LFSMetaObject, error) { var err error - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -185,7 +185,7 @@ func RemoveLFSMetaObjectByOid(repoID int64, oid string) (int64, error) { return 0, ErrLFSObjectNotExist } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return 0, err } @@ -235,14 +235,14 @@ func LFSObjectAccessible(user *user_model.User, oid string) (bool, error) { return count > 0, err } -// LFSObjectIsAssociated checks if a provided Oid is associated -func LFSObjectIsAssociated(oid string) (bool, error) { - return db.GetEngine(db.DefaultContext).Exist(&LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}}) +// ExistsLFSObject checks if a provided Oid exists within the DB +func ExistsLFSObject(ctx context.Context, oid string) (bool, error) { + return db.GetEngine(ctx).Exist(&LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}}) } // LFSAutoAssociate auto associates accessible LFSMetaObjects func LFSAutoAssociate(metas []*LFSMetaObject, user *user_model.User, repoID int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/git/lfs_lock.go b/models/git/lfs_lock.go index 016db899a9..c5a8cc245e 100644 --- a/models/git/lfs_lock.go +++ b/models/git/lfs_lock.go @@ -44,7 +44,7 @@ func cleanPath(p string) string { // CreateLFSLock creates a new lock. func CreateLFSLock(repo *repo_model.Repository, lock *LFSLock) (*LFSLock, error) { - dbCtx, committer, err := db.TxContext() + dbCtx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -137,7 +137,7 @@ func CountLFSLockByRepoID(repoID int64) (int64, error) { // DeleteLFSLockByID deletes a lock by given ID. func DeleteLFSLockByID(id int64, repo *repo_model.Repository, u *user_model.User, force bool) (*LFSLock, error) { - dbCtx, committer, err := db.TxContext() + dbCtx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } diff --git a/models/issues/assignees.go b/models/issues/assignees.go index d960d5ebaf..ce497b116d 100644 --- a/models/issues/assignees.go +++ b/models/issues/assignees.go @@ -64,7 +64,7 @@ func IsUserAssignedToIssue(ctx context.Context, issue *Issue, user *user_model.U // ToggleIssueAssignee changes a user between assigned and not assigned for this issue, and make issue comment for it. func ToggleIssueAssignee(issue *Issue, doer *user_model.User, assigneeID int64) (removed bool, comment *Comment, err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return false, nil, err } diff --git a/models/issues/comment.go b/models/issues/comment.go index 6877991a93..d71c675d23 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -565,7 +565,7 @@ func (c *Comment) LoadAttachments() error { // UpdateAttachments update attachments by UUIDs for the comment func (c *Comment) UpdateAttachments(uuids []string) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -1003,7 +1003,7 @@ type CreateCommentOptions struct { // CreateComment creates comment of issue or commit. func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -1135,7 +1135,7 @@ func CountComments(opts *FindCommentsOptions) (int64, error) { // UpdateComment updates information of comment. func UpdateComment(c *Comment, doer *user_model.User) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/dependency.go b/models/issues/dependency.go index 4754ed0f5f..a02280d8a6 100644 --- a/models/issues/dependency.go +++ b/models/issues/dependency.go @@ -129,7 +129,7 @@ const ( // CreateIssueDependency creates a new dependency for an issue func CreateIssueDependency(user *user_model.User, issue, dep *Issue) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -170,7 +170,7 @@ func CreateIssueDependency(user *user_model.User, issue, dep *Issue) error { // RemoveIssueDependency removes a dependency from an issue func RemoveIssueDependency(user *user_model.User, issue, dep *Issue, depType DependencyType) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/issue.go b/models/issues/issue.go index ca48f425f2..c2f7cb6578 100644 --- a/models/issues/issue.go +++ b/models/issues/issue.go @@ -540,7 +540,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(issue *Issue, doer *user_model.User) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -588,7 +588,7 @@ func (ts labelSorter) Swap(i, j int) { // ReplaceIssueLabels removes all current labels and add new labels to the issue. // Triggers appropriate WebHooks, if any. func ReplaceIssueLabels(issue *Issue, labels []*Label, doer *user_model.User) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -760,7 +760,7 @@ func ChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.User, // ChangeIssueTitle changes the title of this issue, as the given user. func ChangeIssueTitle(issue *Issue, doer *user_model.User, oldTitle string) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -794,7 +794,7 @@ func ChangeIssueTitle(issue *Issue, doer *user_model.User, oldTitle string) (err // ChangeIssueRef changes the branch of this issue, as the given user. func ChangeIssueRef(issue *Issue, doer *user_model.User, oldRef string) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -844,7 +844,7 @@ func AddDeletePRBranchComment(ctx context.Context, doer *user_model.User, repo * // UpdateIssueAttachments update attachments by UUIDs for the issue func UpdateIssueAttachments(issueID int64, uuids []string) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -864,7 +864,7 @@ func UpdateIssueAttachments(issueID int64, uuids []string) (err error) { // ChangeIssueContent changes issue content, as the given user. func ChangeIssueContent(issue *Issue, doer *user_model.User, content string) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -1069,7 +1069,7 @@ func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssue // NewIssue creates new issue with labels for repository. func NewIssue(repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -1986,7 +1986,7 @@ func SearchIssueIDsByKeyword(ctx context.Context, kw string, repoIDs []int64, li // If the issue status is changed a statusChangeComment is returned // similarly if the title is changed the titleChanged bool is set to true func UpdateIssueByAPI(issue *Issue, doer *user_model.User) (statusChangeComment *Comment, titleChanged bool, err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, false, err } @@ -2044,7 +2044,7 @@ func UpdateIssueDeadline(issue *Issue, deadlineUnix timeutil.TimeStamp, doer *us if issue.DeadlineUnix == deadlineUnix { return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -2436,7 +2436,7 @@ func CountOrphanedIssues() (int64, error) { // DeleteOrphanedIssues delete issues without a repo func DeleteOrphanedIssues() error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/issue_index.go b/models/issues/issue_index.go index f4acc5aa1b..1fa79f494a 100644 --- a/models/issues/issue_index.go +++ b/models/issues/issue_index.go @@ -9,7 +9,7 @@ import "code.gitea.io/gitea/models/db" // 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(repoID int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/issue_lock.go b/models/issues/issue_lock.go index 7b52429ef7..608b5fd140 100644 --- a/models/issues/issue_lock.go +++ b/models/issues/issue_lock.go @@ -40,7 +40,7 @@ func updateIssueLock(opts *IssueLockOptions, lock bool) error { commentType = CommentTypeUnlock } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/issue_project.go b/models/issues/issue_project.go index 8299087c5b..0c59f4e82b 100644 --- a/models/issues/issue_project.go +++ b/models/issues/issue_project.go @@ -110,7 +110,7 @@ func LoadIssuesFromBoardList(bs project_model.BoardList) (map[int64]IssueList, e // ChangeProjectAssign changes the project associated with an issue func ChangeProjectAssign(issue *Issue, doer *user_model.User, newProjectID int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -166,7 +166,7 @@ func addUpdateIssueProject(ctx context.Context, issue *Issue, doer *user_model.U // MoveIssueAcrossProjectBoards move a card from one board to another func MoveIssueAcrossProjectBoards(issue *Issue, board *project_model.Board) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/issue_xref_test.go b/models/issues/issue_xref_test.go index 0f72fc7ca6..d6d88db894 100644 --- a/models/issues/issue_xref_test.go +++ b/models/issues/issue_xref_test.go @@ -131,7 +131,7 @@ func testCreateIssue(t *testing.T, repo, doer int64, title, content string, ispu r := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repo}) d := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: doer}) - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) assert.NoError(t, err) defer committer.Close() @@ -174,7 +174,7 @@ func testCreateComment(t *testing.T, repo, doer, issue int64, content string) *i i := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: issue}) c := &issues_model.Comment{Type: issues_model.CommentTypeComment, PosterID: doer, Poster: d, IssueID: issue, Issue: i, Content: content} - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) assert.NoError(t, err) defer committer.Close() err = db.Insert(ctx, c) diff --git a/models/issues/label.go b/models/issues/label.go index bbdc99e265..0b0d1419b1 100644 --- a/models/issues/label.go +++ b/models/issues/label.go @@ -232,7 +232,7 @@ func NewLabel(ctx context.Context, label *Label) error { // NewLabels creates new labels func NewLabels(labels ...*Label) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -267,7 +267,7 @@ func DeleteLabel(id, labelID int64) error { return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -627,7 +627,7 @@ func NewIssueLabel(issue *Issue, label *Label, doer *user_model.User) (err error return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -676,7 +676,7 @@ func newIssueLabels(ctx context.Context, issue *Issue, labels []*Label, doer *us // NewIssueLabels creates a list of issue-label relations. func NewIssueLabels(issue *Issue, labels []*Label, doer *user_model.User) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/label_test.go b/models/issues/label_test.go index 9ad6fd427b..5e6cc9a2a0 100644 --- a/models/issues/label_test.go +++ b/models/issues/label_test.go @@ -370,7 +370,7 @@ func TestDeleteIssueLabel(t *testing.T) { } } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) defer committer.Close() assert.NoError(t, err) assert.NoError(t, issues_model.DeleteIssueLabel(ctx, issue, label, doer)) diff --git a/models/issues/milestone.go b/models/issues/milestone.go index 3ccade7411..9d1778f732 100644 --- a/models/issues/milestone.go +++ b/models/issues/milestone.go @@ -111,7 +111,7 @@ func (m *Milestone) State() api.StateType { // NewMilestone creates new milestone of repository. func NewMilestone(m *Milestone) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -161,7 +161,7 @@ func GetMilestoneByRepoIDANDName(repoID int64, name string) (*Milestone, error) // UpdateMilestone updates information of given milestone. func UpdateMilestone(m *Milestone, oldIsClosed bool) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -219,7 +219,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(repoID, milestoneID int64, isClosed bool) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -246,7 +246,7 @@ func ChangeMilestoneStatusByRepoIDAndID(repoID, milestoneID int64, isClosed bool // ChangeMilestoneStatus changes the milestone open/closed status. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -290,7 +290,7 @@ func DeleteMilestoneByRepoID(repoID, id int64) error { return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/pull.go b/models/issues/pull.go index f03cabc3c8..e906407d31 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -368,7 +368,7 @@ func (pr *PullRequest) getReviewedByLines(writer io.Writer) error { return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -498,7 +498,7 @@ func (pr *PullRequest) SetMerged(ctx context.Context) (bool, error) { // NewPullRequest creates new pull request with labels for repository. func NewPullRequest(outerCtx context.Context, repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(outerCtx) if err != nil { return err } diff --git a/models/issues/reaction.go b/models/issues/reaction.go index c7503c23a2..5cb63795d1 100644 --- a/models/issues/reaction.go +++ b/models/issues/reaction.go @@ -224,7 +224,7 @@ func CreateReaction(opts *ReactionOptions) (*Reaction, error) { return nil, ErrForbiddenIssueReaction{opts.Type} } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } diff --git a/models/issues/review.go b/models/issues/review.go index 3d2fceda2d..f66c70c1fc 100644 --- a/models/issues/review.go +++ b/models/issues/review.go @@ -374,7 +374,7 @@ func IsContentEmptyErr(err error) bool { // SubmitReview creates a review out of the existing pending review or creates a new one if no pending review exist func SubmitReview(doer *user_model.User, issue *Issue, reviewType ReviewType, content, commitID string, stale bool, attachmentUUIDs []string) (*Review, *Comment, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, nil, err } @@ -622,7 +622,7 @@ func DismissReview(review *Review, isDismiss bool) (err error) { // InsertReviews inserts review and review comments func InsertReviews(reviews []*Review) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -664,7 +664,7 @@ func InsertReviews(reviews []*Review) error { // AddReviewRequest add a review request from one reviewer func AddReviewRequest(issue *Issue, reviewer, doer *user_model.User) (*Comment, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -719,7 +719,7 @@ func AddReviewRequest(issue *Issue, reviewer, doer *user_model.User) (*Comment, // RemoveReviewRequest remove a review request from one reviewer func RemoveReviewRequest(issue *Issue, reviewer, doer *user_model.User) (*Comment, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -772,7 +772,7 @@ func RemoveReviewRequest(issue *Issue, reviewer, doer *user_model.User) (*Commen // AddTeamReviewRequest add a review request from one team func AddTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *user_model.User) (*Comment, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -831,7 +831,7 @@ func AddTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *user_ // RemoveTeamReviewRequest remove a review request from one team func RemoveTeamReviewRequest(issue *Issue, reviewer *organization.Team, doer *user_model.User) (*Comment, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -949,7 +949,7 @@ func CanMarkConversation(issue *Issue, doer *user_model.User) (permResult bool, // DeleteReview delete a review and it's code comments func DeleteReview(r *Review) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/stopwatch.go b/models/issues/stopwatch.go index a87fbfafa2..d1d985b746 100644 --- a/models/issues/stopwatch.go +++ b/models/issues/stopwatch.go @@ -261,7 +261,7 @@ func CreateIssueStopwatch(ctx context.Context, user *user_model.User, issue *Iss // CancelStopwatch removes the given stopwatch and logs it into issue's timeline. func CancelStopwatch(user *user_model.User, issue *Issue) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/issues/tracked_time.go b/models/issues/tracked_time.go index ca21eb5149..e1da0f32b0 100644 --- a/models/issues/tracked_time.go +++ b/models/issues/tracked_time.go @@ -149,7 +149,7 @@ func GetTrackedSeconds(ctx context.Context, opts FindTrackedTimesOptions) (track // AddTime will add the given time (in seconds) to the issue func AddTime(user *user_model.User, issue *Issue, amount int64, created time.Time) (*TrackedTime, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -220,7 +220,7 @@ func TotalTimes(options *FindTrackedTimesOptions) (map[*user_model.User]string, // DeleteIssueUserTimes deletes times for issue func DeleteIssueUserTimes(issue *Issue, user *user_model.User) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -257,7 +257,7 @@ func DeleteIssueUserTimes(issue *Issue, user *user_model.User) error { // DeleteTime delete a specific Time func DeleteTime(t *TrackedTime) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/migrate.go b/models/migrate.go index d842fb967b..2a79640b07 100644 --- a/models/migrate.go +++ b/models/migrate.go @@ -20,7 +20,7 @@ func InsertMilestones(ms ...*issues_model.Milestone) (err error) { return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -42,7 +42,7 @@ func InsertMilestones(ms ...*issues_model.Milestone) (err error) { // InsertIssues insert issues to database func InsertIssues(issues ...*issues_model.Issue) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -105,7 +105,7 @@ func InsertIssueComments(comments []*issues_model.Comment) error { issueIDs.Add(comment.IssueID) } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -137,7 +137,7 @@ func InsertIssueComments(comments []*issues_model.Comment) error { // InsertPullRequests inserted pull requests func InsertPullRequests(prs ...*issues_model.PullRequest) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -157,7 +157,7 @@ func InsertPullRequests(prs ...*issues_model.PullRequest) error { // InsertReleases migrates release func InsertReleases(rels ...*repo_model.Release) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/org.go b/models/org.go index 150c41f55d..b56c80522b 100644 --- a/models/org.go +++ b/models/org.go @@ -99,7 +99,7 @@ func removeOrgUser(ctx context.Context, orgID, userID int64) error { // RemoveOrgUser removes user from given organization. func RemoveOrgUser(orgID, userID int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/org_team.go b/models/org_team.go index 290b1c8b6a..792829563a 100644 --- a/models/org_team.go +++ b/models/org_team.go @@ -76,7 +76,7 @@ func addAllRepositories(ctx context.Context, t *organization.Team) error { // AddAllRepositories adds all repositories to the team func AddAllRepositories(t *organization.Team) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -95,7 +95,7 @@ func RemoveAllRepositories(t *organization.Team) (err error) { return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -219,7 +219,7 @@ func RemoveRepository(t *organization.Team, repoID int64) error { return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -263,7 +263,7 @@ func NewTeam(t *organization.Team) (err error) { return organization.ErrTeamAlreadyExist{OrgID: t.OrgID, Name: t.LowerName} } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -308,7 +308,7 @@ func UpdateTeam(t *organization.Team, authChanged, includeAllChanged bool) (err t.Description = t.Description[:255] } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -375,7 +375,7 @@ func UpdateTeam(t *organization.Team, authChanged, includeAllChanged bool) (err // DeleteTeam deletes given team. // It's caller's responsibility to assign organization ID. func DeleteTeam(t *organization.Team) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -460,7 +460,7 @@ func AddTeamMember(team *organization.Team, userID int64) error { return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -598,7 +598,7 @@ func removeTeamMember(ctx context.Context, team *organization.Team, userID int64 // RemoveTeamMember removes member from given team of given organization. func RemoveTeamMember(team *organization.Team, userID int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/organization/org.go b/models/organization/org.go index 993ca3f10d..af9c1f307c 100644 --- a/models/organization/org.go +++ b/models/organization/org.go @@ -277,7 +277,7 @@ func CreateOrganization(org *Organization, owner *user_model.User) (err error) { org.NumMembers = 1 org.Type = user_model.UserTypeOrganization - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -564,7 +564,7 @@ func AddOrgUser(orgID, uid int64) error { return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/organization/team_unit.go b/models/organization/team_unit.go index a712ddb2eb..4ad1ae95dc 100644 --- a/models/organization/team_unit.go +++ b/models/organization/team_unit.go @@ -32,7 +32,7 @@ func getUnitsByTeamID(ctx context.Context, teamID int64) (units []*TeamUnit, err // UpdateTeamUnits updates a teams's units func UpdateTeamUnits(team *Team, units []TeamUnit) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/packages/package_blob.go b/models/packages/package_blob.go index 8c701d4285..fcb71a96ec 100644 --- a/models/packages/package_blob.go +++ b/models/packages/package_blob.go @@ -62,6 +62,13 @@ func GetBlobByID(ctx context.Context, blobID int64) (*PackageBlob, error) { return pb, nil } +// ExistPackageBlobWithSHA returns if a package blob exists with the provided sha +func ExistPackageBlobWithSHA(ctx context.Context, blobSha256 string) (bool, error) { + return db.GetEngine(ctx).Exist(&PackageBlob{ + HashSHA256: blobSha256, + }) +} + // FindExpiredUnreferencedBlobs gets all blobs without associated files older than the specific duration func FindExpiredUnreferencedBlobs(ctx context.Context, olderThan time.Duration) ([]*PackageBlob, error) { pbs := make([]*PackageBlob, 0, 10) diff --git a/models/project/board.go b/models/project/board.go index be7119ee4d..8428fca722 100644 --- a/models/project/board.go +++ b/models/project/board.go @@ -133,7 +133,7 @@ func NewBoard(board *Board) error { // DeleteBoardByID removes all issues references to the project board. func DeleteBoardByID(boardID int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/project/issue.go b/models/project/issue.go index 59af7063a5..da48d4731e 100644 --- a/models/project/issue.go +++ b/models/project/issue.go @@ -78,7 +78,7 @@ func (p *Project) NumOpenIssues() int { // MoveIssuesOnProjectBoard moves or keeps issues in a column and sorts them inside that column func MoveIssuesOnProjectBoard(board *Board, sortedIssueIDs map[int64]int64) error { - return db.WithTx(func(ctx context.Context) error { + return db.WithTx(db.DefaultContext, func(ctx context.Context) error { sess := db.GetEngine(ctx) issueIDs := make([]int64, 0, len(sortedIssueIDs)) diff --git a/models/project/project.go b/models/project/project.go index ccdf5342d4..5dee241dcf 100644 --- a/models/project/project.go +++ b/models/project/project.go @@ -180,7 +180,7 @@ func NewProject(p *Project) error { return errors.New("project type is not valid") } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -248,7 +248,7 @@ func updateRepositoryProjectCount(ctx context.Context, repoID int64) error { // ChangeProjectStatusByRepoIDAndID toggles a project between opened and closed func ChangeProjectStatusByRepoIDAndID(repoID, projectID int64, isClosed bool) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -272,7 +272,7 @@ func ChangeProjectStatusByRepoIDAndID(repoID, projectID int64, isClosed bool) er // ChangeProjectStatus toggle a project between opened and closed func ChangeProjectStatus(p *Project, isClosed bool) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -301,7 +301,7 @@ func changeProjectStatus(ctx context.Context, p *Project, isClosed bool) error { // DeleteProjectByID deletes a project from a repository. func DeleteProjectByID(id int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo.go b/models/repo.go index 569dafee5f..2510f0e6bc 100644 --- a/models/repo.go +++ b/models/repo.go @@ -45,7 +45,7 @@ func Init() error { // DeleteRepository deletes a repository for a user or organization. // make sure if you call this func to close open sessions (sqlite will otherwise get a deadlock) func DeleteRepository(doer *user_model.User, uid, repoID int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -569,7 +569,7 @@ func UpdateRepoStats(ctx context.Context, id int64) error { } func updateUserStarNumbers(users []user_model.User) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo/archiver.go b/models/repo/archiver.go index 003911943f..84358ce0dc 100644 --- a/models/repo/archiver.go +++ b/models/repo/archiver.go @@ -7,11 +7,14 @@ package repo import ( "context" "fmt" + "strconv" + "strings" "time" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" "xorm.io/builder" ) @@ -44,6 +47,28 @@ func (archiver *RepoArchiver) RelativePath() string { return fmt.Sprintf("%d/%s/%s.%s", archiver.RepoID, archiver.CommitID[:2], archiver.CommitID, archiver.Type.String()) } +// repoArchiverForRelativePath takes a relativePath created from (archiver *RepoArchiver) RelativePath() and creates a shell repoArchiver struct representing it +func repoArchiverForRelativePath(relativePath string) (*RepoArchiver, error) { + parts := strings.SplitN(relativePath, "/", 3) + if len(parts) != 3 { + return nil, util.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument} + } + repoID, err := strconv.ParseInt(parts[0], 10, 64) + if err != nil { + return nil, util.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument} + } + nameExts := strings.SplitN(parts[2], ".", 2) + if len(nameExts) != 2 { + return nil, util.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument} + } + + return &RepoArchiver{ + RepoID: repoID, + CommitID: parts[1] + nameExts[0], + Type: git.ToArchiveType(nameExts[1]), + }, nil +} + var delRepoArchiver = new(RepoArchiver) // DeleteRepoArchiver delete archiver @@ -65,6 +90,17 @@ func GetRepoArchiver(ctx context.Context, repoID int64, tp git.ArchiveType, comm return nil, nil } +// ExistsRepoArchiverWithStoragePath checks if there is a RepoArchiver for a given storage path +func ExistsRepoArchiverWithStoragePath(ctx context.Context, storagePath string) (bool, error) { + // We need to invert the path provided func (archiver *RepoArchiver) RelativePath() above + archiver, err := repoArchiverForRelativePath(storagePath) + if err != nil { + return false, err + } + + return db.GetEngine(ctx).Exist(archiver) +} + // AddRepoArchiver adds an archiver func AddRepoArchiver(ctx context.Context, archiver *RepoArchiver) error { _, err := db.GetEngine(ctx).Insert(archiver) diff --git a/models/repo/attachment.go b/models/repo/attachment.go index 180d7730ba..df7528df09 100644 --- a/models/repo/attachment.go +++ b/models/repo/attachment.go @@ -122,9 +122,9 @@ func GetAttachmentsByUUIDs(ctx context.Context, uuids []string) ([]*Attachment, return attachments, db.GetEngine(ctx).In("uuid", uuids).Find(&attachments) } -// ExistAttachmentsByUUID returns true if attachment is exist by given UUID -func ExistAttachmentsByUUID(uuid string) (bool, error) { - return db.GetEngine(db.DefaultContext).Where("`uuid`=?", uuid).Exist(new(Attachment)) +// ExistAttachmentsByUUID returns true if attachment exists with the given UUID +func ExistAttachmentsByUUID(ctx context.Context, uuid string) (bool, error) { + return db.GetEngine(ctx).Where("`uuid`=?", uuid).Exist(new(Attachment)) } // GetAttachmentsByIssueID returns all attachments of an issue. diff --git a/models/repo/avatar.go b/models/repo/avatar.go index 1bc37598fe..84b9f5ac21 100644 --- a/models/repo/avatar.go +++ b/models/repo/avatar.go @@ -24,6 +24,13 @@ func (repo *Repository) CustomAvatarRelativePath() string { return repo.Avatar } +// ExistsWithAvatarAtStoragePath returns true if there is a user with this Avatar +func ExistsWithAvatarAtStoragePath(ctx context.Context, storagePath string) (bool, error) { + // See func (repo *Repository) CustomAvatarRelativePath() + // repo.Avatar is used directly as the storage path - therefore we can check for existence directly using the path + return db.GetEngine(ctx).Where("`avatar`=?", storagePath).Exist(new(Repository)) +} + // RelAvatarLink returns a relative link to the repository's avatar. func (repo *Repository) RelAvatarLink() string { return repo.relAvatarLink(db.DefaultContext) diff --git a/models/repo/collaboration.go b/models/repo/collaboration.go index 0aaa749210..c558259e46 100644 --- a/models/repo/collaboration.go +++ b/models/repo/collaboration.go @@ -138,7 +138,7 @@ func ChangeCollaborationAccessModeCtx(ctx context.Context, repo *Repository, uid // ChangeCollaborationAccessMode sets new access mode for the collaboration. func ChangeCollaborationAccessMode(repo *Repository, uid int64, mode perm.AccessMode) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo/language_stats.go b/models/repo/language_stats.go index b047046aeb..5c4cecdca1 100644 --- a/models/repo/language_stats.go +++ b/models/repo/language_stats.go @@ -110,7 +110,7 @@ func GetTopLanguageStats(repo *Repository, limit int) (LanguageStatList, error) // UpdateLanguageStats updates the language statistics for repository func UpdateLanguageStats(repo *Repository, commitID string, stats map[string]int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -182,7 +182,7 @@ func UpdateLanguageStats(repo *Repository, commitID string, stats map[string]int // CopyLanguageStat Copy originalRepo language stat information to destRepo (use for forked repo) func CopyLanguageStat(originalRepo, destRepo *Repository) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo/repo_unit.go b/models/repo/repo_unit.go index dd85ca9186..c21c1e7a79 100644 --- a/models/repo/repo_unit.go +++ b/models/repo/repo_unit.go @@ -241,7 +241,7 @@ func UpdateRepoUnit(unit *RepoUnit) error { // UpdateRepositoryUnits updates a repository's units func UpdateRepositoryUnits(repo *Repository, units []RepoUnit, deleteUnitTypes []unit.Type) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo/star.go b/models/repo/star.go index 113b56f595..c7459b3655 100644 --- a/models/repo/star.go +++ b/models/repo/star.go @@ -26,7 +26,7 @@ func init() { // StarRepo or unstar repository. func StarRepo(userID, repoID int64, star bool) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo/topic.go b/models/repo/topic.go index 33bbb05af9..db41c85a33 100644 --- a/models/repo/topic.go +++ b/models/repo/topic.go @@ -231,7 +231,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(repoID int64, topicName string) (*Topic, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -293,7 +293,7 @@ func SaveTopics(repoID int64, topicNames ...string) error { return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo/update.go b/models/repo/update.go index cc21deb0bc..39c89a7c7c 100644 --- a/models/repo/update.go +++ b/models/repo/update.go @@ -21,7 +21,7 @@ func UpdateRepositoryOwnerNames(ownerID int64, ownerName string) error { if ownerID == 0 { return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -172,7 +172,7 @@ func ChangeRepositoryName(doer *user_model.User, repo *Repository, newRepoName s } } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo/upload.go b/models/repo/upload.go index e115c8e50e..13b6055701 100644 --- a/models/repo/upload.go +++ b/models/repo/upload.go @@ -121,7 +121,7 @@ func DeleteUploads(uploads ...*Upload) (err error) { return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo_collaboration.go b/models/repo_collaboration.go index 58d6b0f488..684d0e63a0 100644 --- a/models/repo_collaboration.go +++ b/models/repo_collaboration.go @@ -25,7 +25,7 @@ func DeleteCollaboration(repo *repo_model.Repository, uid int64) (err error) { UserID: uid, } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/repo_transfer.go b/models/repo_transfer.go index d6a3985fe5..067ab4122f 100644 --- a/models/repo_transfer.go +++ b/models/repo_transfer.go @@ -124,7 +124,7 @@ func deleteRepositoryTransfer(ctx context.Context, repoID int64) error { // CancelRepositoryTransfer marks the repository as ready and remove pending transfer entry, // thus cancel the transfer process. func CancelRepositoryTransfer(repo *repo_model.Repository) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -156,7 +156,7 @@ func TestRepositoryReadyForTransfer(status repo_model.RepositoryStatus) error { // CreatePendingRepositoryTransfer transfer a repo from one owner to a new one. // it marks the repository transfer as "pending" func CreatePendingRepositoryTransfer(doer, newOwner *user_model.User, repoID int64, teams []*organization.Team) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -243,7 +243,7 @@ func TransferOwnership(doer *user_model.User, newOwnerName string, repo *repo_mo } }() - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/system/appstate.go b/models/system/appstate.go index c11a2512ab..bc18c5181d 100644 --- a/models/system/appstate.go +++ b/models/system/appstate.go @@ -25,7 +25,7 @@ func init() { // SaveAppStateContent saves the app state item to database func SaveAppStateContent(key, content string) error { - return db.WithTx(func(ctx context.Context) error { + return db.WithTx(db.DefaultContext, func(ctx context.Context) error { eng := db.GetEngine(ctx) // try to update existing row res, err := eng.Exec("UPDATE app_state SET revision=revision+1, content=? WHERE id=?", content, key) diff --git a/models/system/setting.go b/models/system/setting.go index b4011b1b3e..22119ee5e6 100644 --- a/models/system/setting.go +++ b/models/system/setting.go @@ -196,7 +196,7 @@ func SetSetting(setting *Setting) error { } func upsertSettingValue(key, value string, version int) error { - return db.WithTx(func(ctx context.Context) error { + return db.WithTx(db.DefaultContext, func(ctx context.Context) error { e := db.GetEngine(ctx) // here we use a general method to do a safe upsert for different databases (and most transaction levels) diff --git a/models/user/avatar.go b/models/user/avatar.go index 102206f3a2..f523766746 100644 --- a/models/user/avatar.go +++ b/models/user/avatar.go @@ -111,3 +111,10 @@ func (u *User) IsUploadAvatarChanged(data []byte) bool { avatarID := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%d-%x", u.ID, md5.Sum(data))))) return u.Avatar != avatarID } + +// ExistsWithAvatarAtStoragePath returns true if there is a user with this Avatar +func ExistsWithAvatarAtStoragePath(ctx context.Context, storagePath string) (bool, error) { + // See func (u *User) CustomAvatarRelativePath() + // u.Avatar is used directly as the storage path - therefore we can check for existence directly using the path + return db.GetEngine(ctx).Where("`avatar`=?", storagePath).Exist(new(User)) +} diff --git a/models/user/email_address.go b/models/user/email_address.go index d6279b6639..b6d372f04d 100644 --- a/models/user/email_address.go +++ b/models/user/email_address.go @@ -321,7 +321,7 @@ func DeleteInactiveEmailAddresses(ctx context.Context) error { // ActivateEmail activates the email address to given user. func ActivateEmail(email *EmailAddress) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -372,7 +372,7 @@ func MakeEmailPrimary(email *EmailAddress) error { } } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -510,7 +510,7 @@ func SearchEmails(opts *SearchEmailOptions) ([]*SearchEmailResult, int64, error) // ActivateUserEmail will change the activated state of an email address, // either primary or secondary (all in the email_address table) func ActivateUserEmail(userID int64, email string, activate bool) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/user/follow.go b/models/user/follow.go index 5f24f706d1..a02ebdb8c2 100644 --- a/models/user/follow.go +++ b/models/user/follow.go @@ -33,7 +33,7 @@ func FollowUser(userID, followID int64) (err error) { return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -59,7 +59,7 @@ func UnfollowUser(userID, followID int64) (err error) { return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/user/setting.go b/models/user/setting.go index 896f3c8da1..41de499de9 100644 --- a/models/user/setting.go +++ b/models/user/setting.go @@ -163,7 +163,7 @@ func SetUserSetting(userID int64, key, value string) error { } func upsertUserSettingValue(userID int64, key, value string) error { - return db.WithTx(func(ctx context.Context) error { + return db.WithTx(db.DefaultContext, func(ctx context.Context) error { e := db.GetEngine(ctx) // here we use a general method to do a safe upsert for different databases (and most transaction levels) diff --git a/models/user/user.go b/models/user/user.go index 84e2c4a9cc..c36fc21c77 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -695,7 +695,7 @@ func CreateUser(u *User, overwriteDefault ...*CreateUserOverwriteOptions) (err e return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -814,7 +814,7 @@ func ChangeUserName(u *User, newUserName string) (err error) { return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -957,7 +957,7 @@ func UpdateUserCols(ctx context.Context, u *User, cols ...string) error { // UpdateUserSetting updates user's settings. func UpdateUserSetting(u *User) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/models/webhook/webhook.go b/models/webhook/webhook.go index 6426b95202..1daf806c4e 100644 --- a/models/webhook/webhook.go +++ b/models/webhook/webhook.go @@ -575,7 +575,7 @@ func UpdateWebhookLastStatus(w *Webhook) error { // deleteWebhook uses argument bean as query condition, // ID must be specified and do not assign unnecessary fields. func deleteWebhook(bean *Webhook) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -610,7 +610,7 @@ func DeleteWebhookByOrgID(orgID, id int64) error { // DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0) func DeleteDefaultSystemWebhook(id int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/modules/convert/pull_review.go b/modules/convert/pull_review.go index 93ce208224..e8a543fea9 100644 --- a/modules/convert/pull_review.go +++ b/modules/convert/pull_review.go @@ -39,6 +39,7 @@ func ToPullReview(ctx context.Context, r *issues_model.Review, doer *user_model. Dismissed: r.Dismissed, CodeCommentsCount: r.GetCodeCommentsCount(), Submitted: r.CreatedUnix.AsTime(), + Updated: r.UpdatedUnix.AsTime(), HTMLURL: r.HTMLURL(), HTMLPullURL: r.Issue.HTMLURL(), } diff --git a/modules/doctor/heads.go b/modules/doctor/heads.go index 33efc27a22..b1bfd50b20 100644 --- a/modules/doctor/heads.go +++ b/modules/doctor/heads.go @@ -19,11 +19,9 @@ func synchronizeRepoHeads(ctx context.Context, logger log.Logger, autofix bool) numReposUpdated := 0 err := iterateRepositories(ctx, func(repo *repo_model.Repository) error { numRepos++ - runOpts := &git.RunOpts{Dir: repo.RepoPath()} + _, _, defaultBranchErr := git.NewCommand(ctx, "rev-parse").AddDashesAndList(repo.DefaultBranch).RunStdString(&git.RunOpts{Dir: repo.RepoPath()}) - _, _, defaultBranchErr := git.NewCommand(ctx, "rev-parse").AddDashesAndList(repo.DefaultBranch).RunStdString(runOpts) - - head, _, headErr := git.NewCommand(ctx, "symbolic-ref", "--short", "HEAD").RunStdString(runOpts) + head, _, headErr := git.NewCommand(ctx, "symbolic-ref", "--short", "HEAD").RunStdString(&git.RunOpts{Dir: repo.RepoPath()}) // what we expect: default branch is valid, and HEAD points to it if headErr == nil && defaultBranchErr == nil && head == repo.DefaultBranch { @@ -49,7 +47,7 @@ func synchronizeRepoHeads(ctx context.Context, logger log.Logger, autofix bool) } // otherwise, let's try fixing HEAD - err := git.NewCommand(ctx, "symbolic-ref").AddDashesAndList("HEAD", git.BranchPrefix+repo.DefaultBranch).Run(runOpts) + err := git.NewCommand(ctx, "symbolic-ref").AddDashesAndList("HEAD", git.BranchPrefix+repo.DefaultBranch).Run(&git.RunOpts{Dir: repo.RepoPath()}) if err != nil { logger.Warn("Failed to fix HEAD for %s/%s: %v", repo.OwnerName, repo.Name, err) return nil @@ -65,7 +63,7 @@ func synchronizeRepoHeads(ctx context.Context, logger log.Logger, autofix bool) logger.Info("Out of %d repos, HEADs for %d are now fixed and HEADS for %d are still broken", numRepos, numReposUpdated, numDefaultBranchesBroken+numHeadsBroken-numReposUpdated) } else { if numHeadsBroken == 0 && numDefaultBranchesBroken == 0 { - logger.Info("All %d repos have their HEADs in the correct state") + logger.Info("All %d repos have their HEADs in the correct state", numRepos) } else { if numHeadsBroken == 0 && numDefaultBranchesBroken != 0 { logger.Critical("Default branches are broken for %d/%d repos", numDefaultBranchesBroken, numRepos) diff --git a/modules/doctor/storage.go b/modules/doctor/storage.go index dafd989cf0..8ae9168ea6 100644 --- a/modules/doctor/storage.go +++ b/modules/doctor/storage.go @@ -6,71 +6,255 @@ package doctor import ( "context" + "errors" + "io/fs" + "strings" - repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/models/git" + "code.gitea.io/gitea/models/packages" + "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/log" + packages_module "code.gitea.io/gitea/modules/packages" "code.gitea.io/gitea/modules/storage" + "code.gitea.io/gitea/modules/util" ) -func checkAttachmentStorageFiles(logger log.Logger, autofix bool) error { - var total, garbageNum int - var deletePaths []string - if err := storage.Attachments.IterateObjects(func(p string, obj storage.Object) error { +type commonStorageCheckOptions struct { + storer storage.ObjectStorage + isOrphaned func(path string, obj storage.Object, stat fs.FileInfo) (bool, error) + name string +} + +func commonCheckStorage(ctx context.Context, logger log.Logger, autofix bool, opts *commonStorageCheckOptions) error { + totalCount, orphanedCount := 0, 0 + totalSize, orphanedSize := int64(0), int64(0) + + var pathsToDelete []string + if err := opts.storer.IterateObjects(func(p string, obj storage.Object) error { defer obj.Close() - total++ + totalCount++ stat, err := obj.Stat() if err != nil { return err } - exist, err := repo_model.ExistAttachmentsByUUID(stat.Name()) + totalSize += stat.Size() + + orphaned, err := opts.isOrphaned(p, obj, stat) if err != nil { return err } - if !exist { - garbageNum++ + if orphaned { + orphanedCount++ + orphanedSize += stat.Size() if autofix { - deletePaths = append(deletePaths, p) + pathsToDelete = append(pathsToDelete, p) } } return nil }); err != nil { - logger.Error("storage.Attachments.IterateObjects failed: %v", err) + logger.Error("Error whilst iterating %s storage: %v", opts.name, err) return err } - if garbageNum > 0 { + if orphanedCount > 0 { if autofix { var deletedNum int - for _, p := range deletePaths { - if err := storage.Attachments.Delete(p); err != nil { - log.Error("Delete attachment %s failed: %v", p, err) + for _, p := range pathsToDelete { + if err := opts.storer.Delete(p); err != nil { + log.Error("Error whilst deleting %s from %s storage: %v", p, opts.name, err) } else { deletedNum++ } } - logger.Info("%d missed information attachment detected, %d deleted.", garbageNum, deletedNum) + logger.Info("Deleted %d/%d orphaned %s(s)", deletedNum, orphanedCount, opts.name) } else { - logger.Warn("Checked %d attachment, %d missed information.", total, garbageNum) + logger.Warn("Found %d/%d (%s/%s) orphaned %s(s)", orphanedCount, totalCount, base.FileSize(orphanedSize), base.FileSize(totalSize), opts.name) } + } else { + logger.Info("Found %d (%s) %s(s)", totalCount, base.FileSize(totalSize), opts.name) } return nil } -func checkStorageFiles(ctx context.Context, logger log.Logger, autofix bool) error { - if err := storage.Init(); err != nil { - logger.Error("storage.Init failed: %v", err) - return err +type checkStorageOptions struct { + All bool + Attachments bool + LFS bool + Avatars bool + RepoAvatars bool + RepoArchives bool + Packages bool +} + +// checkStorage will return a doctor check function to check the requested storage types for "orphaned" stored object/files and optionally delete them +func checkStorage(opts *checkStorageOptions) func(ctx context.Context, logger log.Logger, autofix bool) error { + return func(ctx context.Context, logger log.Logger, autofix bool) error { + if err := storage.Init(); err != nil { + logger.Error("storage.Init failed: %v", err) + return err + } + + if opts.Attachments || opts.All { + if err := commonCheckStorage(ctx, logger, autofix, + &commonStorageCheckOptions{ + storer: storage.Attachments, + isOrphaned: func(path string, obj storage.Object, stat fs.FileInfo) (bool, error) { + exists, err := repo.ExistAttachmentsByUUID(ctx, stat.Name()) + return !exists, err + }, + name: "attachment", + }); err != nil { + return err + } + } + + if opts.LFS || opts.All { + if err := commonCheckStorage(ctx, logger, autofix, + &commonStorageCheckOptions{ + storer: storage.LFS, + isOrphaned: func(path string, obj storage.Object, stat fs.FileInfo) (bool, error) { + // The oid of an LFS stored object is the name but with all the path.Separators removed + oid := strings.ReplaceAll(path, "/", "") + exists, err := git.ExistsLFSObject(ctx, oid) + return !exists, err + }, + name: "LFS file", + }); err != nil { + return err + } + } + + if opts.Avatars || opts.All { + if err := commonCheckStorage(ctx, logger, autofix, + &commonStorageCheckOptions{ + storer: storage.Avatars, + isOrphaned: func(path string, obj storage.Object, stat fs.FileInfo) (bool, error) { + exists, err := user.ExistsWithAvatarAtStoragePath(ctx, path) + return !exists, err + }, + name: "avatar", + }); err != nil { + return err + } + } + + if opts.RepoAvatars || opts.All { + if err := commonCheckStorage(ctx, logger, autofix, + &commonStorageCheckOptions{ + storer: storage.RepoAvatars, + isOrphaned: func(path string, obj storage.Object, stat fs.FileInfo) (bool, error) { + exists, err := repo.ExistsWithAvatarAtStoragePath(ctx, path) + return !exists, err + }, + name: "repo avatar", + }); err != nil { + return err + } + } + + if opts.RepoArchives || opts.All { + if err := commonCheckStorage(ctx, logger, autofix, + &commonStorageCheckOptions{ + storer: storage.RepoAvatars, + isOrphaned: func(path string, obj storage.Object, stat fs.FileInfo) (bool, error) { + exists, err := repo.ExistsRepoArchiverWithStoragePath(ctx, path) + if err == nil || errors.Is(err, util.ErrInvalidArgument) { + // invalid arguments mean that the object is not a valid repo archiver and it should be removed + return !exists, nil + } + return !exists, err + }, + name: "repo archive", + }); err != nil { + return err + } + } + + if opts.Packages || opts.All { + if err := commonCheckStorage(ctx, logger, autofix, + &commonStorageCheckOptions{ + storer: storage.Packages, + isOrphaned: func(path string, obj storage.Object, stat fs.FileInfo) (bool, error) { + key, err := packages_module.RelativePathToKey(path) + if err != nil { + // If there is an error here then the relative path does not match a valid package + // Therefore it is orphaned by default + return true, nil + } + + exists, err := packages.ExistPackageBlobWithSHA(ctx, string(key)) + + return !exists, err + }, + name: "package blob", + }); err != nil { + return err + } + } + + return nil } - return checkAttachmentStorageFiles(logger, autofix) } func init() { Register(&Check{ - Title: "Check if there is garbage storage files", + Title: "Check if there are orphaned storage files", Name: "storages", IsDefault: false, - Run: checkStorageFiles, + Run: checkStorage(&checkStorageOptions{All: true}), + AbortIfFailed: false, + SkipDatabaseInitialization: false, + Priority: 1, + }) + + Register(&Check{ + Title: "Check if there are orphaned attachments in storage", + Name: "storage-attachments", + IsDefault: false, + Run: checkStorage(&checkStorageOptions{Attachments: true}), + AbortIfFailed: false, + SkipDatabaseInitialization: false, + Priority: 1, + }) + + Register(&Check{ + Title: "Check if there are orphaned lfs files in storage", + Name: "storage-lfs", + IsDefault: false, + Run: checkStorage(&checkStorageOptions{LFS: true}), + AbortIfFailed: false, + SkipDatabaseInitialization: false, + Priority: 1, + }) + + Register(&Check{ + Title: "Check if there are orphaned avatars in storage", + Name: "storage-avatars", + IsDefault: false, + Run: checkStorage(&checkStorageOptions{Avatars: true, RepoAvatars: true}), + AbortIfFailed: false, + SkipDatabaseInitialization: false, + Priority: 1, + }) + + Register(&Check{ + Title: "Check if there are orphaned archives in storage", + Name: "storage-archives", + IsDefault: false, + Run: checkStorage(&checkStorageOptions{RepoArchives: true}), + AbortIfFailed: false, + SkipDatabaseInitialization: false, + Priority: 1, + }) + + Register(&Check{ + Title: "Check if there are orphaned package blobs in storage", + Name: "storage-packages", + IsDefault: false, + Run: checkStorage(&checkStorageOptions{Packages: true}), AbortIfFailed: false, SkipDatabaseInitialization: false, Priority: 1, diff --git a/modules/git/command.go b/modules/git/command.go index abf40b0cd7..0d94494f11 100644 --- a/modules/git/command.go +++ b/modules/git/command.go @@ -202,8 +202,11 @@ func (c *Command) Run(opts *RunOpts) error { if opts == nil { opts = &RunOpts{} } - if opts.Timeout <= 0 { - opts.Timeout = defaultCommandExecutionTimeout + + // We must not change the provided options + timeout := opts.Timeout + if timeout <= 0 { + timeout = defaultCommandExecutionTimeout } if len(opts.Dir) == 0 { @@ -238,7 +241,7 @@ func (c *Command) Run(opts *RunOpts) error { if opts.UseContextTimeout { ctx, cancel, finished = process.GetManager().AddContext(c.parentContext, desc) } else { - ctx, cancel, finished = process.GetManager().AddContextTimeout(c.parentContext, opts.Timeout, desc) + ctx, cancel, finished = process.GetManager().AddContextTimeout(c.parentContext, timeout, desc) } defer finished() @@ -339,9 +342,20 @@ func (c *Command) RunStdBytes(opts *RunOpts) (stdout, stderr []byte, runErr RunS } stdoutBuf := &bytes.Buffer{} stderrBuf := &bytes.Buffer{} - opts.Stdout = stdoutBuf - opts.Stderr = stderrBuf - err := c.Run(opts) + + // We must not change the provided options as it could break future calls - therefore make a copy. + newOpts := &RunOpts{ + Env: opts.Env, + Timeout: opts.Timeout, + UseContextTimeout: opts.UseContextTimeout, + Dir: opts.Dir, + Stdout: stdoutBuf, + Stderr: stderrBuf, + Stdin: opts.Stdin, + PipelineFunc: opts.PipelineFunc, + } + + err := c.Run(newOpts) stderr = stderrBuf.Bytes() if err != nil { return nil, stderr, &runStdError{err: err, stderr: bytesToString(stderr)} diff --git a/modules/git/repo_archive.go b/modules/git/repo_archive.go index a0cbfba5d9..13be2004ca 100644 --- a/modules/git/repo_archive.go +++ b/modules/git/repo_archive.go @@ -38,6 +38,18 @@ func (a ArchiveType) String() string { return "unknown" } +func ToArchiveType(s string) ArchiveType { + switch s { + case "zip": + return ZIP + case "tar.gz": + return TARGZ + case "bundle": + return BUNDLE + } + return 0 +} + // CreateArchive create archive content to the target path func (repo *Repository) CreateArchive(ctx context.Context, format ArchiveType, target io.Writer, usePrefix bool, commitID string) error { if format.String() == "unknown" { diff --git a/modules/packages/content_store.go b/modules/packages/content_store.go index a3a5d1a666..be416ac269 100644 --- a/modules/packages/content_store.go +++ b/modules/packages/content_store.go @@ -7,8 +7,10 @@ package packages import ( "io" "path" + "strings" "code.gitea.io/gitea/modules/storage" + "code.gitea.io/gitea/modules/util" ) // BlobHash256Key is the key to address a blob content @@ -45,3 +47,13 @@ func (s *ContentStore) Delete(key BlobHash256Key) error { func KeyToRelativePath(key BlobHash256Key) string { return path.Join(string(key)[0:2], string(key)[2:4], string(key)) } + +// RelativePathToKey converts a relative path aa/bb/aabb000000... to the sha256 key aabb000000... +func RelativePathToKey(relativePath string) (BlobHash256Key, error) { + parts := strings.SplitN(relativePath, "/", 3) + if len(parts) != 3 || len(parts[0]) != 2 || len(parts[1]) != 2 || len(parts[2]) < 4 || parts[0]+parts[1] != parts[2][0:4] { + return "", util.ErrInvalidArgument + } + + return BlobHash256Key(parts[2]), nil +} diff --git a/modules/repository/collaborator.go b/modules/repository/collaborator.go index 9d20a25890..b83d8199b9 100644 --- a/modules/repository/collaborator.go +++ b/modules/repository/collaborator.go @@ -37,7 +37,7 @@ func addCollaborator(ctx context.Context, repo *repo_model.Repository, u *user_m // AddCollaborator adds new collaboration to a repository with default access mode. func AddCollaborator(repo *repo_model.Repository, u *user_model.User) error { - return db.WithTx(func(ctx context.Context) error { + return db.WithTx(db.DefaultContext, func(ctx context.Context) error { return addCollaborator(ctx, repo, u) }) } diff --git a/modules/repository/create.go b/modules/repository/create.go index c30f8c6aa7..a0086a6fac 100644 --- a/modules/repository/create.go +++ b/modules/repository/create.go @@ -213,7 +213,7 @@ func CreateRepository(doer, u *user_model.User, opts CreateRepoOptions) (*repo_m var rollbackRepo *repo_model.Repository - if err := db.WithTx(func(ctx context.Context) error { + if err := db.WithTx(db.DefaultContext, func(ctx context.Context) error { if err := CreateRepositoryByExample(ctx, doer, u, repo, false); err != nil { return err } diff --git a/modules/repository/repo.go b/modules/repository/repo.go index de6de3bda4..1d3a4658c2 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -6,6 +6,7 @@ package repository import ( "context" + "errors" "fmt" "io" "net/http" @@ -79,6 +80,9 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User, Timeout: migrateTimeout, SkipTLSVerify: setting.Migrations.SkipTLSVerify, }); err != nil { + if errors.Is(err, context.DeadlineExceeded) { + return repo, fmt.Errorf("Clone timed out. Consider increasing [git.timeout] MIGRATE in app.ini. Underlying Error: %w", err) + } return repo, fmt.Errorf("Clone: %w", err) } @@ -169,7 +173,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User, } } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } @@ -485,7 +489,7 @@ func pullMirrorReleaseSync(repo *repo_model.Repository, gitRepo *git.Repository) if err != nil { return fmt.Errorf("unable to GetTagInfos in pull-mirror Repo[%d:%s/%s]: %w", repo.ID, repo.OwnerName, repo.Name, err) } - err = db.WithTx(func(ctx context.Context) error { + err = db.WithTx(db.DefaultContext, func(ctx context.Context) error { // // clear out existing releases // diff --git a/modules/structs/pull_review.go b/modules/structs/pull_review.go index 7c9360a0c2..ca2af48657 100644 --- a/modules/structs/pull_review.go +++ b/modules/structs/pull_review.go @@ -40,6 +40,8 @@ type PullReview struct { CodeCommentsCount int `json:"comments_count"` // swagger:strfmt date-time Submitted time.Time `json:"submitted_at"` + // swagger:strfmt date-time + Updated time.Time `json:"updated_at"` HTMLURL string `json:"html_url"` HTMLPullURL string `json:"pull_request_url"` diff --git a/routers/api/packages/api.go b/routers/api/packages/api.go index 6f53bc4ae0..11e7e5d6a6 100644 --- a/routers/api/packages/api.go +++ b/routers/api/packages/api.go @@ -40,7 +40,9 @@ func reqPackageAccess(accessMode perm.AccessMode) func(ctx *context.Context) { } } -func Routes(ctx gocontext.Context) *web.Route { +// CommonRoutes provide endpoints for most package managers (except containers - see below) +// These are mounted on `/api/packages` (not `/api/v1/packages`) +func CommonRoutes(ctx gocontext.Context) *web.Route { r := web.NewRoute() r.Use(context.PackageContexter(ctx)) @@ -301,6 +303,9 @@ func Routes(ctx gocontext.Context) *web.Route { return r } +// ContainerRoutes provides endpoints that implement the OCI API to serve containers +// These have to be mounted on `/v2/...` to comply with the OCI spec: +// https://github.com/opencontainers/distribution-spec/blob/main/spec.md func ContainerRoutes(ctx gocontext.Context) *web.Route { r := web.NewRoute() diff --git a/routers/api/packages/conan/conan.go b/routers/api/packages/conan/conan.go index c8c9dc3e38..b69a674106 100644 --- a/routers/api/packages/conan/conan.go +++ b/routers/api/packages/conan/conan.go @@ -604,7 +604,7 @@ func DeletePackageV2(ctx *context.Context) { } func deleteRecipeOrPackage(apictx *context.Context, rref *conan_module.RecipeReference, ignoreRecipeRevision bool, pref *conan_module.PackageReference, ignorePackageRevision bool) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/routers/api/packages/container/blob.go b/routers/api/packages/container/blob.go index 8a9cbd4a15..df6b7aed92 100644 --- a/routers/api/packages/container/blob.go +++ b/routers/api/packages/container/blob.go @@ -28,7 +28,7 @@ func saveAsPackageBlob(hsr packages_module.HashedSizeReader, pi *packages_servic contentStore := packages_module.NewContentStore() - err := db.WithTx(func(ctx context.Context) error { + err := db.WithTx(db.DefaultContext, func(ctx context.Context) error { created := true p := &packages_model.Package{ OwnerID: pi.Owner.ID, @@ -117,7 +117,7 @@ func saveAsPackageBlob(hsr packages_module.HashedSizeReader, pi *packages_servic } func deleteBlob(ownerID int64, image, digest string) error { - return db.WithTx(func(ctx context.Context) error { + return db.WithTx(db.DefaultContext, func(ctx context.Context) error { pfds, err := container_model.GetContainerBlobs(ctx, &container_model.BlobSearchOptions{ OwnerID: ownerID, Image: image, diff --git a/routers/api/packages/container/manifest.go b/routers/api/packages/container/manifest.go index 8beed3dbb7..a48b1de3b2 100644 --- a/routers/api/packages/container/manifest.go +++ b/routers/api/packages/container/manifest.go @@ -77,7 +77,7 @@ func processImageManifest(mci *manifestCreationInfo, buf *packages_module.Hashed return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -190,7 +190,7 @@ func processImageManifestIndex(mci *manifestCreationInfo, buf *packages_module.H return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/routers/api/packages/npm/npm.go b/routers/api/packages/npm/npm.go index 6d589bde3a..af0e9be56e 100644 --- a/routers/api/packages/npm/npm.go +++ b/routers/api/packages/npm/npm.go @@ -355,7 +355,7 @@ func setPackageTag(tag string, pv *packages_model.PackageVersion, deleteOnly boo return errInvalidTagName } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index ee1804daa7..e41fccb635 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -1073,6 +1073,7 @@ func Routes(ctx gocontext.Context) *web.Route { }, repoAssignment()) }) + // NOTE: these are Gitea package management API - see packages.CommonRoutes and packages.DockerContainerRoutes for endpoints that implement package manager APIs m.Group("/packages/{username}", func() { m.Group("/{type}/{name}/{version}", func() { m.Get("", packages.GetPackage) diff --git a/routers/init.go b/routers/init.go index 53b33f468f..fecc5c439c 100644 --- a/routers/init.go +++ b/routers/init.go @@ -185,8 +185,14 @@ func NormalRoutes(ctx context.Context) *web.Route { r.Mount("/", web_routers.Routes(ctx)) r.Mount("/api/v1", apiv1.Routes(ctx)) r.Mount("/api/internal", private.Routes()) + if setting.Packages.Enabled { - r.Mount("/api/packages", packages_router.Routes(ctx)) + // Add endpoints to match common package manager APIs + + // This implements package support for most package managers + r.Mount("/api/packages", packages_router.CommonRoutes(ctx)) + + // This implements the OCI API (Note this is not preceded by /api but is instead /v2) r.Mount("/v2", packages_router.ContainerRoutes(ctx)) } return r diff --git a/routers/web/repo/lfs.go b/routers/web/repo/lfs.go index 67cb6837a5..9bf4307bfe 100644 --- a/routers/web/repo/lfs.go +++ b/routers/web/repo/lfs.go @@ -478,7 +478,7 @@ func LFSPointerFiles(ctx *context.Context) { return err } if !result.Associatable { - associated, err := git_model.LFSObjectIsAssociated(pointerBlob.Oid) + associated, err := git_model.ExistsLFSObject(ctx, pointerBlob.Oid) if err != nil { return err } diff --git a/services/asymkey/deploy_key.go b/services/asymkey/deploy_key.go index aa0925ab13..4feeadd952 100644 --- a/services/asymkey/deploy_key.go +++ b/services/asymkey/deploy_key.go @@ -13,7 +13,7 @@ import ( // DeleteDeployKey deletes deploy key from its repository authorized_keys file if needed. func DeleteDeployKey(doer *user_model.User, id int64) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/services/asymkey/ssh_key.go b/services/asymkey/ssh_key.go index 143c807a10..06b20001fd 100644 --- a/services/asymkey/ssh_key.go +++ b/services/asymkey/ssh_key.go @@ -26,7 +26,7 @@ func DeletePublicKey(doer *user_model.User, id int64) (err error) { } } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/services/attachment/attachment.go b/services/attachment/attachment.go index 557cc808a3..cc329f568b 100644 --- a/services/attachment/attachment.go +++ b/services/attachment/attachment.go @@ -25,7 +25,7 @@ func NewAttachment(attach *repo_model.Attachment, file io.Reader) (*repo_model.A return nil, fmt.Errorf("attachment %s should belong to a repository", attach.Name) } - err := db.WithTx(func(ctx context.Context) error { + err := db.WithTx(db.DefaultContext, func(ctx context.Context) error { attach.UUID = uuid.New().String() size, err := storage.Attachments.Save(attach.RelativePath(), file, -1) if err != nil { diff --git a/services/automerge/automerge.go b/services/automerge/automerge.go index 3ee8af2344..5c38367e3b 100644 --- a/services/automerge/automerge.go +++ b/services/automerge/automerge.go @@ -63,7 +63,7 @@ func addToQueue(pr *issues_model.PullRequest, sha string) { // ScheduleAutoMerge if schedule is false and no error, pull can be merged directly func ScheduleAutoMerge(ctx context.Context, doer *user_model.User, pull *issues_model.PullRequest, style repo_model.MergeStyle, message string) (scheduled bool, err error) { - err = db.WithTx(func(ctx context.Context) error { + err = db.WithTx(ctx, func(ctx context.Context) error { lastCommitStatus, err := pull_service.GetPullRequestCommitStatusState(ctx, pull) if err != nil { return err @@ -81,20 +81,20 @@ func ScheduleAutoMerge(ctx context.Context, doer *user_model.User, pull *issues_ _, err = issues_model.CreateAutoMergeComment(ctx, issues_model.CommentTypePRScheduledToAutoMerge, pull, doer) return err - }, ctx) + }) return scheduled, err } // RemoveScheduledAutoMerge cancels a previously scheduled pull request func RemoveScheduledAutoMerge(ctx context.Context, doer *user_model.User, pull *issues_model.PullRequest) error { - return db.WithTx(func(ctx context.Context) error { + return db.WithTx(ctx, func(ctx context.Context) error { if err := pull_model.DeleteScheduledAutoMerge(ctx, pull.ID); err != nil { return err } _, err := issues_model.CreateAutoMergeComment(ctx, issues_model.CommentTypePRUnScheduledToAutoMerge, pull, doer) return err - }, ctx) + }) } // MergeScheduledPullRequest merges a previously scheduled pull request when all checks succeeded diff --git a/services/comments/comments.go b/services/comments/comments.go index c40631359b..7167219c20 100644 --- a/services/comments/comments.go +++ b/services/comments/comments.go @@ -72,7 +72,7 @@ func UpdateComment(c *issues_model.Comment, doer *user_model.User, oldContent st // DeleteComment deletes the comment func DeleteComment(doer *user_model.User, comment *issues_model.Comment) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/services/issue/issue.go b/services/issue/issue.go index 47782e50d3..8342d7f5d2 100644 --- a/services/issue/issue.go +++ b/services/issue/issue.go @@ -209,7 +209,7 @@ func GetRefEndNamesAndURLs(issues []*issues_model.Issue, repoLink string) (map[i // deleteIssue deletes the issue func deleteIssue(issue *issues_model.Issue) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/services/issue/label.go b/services/issue/label.go index bc5f9b910e..a9a0c20b01 100644 --- a/services/issue/label.go +++ b/services/issue/label.go @@ -45,7 +45,7 @@ func AddLabels(issue *issues_model.Issue, doer *user_model.User, labels []*issue // RemoveLabel removes a label from issue by given ID. func RemoveLabel(issue *issues_model.Issue, doer *user_model.User, label *issues_model.Label) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/services/issue/milestone.go b/services/issue/milestone.go index 756a8625de..965b07556d 100644 --- a/services/issue/milestone.go +++ b/services/issue/milestone.go @@ -65,7 +65,7 @@ func changeMilestoneAssign(ctx context.Context, doer *user_model.User, issue *is // ChangeMilestoneAssign changes assignment of milestone for issue. func ChangeMilestoneAssign(issue *issues_model.Issue, doer *user_model.User, oldMilestoneID int64) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/services/org/org.go b/services/org/org.go index 39845610d2..d993f82bbf 100644 --- a/services/org/org.go +++ b/services/org/org.go @@ -19,7 +19,7 @@ import ( // DeleteOrganization completely and permanently deletes everything of organization. func DeleteOrganization(org *organization.Organization) error { - ctx, commiter, err := db.TxContext() + ctx, commiter, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/services/org/repo.go b/services/org/repo.go index 769419d45b..88520c819f 100644 --- a/services/org/repo.go +++ b/services/org/repo.go @@ -22,7 +22,7 @@ func TeamAddRepository(t *organization.Team, repo *repo_model.Repository) (err e return nil } - return db.WithTx(func(ctx context.Context) error { + return db.WithTx(db.DefaultContext, func(ctx context.Context) error { return models.AddRepository(ctx, t, repo) }) } diff --git a/services/packages/packages.go b/services/packages/packages.go index 443976e174..4513215ae1 100644 --- a/services/packages/packages.go +++ b/services/packages/packages.go @@ -76,7 +76,7 @@ func CreatePackageOrAddFileToExisting(pvci *PackageCreationInfo, pfci *PackageFi } func createPackageAndAddFile(pvci *PackageCreationInfo, pfci *PackageFileCreationInfo, allowDuplicate bool) (*packages_model.PackageVersion, *packages_model.PackageFile, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, nil, err } @@ -190,7 +190,7 @@ func createPackageAndVersion(ctx context.Context, pvci *PackageCreationInfo, all // AddFileToExistingPackage adds a file to an existing package. If the package does not exist, ErrPackageNotExist is returned func AddFileToExistingPackage(pvi *PackageInfo, pfci *PackageFileCreationInfo) (*packages_model.PackageVersion, *packages_model.PackageFile, error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, nil, err } @@ -388,7 +388,7 @@ func RemovePackageVersionByNameAndVersion(doer *user_model.User, pvi *PackageInf // RemovePackageVersion deletes the package version and all associated files func RemovePackageVersion(doer *user_model.User, pv *packages_model.PackageVersion) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -444,7 +444,7 @@ func DeletePackageFile(ctx context.Context, pf *packages_model.PackageFile) erro // Cleanup removes expired package data func Cleanup(unused context.Context, olderThan time.Duration) error { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/services/pull/check.go b/services/pull/check.go index 830ff640b5..6e91c22f3e 100644 --- a/services/pull/check.go +++ b/services/pull/check.go @@ -63,7 +63,7 @@ func AddToTaskQueue(pr *issues_model.PullRequest) { // CheckPullMergable check if the pull mergable based on all conditions (branch protection, merge options, ...) func CheckPullMergable(stdCtx context.Context, doer *user_model.User, perm *access_model.Permission, pr *issues_model.PullRequest, manuallMerge, force bool) error { - return db.WithTx(func(ctx context.Context) error { + return db.WithTx(stdCtx, func(ctx context.Context) error { if pr.HasMerged { return ErrHasMerged } @@ -122,7 +122,7 @@ func CheckPullMergable(stdCtx context.Context, doer *user_model.User, perm *acce } return nil - }, stdCtx) + }) } // isSignedIfRequired check if merge will be signed if required diff --git a/services/pull/merge.go b/services/pull/merge.go index 56ee9c9a73..6f37a887db 100644 --- a/services/pull/merge.go +++ b/services/pull/merge.go @@ -828,7 +828,7 @@ func MergedManually(pr *issues_model.PullRequest, doer *user_model.User, baseGit pullWorkingPool.CheckIn(fmt.Sprint(pr.ID)) defer pullWorkingPool.CheckOut(fmt.Sprint(pr.ID)) - if err := db.WithTx(func(ctx context.Context) error { + if err := db.WithTx(db.DefaultContext, func(ctx context.Context) error { prUnit, err := pr.BaseRepo.GetUnitCtx(ctx, unit.TypePullRequests) if err != nil { return err diff --git a/services/release/release.go b/services/release/release.go index 8ccc222fb2..cf398debbb 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -196,7 +196,7 @@ func UpdateRelease(doer *user_model.User, gitRepo *git.Repository, rel *repo_mod } rel.LowerTagName = strings.ToLower(rel.TagName) - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/services/repository/adopt.go b/services/repository/adopt.go index a9a0639548..57a21f500c 100644 --- a/services/repository/adopt.go +++ b/services/repository/adopt.go @@ -54,7 +54,7 @@ func AdoptRepository(doer, u *user_model.User, opts repo_module.CreateRepoOption IsEmpty: !opts.AutoInit, } - if err := db.WithTx(func(ctx context.Context) error { + if err := db.WithTx(db.DefaultContext, func(ctx context.Context) error { repoPath := repo_model.RepoPath(u.Name, repo.Name) isExist, err := util.IsExist(repoPath) if err != nil { diff --git a/services/repository/archiver/archiver.go b/services/repository/archiver/archiver.go index 79537ea051..f1c2693aeb 100644 --- a/services/repository/archiver/archiver.go +++ b/services/repository/archiver/archiver.go @@ -174,7 +174,7 @@ func (aReq *ArchiveRequest) Await(ctx context.Context) (*repo_model.RepoArchiver } func doArchive(r *ArchiveRequest) (*repo_model.RepoArchiver, error) { - txCtx, committer, err := db.TxContext() + txCtx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return nil, err } diff --git a/services/repository/avatar.go b/services/repository/avatar.go index 1cf9e869c0..e4c8da3ba2 100644 --- a/services/repository/avatar.go +++ b/services/repository/avatar.go @@ -33,7 +33,7 @@ func UploadAvatar(repo *repo_model.Repository, data []byte) error { return nil } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -76,7 +76,7 @@ func DeleteAvatar(repo *repo_model.Repository) error { avatarPath := repo.CustomAvatarRelativePath() log.Trace("DeleteAvatar[%d]: %s", repo.ID, avatarPath) - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/services/repository/fork.go b/services/repository/fork.go index 136d7ccab6..e597bfa449 100644 --- a/services/repository/fork.go +++ b/services/repository/fork.go @@ -112,7 +112,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork panic(panicErr) }() - err = db.WithTx(func(txCtx context.Context) error { + err = db.WithTx(ctx, func(txCtx context.Context) error { if err = repo_module.CreateRepositoryByExample(txCtx, doer, owner, repo, false); err != nil { return err } @@ -184,7 +184,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork // ConvertForkToNormalRepository convert the provided repo from a forked repo to normal repo func ConvertForkToNormalRepository(repo *repo_model.Repository) error { - err := db.WithTx(func(ctx context.Context) error { + err := db.WithTx(db.DefaultContext, func(ctx context.Context) error { repo, err := repo_model.GetRepositoryByIDCtx(ctx, repo.ID) if err != nil { return err diff --git a/services/repository/push.go b/services/repository/push.go index 3a7205d18b..6776ff9f74 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -290,7 +290,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { // PushUpdateAddDeleteTags updates a number of added and delete tags func PushUpdateAddDeleteTags(repo *repo_model.Repository, gitRepo *git.Repository, addTags, delTags []string) error { - return db.WithTx(func(ctx context.Context) error { + return db.WithTx(db.DefaultContext, func(ctx context.Context) error { if err := repo_model.PushUpdateDeleteTagsContext(ctx, repo, delTags); err != nil { return err } diff --git a/services/repository/repository.go b/services/repository/repository.go index 763ef74927..2f2c27ff20 100644 --- a/services/repository/repository.go +++ b/services/repository/repository.go @@ -90,7 +90,7 @@ func Init() error { // UpdateRepository updates a repository func UpdateRepository(repo *repo_model.Repository, visibilityChanged bool) (err error) { - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/services/repository/template.go b/services/repository/template.go index b73abdce58..bb5e3f4668 100644 --- a/services/repository/template.go +++ b/services/repository/template.go @@ -48,7 +48,7 @@ func GenerateRepository(doer, owner *user_model.User, templateRepo *repo_model.R } var generateRepo *repo_model.Repository - if err = db.WithTx(func(ctx context.Context) error { + if err = db.WithTx(db.DefaultContext, func(ctx context.Context) error { generateRepo, err = repo_module.GenerateRepository(ctx, doer, owner, templateRepo, opts) if err != nil { return err diff --git a/services/user/user.go b/services/user/user.go index c8b497a5c4..4186efe682 100644 --- a/services/user/user.go +++ b/services/user/user.go @@ -132,7 +132,7 @@ func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error { } } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } @@ -235,7 +235,7 @@ func UploadAvatar(u *user_model.User, data []byte) error { return err } - ctx, committer, err := db.TxContext() + ctx, committer, err := db.TxContext(db.DefaultContext) if err != nil { return err } diff --git a/services/webhook/general.go b/services/webhook/general.go index 5be177d339..f6ef673923 100644 --- a/services/webhook/general.go +++ b/services/webhook/general.go @@ -84,11 +84,13 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm issueTitle := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title) titleLink := linkFormatter(p.PullRequest.URL, issueTitle) var text string + var attachmentText string color := yellowColor switch p.Action { case api.HookIssueOpened: text = fmt.Sprintf("[%s] Pull request opened: %s", repoLink, titleLink) + attachmentText = p.PullRequest.Body color = greenColor case api.HookIssueClosed: if p.PullRequest.HasMerged { @@ -102,6 +104,7 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm text = fmt.Sprintf("[%s] Pull request re-opened: %s", repoLink, titleLink) case api.HookIssueEdited: text = fmt.Sprintf("[%s] Pull request edited: %s", repoLink, titleLink) + attachmentText = p.PullRequest.Body case api.HookIssueAssigned: list := make([]string, len(p.PullRequest.Assignees)) for i, user := range p.PullRequest.Assignees { @@ -126,16 +129,12 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm text = fmt.Sprintf("[%s] Pull request milestone cleared: %s", repoLink, titleLink) case api.HookIssueReviewed: text = fmt.Sprintf("[%s] Pull request reviewed: %s", repoLink, titleLink) + attachmentText = p.Review.Content } if withSender { text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)) } - var attachmentText string - if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited { - attachmentText = p.PullRequest.Body - } - return text, issueTitle, attachmentText, color } diff --git a/templates/repo/home.tmpl b/templates/repo/home.tmpl index 6b4a0008d6..a798d3cc9e 100644 --- a/templates/repo/home.tmpl +++ b/templates/repo/home.tmpl @@ -47,7 +47,7 @@ diff --git a/templates/repo/sub_menu.tmpl b/templates/repo/sub_menu.tmpl index fac8e5ac99..e74823e36d 100644 --- a/templates/repo/sub_menu.tmpl +++ b/templates/repo/sub_menu.tmpl @@ -4,7 +4,7 @@