Skip to content

Commit e1026fe

Browse files
GiteaBotwxiaoguang
andauthored
Fix repo avatar conflict (#32958) (#32960)
Backport #32958 by wxiaoguang Continue even if the avatar deleting fails Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent d670820 commit e1026fe

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

modules/repository/license_test.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ func Test_getLicense(t *testing.T) {
3131
3232
Copyright (c) 2023 Gitea
3333
34-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
35-
36-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
37-
38-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39-
`,
34+
Permission is hereby granted`,
4035
wantErr: assert.NoError,
4136
},
4237
{
@@ -53,7 +48,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
5348
if !tt.wantErr(t, err, fmt.Sprintf("GetLicense(%v, %v)", tt.args.name, tt.args.values)) {
5449
return
5550
}
56-
assert.Equalf(t, tt.want, string(got), "GetLicense(%v, %v)", tt.args.name, tt.args.values)
51+
assert.Contains(t, string(got), tt.want, "GetLicense(%v, %v)", tt.args.name, tt.args.values)
5752
})
5853
}
5954
}

services/repository/avatar.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"io"
1010
"strconv"
11-
"strings"
1211

1312
"code.gitea.io/gitea/models/db"
1413
repo_model "code.gitea.io/gitea/models/repo"
@@ -107,7 +106,8 @@ func RemoveRandomAvatars(ctx context.Context) error {
107106

108107
// generateAvatar generates the avatar from a template repository
109108
func generateAvatar(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error {
110-
generateRepo.Avatar = strings.Replace(templateRepo.Avatar, strconv.FormatInt(templateRepo.ID, 10), strconv.FormatInt(generateRepo.ID, 10), 1)
109+
// generate a new different hash, whatever the "hash data" is, it doesn't matter
110+
generateRepo.Avatar = avatar.HashAvatar(generateRepo.ID, []byte("new-avatar"))
111111
if _, err := storage.Copy(storage.RepoAvatars, generateRepo.CustomAvatarRelativePath(), storage.RepoAvatars, templateRepo.CustomAvatarRelativePath()); err != nil {
112112
return err
113113
}

services/repository/avatar_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,11 @@ func TestDeleteAvatar(t *testing.T) {
6161

6262
assert.Equal(t, "", repo.Avatar)
6363
}
64+
65+
func TestGenerateAvatar(t *testing.T) {
66+
templateRepo := &repo_model.Repository{ID: 10, Avatar: "a"}
67+
generateRepo := &repo_model.Repository{ID: 11}
68+
_ = generateAvatar(db.DefaultContext, templateRepo, generateRepo)
69+
assert.NotEmpty(t, generateRepo.Avatar)
70+
assert.NotEqual(t, templateRepo.Avatar, generateRepo.Avatar)
71+
}

services/repository/delete.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ func DeleteRepositoryDirectly(ctx context.Context, doer *user_model.User, repoID
324324

325325
if len(repo.Avatar) > 0 {
326326
if err := storage.RepoAvatars.Delete(repo.CustomAvatarRelativePath()); err != nil {
327-
return fmt.Errorf("Failed to remove %s: %w", repo.Avatar, err)
327+
log.Error("remove avatar file %q: %v", repo.CustomAvatarRelativePath(), err)
328+
// go on
328329
}
329330
}
330331

0 commit comments

Comments
 (0)