Skip to content

Commit fa05dd2

Browse files
wxiaoguangpull[bot]
authored andcommitted
Refactor some legacy code and remove unused code (#28622)
1. use slices.Contains, remove Int64sContains 2. use HashEmail, remove base.EncodeMD5 3. remove BasicAuthEncode, IsLetter
1 parent 5a4eb10 commit fa05dd2

File tree

7 files changed

+14
-76
lines changed

7 files changed

+14
-76
lines changed

models/avatars/avatar.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package avatars
55

66
import (
77
"context"
8+
"crypto/md5"
9+
"encoding/hex"
810
"fmt"
911
"net/url"
1012
"path"
@@ -13,7 +15,6 @@ import (
1315
"sync/atomic"
1416

1517
"code.gitea.io/gitea/models/db"
16-
"code.gitea.io/gitea/modules/base"
1718
"code.gitea.io/gitea/modules/cache"
1819
"code.gitea.io/gitea/modules/log"
1920
"code.gitea.io/gitea/modules/setting"
@@ -90,7 +91,9 @@ func DefaultAvatarLink() string {
9091

9192
// HashEmail hashes email address to MD5 string. https://en.gravatar.com/site/implement/hash/
9293
func HashEmail(email string) string {
93-
return base.EncodeMD5(strings.ToLower(strings.TrimSpace(email)))
94+
m := md5.New()
95+
_, _ = m.Write([]byte(strings.ToLower(strings.TrimSpace(email))))
96+
return hex.EncodeToString(m.Sum(nil))
9497
}
9598

9699
// GetEmailForHash converts a provided md5sum to the email

models/git/protected_branch.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
repo_model "code.gitea.io/gitea/models/repo"
1818
"code.gitea.io/gitea/models/unit"
1919
user_model "code.gitea.io/gitea/models/user"
20-
"code.gitea.io/gitea/modules/base"
2120
"code.gitea.io/gitea/modules/log"
2221
"code.gitea.io/gitea/modules/timeutil"
2322
"code.gitea.io/gitea/modules/util"
@@ -127,7 +126,7 @@ func (protectBranch *ProtectedBranch) CanUserPush(ctx context.Context, user *use
127126
return writeAccess
128127
}
129128

130-
if base.Int64sContains(protectBranch.WhitelistUserIDs, user.ID) {
129+
if slices.Contains(protectBranch.WhitelistUserIDs, user.ID) {
131130
return true
132131
}
133132

@@ -150,7 +149,7 @@ func IsUserMergeWhitelisted(ctx context.Context, protectBranch *ProtectedBranch,
150149
return permissionInRepo.CanWrite(unit.TypeCode)
151150
}
152151

153-
if base.Int64sContains(protectBranch.MergeWhitelistUserIDs, userID) {
152+
if slices.Contains(protectBranch.MergeWhitelistUserIDs, userID) {
154153
return true
155154
}
156155

@@ -182,7 +181,7 @@ func IsUserOfficialReviewer(ctx context.Context, protectBranch *ProtectedBranch,
182181
return writeAccess, nil
183182
}
184183

185-
if base.Int64sContains(protectBranch.ApprovalsWhitelistUserIDs, user.ID) {
184+
if slices.Contains(protectBranch.ApprovalsWhitelistUserIDs, user.ID) {
186185
return true, nil
187186
}
188187

models/git/protected_tag.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ package git
66
import (
77
"context"
88
"regexp"
9+
"slices"
910
"strings"
1011

1112
"code.gitea.io/gitea/models/db"
1213
"code.gitea.io/gitea/models/organization"
13-
"code.gitea.io/gitea/modules/base"
1414
"code.gitea.io/gitea/modules/timeutil"
1515

1616
"github.com/gobwas/glob"
@@ -76,7 +76,7 @@ func DeleteProtectedTag(ctx context.Context, pt *ProtectedTag) error {
7676

7777
// IsUserAllowedModifyTag returns true if the user is allowed to modify the tag
7878
func IsUserAllowedModifyTag(ctx context.Context, pt *ProtectedTag, userID int64) (bool, error) {
79-
if base.Int64sContains(pt.AllowlistUserIDs, userID) {
79+
if slices.Contains(pt.AllowlistUserIDs, userID) {
8080
return true, nil
8181
}
8282

models/issues/review.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package issues
66
import (
77
"context"
88
"fmt"
9+
"slices"
910
"strings"
1011

1112
"code.gitea.io/gitea/models/db"
@@ -15,7 +16,6 @@ import (
1516
access_model "code.gitea.io/gitea/models/perm/access"
1617
"code.gitea.io/gitea/models/unit"
1718
user_model "code.gitea.io/gitea/models/user"
18-
"code.gitea.io/gitea/modules/base"
1919
"code.gitea.io/gitea/modules/structs"
2020
"code.gitea.io/gitea/modules/timeutil"
2121
"code.gitea.io/gitea/modules/util"
@@ -279,7 +279,7 @@ func IsOfficialReviewerTeam(ctx context.Context, issue *Issue, team *organizatio
279279
return team.UnitAccessMode(ctx, unit.TypeCode) >= perm.AccessModeWrite, nil
280280
}
281281

282-
return base.Int64sContains(pb.ApprovalsWhitelistTeamIDs, team.ID), nil
282+
return slices.Contains(pb.ApprovalsWhitelistTeamIDs, team.ID), nil
283283
}
284284

285285
// CreateReview creates a new review based on opts

modules/base/tool.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package base
55

66
import (
7-
"crypto/md5"
87
"crypto/sha1"
98
"encoding/base64"
109
"encoding/hex"
@@ -16,7 +15,6 @@ import (
1615
"strconv"
1716
"strings"
1817
"time"
19-
"unicode"
2018
"unicode/utf8"
2119

2220
"code.gitea.io/gitea/modules/git"
@@ -27,13 +25,6 @@ import (
2725
"github.com/minio/sha256-simd"
2826
)
2927

30-
// EncodeMD5 encodes string to md5 hex value.
31-
func EncodeMD5(str string) string {
32-
m := md5.New()
33-
_, _ = m.Write([]byte(str))
34-
return hex.EncodeToString(m.Sum(nil))
35-
}
36-
3728
// EncodeSha1 string to sha1 hex value.
3829
func EncodeSha1(str string) string {
3930
h := sha1.New()
@@ -70,11 +61,6 @@ func BasicAuthDecode(encoded string) (string, string, error) {
7061
return auth[0], auth[1], nil
7162
}
7263

73-
// BasicAuthEncode encode basic auth string
74-
func BasicAuthEncode(username, password string) string {
75-
return base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
76-
}
77-
7864
// VerifyTimeLimitCode verify time limit code
7965
func VerifyTimeLimitCode(data string, minutes int, code string) bool {
8066
if len(code) <= 18 {
@@ -184,22 +170,6 @@ func Int64sToStrings(ints []int64) []string {
184170
return strs
185171
}
186172

187-
// Int64sContains returns if a int64 in a slice of int64
188-
func Int64sContains(intsSlice []int64, a int64) bool {
189-
for _, c := range intsSlice {
190-
if c == a {
191-
return true
192-
}
193-
}
194-
return false
195-
}
196-
197-
// IsLetter reports whether the rune is a letter (category L).
198-
// https://github.com/golang/go/blob/c3b4918/src/go/scanner/scanner.go#L342
199-
func IsLetter(ch rune) bool {
200-
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= 0x80 && unicode.IsLetter(ch)
201-
}
202-
203173
// EntryIcon returns the octicon class for displaying files/directories
204174
func EntryIcon(entry *git.TreeEntry) string {
205175
switch {

modules/base/tool_test.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ import (
1111
"github.com/stretchr/testify/assert"
1212
)
1313

14-
func TestEncodeMD5(t *testing.T) {
15-
assert.Equal(t,
16-
"3858f62230ac3c915f300c664312c63f",
17-
EncodeMD5("foobar"),
18-
)
19-
}
20-
2114
func TestEncodeSha1(t *testing.T) {
2215
assert.Equal(t,
2316
"8843d7f92416211de9ebb963ff4ce28125932878",
@@ -52,11 +45,6 @@ func TestBasicAuthDecode(t *testing.T) {
5245
assert.Error(t, err)
5346
}
5447

55-
func TestBasicAuthEncode(t *testing.T) {
56-
assert.Equal(t, "Zm9vOmJhcg==", BasicAuthEncode("foo", "bar"))
57-
assert.Equal(t, "MjM6IjotLS0t", BasicAuthEncode("23:\"", "----"))
58-
}
59-
6048
func TestVerifyTimeLimitCode(t *testing.T) {
6149
tc := []struct {
6250
data string
@@ -167,29 +155,6 @@ func TestInt64sToStrings(t *testing.T) {
167155
)
168156
}
169157

170-
func TestInt64sContains(t *testing.T) {
171-
assert.True(t, Int64sContains([]int64{6, 44324, 4324, 32, 1, 2323}, 1))
172-
assert.True(t, Int64sContains([]int64{2323}, 2323))
173-
assert.False(t, Int64sContains([]int64{6, 44324, 4324, 32, 1, 2323}, 232))
174-
}
175-
176-
func TestIsLetter(t *testing.T) {
177-
assert.True(t, IsLetter('a'))
178-
assert.True(t, IsLetter('e'))
179-
assert.True(t, IsLetter('q'))
180-
assert.True(t, IsLetter('z'))
181-
assert.True(t, IsLetter('A'))
182-
assert.True(t, IsLetter('E'))
183-
assert.True(t, IsLetter('Q'))
184-
assert.True(t, IsLetter('Z'))
185-
assert.True(t, IsLetter('_'))
186-
assert.False(t, IsLetter('-'))
187-
assert.False(t, IsLetter('1'))
188-
assert.False(t, IsLetter('$'))
189-
assert.False(t, IsLetter(0x00))
190-
assert.False(t, IsLetter(0x93))
191-
}
192-
193158
// TODO: Test EntryIcon
194159

195160
func TestSetupGiteaRoot(t *testing.T) {

routers/web/user/setting/profile.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"path/filepath"
1515
"strings"
1616

17+
"code.gitea.io/gitea/models/avatars"
1718
"code.gitea.io/gitea/models/db"
1819
"code.gitea.io/gitea/models/organization"
1920
repo_model "code.gitea.io/gitea/models/repo"
@@ -130,7 +131,7 @@ func UpdateAvatarSetting(ctx *context.Context, form *forms.AvatarForm, ctxUser *
130131
ctxUser.UseCustomAvatar = form.Source == forms.AvatarLocal
131132
if len(form.Gravatar) > 0 {
132133
if form.Avatar != nil {
133-
ctxUser.Avatar = base.EncodeMD5(form.Gravatar)
134+
ctxUser.Avatar = avatars.HashEmail(form.Gravatar)
134135
} else {
135136
ctxUser.Avatar = ""
136137
}

0 commit comments

Comments
 (0)