@@ -49,14 +49,14 @@ func getCreateFileOptions() api.CreateFileOptions {
49
49
}
50
50
}
51
51
52
- func getExpectedFileResponseForCreate (commitID , treePath string ) * api.FileResponse {
52
+ func getExpectedFileResponseForCreate (repoFullName , commitID , treePath string ) * api.FileResponse {
53
53
sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
54
54
encoding := "base64"
55
55
content := "VGhpcyBpcyBuZXcgdGV4dA=="
56
- selfURL := setting .AppURL + "api/v1/repos/user2/repo1 /contents/" + treePath + "?ref=master"
57
- htmlURL := setting .AppURL + "user2/repo1 /src/branch/master/" + treePath
58
- gitURL := setting .AppURL + "api/v1/repos/user2/repo1 /git/blobs/" + sha
59
- downloadURL := setting .AppURL + "user2/repo1 /raw/branch/master/" + treePath
56
+ selfURL := setting .AppURL + "api/v1/repos/" + repoFullName + " /contents/" + treePath + "?ref=master"
57
+ htmlURL := setting .AppURL + repoFullName + " /src/branch/master/" + treePath
58
+ gitURL := setting .AppURL + "api/v1/repos/" + repoFullName + " /git/blobs/" + sha
59
+ downloadURL := setting .AppURL + repoFullName + " /raw/branch/master/" + treePath
60
60
return & api.FileResponse {
61
61
Content : & api.ContentsResponse {
62
62
Name : filepath .Base (treePath ),
@@ -78,10 +78,10 @@ func getExpectedFileResponseForCreate(commitID, treePath string) *api.FileRespon
78
78
},
79
79
Commit : & api.FileCommitResponse {
80
80
CommitMeta : api.CommitMeta {
81
- URL : setting .AppURL + "api/v1/repos/user2/repo1 /git/commits/" + commitID ,
81
+ URL : setting .AppURL + "api/v1/repos/" + repoFullName + " /git/commits/" + commitID ,
82
82
SHA : commitID ,
83
83
},
84
- HTMLURL : setting .AppURL + "user2/repo1 /commit/" + commitID ,
84
+ HTMLURL : setting .AppURL + repoFullName + " /commit/" + commitID ,
85
85
Author : & api.CommitUser {
86
86
Identity : api.Identity {
87
87
Name : "Anne Doe" ,
@@ -169,7 +169,7 @@ func TestAPICreateFile(t *testing.T) {
169
169
resp := session .MakeRequest (t , req , http .StatusCreated )
170
170
gitRepo , _ := git .OpenRepository (repo1 .RepoPath ())
171
171
commitID , _ := gitRepo .GetBranchCommitID (createFileOptions .NewBranchName )
172
- expectedFileResponse := getExpectedFileResponseForCreate (commitID , treePath )
172
+ expectedFileResponse := getExpectedFileResponseForCreate ("user2/repo1" , commitID , treePath )
173
173
var fileResponse api.FileResponse
174
174
DecodeJSON (t , resp , & fileResponse )
175
175
assert .EqualValues (t , expectedFileResponse .Content , fileResponse .Content )
@@ -276,5 +276,29 @@ func TestAPICreateFile(t *testing.T) {
276
276
url = fmt .Sprintf ("/api/v1/repos/%s/%s/contents/%s?token=%s" , user2 .Name , repo1 .Name , treePath , token4 )
277
277
req = NewRequestWithJSON (t , "POST" , url , & createFileOptions )
278
278
session .MakeRequest (t , req , http .StatusForbidden )
279
+
280
+ // Test creating a file in an empty repository
281
+ doAPICreateRepository (NewAPITestContext (t , "user2" , "empty-repo" ), true )(t )
282
+ createFileOptions = getCreateFileOptions ()
283
+ fileID ++
284
+ treePath = fmt .Sprintf ("new/file%d.txt" , fileID )
285
+ url = fmt .Sprintf ("/api/v1/repos/%s/%s/contents/%s?token=%s" , user2 .Name , "empty-repo" , treePath , token2 )
286
+ req = NewRequestWithJSON (t , "POST" , url , & createFileOptions )
287
+ resp = session .MakeRequest (t , req , http .StatusCreated )
288
+ emptyRepo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {OwnerName : "user2" , Name : "empty-repo" }).(* repo_model.Repository ) // public repo
289
+ gitRepo , _ := git .OpenRepository (emptyRepo .RepoPath ())
290
+ commitID , _ := gitRepo .GetBranchCommitID (createFileOptions .NewBranchName )
291
+ expectedFileResponse := getExpectedFileResponseForCreate ("user2/empty-repo" , commitID , treePath )
292
+ DecodeJSON (t , resp , & fileResponse )
293
+ assert .EqualValues (t , expectedFileResponse .Content , fileResponse .Content )
294
+ assert .EqualValues (t , expectedFileResponse .Commit .SHA , fileResponse .Commit .SHA )
295
+ assert .EqualValues (t , expectedFileResponse .Commit .HTMLURL , fileResponse .Commit .HTMLURL )
296
+ assert .EqualValues (t , expectedFileResponse .Commit .Author .Email , fileResponse .Commit .Author .Email )
297
+ assert .EqualValues (t , expectedFileResponse .Commit .Author .Name , fileResponse .Commit .Author .Name )
298
+ assert .EqualValues (t , expectedFileResponse .Commit .Author .Date , fileResponse .Commit .Author .Date )
299
+ assert .EqualValues (t , expectedFileResponse .Commit .Committer .Email , fileResponse .Commit .Committer .Email )
300
+ assert .EqualValues (t , expectedFileResponse .Commit .Committer .Name , fileResponse .Commit .Committer .Name )
301
+ assert .EqualValues (t , expectedFileResponse .Commit .Committer .Date , fileResponse .Commit .Committer .Date )
302
+ gitRepo .Close ()
279
303
})
280
304
}
0 commit comments