Skip to content

Commit

Permalink
Less naked returns (#25713)
Browse files Browse the repository at this point in the history
just a step towards  #25655

and some related refactoring
  • Loading branch information
6543 authored Jul 7, 2023
1 parent b1eb167 commit 8995046
Show file tree
Hide file tree
Showing 32 changed files with 254 additions and 239 deletions.
9 changes: 5 additions & 4 deletions models/asymkey/gpg_key_commit_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,9 @@ func hashAndVerifyForKeyID(sig *packet.Signature, payload string, committer *use

// CalculateTrustStatus will calculate the TrustStatus for a commit verification within a repository
// There are several trust models in Gitea
func CalculateTrustStatus(verification *CommitVerification, repoTrustModel repo_model.TrustModelType, isOwnerMemberCollaborator func(*user_model.User) (bool, error), keyMap *map[string]bool) (err error) {
func CalculateTrustStatus(verification *CommitVerification, repoTrustModel repo_model.TrustModelType, isOwnerMemberCollaborator func(*user_model.User) (bool, error), keyMap *map[string]bool) error {
if !verification.Verified {
return
return nil
}

// In the Committer trust model a signature is trusted if it matches the committer
Expand All @@ -475,7 +475,7 @@ func CalculateTrustStatus(verification *CommitVerification, repoTrustModel repo_
verification.SigningUser.Email == verification.CommittingUser.Email) {
verification.TrustStatus = "trusted"
}
return
return nil
}

// Now we drop to the more nuanced trust models...
Expand All @@ -490,10 +490,11 @@ func CalculateTrustStatus(verification *CommitVerification, repoTrustModel repo_
verification.SigningUser.Email != verification.CommittingUser.Email) {
verification.TrustStatus = "untrusted"
}
return
return nil
}

// Check we actually have a GPG SigningKey
var err error
if verification.SigningKey != nil {
var isMember bool
if keyMap != nil {
Expand Down
7 changes: 4 additions & 3 deletions models/auth/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,10 @@ func (code *OAuth2AuthorizationCode) TableName() string {
}

// GenerateRedirectURI generates a redirect URI for a successful authorization request. State will be used if not empty.
func (code *OAuth2AuthorizationCode) GenerateRedirectURI(state string) (redirect *url.URL, err error) {
if redirect, err = url.Parse(code.RedirectURI); err != nil {
return
func (code *OAuth2AuthorizationCode) GenerateRedirectURI(state string) (*url.URL, error) {
redirect, err := url.Parse(code.RedirectURI)
if err != nil {
return nil, err
}
q := redirect.Query()
if state != "" {
Expand Down
8 changes: 4 additions & 4 deletions models/migrations/v1_11/v106.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ type Watch struct {
Mode RepoWatchMode `xorm:"SMALLINT NOT NULL DEFAULT 1"`
}

func AddModeColumnToWatch(x *xorm.Engine) (err error) {
if err = x.Sync2(new(Watch)); err != nil {
return
func AddModeColumnToWatch(x *xorm.Engine) error {
if err := x.Sync2(new(Watch)); err != nil {
return err
}
_, err = x.Exec("UPDATE `watch` SET `mode` = 1")
_, err := x.Exec("UPDATE `watch` SET `mode` = 1")
return err
}
16 changes: 8 additions & 8 deletions models/migrations/v1_13/v143.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ func RecalculateStars(x *xorm.Engine) (err error) {

for start := 0; ; start += batchSize {
users := make([]User, 0, batchSize)
if err = sess.Limit(batchSize, start).Where("type = ?", 0).Cols("id").Find(&users); err != nil {
return
if err := sess.Limit(batchSize, start).Where("type = ?", 0).Cols("id").Find(&users); err != nil {
return err
}
if len(users) == 0 {
break
}

if err = sess.Begin(); err != nil {
return
if err := sess.Begin(); err != nil {
return err
}

for _, user := range users {
if _, err = sess.Exec("UPDATE `user` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE uid=?) WHERE id=?", user.ID, user.ID); err != nil {
return
if _, err := sess.Exec("UPDATE `user` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE uid=?) WHERE id=?", user.ID, user.ID); err != nil {
return err
}
}

if err = sess.Commit(); err != nil {
return
if err := sess.Commit(); err != nil {
return err
}
}

Expand Down
18 changes: 9 additions & 9 deletions models/migrations/v1_15/v180.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,25 @@ func DeleteMigrationCredentials(x *xorm.Engine) (err error) {

for start := 0; ; start += batchSize {
tasks := make([]*Task, 0, batchSize)
if err = sess.Limit(batchSize, start).Where(cond, 0).Find(&tasks); err != nil {
return
if err := sess.Limit(batchSize, start).Where(cond, 0).Find(&tasks); err != nil {
return err
}
if len(tasks) == 0 {
break
}
if err = sess.Begin(); err != nil {
return
if err := sess.Begin(); err != nil {
return err
}
for _, t := range tasks {
if t.PayloadContent, err = removeCredentials(t.PayloadContent); err != nil {
return
return err
}
if _, err = sess.ID(t.ID).Cols("payload_content").Update(t); err != nil {
return
if _, err := sess.ID(t.ID).Cols("payload_content").Update(t); err != nil {
return err
}
}
if err = sess.Commit(); err != nil {
return
if err := sess.Commit(); err != nil {
return err
}
}
return err
Expand Down
31 changes: 15 additions & 16 deletions models/migrations/v1_15/v181.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"xorm.io/xorm"
)

func AddPrimaryEmail2EmailAddress(x *xorm.Engine) (err error) {
func AddPrimaryEmail2EmailAddress(x *xorm.Engine) error {
type User struct {
ID int64 `xorm:"pk autoincr"`
Email string `xorm:"NOT NULL"`
Expand All @@ -26,12 +26,12 @@ func AddPrimaryEmail2EmailAddress(x *xorm.Engine) (err error) {
}

// Add lower_email and is_primary columns
if err = x.Table("email_address").Sync2(new(EmailAddress1)); err != nil {
return
if err := x.Table("email_address").Sync2(new(EmailAddress1)); err != nil {
return err
}

if _, err = x.Exec("UPDATE email_address SET lower_email=LOWER(email), is_primary=?", false); err != nil {
return
if _, err := x.Exec("UPDATE email_address SET lower_email=LOWER(email), is_primary=?", false); err != nil {
return err
}

type EmailAddress struct {
Expand All @@ -44,8 +44,8 @@ func AddPrimaryEmail2EmailAddress(x *xorm.Engine) (err error) {
}

// change lower_email as unique
if err = x.Sync2(new(EmailAddress)); err != nil {
return
if err := x.Sync2(new(EmailAddress)); err != nil {
return err
}

sess := x.NewSession()
Expand All @@ -55,34 +55,33 @@ func AddPrimaryEmail2EmailAddress(x *xorm.Engine) (err error) {

for start := 0; ; start += batchSize {
users := make([]*User, 0, batchSize)
if err = sess.Limit(batchSize, start).Find(&users); err != nil {
return
if err := sess.Limit(batchSize, start).Find(&users); err != nil {
return err
}
if len(users) == 0 {
break
}

for _, user := range users {
var exist bool
exist, err = sess.Where("email=?", user.Email).Table("email_address").Exist()
exist, err := sess.Where("email=?", user.Email).Table("email_address").Exist()
if err != nil {
return
return err
}
if !exist {
if _, err = sess.Insert(&EmailAddress{
if _, err := sess.Insert(&EmailAddress{
UID: user.ID,
Email: user.Email,
LowerEmail: strings.ToLower(user.Email),
IsActivated: user.IsActive,
IsPrimary: true,
}); err != nil {
return
return err
}
} else {
if _, err = sess.Where("email=?", user.Email).Cols("is_primary").Update(&EmailAddress{
if _, err := sess.Where("email=?", user.Email).Cols("is_primary").Update(&EmailAddress{
IsPrimary: true,
}); err != nil {
return
return err
}
}
}
Expand Down
Loading

0 comments on commit 8995046

Please sign in to comment.