1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-08-07 02:45:22 +02:00

chore(cleanup): replaces unnecessary calls to formatting functions by non-formatting equivalents (#7994)

This PR replaces unnecessary calls to formatting functions (`fmt.Printf`, `fmt.Errorf`, ...) by non-formatting equivalents.
Resolves #7967

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7994
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: chavacava <chavacava@noreply.codeberg.org>
Co-committed-by: chavacava <chavacava@noreply.codeberg.org>
This commit is contained in:
chavacava 2025-05-29 17:34:29 +02:00 committed by Gusted
parent 25f3f8e1d2
commit 99d697263f
126 changed files with 340 additions and 281 deletions

View file

@ -5,6 +5,7 @@ package actions
import (
"context"
"errors"
"fmt"
"slices"
"strings"
@ -355,7 +356,7 @@ func UpdateRunWithoutNotification(ctx context.Context, run *ActionRun, cols ...s
return err
}
if affected == 0 {
return fmt.Errorf("run has changed")
return errors.New("run has changed")
// It's impossible that the run is not found, since Gitea never deletes runs.
}

View file

@ -7,6 +7,7 @@ package activities
import (
"context"
"errors"
"fmt"
"net/url"
"path"
@ -458,7 +459,7 @@ type GetFeedsOptions struct {
// GetFeeds returns actions according to the provided options
func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, int64, error) {
if opts.RequestedUser == nil && opts.RequestedTeam == nil && opts.RequestedRepo == nil {
return nil, 0, fmt.Errorf("need at least one of these filters: RequestedUser, RequestedTeam, RequestedRepo")
return nil, 0, errors.New("need at least one of these filters: RequestedUser, RequestedTeam, RequestedRepo")
}
cond, err := activityQueryCondition(ctx, opts)

View file

@ -5,6 +5,7 @@ package asymkey
import (
"context"
"errors"
"fmt"
"strings"
"time"
@ -209,7 +210,7 @@ func parseGPGKey(ctx context.Context, ownerID int64, e *openpgp.Entity, verified
// deleteGPGKey does the actual key deletion
func deleteGPGKey(ctx context.Context, keyID string) (int64, error) {
if keyID == "" {
return 0, fmt.Errorf("empty KeyId forbidden") // Should never happen but just to be sure
return 0, errors.New("empty KeyId forbidden") // Should never happen but just to be sure
}
// Delete imported key
n, err := db.GetEngine(ctx).Where("key_id=?", keyID).Delete(new(GPGKeyImport))

View file

@ -7,6 +7,7 @@ import (
"bytes"
"crypto"
"encoding/base64"
"errors"
"fmt"
"hash"
"io"
@ -75,7 +76,7 @@ func base64DecPubKey(content string) (*packet.PublicKey, error) {
// Check type
pkey, ok := p.(*packet.PublicKey)
if !ok {
return nil, fmt.Errorf("key is not a public key")
return nil, errors.New("key is not a public key")
}
return pkey, nil
}
@ -122,15 +123,15 @@ func readArmoredSign(r io.Reader) (body io.Reader, err error) {
func extractSignature(s string) (*packet.Signature, error) {
r, err := readArmoredSign(strings.NewReader(s))
if err != nil {
return nil, fmt.Errorf("Failed to read signature armor")
return nil, errors.New("Failed to read signature armor")
}
p, err := packet.Read(r)
if err != nil {
return nil, fmt.Errorf("Failed to read signature packet")
return nil, errors.New("Failed to read signature packet")
}
sig, ok := p.(*packet.Signature)
if !ok {
return nil, fmt.Errorf("Packet is not a signature")
return nil, errors.New("Packet is not a signature")
}
return sig, nil
}

View file

@ -6,6 +6,7 @@ package asymkey
import (
"context"
"errors"
"fmt"
"hash"
"strings"
@ -316,7 +317,7 @@ func verifyWithGPGSettings(ctx context.Context, gpgSettings *git.GPGSettings, si
func verifySign(s *packet.Signature, h hash.Hash, k *GPGKey) error {
// Check if key can sign
if !k.CanSign {
return fmt.Errorf("key can not sign")
return errors.New("key can not sign")
}
// Decode key
pkey, err := base64DecPubKey(k.Content)

View file

@ -10,6 +10,7 @@ import (
"encoding/base64"
"encoding/binary"
"encoding/pem"
"errors"
"fmt"
"math/big"
"os"
@ -93,7 +94,7 @@ func parseKeyString(content string) (string, error) {
block, _ := pem.Decode([]byte(content))
if block == nil {
return "", fmt.Errorf("failed to parse PEM block containing the public key")
return "", errors.New("failed to parse PEM block containing the public key")
}
if strings.Contains(block.Type, "PRIVATE") {
return "", ErrKeyIsPrivate

View file

@ -35,7 +35,7 @@ func Test_FederationHostValidation(t *testing.T) {
HostSchema: "https",
}
if res, _ := validation.IsValid(sut); res {
t.Errorf("sut should be invalid: HostFqdn empty")
t.Error("sut should be invalid: HostFqdn empty")
}
sut = FederationHost{
@ -48,7 +48,7 @@ func Test_FederationHostValidation(t *testing.T) {
HostSchema: "https",
}
if res, _ := validation.IsValid(sut); res {
t.Errorf("sut should be invalid: HostFqdn too long (len=256)")
t.Error("sut should be invalid: HostFqdn too long (len=256)")
}
sut = FederationHost{
@ -59,7 +59,7 @@ func Test_FederationHostValidation(t *testing.T) {
HostSchema: "https",
}
if res, _ := validation.IsValid(sut); res {
t.Errorf("sut should be invalid: NodeInfo invalid")
t.Error("sut should be invalid: NodeInfo invalid")
}
sut = FederationHost{
@ -72,7 +72,7 @@ func Test_FederationHostValidation(t *testing.T) {
HostSchema: "https",
}
if res, _ := validation.IsValid(sut); res {
t.Errorf("sut should be invalid: Future timestamp")
t.Error("sut should be invalid: Future timestamp")
}
sut = FederationHost{
@ -85,6 +85,6 @@ func Test_FederationHostValidation(t *testing.T) {
HostSchema: "https",
}
if res, _ := validation.IsValid(sut); res {
t.Errorf("sut should be invalid: HostFqdn lower case")
t.Error("sut should be invalid: HostFqdn lower case")
}
}

View file

@ -4,7 +4,7 @@
package forgefed
import (
"fmt"
"errors"
"reflect"
"strings"
"testing"
@ -28,7 +28,7 @@ func Test_NodeInfoWellKnownUnmarshalJSON(t *testing.T) {
},
"empty": {
item: []byte(``),
wantErr: fmt.Errorf("cannot parse JSON: cannot parse empty string; unparsed tail: \"\""),
wantErr: errors.New("cannot parse JSON: cannot parse empty string; unparsed tail: \"\""),
},
}
@ -74,7 +74,7 @@ func Test_NewNodeInfoWellKnown(t *testing.T) {
_, err := NewNodeInfoWellKnown([]byte(`invalid`))
if err == nil {
t.Errorf("error was expected here")
t.Error("error was expected here")
}
}
@ -87,6 +87,6 @@ func Test_NewNodeInfo(t *testing.T) {
_, err := NewNodeInfo([]byte(`invalid`))
if err == nil {
t.Errorf("error was expected here")
t.Error("error was expected here")
}
}

View file

@ -5,6 +5,7 @@ package forgejo_migrations //nolint:revive
import (
"context"
"errors"
"fmt"
"os"
@ -130,7 +131,7 @@ func EnsureUpToDate(x *xorm.Engine) error {
}
if currentDB < 0 {
return fmt.Errorf("database has not been initialized")
return errors.New("database has not been initialized")
}
expected := ExpectedVersion()

View file

@ -6,6 +6,7 @@ package issues
import (
"context"
"errors"
"fmt"
"html/template"
"regexp"
@ -804,7 +805,7 @@ func (issue *Issue) MovePin(ctx context.Context, newPosition int) error {
}
if newPosition < 1 {
return fmt.Errorf("The Position can't be lower than 1")
return errors.New("The Position can't be lower than 1")
}
dbctx, committer, err := db.TxContext(ctx)

View file

@ -6,6 +6,7 @@ package issues
import (
"context"
"errors"
"fmt"
"strings"
@ -338,10 +339,10 @@ func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssue
}
if opts.Issue.Index <= 0 {
return fmt.Errorf("no issue index provided")
return errors.New("no issue index provided")
}
if opts.Issue.ID > 0 {
return fmt.Errorf("issue exist")
return errors.New("issue exist")
}
opts.Issue.Created = timeutil.TimeStampNanoNow()

View file

@ -6,6 +6,7 @@ package issues
import (
"context"
"errors"
"fmt"
"io"
"regexp"
@ -795,7 +796,7 @@ func (pr *PullRequest) GetWorkInProgressPrefix(ctx context.Context) string {
// UpdateCommitDivergence update Divergence of a pull request
func (pr *PullRequest) UpdateCommitDivergence(ctx context.Context, ahead, behind int) error {
if pr.ID == 0 {
return fmt.Errorf("pull ID is 0")
return errors.New("pull ID is 0")
}
pr.CommitsAhead = ahead
pr.CommitsBehind = behind

View file

@ -5,6 +5,7 @@ package issues
import (
"context"
"errors"
"fmt"
"slices"
"strings"
@ -349,7 +350,7 @@ func CreateReview(ctx context.Context, opts CreateReviewOptions) (*Review, error
review.Type = ReviewTypeRequest
review.ReviewerTeamID = opts.ReviewerTeam.ID
} else {
return nil, fmt.Errorf("provide either reviewer or reviewer team")
return nil, errors.New("provide either reviewer or reviewer team")
}
if _, err := sess.Insert(review); err != nil {
@ -908,7 +909,7 @@ func MarkConversation(ctx context.Context, comment *Comment, doer *user_model.Us
// the PR writer , offfcial reviewer and poster can do it
func CanMarkConversation(ctx context.Context, issue *Issue, doer *user_model.User) (permResult bool, err error) {
if doer == nil || issue == nil {
return false, fmt.Errorf("issue or doer is nil")
return false, errors.New("issue or doer is nil")
}
if doer.ID != issue.PosterID {
@ -945,11 +946,11 @@ func DeleteReview(ctx context.Context, r *Review) error {
defer committer.Close()
if r.ID == 0 {
return fmt.Errorf("review is not allowed to be 0")
return errors.New("review is not allowed to be 0")
}
if r.Type == ReviewTypeRequest {
return fmt.Errorf("review request can not be deleted using this method")
return errors.New("review request can not be deleted using this method")
}
opts := FindCommentsOptions{

View file

@ -74,7 +74,7 @@ func RecreateTable(sess *xorm.Session, bean any) error {
}
newTableColumns := table.Columns()
if len(newTableColumns) == 0 {
return fmt.Errorf("no columns in new table")
return errors.New("no columns in new table")
}
hasID := false
for _, column := range newTableColumns {

View file

@ -6,6 +6,7 @@ package migrations
import (
"context"
"errors"
"fmt"
"forgejo.org/models/db"
@ -412,7 +413,7 @@ func EnsureUpToDate(x *xorm.Engine) error {
}
if currentDB < 0 {
return fmt.Errorf("database has not been initialized")
return errors.New("database has not been initialized")
}
if minDBVersion > currentDB {

View file

@ -5,6 +5,7 @@ package v1_13 //nolint
import (
"context"
"errors"
"fmt"
"strings"
@ -83,7 +84,7 @@ func SetDefaultPasswordToArgon2(x *xorm.Engine) error {
newTableColumns := table.Columns()
if len(newTableColumns) == 0 {
return fmt.Errorf("no columns in new table")
return errors.New("no columns in new table")
}
hasID := false
for _, column := range newTableColumns {

View file

@ -4,7 +4,7 @@
package v1_14 //nolint
import (
"fmt"
"errors"
"strconv"
"forgejo.org/modules/log"
@ -72,7 +72,7 @@ func UpdateCodeCommentReplies(x *xorm.Engine) error {
case setting.Database.Type.IsSQLite3():
sqlCmd = sqlSelect + sqlTail + " LIMIT " + strconv.Itoa(batchSize) + " OFFSET " + strconv.Itoa(start)
default:
return fmt.Errorf("Unsupported database type")
return errors.New("Unsupported database type")
}
if err := sess.SQL(sqlCmd).Find(&comments); err != nil {

View file

@ -73,12 +73,12 @@ func Test_DeleteOrphanedIssueLabels(t *testing.T) {
// Now test what is left
if _, ok := postMigration[2]; ok {
t.Errorf("Orphaned Label[2] survived the migration")
t.Error("Orphaned Label[2] survived the migration")
return
}
if _, ok := postMigration[5]; ok {
t.Errorf("Orphaned Label[5] survived the migration")
t.Error("Orphaned Label[5] survived the migration")
return
}

View file

@ -5,6 +5,7 @@ package v1_17 //nolint
import (
"context"
"errors"
"fmt"
"forgejo.org/models/migrations/base"
@ -29,7 +30,7 @@ func DropOldCredentialIDColumn(x *xorm.Engine) error {
}
if !credentialIDBytesExists {
// looks like 221 hasn't properly run
return fmt.Errorf("webauthn_credential does not have a credential_id_bytes column... it is not safe to run this migration")
return errors.New("webauthn_credential does not have a credential_id_bytes column... it is not safe to run this migration")
}
// Create webauthnCredential table

View file

@ -5,7 +5,7 @@ package v1_21 //nolint
import (
"context"
"fmt"
"errors"
"forgejo.org/models/db"
"forgejo.org/modules/timeutil"
@ -57,7 +57,7 @@ func AddBranchTable(x *xorm.Engine) error {
if err != nil {
return err
} else if !has {
return fmt.Errorf("no admin user found")
return errors.New("no admin user found")
}
branches := make([]Branch, 0, 100)

View file

@ -145,7 +145,7 @@ func NewColumn(ctx context.Context, column *Column) error {
return err
}
if res.ColumnCount >= maxProjectColumns {
return fmt.Errorf("NewBoard: maximum number of columns reached")
return errors.New("NewBoard: maximum number of columns reached")
}
column.Sorting = int8(util.Iif(res.ColumnCount > 0, res.MaxSorting+1, 0))
_, err := db.GetEngine(ctx).Insert(column)
@ -170,7 +170,7 @@ func deleteColumnByID(ctx context.Context, columnID int64) error {
}
if column.Default {
return fmt.Errorf("deleteColumnByID: cannot delete default column")
return errors.New("deleteColumnByID: cannot delete default column")
}
// move all issues to the default column

View file

@ -5,7 +5,7 @@ package project
import (
"context"
"fmt"
"errors"
"forgejo.org/models/db"
"forgejo.org/modules/log"
@ -73,7 +73,7 @@ func MoveIssuesOnProjectColumn(ctx context.Context, column *Column, sortedIssueI
return err
}
if int(count) != len(sortedIssueIDs) {
return fmt.Errorf("all issues have to be added to a project first")
return errors.New("all issues have to be added to a project first")
}
for sorting, issueID := range sortedIssueIDs {
@ -88,7 +88,7 @@ func MoveIssuesOnProjectColumn(ctx context.Context, column *Column, sortedIssueI
func (c *Column) moveIssuesToAnotherColumn(ctx context.Context, newColumn *Column) error {
if c.ProjectID != newColumn.ProjectID {
return fmt.Errorf("columns have to be in the same project")
return errors.New("columns have to be in the same project")
}
if c.ID == newColumn.ID {

View file

@ -5,6 +5,7 @@ package repo
import (
"context"
"errors"
"fmt"
"net/url"
"path"
@ -232,7 +233,7 @@ func DeleteAttachmentsByComment(ctx context.Context, commentID int64, remove boo
// UpdateAttachmentByUUID Updates attachment via uuid
func UpdateAttachmentByUUID(ctx context.Context, attach *Attachment, cols ...string) error {
if attach.UUID == "" {
return fmt.Errorf("attachment uuid should be not blank")
return errors.New("attachment uuid should be not blank")
}
if attach.ExternalURL != "" && !validation.IsValidExternalURL(attach.ExternalURL) {
return ErrInvalidExternalURL{ExternalURL: attach.ExternalURL}

View file

@ -26,6 +26,6 @@ func Test_FollowingRepoValidation(t *testing.T) {
URI: "http://localhost:3000/api/v1/activitypub/repo-id/1",
}
if res, _ := validation.IsValid(sut); res {
t.Errorf("sut should be invalid")
t.Error("sut should be invalid")
}
}

View file

@ -6,6 +6,7 @@ package repo
import (
"context"
"errors"
"fmt"
"html/template"
"net"
@ -820,7 +821,7 @@ func GetRepositoryByURL(ctx context.Context, repoURL string) (*Repository, error
pathSegments := getRepositoryURLPathSegments(repoURL)
if len(pathSegments) != 2 {
return nil, fmt.Errorf("unknown or malformed repository URL")
return nil, errors.New("unknown or malformed repository URL")
}
ownerName := pathSegments[0]

View file

@ -24,6 +24,6 @@ func Test_FederatedUserValidation(t *testing.T) {
FederationHostID: 1,
}
if res, _ := validation.IsValid(sut); res {
t.Errorf("sut should be invalid")
t.Error("sut should be invalid")
}
}

View file

@ -5,6 +5,7 @@ package user
import (
"context"
"errors"
"fmt"
"strings"
@ -114,10 +115,10 @@ func GetUserAllSettings(ctx context.Context, uid int64) (map[string]*Setting, er
func validateUserSettingKey(key string) error {
if len(key) == 0 {
return fmt.Errorf("setting key must be set")
return errors.New("setting key must be set")
}
if strings.ToLower(key) != key {
return fmt.Errorf("setting key should be lowercase")
return errors.New("setting key should be lowercase")
}
return nil
}