2020-10-22 06:25:25 +05:30
|
|
|
// 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"
|
2020-10-22 21:15:54 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-10-22 06:25:25 +05:30
|
|
|
"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 {
|
2020-10-22 21:15:54 +05:30
|
|
|
ID int64
|
|
|
|
OwnerID int64
|
|
|
|
OwnerName string
|
|
|
|
Name string
|
2020-10-22 06:25:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
ID int64
|
|
|
|
Name string
|
|
|
|
Email string
|
|
|
|
}
|
|
|
|
|
|
|
|
const batchSize = 100
|
|
|
|
sess := x.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
|
|
|
|
var (
|
2020-10-22 21:15:54 +05:30
|
|
|
repo *Repository
|
|
|
|
gitRepo *git.Repository
|
2020-10-22 06:25:25 +05:30
|
|
|
)
|
|
|
|
defer func() {
|
2020-10-22 21:15:54 +05:30
|
|
|
if gitRepo != nil {
|
|
|
|
gitRepo.Close()
|
2020-10-22 06:25:25 +05:30
|
|
|
}
|
|
|
|
}()
|
|
|
|
for start := 0; ; start += batchSize {
|
|
|
|
releases := make([]*Release, 0, batchSize)
|
|
|
|
|
2020-10-22 21:15:54 +05:30
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sess.Limit(batchSize, start).Asc("repo_id", "id").Where("is_tag=?", true).Find(&releases); err != nil {
|
2020-10-22 06:25:25 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(releases) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, release := range releases {
|
2020-10-22 21:15:54 +05:30
|
|
|
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
|
2020-10-22 06:25:25 +05:30
|
|
|
}
|
|
|
|
|
2020-10-22 21:15:54 +05:30
|
|
|
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 {
|
2020-10-22 06:25:25 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-22 21:15:54 +05:30
|
|
|
if _, err := sess.ID(release.RepoID).Get(repo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-22 06:25:25 +05:30
|
|
|
}
|
2020-10-22 21:15:54 +05:30
|
|
|
gitRepo, err = git.OpenRepository(repoPath(repo.OwnerName, repo.Name))
|
2020-10-22 06:25:25 +05:30
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2020-10-22 21:15:54 +05:30
|
|
|
|
|
|
|
if err := sess.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-22 06:25:25 +05:30
|
|
|
}
|
|
|
|
|
2020-10-22 21:15:54 +05:30
|
|
|
return nil
|
2020-10-22 06:25:25 +05:30
|
|
|
}
|