Skip to content

Commit

Permalink
Move repofiles from modules/repofiles to services/repository/files (#…
Browse files Browse the repository at this point in the history
…17774)

* Move repofiles from modules to services

* rename services/repository/repofiles -> services/repository/files

* Fix test

Co-authored-by: 6543 <6543@obermui.de>
  • Loading branch information
lunny and 6543 authored Nov 24, 2021
1 parent 754fdd8 commit c97d66d
Show file tree
Hide file tree
Showing 37 changed files with 277 additions and 353 deletions.
6 changes: 3 additions & 3 deletions integrations/api_repo_file_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ package integrations

import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/repofiles"
api "code.gitea.io/gitea/modules/structs"
files_service "code.gitea.io/gitea/services/repository/files"
)

func createFileInBranch(user *models.User, repo *models.Repository, treePath, branchName, content string) (*api.FileResponse, error) {
opts := &repofiles.UpdateRepoFileOptions{
opts := &files_service.UpdateRepoFileOptions{
OldBranch: branchName,
TreePath: treePath,
Content: content,
IsNewFile: true,
Author: nil,
Committer: nil,
}
return repofiles.CreateOrUpdateRepoFile(repo, user, opts)
return files_service.CreateOrUpdateRepoFile(repo, user, opts)
}

func createFile(user *models.User, repo *models.Repository, treePath string) (*api.FileResponse, error) {
Expand Down
18 changes: 9 additions & 9 deletions integrations/pull_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/repofiles"
pull_service "code.gitea.io/gitea/services/pull"
repo_service "code.gitea.io/gitea/services/repository"
files_service "code.gitea.io/gitea/services/repository/files"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -97,45 +97,45 @@ func createOutdatedPR(t *testing.T, actor, forkOrg *models.User) *models.PullReq
assert.NotEmpty(t, headRepo)

//create a commit on base Repo
_, err = repofiles.CreateOrUpdateRepoFile(baseRepo, actor, &repofiles.UpdateRepoFileOptions{
_, err = files_service.CreateOrUpdateRepoFile(baseRepo, actor, &files_service.UpdateRepoFileOptions{
TreePath: "File_A",
Message: "Add File A",
Content: "File A",
IsNewFile: true,
OldBranch: "master",
NewBranch: "master",
Author: &repofiles.IdentityOptions{
Author: &files_service.IdentityOptions{
Name: actor.Name,
Email: actor.Email,
},
Committer: &repofiles.IdentityOptions{
Committer: &files_service.IdentityOptions{
Name: actor.Name,
Email: actor.Email,
},
Dates: &repofiles.CommitDateOptions{
Dates: &files_service.CommitDateOptions{
Author: time.Now(),
Committer: time.Now(),
},
})
assert.NoError(t, err)

//create a commit on head Repo
_, err = repofiles.CreateOrUpdateRepoFile(headRepo, actor, &repofiles.UpdateRepoFileOptions{
_, err = files_service.CreateOrUpdateRepoFile(headRepo, actor, &files_service.UpdateRepoFileOptions{
TreePath: "File_B",
Message: "Add File on PR branch",
Content: "File B",
IsNewFile: true,
OldBranch: "master",
NewBranch: "newBranch",
Author: &repofiles.IdentityOptions{
Author: &files_service.IdentityOptions{
Name: actor.Name,
Email: actor.Email,
},
Committer: &repofiles.IdentityOptions{
Committer: &files_service.IdentityOptions{
Name: actor.Name,
Email: actor.Email,
},
Dates: &repofiles.CommitDateOptions{
Dates: &files_service.CommitDateOptions{
Author: time.Now(),
Committer: time.Now(),
},
Expand Down
24 changes: 12 additions & 12 deletions integrations/repofiles_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/repofiles"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/test"
files_service "code.gitea.io/gitea/services/repository/files"

"github.com/stretchr/testify/assert"
)

func getDeleteRepoFileOptions(repo *models.Repository) *repofiles.DeleteRepoFileOptions {
return &repofiles.DeleteRepoFileOptions{
func getDeleteRepoFileOptions(repo *models.Repository) *files_service.DeleteRepoFileOptions {
return &files_service.DeleteRepoFileOptions{
LastCommitID: "",
OldBranch: repo.DefaultBranch,
NewBranch: repo.DefaultBranch,
TreePath: "README.md",
Message: "Deletes README.md",
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
Author: &repofiles.IdentityOptions{
Author: &files_service.IdentityOptions{
Name: "Bob Smith",
Email: "bob@smith.com",
},
Expand Down Expand Up @@ -80,7 +80,7 @@ func testDeleteRepoFile(t *testing.T, u *url.URL) {
opts := getDeleteRepoFileOptions(repo)

t.Run("Delete README.md file", func(t *testing.T) {
fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
assert.NoError(t, err)
expectedFileResponse := getExpectedDeleteFileResponse(u)
assert.NotNil(t, fileResponse)
Expand All @@ -92,7 +92,7 @@ func testDeleteRepoFile(t *testing.T, u *url.URL) {
})

t.Run("Verify README.md has been deleted", func(t *testing.T) {
fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
assert.Nil(t, fileResponse)
expectedError := "repository file does not exist [path: " + opts.TreePath + "]"
assert.EqualError(t, err, expectedError)
Expand Down Expand Up @@ -122,7 +122,7 @@ func testDeleteRepoFileWithoutBranchNames(t *testing.T, u *url.URL) {
opts.NewBranch = ""

t.Run("Delete README.md without Branch Name", func(t *testing.T) {
fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
assert.NoError(t, err)
expectedFileResponse := getExpectedDeleteFileResponse(u)
assert.NotNil(t, fileResponse)
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestDeleteRepoFileErrors(t *testing.T) {
t.Run("Bad branch", func(t *testing.T) {
opts := getDeleteRepoFileOptions(repo)
opts.OldBranch = "bad_branch"
fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
assert.Error(t, err)
assert.Nil(t, fileResponse)
expectedError := "branch does not exist [name: " + opts.OldBranch + "]"
Expand All @@ -162,7 +162,7 @@ func TestDeleteRepoFileErrors(t *testing.T) {
opts := getDeleteRepoFileOptions(repo)
origSHA := opts.SHA
opts.SHA = "bad_sha"
fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
assert.Nil(t, fileResponse)
assert.Error(t, err)
expectedError := "sha does not match [given: " + opts.SHA + ", expected: " + origSHA + "]"
Expand All @@ -172,7 +172,7 @@ func TestDeleteRepoFileErrors(t *testing.T) {
t.Run("New branch already exists", func(t *testing.T) {
opts := getDeleteRepoFileOptions(repo)
opts.NewBranch = "develop"
fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
assert.Nil(t, fileResponse)
assert.Error(t, err)
expectedError := "branch already exists [name: " + opts.NewBranch + "]"
Expand All @@ -182,7 +182,7 @@ func TestDeleteRepoFileErrors(t *testing.T) {
t.Run("TreePath is empty:", func(t *testing.T) {
opts := getDeleteRepoFileOptions(repo)
opts.TreePath = ""
fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
assert.Nil(t, fileResponse)
assert.Error(t, err)
expectedError := "path contains a malformed path component [path: ]"
Expand All @@ -192,7 +192,7 @@ func TestDeleteRepoFileErrors(t *testing.T) {
t.Run("TreePath is a git directory:", func(t *testing.T) {
opts := getDeleteRepoFileOptions(repo)
opts.TreePath = ".git"
fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
assert.Nil(t, fileResponse)
assert.Error(t, err)
expectedError := "path contains a malformed path component [path: " + opts.TreePath + "]"
Expand Down
30 changes: 15 additions & 15 deletions integrations/repofiles_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/repofiles"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/test"
files_service "code.gitea.io/gitea/services/repository/files"

"github.com/stretchr/testify/assert"
)

func getCreateRepoFileOptions(repo *models.Repository) *repofiles.UpdateRepoFileOptions {
return &repofiles.UpdateRepoFileOptions{
func getCreateRepoFileOptions(repo *models.Repository) *files_service.UpdateRepoFileOptions {
return &files_service.UpdateRepoFileOptions{
OldBranch: repo.DefaultBranch,
NewBranch: repo.DefaultBranch,
TreePath: "new/file.txt",
Expand All @@ -33,8 +33,8 @@ func getCreateRepoFileOptions(repo *models.Repository) *repofiles.UpdateRepoFile
}
}

func getUpdateRepoFileOptions(repo *models.Repository) *repofiles.UpdateRepoFileOptions {
return &repofiles.UpdateRepoFileOptions{
func getUpdateRepoFileOptions(repo *models.Repository) *files_service.UpdateRepoFileOptions {
return &files_service.UpdateRepoFileOptions{
OldBranch: repo.DefaultBranch,
NewBranch: repo.DefaultBranch,
TreePath: "README.md",
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestCreateOrUpdateRepoFileForCreate(t *testing.T) {
opts := getCreateRepoFileOptions(repo)

// test
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)

// asserts
assert.NoError(t, err)
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestCreateOrUpdateRepoFileForUpdate(t *testing.T) {
opts := getUpdateRepoFileOptions(repo)

// test
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)

// asserts
assert.NoError(t, err)
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestCreateOrUpdateRepoFileForUpdateWithFileMove(t *testing.T) {
opts.TreePath = "README_new.md" // new file name, README_new.md

// test
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)

// asserts
assert.NoError(t, err)
Expand Down Expand Up @@ -319,7 +319,7 @@ func TestCreateOrUpdateRepoFileWithoutBranchNames(t *testing.T) {
opts.NewBranch = ""

// test
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)

// asserts
assert.NoError(t, err)
Expand Down Expand Up @@ -349,7 +349,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) {
t.Run("bad branch", func(t *testing.T) {
opts := getUpdateRepoFileOptions(repo)
opts.OldBranch = "bad_branch"
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)
assert.Error(t, err)
assert.Nil(t, fileResponse)
expectedError := "branch does not exist [name: " + opts.OldBranch + "]"
Expand All @@ -360,7 +360,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) {
opts := getUpdateRepoFileOptions(repo)
origSHA := opts.SHA
opts.SHA = "bad_sha"
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)
assert.Nil(t, fileResponse)
assert.Error(t, err)
expectedError := "sha does not match [given: " + opts.SHA + ", expected: " + origSHA + "]"
Expand All @@ -370,7 +370,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) {
t.Run("new branch already exists", func(t *testing.T) {
opts := getUpdateRepoFileOptions(repo)
opts.NewBranch = "develop"
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)
assert.Nil(t, fileResponse)
assert.Error(t, err)
expectedError := "branch already exists [name: " + opts.NewBranch + "]"
Expand All @@ -380,7 +380,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) {
t.Run("treePath is empty:", func(t *testing.T) {
opts := getUpdateRepoFileOptions(repo)
opts.TreePath = ""
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)
assert.Nil(t, fileResponse)
assert.Error(t, err)
expectedError := "path contains a malformed path component [path: ]"
Expand All @@ -390,7 +390,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) {
t.Run("treePath is a git directory:", func(t *testing.T) {
opts := getUpdateRepoFileOptions(repo)
opts.TreePath = ".git"
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)
assert.Nil(t, fileResponse)
assert.Error(t, err)
expectedError := "path contains a malformed path component [path: " + opts.TreePath + "]"
Expand All @@ -400,7 +400,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) {
t.Run("create file that already exists", func(t *testing.T) {
opts := getCreateRepoFileOptions(repo)
opts.TreePath = "README.md" //already exists
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)
assert.Nil(t, fileResponse)
assert.Error(t, err)
expectedError := "repository file already exists [path: " + opts.TreePath + "]"
Expand Down
10 changes: 8 additions & 2 deletions modules/convert/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
repo_module "code.gitea.io/gitea/modules/repository"
api "code.gitea.io/gitea/modules/structs"
)

Expand Down Expand Up @@ -83,7 +82,14 @@ func ToAPIPullRequest(pr *models.PullRequest, doer *models.User) *api.PullReques
},
}

baseBranch, err = repo_module.GetBranch(pr.BaseRepo, pr.BaseBranch)
gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
if err != nil {
log.Error("OpenRepository[%s]: %v", pr.BaseRepo.RepoPath(), err)
return nil
}
defer gitRepo.Close()

baseBranch, err = gitRepo.GetBranch(pr.BaseBranch)
if err != nil && !git.IsErrBranchNotExist(err) {
log.Error("GetBranch[%s]: %v", pr.BaseBranch, err)
return nil
Expand Down
41 changes: 0 additions & 41 deletions modules/repofiles/blob.go

This file was deleted.

Loading

0 comments on commit c97d66d

Please sign in to comment.