Skip to content

Commit c97d66d

Browse files
lunny6543
andauthored
Move repofiles from modules/repofiles to services/repository/files (#17774)
* Move repofiles from modules to services * rename services/repository/repofiles -> services/repository/files * Fix test Co-authored-by: 6543 <[email protected]>
1 parent 754fdd8 commit c97d66d

37 files changed

+277
-353
lines changed

integrations/api_repo_file_helpers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ package integrations
66

77
import (
88
"code.gitea.io/gitea/models"
9-
"code.gitea.io/gitea/modules/repofiles"
109
api "code.gitea.io/gitea/modules/structs"
10+
files_service "code.gitea.io/gitea/services/repository/files"
1111
)
1212

1313
func createFileInBranch(user *models.User, repo *models.Repository, treePath, branchName, content string) (*api.FileResponse, error) {
14-
opts := &repofiles.UpdateRepoFileOptions{
14+
opts := &files_service.UpdateRepoFileOptions{
1515
OldBranch: branchName,
1616
TreePath: treePath,
1717
Content: content,
1818
IsNewFile: true,
1919
Author: nil,
2020
Committer: nil,
2121
}
22-
return repofiles.CreateOrUpdateRepoFile(repo, user, opts)
22+
return files_service.CreateOrUpdateRepoFile(repo, user, opts)
2323
}
2424

2525
func createFile(user *models.User, repo *models.Repository, treePath string) (*api.FileResponse, error) {

integrations/pull_update_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212

1313
"code.gitea.io/gitea/models"
1414
"code.gitea.io/gitea/models/unittest"
15-
"code.gitea.io/gitea/modules/repofiles"
1615
pull_service "code.gitea.io/gitea/services/pull"
1716
repo_service "code.gitea.io/gitea/services/repository"
17+
files_service "code.gitea.io/gitea/services/repository/files"
1818

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

9999
//create a commit on base Repo
100-
_, err = repofiles.CreateOrUpdateRepoFile(baseRepo, actor, &repofiles.UpdateRepoFileOptions{
100+
_, err = files_service.CreateOrUpdateRepoFile(baseRepo, actor, &files_service.UpdateRepoFileOptions{
101101
TreePath: "File_A",
102102
Message: "Add File A",
103103
Content: "File A",
104104
IsNewFile: true,
105105
OldBranch: "master",
106106
NewBranch: "master",
107-
Author: &repofiles.IdentityOptions{
107+
Author: &files_service.IdentityOptions{
108108
Name: actor.Name,
109109
Email: actor.Email,
110110
},
111-
Committer: &repofiles.IdentityOptions{
111+
Committer: &files_service.IdentityOptions{
112112
Name: actor.Name,
113113
Email: actor.Email,
114114
},
115-
Dates: &repofiles.CommitDateOptions{
115+
Dates: &files_service.CommitDateOptions{
116116
Author: time.Now(),
117117
Committer: time.Now(),
118118
},
119119
})
120120
assert.NoError(t, err)
121121

122122
//create a commit on head Repo
123-
_, err = repofiles.CreateOrUpdateRepoFile(headRepo, actor, &repofiles.UpdateRepoFileOptions{
123+
_, err = files_service.CreateOrUpdateRepoFile(headRepo, actor, &files_service.UpdateRepoFileOptions{
124124
TreePath: "File_B",
125125
Message: "Add File on PR branch",
126126
Content: "File B",
127127
IsNewFile: true,
128128
OldBranch: "master",
129129
NewBranch: "newBranch",
130-
Author: &repofiles.IdentityOptions{
130+
Author: &files_service.IdentityOptions{
131131
Name: actor.Name,
132132
Email: actor.Email,
133133
},
134-
Committer: &repofiles.IdentityOptions{
134+
Committer: &files_service.IdentityOptions{
135135
Name: actor.Name,
136136
Email: actor.Email,
137137
},
138-
Dates: &repofiles.CommitDateOptions{
138+
Dates: &files_service.CommitDateOptions{
139139
Author: time.Now(),
140140
Committer: time.Now(),
141141
},

integrations/repofiles_delete_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ import (
1010

1111
"code.gitea.io/gitea/models"
1212
"code.gitea.io/gitea/models/unittest"
13-
"code.gitea.io/gitea/modules/repofiles"
1413
api "code.gitea.io/gitea/modules/structs"
1514
"code.gitea.io/gitea/modules/test"
15+
files_service "code.gitea.io/gitea/services/repository/files"
1616

1717
"github.com/stretchr/testify/assert"
1818
)
1919

20-
func getDeleteRepoFileOptions(repo *models.Repository) *repofiles.DeleteRepoFileOptions {
21-
return &repofiles.DeleteRepoFileOptions{
20+
func getDeleteRepoFileOptions(repo *models.Repository) *files_service.DeleteRepoFileOptions {
21+
return &files_service.DeleteRepoFileOptions{
2222
LastCommitID: "",
2323
OldBranch: repo.DefaultBranch,
2424
NewBranch: repo.DefaultBranch,
2525
TreePath: "README.md",
2626
Message: "Deletes README.md",
2727
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
28-
Author: &repofiles.IdentityOptions{
28+
Author: &files_service.IdentityOptions{
2929
Name: "Bob Smith",
3030
3131
},
@@ -80,7 +80,7 @@ func testDeleteRepoFile(t *testing.T, u *url.URL) {
8080
opts := getDeleteRepoFileOptions(repo)
8181

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

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

124124
t.Run("Delete README.md without Branch Name", func(t *testing.T) {
125-
fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
125+
fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
126126
assert.NoError(t, err)
127127
expectedFileResponse := getExpectedDeleteFileResponse(u)
128128
assert.NotNil(t, fileResponse)
@@ -151,7 +151,7 @@ func TestDeleteRepoFileErrors(t *testing.T) {
151151
t.Run("Bad branch", func(t *testing.T) {
152152
opts := getDeleteRepoFileOptions(repo)
153153
opts.OldBranch = "bad_branch"
154-
fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
154+
fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
155155
assert.Error(t, err)
156156
assert.Nil(t, fileResponse)
157157
expectedError := "branch does not exist [name: " + opts.OldBranch + "]"
@@ -162,7 +162,7 @@ func TestDeleteRepoFileErrors(t *testing.T) {
162162
opts := getDeleteRepoFileOptions(repo)
163163
origSHA := opts.SHA
164164
opts.SHA = "bad_sha"
165-
fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
165+
fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
166166
assert.Nil(t, fileResponse)
167167
assert.Error(t, err)
168168
expectedError := "sha does not match [given: " + opts.SHA + ", expected: " + origSHA + "]"
@@ -172,7 +172,7 @@ func TestDeleteRepoFileErrors(t *testing.T) {
172172
t.Run("New branch already exists", func(t *testing.T) {
173173
opts := getDeleteRepoFileOptions(repo)
174174
opts.NewBranch = "develop"
175-
fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
175+
fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
176176
assert.Nil(t, fileResponse)
177177
assert.Error(t, err)
178178
expectedError := "branch already exists [name: " + opts.NewBranch + "]"
@@ -182,7 +182,7 @@ func TestDeleteRepoFileErrors(t *testing.T) {
182182
t.Run("TreePath is empty:", func(t *testing.T) {
183183
opts := getDeleteRepoFileOptions(repo)
184184
opts.TreePath = ""
185-
fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
185+
fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
186186
assert.Nil(t, fileResponse)
187187
assert.Error(t, err)
188188
expectedError := "path contains a malformed path component [path: ]"
@@ -192,7 +192,7 @@ func TestDeleteRepoFileErrors(t *testing.T) {
192192
t.Run("TreePath is a git directory:", func(t *testing.T) {
193193
opts := getDeleteRepoFileOptions(repo)
194194
opts.TreePath = ".git"
195-
fileResponse, err := repofiles.DeleteRepoFile(repo, doer, opts)
195+
fileResponse, err := files_service.DeleteRepoFile(repo, doer, opts)
196196
assert.Nil(t, fileResponse)
197197
assert.Error(t, err)
198198
expectedError := "path contains a malformed path component [path: " + opts.TreePath + "]"

integrations/repofiles_update_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ import (
1212

1313
"code.gitea.io/gitea/models"
1414
"code.gitea.io/gitea/modules/git"
15-
"code.gitea.io/gitea/modules/repofiles"
1615
"code.gitea.io/gitea/modules/setting"
1716
api "code.gitea.io/gitea/modules/structs"
1817
"code.gitea.io/gitea/modules/test"
18+
files_service "code.gitea.io/gitea/services/repository/files"
1919

2020
"github.com/stretchr/testify/assert"
2121
)
2222

23-
func getCreateRepoFileOptions(repo *models.Repository) *repofiles.UpdateRepoFileOptions {
24-
return &repofiles.UpdateRepoFileOptions{
23+
func getCreateRepoFileOptions(repo *models.Repository) *files_service.UpdateRepoFileOptions {
24+
return &files_service.UpdateRepoFileOptions{
2525
OldBranch: repo.DefaultBranch,
2626
NewBranch: repo.DefaultBranch,
2727
TreePath: "new/file.txt",
@@ -33,8 +33,8 @@ func getCreateRepoFileOptions(repo *models.Repository) *repofiles.UpdateRepoFile
3333
}
3434
}
3535

36-
func getUpdateRepoFileOptions(repo *models.Repository) *repofiles.UpdateRepoFileOptions {
37-
return &repofiles.UpdateRepoFileOptions{
36+
func getUpdateRepoFileOptions(repo *models.Repository) *files_service.UpdateRepoFileOptions {
37+
return &files_service.UpdateRepoFileOptions{
3838
OldBranch: repo.DefaultBranch,
3939
NewBranch: repo.DefaultBranch,
4040
TreePath: "README.md",
@@ -198,7 +198,7 @@ func TestCreateOrUpdateRepoFileForCreate(t *testing.T) {
198198
opts := getCreateRepoFileOptions(repo)
199199

200200
// test
201-
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
201+
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)
202202

203203
// asserts
204204
assert.NoError(t, err)
@@ -234,7 +234,7 @@ func TestCreateOrUpdateRepoFileForUpdate(t *testing.T) {
234234
opts := getUpdateRepoFileOptions(repo)
235235

236236
// test
237-
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
237+
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)
238238

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

271271
// test
272-
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
272+
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)
273273

274274
// asserts
275275
assert.NoError(t, err)
@@ -319,7 +319,7 @@ func TestCreateOrUpdateRepoFileWithoutBranchNames(t *testing.T) {
319319
opts.NewBranch = ""
320320

321321
// test
322-
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
322+
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)
323323

324324
// asserts
325325
assert.NoError(t, err)
@@ -349,7 +349,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) {
349349
t.Run("bad branch", func(t *testing.T) {
350350
opts := getUpdateRepoFileOptions(repo)
351351
opts.OldBranch = "bad_branch"
352-
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
352+
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)
353353
assert.Error(t, err)
354354
assert.Nil(t, fileResponse)
355355
expectedError := "branch does not exist [name: " + opts.OldBranch + "]"
@@ -360,7 +360,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) {
360360
opts := getUpdateRepoFileOptions(repo)
361361
origSHA := opts.SHA
362362
opts.SHA = "bad_sha"
363-
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
363+
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)
364364
assert.Nil(t, fileResponse)
365365
assert.Error(t, err)
366366
expectedError := "sha does not match [given: " + opts.SHA + ", expected: " + origSHA + "]"
@@ -370,7 +370,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) {
370370
t.Run("new branch already exists", func(t *testing.T) {
371371
opts := getUpdateRepoFileOptions(repo)
372372
opts.NewBranch = "develop"
373-
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
373+
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)
374374
assert.Nil(t, fileResponse)
375375
assert.Error(t, err)
376376
expectedError := "branch already exists [name: " + opts.NewBranch + "]"
@@ -380,7 +380,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) {
380380
t.Run("treePath is empty:", func(t *testing.T) {
381381
opts := getUpdateRepoFileOptions(repo)
382382
opts.TreePath = ""
383-
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
383+
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)
384384
assert.Nil(t, fileResponse)
385385
assert.Error(t, err)
386386
expectedError := "path contains a malformed path component [path: ]"
@@ -390,7 +390,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) {
390390
t.Run("treePath is a git directory:", func(t *testing.T) {
391391
opts := getUpdateRepoFileOptions(repo)
392392
opts.TreePath = ".git"
393-
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
393+
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)
394394
assert.Nil(t, fileResponse)
395395
assert.Error(t, err)
396396
expectedError := "path contains a malformed path component [path: " + opts.TreePath + "]"
@@ -400,7 +400,7 @@ func TestCreateOrUpdateRepoFileErrors(t *testing.T) {
400400
t.Run("create file that already exists", func(t *testing.T) {
401401
opts := getCreateRepoFileOptions(repo)
402402
opts.TreePath = "README.md" //already exists
403-
fileResponse, err := repofiles.CreateOrUpdateRepoFile(repo, doer, opts)
403+
fileResponse, err := files_service.CreateOrUpdateRepoFile(repo, doer, opts)
404404
assert.Nil(t, fileResponse)
405405
assert.Error(t, err)
406406
expectedError := "repository file already exists [path: " + opts.TreePath + "]"

modules/convert/pull.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"code.gitea.io/gitea/models"
1111
"code.gitea.io/gitea/modules/git"
1212
"code.gitea.io/gitea/modules/log"
13-
repo_module "code.gitea.io/gitea/modules/repository"
1413
api "code.gitea.io/gitea/modules/structs"
1514
)
1615

@@ -83,7 +82,14 @@ func ToAPIPullRequest(pr *models.PullRequest, doer *models.User) *api.PullReques
8382
},
8483
}
8584

86-
baseBranch, err = repo_module.GetBranch(pr.BaseRepo, pr.BaseBranch)
85+
gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
86+
if err != nil {
87+
log.Error("OpenRepository[%s]: %v", pr.BaseRepo.RepoPath(), err)
88+
return nil
89+
}
90+
defer gitRepo.Close()
91+
92+
baseBranch, err = gitRepo.GetBranch(pr.BaseBranch)
8793
if err != nil && !git.IsErrBranchNotExist(err) {
8894
log.Error("GetBranch[%s]: %v", pr.BaseBranch, err)
8995
return nil

modules/repofiles/blob.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)