Verified Commit 3008323e authored by Loïc Dachary's avatar Loïc Dachary
Browse files

projectbase: implement tests for UpdateProjectBase

Fixes: #2

Signed-off-by: Loïc Dachary's avatarLoïc Dachary <loic@dachary.org>
parent f7c1894e
Pipeline #628 passed with stage
in 19 minutes and 56 seconds
// Copyright 2022 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 integrations
import (
"fmt"
"net/url"
"sort"
"testing"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/migrations"
projectbase_service "code.gitea.io/gitea/services/projectbase"
"github.com/stretchr/testify/assert"
)
func TestProjectBaseUpdate(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
AllowedDomains := setting.Migrations.AllowedDomains
setting.Migrations.AllowedDomains = "loopback"
AllowLocalNetworks := setting.Migrations.AllowLocalNetworks
setting.Migrations.AllowLocalNetworks = true
setting.Database.LogSQL = true
AppVer := setting.AppVer
setting.AppVer = "1.15"
defer func() {
setting.Migrations.AllowedDomains = AllowedDomains
setting.Migrations.AllowLocalNetworks = AllowLocalNetworks
setting.Database.LogSQL = false
setting.AppVer = AppVer
}()
assert.NoError(t, migrations.Init())
reponame := "repo1"
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame}).(*repo_model.Repository)
repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
session := loginUser(t, repoOwner.Name)
token := getTokenForLoggedInUser(t, session)
assert.False(t, repo.HasProjectBase())
assert.NoError(t, projectbase_service.InitProjectBase(repo))
assert.True(t, repo.HasProjectBase())
assert.NoError(t, projectbase_service.UpdateProjectBase(token, repo))
gitRepo, err := git.OpenRepository(repo.ProjectBasePath())
fmt.Println(repo.ProjectBasePath())
assert.NoError(t, err)
defer gitRepo.Close()
files := []string{"repo.yml", "issue.yml", "comments"}
filelist, err := gitRepo.LsTree("HEAD", files...)
assert.NoError(t, err)
sort.Strings(filelist)
var expected = append(files, "")
sort.Strings(expected)
assert.Equal(t, expected, filelist)
})
}
......@@ -5,10 +5,19 @@
package projectbase
import (
"context"
"fmt"
"os"
"path/filepath"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/migrations"
files_service "code.gitea.io/gitea/services/repository/files"
)
// InitProjectBase function
......@@ -24,3 +33,89 @@ func InitProjectBase(repo *repo_model.Repository) error {
}
return nil
}
// UpdateProjectBase function
func UpdateProjectBase(token string, repo *repo_model.Repository) error {
//
// Checkout repository
//
temp, err := files_service.NewTemporaryUploadRepository(repo)
if err != nil {
return err
}
basePath := temp.BasePath()
defer temp.Close()
if stdout, err := git.NewCommand("clone", "--no-checkout", "--shared", "--depth=1", repo.ProjectBasePath(), basePath).Run(); err != nil {
log.Error("Failed to clone projectbase repository: %s %s (%v)", repo.FullName(), stdout, err)
return fmt.Errorf("Failed to clone projectbase repository: %s %s (%v)", repo.FullName(), stdout, err)
}
//
// Create downloader & uploader
//
var opts = migrations.MigrateOptions{
GitServiceType: structs.GiteaService,
Issues: true,
Comments: true,
AuthToken: token,
CloneAddr: repo.CloneLink().HTTPS,
}
downloaderFactory := &migrations.GiteaDownloaderFactory{}
ctx := context.Background()
downloader, err := downloaderFactory.New(ctx, opts)
if err != nil {
return err
}
uploader, err := migrations.NewRepositoryDumper(ctx, basePath, repo.OwnerName, repo.Name, opts)
if err != nil {
return err
}
//
// Perform the migration
//
if err := migrations.InternalMigrateRepository(downloader, uploader, opts); err != nil {
if err1 := uploader.Rollback(); err1 != nil {
log.Error("rollback failed: %v", err1)
}
return err
}
d := filepath.Join(basePath, repo.OwnerName, repo.Name)
for _, f := range []string{"repo.yml", "topic.yml", "issue.yml", "comments"} {
if err := os.Rename(filepath.Join(d, f), filepath.Join(basePath, f)); err != nil {
return err
}
}
if err := util.RemoveAll(filepath.Join(basePath, repo.OwnerName)); err != nil {
return err
}
//
// Commit the migration to the repository & push it
//
if stdout, err := git.NewCommand("add", ".").RunInDir(basePath); err != nil {
log.Error("Failed to git add. : %s %s (%v)", repo.FullName(), stdout, err)
return fmt.Errorf("Failed to git add .: %s %s (%v)", repo.FullName(), stdout, err)
}
owner, err := user_model.GetUserByName(repo.OwnerName)
if err != nil {
log.Error("GetUserByName(repo.OwnerName=%s) (%v)", repo.OwnerName, err)
return err
}
author := owner.NewGitSig()
env := append(os.Environ(),
"GIT_AUTHOR_NAME="+author.Name,
"GIT_AUTHOR_EMAIL="+author.Email,
"GIT_COMMITTER_NAME="+author.Name,
"GIT_COMMITTER_EMAIL="+author.Email,
)
if stdout, err := git.NewCommand("commit", "-m", "project info").RunInDirWithEnv(basePath, env); err != nil {
log.Error("Failed to git add . : %s %s (%v)", repo.FullName(), stdout, err)
return fmt.Errorf("Failed to git add .: %s %s (%v)", repo.FullName(), stdout, err)
}
if stdout, err := git.NewCommand("push").RunInDir(basePath); err != nil {
log.Error("Failed to git push: %s %s (%v)", repo.FullName(), stdout, err)
return fmt.Errorf("Failed to git push: %s %s (%v)", repo.FullName(), stdout, err)
}
return nil
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment