1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package migrations
import (
"fmt"
"path/filepath"
"strings"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
)
// Copy paste from models/repo.go because we cannot import models package
func repoPath(userName, repoName string) string {
return filepath.Join(userPath(userName), strings.ToLower(repoName)+".git")
}
func userPath(userName string) string {
return filepath.Join(setting.RepoRootPath, strings.ToLower(userName))
}
func fixPublisherIDforTagReleases(x *xorm.Engine) error {
type Release struct {
ID int64
RepoID int64
Sha1 string
TagName string
PublisherID int64
}
type Repository struct {
ID int64
OwnerID int64
OwnerName string
Name string
}
type User struct {
ID int64
Name string
Email string
}
const batchSize = 100
sess := x.NewSession()
defer sess.Close()
var (
repo *Repository
gitRepo *git.Repository
)
defer func() {
if gitRepo != nil {
gitRepo.Close()
}
}()
for start := 0; ; start += batchSize {
releases := make([]*Release, 0, batchSize)
if err := sess.Begin(); err != nil {
return err
}
if err := sess.Limit(batchSize, start).
Where("publisher_id = 0").
Asc("repo_id", "id").Where("is_tag=?", true).
Find(&releases); err != nil {
return err
}
if len(releases) == 0 {
break
}
for _, release := range releases {
if repo == nil || repo.ID != release.RepoID {
if gitRepo != nil {
gitRepo.Close()
gitRepo = nil
}
repo = new(Repository)
has, err := sess.ID(release.RepoID).Get(repo)
if err != nil {
return err
} else if !has {
log.Warn("Release[%d] is orphaned and refers to non-existing repository %d", release.ID, release.RepoID)
log.Warn("This release should be deleted")
continue
}
if repo.OwnerName == "" {
// v120.go migration may not have been run correctly - we'll just replicate it here
// because this appears to be a common-ish problem.
if _, err := sess.Exec("UPDATE repository SET owner_name = (SELECT name FROM `user` WHERE `user`.id = repository.owner_id)"); err != nil {
return err
}
if _, err := sess.ID(release.RepoID).Get(repo); err != nil {
return err
}
}
gitRepo, err = git.OpenRepository(repoPath(repo.OwnerName, repo.Name))
if err != nil {
return err
}
}
commit, err := gitRepo.GetTagCommit(release.TagName)
if err != nil {
return fmt.Errorf("GetTagCommit: %v", err)
}
u := new(User)
exists, err := sess.Where("email=?", commit.Author.Email).Get(u)
if err != nil {
return err
}
if !exists {
continue
}
release.PublisherID = u.ID
if _, err := sess.ID(release.ID).Cols("publisher_id").Update(release); err != nil {
return err
}
}
if err := sess.Commit(); err != nil {
return err
}
}
return nil
}
|