-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
add k/v store for repo settings #19400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 4 commits
Commits
Show all changes
1289 commits
Select commit
Hold shift + click to select a range
3e3975e
fix webpackChunkName for citation-js-csl (#21806)
silverwind d9ba7f7
Prevent panic in doctor command when running default checks (#21791)
zeripath 55115db
Improve pull/ push mirror documentation (especially for GitHub) (#21801)
delvh de6dfb7
Remove `href="javascript:;"` in "save topics (Done)" button (#21813)
yardenshoham c772934
Adjust gitea doctor --run storages to check all storage types (#21785)
zeripath 6c8ff32
Add `updated_at` field to PullReview API object (#21812)
yardenshoham 40229a7
Skip GitHub migration tests if the API token is undefined (#21824)
garymoon f311d15
Added space between avatar and username (#21825)
MayMeow 92dd247
Ignore issue template with a special name (#21830)
wolfogre c144942
Tweak katex options (#21828)
silverwind 43ab932
Fix setting HTTP headers after write (#21833)
KN4CK3R 88d5275
Do not allow Ghost access to limited visible user/org (#21849)
KN4CK3R 6dbcf72
Bump loader-utils from 2.0.3 to 2.0.4 (#21852)
dependabot[bot] 0b993a0
Fix "build from source" document to clarify the `bindata` tag is requ…
wxiaoguang 20385b5
Prevent dangling user redirects (#21856)
6da8bc6
chore: add webpack export type check (#21857)
kecrily 595d940
Fix webpack license warning (#21815)
silverwind fefdb7f
Timeline and color tweaks (#21799)
silverwind 044c754
Add `context.Context` to more methods (#21546)
KN4CK3R eec1c71
Show syntax lexer name in file view/blame (#21814)
silverwind c8f3eb6
Fix wechatwork webhook sends empty content in PR review (#21762)
jim-kirisame d3f850c
Support comma-delimited string as labels in issue template (#21831)
wolfogre 32db625
Add package registry cleanup rules (#21658)
KN4CK3R 43aafc5
Improve documentation for PAM and static deployment (#21866)
KB3HNS 9380bb6
Consolidate security-check into checks-backend (#21882)
silverwind b4802b9
Allow disable RSS/Atom feed (#21622)
e4eaa68
Replace yaml.v2 with yaml.v3 (#21832)
wolfogre c2fb27b
Improvements for Content Copy (#21842)
silverwind 2836382
fix(web): add `alt` for logo in home page (#21887)
kecrily 2e5ac53
Embed Matrix icon as SVG (#21890)
silverwind 1c7496c
Webhook list enhancements (#21893)
silverwind c8b2171
Update JS dependencies (#21881)
silverwind 371dd96
Clarify logging documentation (#21665)
mpeter50 1d52228
Fixes #21895: standardize UTC tz for util tests (#21897)
fsologureng e77b764
Prepend refs/heads/ to issue template refs (#20461)
zeripath 68e934a
Add option to enable CAPTCHA validation for login (#21638)
ee21d54
Move all remaining colors into CSS variables (#21903)
silverwind 4d42cbb
Handle empty author names (#21902)
zeripath 13746f0
feat: add button to quickly clear merge message (#21548)
kolaente 4c00d8f
Move migration test fixtures to the correct directories (#21901)
zeripath 787f6c3
Ensure that Webhook tasks are not double delivered (#21558)
zeripath a3c09fb
Improve docker rootless documentation (#21913)
e483ec3
Fix vertical align of committer avatar rendered by email address (#21…
702f4b5
Add index for access_token (#21908)
wolfogre 09b7d81
Fix flex layout for repo list icons (#21896)
b97548a
Fix table misalignments and tweak webhook and githook lists (#21917)
silverwind b2269de
Fix typo in sidebar (#21922)
KN4CK3R 26f941f
Fix button in branch list, avoid unexpected page jump before restore …
fc7a2d5
Add support for HEAD requests in Maven registry (#21834)
KN4CK3R 9ce5e09
Fix scroll over mermaid frame (#21925)
silverwind a1ae83f
Workaround for container registry push/pull errors (#21862)
KN4CK3R b91167b
fix(web): keep the pages of the navigation in the center (#21867)
kecrily 7a4f7b3
Fix typo in sidebar (#21934)
KN4CK3R abecf63
add rel="me" to org.website link (#21941)
MFTabriz d7f12af
Prevent NPE if trying to restore an already restored deleted branch (…
zeripath 5f38acd
Fix markdown anchor re-clicking (#21931)
silverwind 4b5a6e5
Fix typos (#21947)
KN4CK3R 32590db
[skip ci] Updated licenses and gitignores
KN4CK3R f6fd501
Correct the fallbacks for mailer configuration (#21945)
zeripath e81ccc4
Implement FSFE REUSE for golang files (#21840)
flynnnnnnnnnn 9607750
Replace fmt.Sprintf with hex.EncodeToString (#21960)
wolfogre f047ee0
Use random bytes to generate access token (#21959)
wolfogre 715cf46
Normalize `AppURL` according to RFC 3986 (#21950)
SaswatPadhi fdfd77f
Fix typos (#21979)
luzpaz 7020c4a
Fix leaving organization bug on user settings -> orgs (#21983)
Cosin 67881ae
Skip initing disabled storages (#21985)
wolfogre b2c4870
Fix parallel creating commit status bug with tests (#21911)
lunny 4e5d4d0
Skip initing LFS storage if disabled (#21996)
wolfogre f9cbf5a
Util type to parse ref name (#21969)
wolfogre f0bd219
Update chroma to v2.4.0 (#22000)
silverwind 64973cf
Use path not filepath in template filenames (#21993)
zeripath f7ade6d
Fix generate index failure possibility on postgres (#21998)
lunny 665d02e
Remove duplicate "Actions" label in mobile view (#21974)
markormesher df676a4
Remove session in api tests (#21984)
lunny d640632
Multiple improvements for comment edit diff (#21990)
silverwind f59a748
Update gitea-vet to check FSFE REUSE (#22004)
wolfogre 350bc83
Update to Alpine 3.17 (#21904)
flynnnnnnnnnn 21bcb92
Add pnpm to packages/overview (#22008)
xtexx 8698458
Remove deprecated packages & staticcheck fixes (#22012)
harryzcy 0a7d3ff
refactor some functions to support ctx as first parameter (#21878)
lunny 181f4bf
docs: add `Edit this page` (#21981)
0e46499
Do not emit ambiguous character warning on rendered pages (#22016)
zeripath d084ce3
[skip ci] Updated licenses and gitignores
zeripath 9eb9cf5
fix(web): reduce page jitter on browsers that support overlay scrollb…
kecrily 36cbaec
Fix ListBranches to handle empty case (#21921)
lunny 4648584
On tag/branch-exist check, dont panic if repo is nil (#21787)
6543 84d2a82
Add dumb-init to rootless docker (#21775)
viceice ea86c2b
Use GhostUser if needed for TrackedTimes (#22021)
zeripath a08584e
Ensure that Chinese punctuation is not ambiguous when locale is Chine…
zeripath 77f5035
Remove useless "Cancel" buttons (#21872)
yardenshoham 74874d0
Fix issue/PR numbers (#22037)
wolfogre e2fa84f
Release and Tag List tweaks (#21712)
silverwind 0a85537
Support disabling database auto migration (#22053)
wolfogre cf27403
Round language stats percentage using largest remainder (#22026)
hr-98 0585ac3
Update go dev dependencies (#22064)
silverwind 8fb1e53
Rename actions to operations on UI (#22067)
wolfogre 3c59d31
Add API management for issue/pull and comment attachments (#21783)
KN4CK3R 2779d47
Optimize html templates (#22080)
wolfogre 097d4e3
Change ID pattern of raw content container for issue (#21966)
fsologureng 6870453
Rename almost all Ctx functions (#22071)
lunny 9745c33
Remove unnecessary whitespace in snapcraft.yaml (#22090)
yardenshoham 0e2d046
Update xorm (#22094)
KN4CK3R 601d126
[skip ci] Updated licenses and gitignores
KN4CK3R 112e425
fix(config): remove context on config template (#22096)
restray c9ef03a
Fix wrong default value for update checker on app.example.ini (#22084)
lunny 352a50d
Fix sorting admin user list by last login (#22081)
aceArt-GmbH 3e8285b
Use multi reader instead to concat strings (#22099)
lunny 003b4e2
Allow disable code tab (#20805)
lunny 6398ca7
refactor bind functions based on generics (#22055)
lunny 36a2d2f
Add a simple test for external renderer (#20033)
lunny 87c64f6
Fix permission check on issue/pull lock (#22110)
lunny a95247b
Fix autofilled text visibility in dark mode (#22088)
harryzcy 5aa8546
Fix markdown typo of an extra backtick in docs (#22123)
Yakov5776 4fb2006
Make gitea work using cmd.exe again (#22073)
zeripath 07461e1
Fix condition for is_internal (#22095)
KN4CK3R 1b32ed0
remove duplicated read file code (#22042)
lunny cdc4345
Correctly handle moved files in apply patch (#22118)
zeripath ae97197
Fixed Project view .board-column height for tall screens. (#22108)
gnat 3243dbe
remove silentcode from MAINTAINERS (#22143)
silentcodeg 651fe4b
Add doctor command for full GC of LFS (#21978)
zeripath 84285a1
Do not list active repositories as unadopted (#22034)
chrullrich 8400146
Update username (#22147)
c4c4151
Fix margin and alignment in dashboard repolist (#22120)
silverwind f3370ee
verify nodeinfo response by schema (#22137)
MFTabriz 6e22605
Ensure that plain files are rendered correctly even when containing a…
zeripath 998fe26
[skip ci] Updated licenses and gitignores
zeripath a89b399
Local storage should not store files as executable (#22162)
zeripath d6b9662
Add setting to disable the git apply step in test patch (#22130)
zeripath 2774671
Fix heatmap first color being unused (#22157)
silverwind 40ba750
Check for zero time instant in `TimeStamp.IsZero()` (#22171)
Gusted 6590551
Secrets storage with SecretKey encrypted (#22142)
wolfogre 495b8b3
Fix delete secret modal (#22187)
lunny e767b33
Update JS dependencies and eslint (#22190)
silverwind 70d15e7
Fix misc whitespace issues in install docs (#22189)
h3xx c4df10d
Repair LFS web rendering. (#22195)
kousu 90572c5
Specify ID in `TestAPITeam` (#22192)
Gusted fe6608f
Attempt to fix TestExportUserGPGKeys (#22159)
zeripath 86ace4b
Normalize NuGet package version on upload (#22186)
KN4CK3R 8e267af
Mobile fix for Project view: Add delay to Sortable.js on mobile, to e…
gnat c403e2f
Fixed colour transparency regex matching in project board sorting (#2…
MisterCavespider 9914b21
fix: update libcurl in docs pipeline (#22203)
jolheiser 48d71b7
Add Feed for Releases and Tags (#21696)
reopt999 b767182
Allow empty assignees on pull request edit (#22150)
KN4CK3R 8e17fb5
Update bleve and zapx to fix unaligned atomic (#22031)
zeripath 3affb02
Run hugo via `go run` and lock its version (#22206)
silverwind 6037043
Fix container layer display overflow (#22208)
lafriks ac5b44b
Upgrade hugo to 0.82 (#22209)
silverwind 8c1bb77
Remove test session cache to reduce possible concurrent problem (#22199)
lunny 7bf7c13
Frontport 1.17.4 changelog (#22216)
lunny ea5a752
update docs latest to 1.17.4
techknowlogick a2779de
Test views of LFS files (#22196)
kousu 41f0668
Hide file borders on sticky diff box (#22217)
silverwind 71ca306
Check primary keys for all tables and drop ForeignReference (#21721)
wolfogre 2cf0cf0
JS refactors (#22227)
silverwind 2b0b563
Improve testing for pgsql empty repository (#22223)
lunny 3bd49f7
[skip ci] Updated licenses and gitignores
lunny f5cd0d9
Add Mermaid copy button, avoid unnecessary tooltip hide (#22225)
silverwind 814b44a
Fix typo of Asia/Shanghai (#22242)
wolfogre 83640c4
Remove ReverseProxy authentication from the API (#22219)
zeripath b48cf03
Remove deadcode (#22245)
Gusted 90237d8
Add more test directory to exclude dir of air, remove watching templa…
lunny 6cf09cc
Use complete SHA to create and query commit status (#22244)
wolfogre d0c3d0b
Add the 'ui.user' section to the cheat sheet (#22249)
chrullrich 22a6e97
Update standard copyright header to use a placeholder year (#22254)
yardenshoham 7cc7db7
Add option to prohibit fork if user reached maximum limit of reposito…
ca67c5a
refactor auth interface to return error when verify failure (#22119)
lunny 309e86a
Use dynamic package type list (#22263)
KN4CK3R a357498
Move `convert` package to services (#22264)
KN4CK3R 47efba7
Support template for merge message description (#22248)
wolfogre a609cae
Correctly handle select on multiple channels in Queues (#22146)
zeripath b76970f
Fix key signature error page (#22229)
Gusted 9dcaf14
Add `sync_on_commit` option for push mirrors api (#22271)
harryzcy d34f3a2
Fix sitemap (#22272)
wolfogre cf07f24
Add Gentoo to the from package providers (#22284)
eeyrjmr e5deeda
Support estimated count with multiple schemas (#22276)
wolfogre dce8887
Changelog v1.18.0 (#22215) (#22269)
6543 3fef47b
Use ErrInvalidArgument in packages (#22268)
KN4CK3R b1a4eb4
[skip ci] Updated licenses and gitignores
KN4CK3R 2bbf9e7
Upgrade go-chi to v5.0.8 (#22304)
lunny 9c8fc7f
Fix bug of DisableGravatar default value (#22296)
lunny f8e93ce
Reminder for no more logs to console (#22282)
wolfogre 0f4e1b9
Restructure `webhook` module (#22256)
delvh a1c3074
Fix get system setting bug when enabled redis cache (#22295)
lunny b994b2e
Fix due date rendering the wrong date in issue (#22302)
yardenshoham e61ce93
Update Gmail mailer configuration (#22291)
disposedtrolley fcd6cee
fix: code search title translation (#22285)
jolheiser 96797fe
Unify hashing for avatar (#22289)
Gusted f0159c3
Add deprecated warning for DISABLE_GRAVATAR and ENABLE_FEDERATED_AVAT…
kdumontnu c59e153
Display error log when a modified template has an error so that it co…
lunny efa7085
Use git command instead of exec.Cmd in blame (#22098)
lunny 32e8634
fix: update settings table on install (#22326)
jolheiser fdbb0b4
fix gravatar disable bug (#22336)
lunny f41ad34
Update Emoji dataset to Unicode 14 (#22342)
mrsdizzie 906d8cc
Rewrite fuzzers to native Go harnesses (#22313)
AdamKorcz f74293f
Fix unstable emoji sort (#22346)
KN4CK3R 3dbd2d9
Remove old HookEventType (#22358)
wolfogre d42b52f
make /{username}.png redirect to user/org avatar (#22356)
crystalcommunication 6135359
Always reuse transaction (#22362)
wolfogre bdf8c80
Bump json5 from 1.0.1 to 1.0.2 (#22365)
dependabot[bot] dfd2db5
Fix set system setting failure once it cached (#22333)
lunny b878155
Replace `can not` with `cannot` (#22372)
delvh 7adc2de
Use context parameter in models/git (#22367)
wolfogre 9e94346
Remove satori/go.uuid (#22375)
wolfogre dbfc5aa
Move fuzz tests into tests/fuzz (#22376)
kyakdan 82235fb
Update index.de-de.md (#22363)
segler89 99a675f
Don't lookup mail server when using sendmail (#22300)
lunny a357143
Fix halfCommitter and WithTx (#22366)
wolfogre 50f67d7
Don't display stop watch top bar icon when disabled and hidden when c…
lunny 3b804ff
Fix "remember this device" case (#22388)
yardenshoham e7f1d45
fix: omit avatar_url in discord payload when empty (#22393)
jolheiser 70043da
Fix typo (#22396)
delvh dc5f2cf
cgo cross-compile for freebsd (#22397)
techknowlogick 477a1cc
Improve utils of slices (#22379)
wolfogre 2220e5d
Allow HOST has no port (#22280)
lunny 6f231a7
Replace deprecated Webauthn library (#22400)
techknowlogick b36854d
Update golang deps (#22410)
techknowlogick 9ffaf19
fix wrong theme class when logged out if default theme is changed (#2…
crystalcommunication c86be81
Add more packages to denylist (#22412)
wolfogre 1ee1147
fix: don't replace err variable in nested check (#22416)
jolheiser f1c13b4
improve explore relevancy note (#22421)
crystalcommunication 99cf0d3
Continue GCing other repos on error in one repo (#22422)
zeripath c0da363
Restore function to "Show more" buttons (#22399)
zeripath 4fc1517
clarify "open issues" text for labels (#22431)
crystalcommunication a3ab82e
Fix error when calculate the repository size (#22392)
lunny 02ae632
Log STDERR of external renderer when it fails (#22442)
jtran 20e3ffd
Fix stylesheet HTML snippet for external renderers documentation (#22…
sbannier fc037b4
Add support for incoming emails (#22056)
KN4CK3R fe519d8
Restore previous official review when an official review is deleted (…
jpraet cc1f8cb
Prevent panic on looking at api "git" endpoints for empty repos (#22457)
zeripath 2782c14
Supports wildcard protected branch (#20825)
lunny 1e7f3c1
Fix environments for KaTeX and error reporting (#22453)
zeripath 16e9dec
Fix Operator does not exist bug on explore page with ONLY_SHOW_RELEVA…
zeripath da27438
Remove the netgo tag for Windows build (#22467)
wxiaoguang 04c97aa
Change use of Walk to WalkDir to improve disk performance (#22462)
fsologureng 2cc3a63
Add cron method to gc LFS MetaObjects (#22385)
zeripath 8117e41
Add reply hint to mail text (#22459)
KN4CK3R 1aba53d
Update overview.en-us.md (#22463)
tallship 3510d7e
Fix container blob mount (#22226)
KN4CK3R 9edf80f
Fix migration from gitbucket (repost) (#22477)
haruo31 e763fab
Fix pull request API field `closed_at` always being `null` (#22482)
yardenshoham 21c91b7
Set disable_gravatar/enable_federated_avatar when offline mode is tru…
wolfogre 7a0f2fb
Fixed lint warnings in Grafana raised by Mixtool (#22486)
Dasomeone 51951d4
chore: changelog 1.18.1 (#22471) (#22487)
jolheiser 0a6b57b
docs: bump Gitea version (#22490)
jolheiser 60c4725
docs: add swagger.json file location to FAQ (#22489)
jolheiser db2286b
some refactor about code comments (#20821)
lunny de484e8
Support scoped access tokens (#20908)
harryzcy d9f748a
Support asciicast files as new markup (#22448)
wolfogre 7ddc11d
Simplify the error message when `index.js` couldn't be loaded (#22354)
wxiaoguang 2e59cc7
improvements
lunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 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 migrations | ||
|
||
import ( | ||
"fmt" | ||
|
||
"xorm.io/xorm" | ||
) | ||
|
||
func createRepoSettingsTable(x *xorm.Engine) error { | ||
type RepoSetting struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
RepoID int64 `xorm:"index unique(key_repoid)"` // to load all of someone's settings | ||
SettingKey string `xorm:"varchar(255) index unique(key_repoid)"` // ensure key is always lowercase | ||
SettingValue string `xorm:"text"` | ||
} | ||
return x.Sync2(new(RepoSetting)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// 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 repo | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/models/db" | ||
|
||
"xorm.io/builder" | ||
) | ||
|
||
// Setting is a key value store of repo settings | ||
type Setting struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
RepoID int64 `xorm:"index unique(key_repoid)"` // to load all of someone's settings | ||
SettingKey string `xorm:"varchar(255) index unique(key_repoid)"` // ensure key is always lowercase | ||
SettingValue string `xorm:"text"` | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// TableName sets the table name for the settings struct | ||
func (s *Setting) TableName() string { | ||
return "repo_setting" | ||
} | ||
|
||
func init() { | ||
db.RegisterModel(new(Setting)) | ||
} | ||
|
||
// GetRepoSettings returns specific settings from repo | ||
func GetRepoSettings(rid int64, keys []string) (map[string]*Setting, error) { | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
settings := make([]*Setting, 0, len(keys)) | ||
if err := db.GetEngine(db.DefaultContext). | ||
Where("repo_id=?", rid). | ||
And(builder.In("setting_key", keys)). | ||
Find(&settings); err != nil { | ||
return nil, err | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
settingsMap := make(map[string]*Setting) | ||
for _, s := range settings { | ||
settingsMap[s.SettingKey] = s | ||
} | ||
return settingsMap, nil | ||
} | ||
|
||
// GetRepoAllSettings returns all settings from repo | ||
func GetRepoAllSettings(rid int64) (map[string]*Setting, error) { | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
settings := make([]*Setting, 0, 5) | ||
if err := db.GetEngine(db.DefaultContext). | ||
Where("repo_id=?", rid). | ||
Find(&settings); err != nil { | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil, err | ||
} | ||
settingsMap := make(map[string]*Setting) | ||
for _, s := range settings { | ||
settingsMap[s.SettingKey] = s | ||
} | ||
return settingsMap, nil | ||
} | ||
|
||
func validateRepoSettingKey(key string) error { | ||
if len(key) == 0 { | ||
return fmt.Errorf("setting key must be set") | ||
} | ||
if strings.ToLower(key) != key { | ||
return fmt.Errorf("setting key should be lowercase") | ||
} | ||
return nil | ||
} | ||
|
||
// GetRepoSetting gets a specific setting for a repo | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func GetRepoSetting(repoID int64, key string, def ...string) (string, error) { | ||
if err := validateRepoSettingKey(key); err != nil { | ||
return "", err | ||
} | ||
setting := &Setting{RepoID: repoID, SettingKey: key} | ||
has, err := db.GetEngine(db.DefaultContext).Get(setting) | ||
if err != nil { | ||
return "", err | ||
} | ||
if !has { | ||
if len(def) == 1 { | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return def[0], nil | ||
} | ||
return "", nil | ||
} | ||
return setting.SettingValue, nil | ||
} | ||
|
||
// DeleteRepoSetting deletes a specific setting for a repo | ||
func DeleteRepoSetting(repoID int64, key string) error { | ||
if err := validateRepoSettingKey(key); err != nil { | ||
return err | ||
} | ||
_, err := db.GetEngine(db.DefaultContext).Delete(&Setting{RepoID: repoID, SettingKey: key}) | ||
return err | ||
} | ||
|
||
// SetRepoSetting updates a repos' setting for a specific key | ||
func SetRepoSetting(repoID int64, key, value string) error { | ||
if err := validateRepoSettingKey(key); err != nil { | ||
return err | ||
} | ||
return upsertRepoSettingValue(repoID, key, value) | ||
} | ||
|
||
func upsertRepoSettingValue(repoID int64, key, value string) error { | ||
return db.WithTx(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) | ||
// 1. try to UPDATE the record and acquire the transaction write lock | ||
// if UPDATE returns non-zero rows are changed, OK, the setting is saved correctly | ||
// if UPDATE returns "0 rows changed", two possibilities: (a) record doesn't exist (b) value is not changed | ||
// 2. do a SELECT to check if the row exists or not (we already have the transaction lock) | ||
// 3. if the row doesn't exist, do an INSERT (we are still protected by the transaction lock, so it's safe) | ||
// | ||
// to optimize the SELECT in step 2, we can use an extra column like `revision=revision+1` | ||
// to make sure the UPDATE always returns a non-zero value for existing (unchanged) records. | ||
|
||
res, err := e.Exec("UPDATE repo_setting SET setting_value=? WHERE setting_key=? AND repo_id=?", value, key, repoID) | ||
if err != nil { | ||
return err | ||
} | ||
rows, _ := res.RowsAffected() | ||
if rows > 0 { | ||
// the existing row is updated, so we can return | ||
return nil | ||
} | ||
|
||
// in case the value isn't changed, update would return 0 rows changed, so we need this check | ||
has, err := e.Exist(&Setting{RepoID: repoID, SettingKey: key}) | ||
if err != nil { | ||
return err | ||
} | ||
if has { | ||
return nil | ||
} | ||
|
||
// if no existing row, insert a new row | ||
_, err = e.Insert(&Setting{RepoID: repoID, SettingKey: key, SettingValue: value}) | ||
return err | ||
}) | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// 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 repo | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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 repo | ||
|
||
import ( | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models/unittest" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSettings(t *testing.T) { | ||
keyName := "test_repo_setting" | ||
assert.NoError(t, unittest.PrepareTestDatabase()) | ||
|
||
newSetting := &Setting{RepoID: 99, SettingKey: keyName, SettingValue: "Gitea Repo Setting Test"} | ||
|
||
// create setting | ||
err := SetRepoSetting(newSetting.RepoID, newSetting.SettingKey, newSetting.SettingValue) | ||
assert.NoError(t, err) | ||
// test about saving unchanged values | ||
err = SetRepoSetting(newSetting.RepoID, newSetting.SettingKey, newSetting.SettingValue) | ||
assert.NoError(t, err) | ||
|
||
// get specific setting | ||
settings, err := GetRepoSettings(99, []string{keyName}) | ||
assert.NoError(t, err) | ||
assert.Len(t, settings, 1) | ||
assert.EqualValues(t, newSetting.SettingValue, settings[keyName].SettingValue) | ||
|
||
settingValue, err := GetRepoSetting(99, keyName) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, newSetting.SettingValue, settingValue) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the case with the default value could be added... |
||
|
||
settingValue, err = GetRepoSetting(99, "no_such") | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, "", settingValue) | ||
|
||
// updated setting | ||
updatedSetting := &Setting{RepoID: 99, SettingKey: keyName, SettingValue: "Updated"} | ||
err = SetRepoSetting(updatedSetting.RepoID, updatedSetting.SettingKey, updatedSetting.SettingValue) | ||
assert.NoError(t, err) | ||
|
||
// get all settings | ||
settings, err = GetRepoAllSettings(99) | ||
assert.NoError(t, err) | ||
assert.Len(t, settings, 1) | ||
assert.EqualValues(t, updatedSetting.SettingValue, settings[updatedSetting.SettingKey].SettingValue) | ||
|
||
// delete setting | ||
err = DeleteRepoSetting(99, keyName) | ||
assert.NoError(t, err) | ||
settings, err = GetRepoAllSettings(99) | ||
assert.NoError(t, err) | ||
assert.Len(t, settings, 0) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.