Sort secret lists and events (#1223)

close  #1176
This commit is contained in:
6543 2022-09-29 22:46:20 +02:00 committed by GitHub
parent 203ef518df
commit 612eee8269
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 3 deletions

View file

@ -25,6 +25,12 @@ const (
EventManual WebhookEvent = "manual"
)
type WebhookEventList []WebhookEvent
func (wel WebhookEventList) Len() int { return len(wel) }
func (wel WebhookEventList) Swap(i, j int) { wel[i], wel[j] = wel[j], wel[i] }
func (wel WebhookEventList) Less(i, j int) bool { return wel[i] < wel[j] }
func ValidateWebhookEvent(s WebhookEvent) bool {
switch s {
case EventPush, EventPull, EventTag, EventDeploy, EventCron, EventManual:

View file

@ -20,6 +20,7 @@ import (
"fmt"
"path/filepath"
"regexp"
"sort"
)
var (
@ -83,6 +84,11 @@ func (Secret) TableName() string {
return "secrets"
}
// BeforeInsert will sort events before inserted into database
func (s *Secret) BeforeInsert() {
s.Events = sortEvents(s.Events)
}
// Global secret.
func (s Secret) Global() bool {
return s.RepoID == 0 && s.Owner == ""
@ -151,6 +157,11 @@ func (s *Secret) Copy() *Secret {
RepoID: s.RepoID,
Name: s.Name,
Images: s.Images,
Events: s.Events,
Events: sortEvents(s.Events),
}
}
func sortEvents(wel WebhookEventList) WebhookEventList {
sort.Sort(wel)
return wel
}

View file

@ -20,6 +20,8 @@ import (
"xorm.io/builder"
)
const orderSecretsBy = "secret_name"
func (s storage) SecretFind(repo *model.Repo, name string) (*model.Secret, error) {
secret := &model.Secret{
RepoID: repo.ID,
@ -35,7 +37,7 @@ func (s storage) SecretList(repo *model.Repo, includeGlobalAndOrgSecrets bool) (
cond = cond.Or(builder.Eq{"secret_owner": repo.Owner}).
Or(builder.And(builder.Eq{"secret_owner": ""}, builder.Eq{"secret_repo_id": 0}))
}
return secrets, s.engine.Where(cond).Find(&secrets)
return secrets, s.engine.Where(cond).OrderBy(orderSecretsBy).Find(&secrets)
}
func (s storage) SecretCreate(secret *model.Secret) error {
@ -76,5 +78,5 @@ func (s storage) GlobalSecretFind(name string) (*model.Secret, error) {
func (s storage) GlobalSecretList() ([]*model.Secret, error) {
secrets := make([]*model.Secret, 0, perPage)
return secrets, s.engine.Where(builder.And(builder.Eq{"secret_owner": ""}, builder.Eq{"secret_repo_id": 0})).Find(&secrets)
return secrets, s.engine.Where(builder.And(builder.Eq{"secret_owner": ""}, builder.Eq{"secret_repo_id": 0})).OrderBy(orderSecretsBy).Find(&secrets)
}