Skip to content

Commit 4502328

Browse files
committed
fix
1 parent c27d87a commit 4502328

11 files changed

+264
-176
lines changed

modules/structs/repo_file.go

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package structs
66

7+
import "time"
8+
79
// FileOptions options for all file APIs
810
type FileOptions struct {
911
// message (optional) for the commit of this file. if not supplied, a default message will be used
@@ -121,6 +123,10 @@ type ContentsResponse struct {
121123
Path string `json:"path"`
122124
SHA string `json:"sha"`
123125
LastCommitSHA string `json:"last_commit_sha"`
126+
// swagger:strfmt date-time
127+
LastCommitterDate time.Time `json:"last_committer_date"`
128+
// swagger:strfmt date-time
129+
LastAuthorDate time.Time `json:"last_author_date"`
124130
// `type` will be `file`, `dir`, `symlink`, or `submodule`
125131
Type string `json:"type"`
126132
Size int64 `json:"size"`

services/repository/files/content.go

+8
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref
188188
},
189189
}
190190

191+
// GitHub doesn't have these fields in the response, but we could follow other similar APIs to name them
192+
// https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28#list-commits
193+
if lastCommit.Committer != nil {
194+
contentsResponse.LastCommitterDate = lastCommit.Committer.When
195+
}
196+
if lastCommit.Author != nil {
197+
contentsResponse.LastAuthorDate = lastCommit.Author.When
198+
}
191199
// Now populate the rest of the ContentsResponse based on entry type
192200
if entry.IsRegular() || entry.IsExecutable() {
193201
contentsResponse.Type = string(ContentTypeRegular)

services/repository/files/content_test.go

+15-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package files
55

66
import (
77
"testing"
8+
"time"
89

910
"code.gitea.io/gitea/models/unittest"
1011
"code.gitea.io/gitea/modules/gitrepo"
@@ -30,18 +31,20 @@ func getExpectedReadmeContentsResponse() *api.ContentsResponse {
3031
gitURL := "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/" + sha
3132
downloadURL := "https://try.gitea.io/user2/repo1/raw/branch/master/" + treePath
3233
return &api.ContentsResponse{
33-
Name: treePath,
34-
Path: treePath,
35-
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
36-
LastCommitSHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
37-
Type: "file",
38-
Size: 30,
39-
Encoding: &encoding,
40-
Content: &content,
41-
URL: &selfURL,
42-
HTMLURL: &htmlURL,
43-
GitURL: &gitURL,
44-
DownloadURL: &downloadURL,
34+
Name: treePath,
35+
Path: treePath,
36+
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
37+
LastCommitSHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
38+
LastCommitterDate: time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400)),
39+
LastAuthorDate: time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400)),
40+
Type: "file",
41+
Size: 30,
42+
Encoding: &encoding,
43+
Content: &content,
44+
URL: &selfURL,
45+
HTMLURL: &htmlURL,
46+
GitURL: &gitURL,
47+
DownloadURL: &downloadURL,
4548
Links: &api.FileLinksResponse{
4649
Self: &selfURL,
4750
GitURL: &gitURL,

services/repository/files/file_test.go

+15-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package files
55

66
import (
77
"testing"
8+
"time"
89

910
"code.gitea.io/gitea/models/unittest"
1011
"code.gitea.io/gitea/modules/gitrepo"
@@ -42,18 +43,20 @@ func getExpectedFileResponse() *api.FileResponse {
4243
downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + treePath
4344
return &api.FileResponse{
4445
Content: &api.ContentsResponse{
45-
Name: treePath,
46-
Path: treePath,
47-
SHA: sha,
48-
LastCommitSHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
49-
Type: "file",
50-
Size: 30,
51-
Encoding: &encoding,
52-
Content: &content,
53-
URL: &selfURL,
54-
HTMLURL: &htmlURL,
55-
GitURL: &gitURL,
56-
DownloadURL: &downloadURL,
46+
Name: treePath,
47+
Path: treePath,
48+
SHA: sha,
49+
LastCommitSHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
50+
LastCommitterDate: time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400)),
51+
LastAuthorDate: time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400)),
52+
Type: "file",
53+
Size: 30,
54+
Encoding: &encoding,
55+
Content: &content,
56+
URL: &selfURL,
57+
HTMLURL: &htmlURL,
58+
GitURL: &gitURL,
59+
DownloadURL: &downloadURL,
5760
Links: &api.FileLinksResponse{
5861
Self: &selfURL,
5962
GitURL: &gitURL,

templates/swagger/v1_json.tmpl

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/api_repo_file_create_test.go

+59-27
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"fmt"
99
"net/http"
1010
"net/url"
11-
"path/filepath"
11+
"path"
1212
"testing"
1313
"time"
1414

@@ -49,28 +49,42 @@ func getCreateFileOptions() api.CreateFileOptions {
4949
}
5050
}
5151

52-
func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCommitSHA string) *api.FileResponse {
52+
func normalizeFileContentResponseCommitTime(c *api.ContentsResponse) {
53+
// decoded JSON response may contain different timezone from the one parsed by git commit
54+
// so we need to normalize the time to UTC to make "assert.Equal" pass
55+
c.LastCommitterDate = c.LastCommitterDate.UTC()
56+
c.LastAuthorDate = c.LastAuthorDate.UTC()
57+
}
58+
59+
type apiFileResponseInfo struct {
60+
repoFullName, commitID, treePath, lastCommitSHA string
61+
lastCommitterWhen, lastAuthorWhen time.Time
62+
}
63+
64+
func getExpectedFileResponseForCreate(info apiFileResponseInfo) *api.FileResponse {
5365
sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
5466
encoding := "base64"
5567
content := "VGhpcyBpcyBuZXcgdGV4dA=="
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-
return &api.FileResponse{
68+
selfURL := setting.AppURL + "api/v1/repos/" + info.repoFullName + "/contents/" + info.treePath + "?ref=master"
69+
htmlURL := setting.AppURL + info.repoFullName + "/src/branch/master/" + info.treePath
70+
gitURL := setting.AppURL + "api/v1/repos/" + info.repoFullName + "/git/blobs/" + sha
71+
downloadURL := setting.AppURL + info.repoFullName + "/raw/branch/master/" + info.treePath
72+
ret := &api.FileResponse{
6173
Content: &api.ContentsResponse{
62-
Name: filepath.Base(treePath),
63-
Path: treePath,
64-
SHA: sha,
65-
LastCommitSHA: latestCommitSHA,
66-
Size: 16,
67-
Type: "file",
68-
Encoding: &encoding,
69-
Content: &content,
70-
URL: &selfURL,
71-
HTMLURL: &htmlURL,
72-
GitURL: &gitURL,
73-
DownloadURL: &downloadURL,
74+
Name: path.Base(info.treePath),
75+
Path: info.treePath,
76+
SHA: sha,
77+
LastCommitSHA: info.lastCommitSHA,
78+
LastCommitterDate: info.lastCommitterWhen,
79+
LastAuthorDate: info.lastAuthorWhen,
80+
Size: 16,
81+
Type: "file",
82+
Encoding: &encoding,
83+
Content: &content,
84+
URL: &selfURL,
85+
HTMLURL: &htmlURL,
86+
GitURL: &gitURL,
87+
DownloadURL: &downloadURL,
7488
Links: &api.FileLinksResponse{
7589
Self: &selfURL,
7690
GitURL: &gitURL,
@@ -79,10 +93,10 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
7993
},
8094
Commit: &api.FileCommitResponse{
8195
CommitMeta: api.CommitMeta{
82-
URL: setting.AppURL + "api/v1/repos/" + repoFullName + "/git/commits/" + commitID,
83-
SHA: commitID,
96+
URL: setting.AppURL + "api/v1/repos/" + info.repoFullName + "/git/commits/" + info.commitID,
97+
SHA: info.commitID,
8498
},
85-
HTMLURL: setting.AppURL + repoFullName + "/commit/" + commitID,
99+
HTMLURL: setting.AppURL + info.repoFullName + "/commit/" + info.commitID,
86100
Author: &api.CommitUser{
87101
Identity: api.Identity{
88102
Name: "Anne Doe",
@@ -106,6 +120,8 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
106120
Payload: "",
107121
},
108122
}
123+
normalizeFileContentResponseCommitTime(ret.Content)
124+
return ret
109125
}
110126

111127
func BenchmarkAPICreateFileSmall(b *testing.B) {
@@ -167,11 +183,20 @@ func TestAPICreateFile(t *testing.T) {
167183
AddTokenAuth(token2)
168184
resp := MakeRequest(t, req, http.StatusCreated)
169185
gitRepo, _ := gitrepo.OpenRepository(t.Context(), repo1)
186+
defer gitRepo.Close()
170187
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
171-
latestCommit, _ := gitRepo.GetCommitByPath(treePath)
172-
expectedFileResponse := getExpectedFileResponseForCreate("user2/repo1", commitID, treePath, latestCommit.ID.String())
188+
lastCommit, _ := gitRepo.GetCommitByPath(treePath)
189+
expectedFileResponse := getExpectedFileResponseForCreate(apiFileResponseInfo{
190+
repoFullName: "user2/repo1",
191+
commitID: commitID,
192+
treePath: treePath,
193+
lastCommitSHA: lastCommit.ID.String(),
194+
lastCommitterWhen: lastCommit.Committer.When,
195+
lastAuthorWhen: lastCommit.Author.When,
196+
})
173197
var fileResponse api.FileResponse
174198
DecodeJSON(t, resp, &fileResponse)
199+
normalizeFileContentResponseCommitTime(fileResponse.Content)
175200
assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
176201
assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
177202
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
@@ -181,7 +206,6 @@ func TestAPICreateFile(t *testing.T) {
181206
assert.Equal(t, expectedFileResponse.Commit.Committer.Email, fileResponse.Commit.Committer.Email)
182207
assert.Equal(t, expectedFileResponse.Commit.Committer.Name, fileResponse.Commit.Committer.Name)
183208
assert.Equal(t, expectedFileResponse.Commit.Committer.Date, fileResponse.Commit.Committer.Date)
184-
gitRepo.Close()
185209
}
186210

187211
// Test creating a file in a new branch
@@ -285,10 +309,19 @@ func TestAPICreateFile(t *testing.T) {
285309
resp = MakeRequest(t, req, http.StatusCreated)
286310
emptyRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "empty-repo"}) // public repo
287311
gitRepo, _ := gitrepo.OpenRepository(t.Context(), emptyRepo)
312+
defer gitRepo.Close()
288313
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
289314
latestCommit, _ := gitRepo.GetCommitByPath(treePath)
290-
expectedFileResponse := getExpectedFileResponseForCreate("user2/empty-repo", commitID, treePath, latestCommit.ID.String())
315+
expectedFileResponse := getExpectedFileResponseForCreate(apiFileResponseInfo{
316+
repoFullName: "user2/empty-repo",
317+
commitID: commitID,
318+
treePath: treePath,
319+
lastCommitSHA: latestCommit.ID.String(),
320+
lastCommitterWhen: latestCommit.Committer.When,
321+
lastAuthorWhen: latestCommit.Author.When,
322+
})
291323
DecodeJSON(t, resp, &fileResponse)
324+
normalizeFileContentResponseCommitTime(fileResponse.Content)
292325
assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
293326
assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
294327
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
@@ -298,6 +331,5 @@ func TestAPICreateFile(t *testing.T) {
298331
assert.Equal(t, expectedFileResponse.Commit.Committer.Email, fileResponse.Commit.Committer.Email)
299332
assert.Equal(t, expectedFileResponse.Commit.Committer.Name, fileResponse.Commit.Committer.Name)
300333
assert.Equal(t, expectedFileResponse.Commit.Committer.Date, fileResponse.Commit.Committer.Date)
301-
gitRepo.Close()
302334
})
303335
}

tests/integration/api_repo_file_update_test.go

+34-23
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"fmt"
99
"net/http"
1010
"net/url"
11-
"path/filepath"
11+
"path"
1212
"testing"
1313

1414
auth_model "code.gitea.io/gitea/models/auth"
@@ -47,28 +47,30 @@ func getUpdateFileOptions() *api.UpdateFileOptions {
4747
}
4848
}
4949

50-
func getExpectedFileResponseForUpdate(commitID, treePath, lastCommitSHA string) *api.FileResponse {
50+
func getExpectedFileResponseForUpdate(info apiFileResponseInfo) *api.FileResponse {
5151
sha := "08bd14b2e2852529157324de9c226b3364e76136"
5252
encoding := "base64"
5353
content := "VGhpcyBpcyB1cGRhdGVkIHRleHQ="
54-
selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master"
55-
htmlURL := setting.AppURL + "user2/repo1/src/branch/master/" + treePath
54+
selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + info.treePath + "?ref=master"
55+
htmlURL := setting.AppURL + "user2/repo1/src/branch/master/" + info.treePath
5656
gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha
57-
downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + treePath
58-
return &api.FileResponse{
57+
downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + info.treePath
58+
ret := &api.FileResponse{
5959
Content: &api.ContentsResponse{
60-
Name: filepath.Base(treePath),
61-
Path: treePath,
62-
SHA: sha,
63-
LastCommitSHA: lastCommitSHA,
64-
Type: "file",
65-
Size: 20,
66-
Encoding: &encoding,
67-
Content: &content,
68-
URL: &selfURL,
69-
HTMLURL: &htmlURL,
70-
GitURL: &gitURL,
71-
DownloadURL: &downloadURL,
60+
Name: path.Base(info.treePath),
61+
Path: info.treePath,
62+
SHA: sha,
63+
LastCommitSHA: info.lastCommitSHA,
64+
LastCommitterDate: info.lastCommitterWhen,
65+
LastAuthorDate: info.lastAuthorWhen,
66+
Type: "file",
67+
Size: 20,
68+
Encoding: &encoding,
69+
Content: &content,
70+
URL: &selfURL,
71+
HTMLURL: &htmlURL,
72+
GitURL: &gitURL,
73+
DownloadURL: &downloadURL,
7274
Links: &api.FileLinksResponse{
7375
Self: &selfURL,
7476
GitURL: &gitURL,
@@ -77,10 +79,10 @@ func getExpectedFileResponseForUpdate(commitID, treePath, lastCommitSHA string)
7779
},
7880
Commit: &api.FileCommitResponse{
7981
CommitMeta: api.CommitMeta{
80-
URL: setting.AppURL + "api/v1/repos/user2/repo1/git/commits/" + commitID,
81-
SHA: commitID,
82+
URL: setting.AppURL + "api/v1/repos/user2/repo1/git/commits/" + info.commitID,
83+
SHA: info.commitID,
8284
},
83-
HTMLURL: setting.AppURL + "user2/repo1/commit/" + commitID,
85+
HTMLURL: setting.AppURL + "user2/repo1/commit/" + info.commitID,
8486
Author: &api.CommitUser{
8587
Identity: api.Identity{
8688
Name: "John Doe",
@@ -102,6 +104,8 @@ func getExpectedFileResponseForUpdate(commitID, treePath, lastCommitSHA string)
102104
Payload: "",
103105
},
104106
}
107+
normalizeFileContentResponseCommitTime(ret.Content)
108+
return ret
105109
}
106110

107111
func TestAPIUpdateFile(t *testing.T) {
@@ -135,17 +139,24 @@ func TestAPIUpdateFile(t *testing.T) {
135139
AddTokenAuth(token2)
136140
resp := MakeRequest(t, req, http.StatusOK)
137141
gitRepo, _ := gitrepo.OpenRepository(t.Context(), repo1)
142+
defer gitRepo.Close()
138143
commitID, _ := gitRepo.GetBranchCommitID(updateFileOptions.NewBranchName)
139144
lasCommit, _ := gitRepo.GetCommitByPath(treePath)
140-
expectedFileResponse := getExpectedFileResponseForUpdate(commitID, treePath, lasCommit.ID.String())
145+
expectedFileResponse := getExpectedFileResponseForUpdate(apiFileResponseInfo{
146+
commitID: commitID,
147+
treePath: treePath,
148+
lastCommitSHA: lasCommit.ID.String(),
149+
lastCommitterWhen: lasCommit.Committer.When,
150+
lastAuthorWhen: lasCommit.Author.When,
151+
})
141152
var fileResponse api.FileResponse
142153
DecodeJSON(t, resp, &fileResponse)
154+
normalizeFileContentResponseCommitTime(fileResponse.Content)
143155
assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
144156
assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
145157
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
146158
assert.Equal(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
147159
assert.Equal(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
148-
gitRepo.Close()
149160
}
150161

151162
// Test updating a file in a new branch

0 commit comments

Comments
 (0)