2020-10-22 08:55:25 +08:00
// Copyright 2020 The Gitea Authors. All rights reserved.
2022-11-28 02:20:29 +08:00
// SPDX-License-Identifier: MIT
2020-10-22 08:55:25 +08:00
2022-11-02 16:54:36 +08:00
package v1_14 //nolint
2020-10-22 08:55:25 +08:00
import (
"fmt"
"path/filepath"
"strings"
"code.gitea.io/gitea/modules/git"
2020-10-22 23:45:54 +08:00
"code.gitea.io/gitea/modules/log"
2020-10-22 08:55:25 +08:00
"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 ) )
}
2022-11-02 16:54:36 +08:00
func FixPublisherIDforTagReleases ( x * xorm . Engine ) error {
2020-10-22 08:55:25 +08:00
type Release struct {
ID int64
RepoID int64
Sha1 string
TagName string
PublisherID int64
}
type Repository struct {
2020-10-22 23:45:54 +08:00
ID int64
OwnerID int64
OwnerName string
Name string
2020-10-22 08:55:25 +08:00
}
type User struct {
ID int64
Name string
Email string
}
const batchSize = 100
sess := x . NewSession ( )
defer sess . Close ( )
var (
2020-10-22 23:45:54 +08:00
repo * Repository
gitRepo * git . Repository
2020-11-10 19:44:45 +08:00
user * User
2020-10-22 08:55:25 +08:00
)
defer func ( ) {
2020-10-22 23:45:54 +08:00
if gitRepo != nil {
gitRepo . Close ( )
2020-10-22 08:55:25 +08:00
}
} ( )
for start := 0 ; ; start += batchSize {
releases := make ( [ ] * Release , 0 , batchSize )
2020-10-22 23:45:54 +08:00
if err := sess . Begin ( ) ; err != nil {
return err
}
2020-11-03 23:47:54 +08:00
if err := sess . Limit ( batchSize , start ) .
2020-11-10 19:44:45 +08:00
Where ( "publisher_id = 0 OR publisher_id is null" ) .
2020-11-03 23:47:54 +08:00
Asc ( "repo_id" , "id" ) . Where ( "is_tag=?" , true ) .
Find ( & releases ) ; err != nil {
2020-10-22 08:55:25 +08:00
return err
}
if len ( releases ) == 0 {
break
}
for _ , release := range releases {
2020-10-22 23:45:54 +08:00
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 {
2021-05-12 13:11:42 +08:00
log . Error ( "Error whilst loading repository[%d] for release[%d] with tag name %s. Error: %v" , release . RepoID , release . ID , release . TagName , err )
2020-10-22 23:45:54 +08:00
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 08:55:25 +08:00
}
2020-10-22 23:45:54 +08:00
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 {
2021-04-17 07:28:40 +08:00
log . Error ( "Error whilst updating repository[%d] owner name" , repo . ID )
2020-10-22 08:55:25 +08:00
return err
}
2020-10-22 23:45:54 +08:00
if _ , err := sess . ID ( release . RepoID ) . Get ( repo ) ; err != nil {
2021-05-12 13:11:42 +08:00
log . Error ( "Error whilst loading repository[%d] for release[%d] with tag name %s. Error: %v" , release . RepoID , release . ID , release . TagName , err )
2020-10-22 23:45:54 +08:00
return err
}
2020-10-22 08:55:25 +08:00
}
2022-03-30 03:13:41 +08:00
gitRepo , err = git . OpenRepository ( git . DefaultContext , repoPath ( repo . OwnerName , repo . Name ) )
2020-10-22 08:55:25 +08:00
if err != nil {
2021-05-12 13:11:42 +08:00
log . Error ( "Error whilst opening git repo for [%d]%s/%s. Error: %v" , repo . ID , repo . OwnerName , repo . Name , err )
2020-10-22 08:55:25 +08:00
return err
}
}
commit , err := gitRepo . GetTagCommit ( release . TagName )
if err != nil {
2021-04-17 07:28:40 +08:00
if git . IsErrNotExist ( err ) {
2021-05-12 13:11:42 +08:00
log . Warn ( "Unable to find commit %s for Tag: %s in [%d]%s/%s. Cannot update publisher ID." , err . ( git . ErrNotExist ) . ID , release . TagName , repo . ID , repo . OwnerName , repo . Name )
2021-04-17 07:28:40 +08:00
continue
}
2021-05-12 13:11:42 +08:00
log . Error ( "Error whilst getting commit for Tag: %s in [%d]%s/%s. Error: %v" , release . TagName , repo . ID , repo . OwnerName , repo . Name , err )
2022-10-25 03:29:17 +08:00
return fmt . Errorf ( "GetTagCommit: %w" , err )
2020-10-22 08:55:25 +08:00
}
2021-05-12 13:11:42 +08:00
if commit . Author . Email == "" {
log . Warn ( "Tag: %s in Repo[%d]%s/%s does not have a tagger." , release . TagName , repo . ID , repo . OwnerName , repo . Name )
commit , err = gitRepo . GetCommit ( commit . ID . String ( ) )
if err != nil {
if git . IsErrNotExist ( err ) {
log . Warn ( "Unable to find commit %s for Tag: %s in [%d]%s/%s. Cannot update publisher ID." , err . ( git . ErrNotExist ) . ID , release . TagName , repo . ID , repo . OwnerName , repo . Name )
continue
}
log . Error ( "Error whilst getting commit for Tag: %s in [%d]%s/%s. Error: %v" , release . TagName , repo . ID , repo . OwnerName , repo . Name , err )
2022-10-25 03:29:17 +08:00
return fmt . Errorf ( "GetCommit: %w" , err )
2021-05-12 13:11:42 +08:00
}
}
if commit . Author . Email == "" {
log . Warn ( "Tag: %s in Repo[%d]%s/%s does not have a Tagger and its underlying commit does not have an Author either!" , release . TagName , repo . ID , repo . OwnerName , repo . Name )
continue
}
2020-11-10 19:44:45 +08:00
if user == nil || ! strings . EqualFold ( user . Email , commit . Author . Email ) {
user = new ( User )
_ , err = sess . Where ( "email=?" , commit . Author . Email ) . Get ( user )
if err != nil {
2021-05-12 13:11:42 +08:00
log . Error ( "Error whilst getting commit author by email: %s for Tag: %s in [%d]%s/%s. Error: %v" , commit . Author . Email , release . TagName , repo . ID , repo . OwnerName , repo . Name , err )
2020-11-10 19:44:45 +08:00
return err
}
user . Email = commit . Author . Email
2020-10-22 08:55:25 +08:00
}
2020-11-10 19:44:45 +08:00
if user . ID <= 0 {
2020-10-22 08:55:25 +08:00
continue
}
2020-11-10 19:44:45 +08:00
release . PublisherID = user . ID
2020-10-22 08:55:25 +08:00
if _ , err := sess . ID ( release . ID ) . Cols ( "publisher_id" ) . Update ( release ) ; err != nil {
2021-05-12 13:11:42 +08:00
log . Error ( "Error whilst updating publisher[%d] for release[%d] with tag name %s. Error: %v" , release . PublisherID , release . ID , release . TagName , err )
2020-10-22 08:55:25 +08:00
return err
}
}
2021-01-07 03:23:57 +08:00
if gitRepo != nil {
gitRepo . Close ( )
}
2020-10-22 23:45:54 +08:00
if err := sess . Commit ( ) ; err != nil {
return err
}
2020-10-22 08:55:25 +08:00
}
2020-10-22 23:45:54 +08:00
return nil
2020-10-22 08:55:25 +08:00
}