Skip to content

Commit 785b918

Browse files
committed
fix
1 parent 84b6ae4 commit 785b918

File tree

11 files changed

+230
-189
lines changed

11 files changed

+230
-189
lines changed

modules/structs/repo_file.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ type ContentsResponse struct {
124124
SHA string `json:"sha"`
125125
LastCommitSHA string `json:"last_commit_sha"`
126126
// swagger:strfmt date-time
127-
LastCommitWhen time.Time `json:"last_commit_when"`
127+
LastCommitterWhen time.Time `json:"last_committer_when"`
128+
// swagger:strfmt date-time
129+
LastAuthorWhen time.Time `json:"last_author_when"`
128130
// `type` will be `file`, `dir`, `symlink`, or `submodule`
129131
Type string `json:"type"`
130132
Size int64 `json:"size"`

services/repository/files/content.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,22 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref
177177

178178
// All content types have these fields in populated
179179
contentsResponse := &api.ContentsResponse{
180-
Name: entry.Name(),
181-
Path: treePath,
182-
SHA: entry.ID.String(),
183-
LastCommitSHA: lastCommit.ID.String(),
184-
LastCommitWhen: lastCommit.Committer.When,
185-
Size: entry.Size(),
186-
URL: &selfURLString,
180+
Name: entry.Name(),
181+
Path: treePath,
182+
SHA: entry.ID.String(),
183+
LastCommitSHA: lastCommit.ID.String(),
184+
Size: entry.Size(),
185+
URL: &selfURLString,
187186
Links: &api.FileLinksResponse{
188187
Self: &selfURLString,
189188
},
190189
}
191-
190+
if lastCommit.Committer != nil {
191+
contentsResponse.LastCommitterWhen = lastCommit.Committer.When
192+
}
193+
if lastCommit.Author != nil {
194+
contentsResponse.LastAuthorWhen = lastCommit.Author.When
195+
}
192196
// Now populate the rest of the ContentsResponse based on entry type
193197
if entry.IsRegular() || entry.IsExecutable() {
194198
contentsResponse.Type = string(ContentTypeRegular)

services/repository/files/content_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,20 @@ func getExpectedReadmeContentsResponse() *api.ContentsResponse {
3131
gitURL := "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/" + sha
3232
downloadURL := "https://try.gitea.io/user2/repo1/raw/branch/master/" + treePath
3333
return &api.ContentsResponse{
34-
Name: treePath,
35-
Path: treePath,
36-
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
37-
LastCommitSHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
38-
LastCommitWhen: time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400)),
39-
Type: "file",
40-
Size: 30,
41-
Encoding: &encoding,
42-
Content: &content,
43-
URL: &selfURL,
44-
HTMLURL: &htmlURL,
45-
GitURL: &gitURL,
46-
DownloadURL: &downloadURL,
34+
Name: treePath,
35+
Path: treePath,
36+
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
37+
LastCommitSHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
38+
LastCommitterWhen: time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400)),
39+
LastAuthorWhen: 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,
4748
Links: &api.FileLinksResponse{
4849
Self: &selfURL,
4950
GitURL: &gitURL,

services/repository/files/file_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,20 @@ func getExpectedFileResponse() *api.FileResponse {
4343
downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + treePath
4444
return &api.FileResponse{
4545
Content: &api.ContentsResponse{
46-
Name: treePath,
47-
Path: treePath,
48-
SHA: sha,
49-
LastCommitSHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
50-
LastCommitWhen: time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400)),
51-
Type: "file",
52-
Size: 30,
53-
Encoding: &encoding,
54-
Content: &content,
55-
URL: &selfURL,
56-
HTMLURL: &htmlURL,
57-
GitURL: &gitURL,
58-
DownloadURL: &downloadURL,
46+
Name: treePath,
47+
Path: treePath,
48+
SHA: sha,
49+
LastCommitSHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
50+
LastCommitterWhen: time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400)),
51+
LastAuthorWhen: 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,
5960
Links: &api.FileLinksResponse{
6061
Self: &selfURL,
6162
GitURL: &gitURL,

templates/swagger/v1_json.tmpl

Lines changed: 7 additions & 2 deletions
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

Lines changed: 59 additions & 38 deletions
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,29 +49,42 @@ func getCreateFileOptions() api.CreateFileOptions {
4949
}
5050
}
5151

52-
func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCommitSHA string, latestCommitWhen time.Time) *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.LastCommitterWhen = c.LastCommitterWhen.UTC()
56+
c.LastAuthorWhen = c.LastAuthorWhen.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-
LastCommitWhen: latestCommitWhen,
67-
Size: 16,
68-
Type: "file",
69-
Encoding: &encoding,
70-
Content: &content,
71-
URL: &selfURL,
72-
HTMLURL: &htmlURL,
73-
GitURL: &gitURL,
74-
DownloadURL: &downloadURL,
74+
Name: path.Base(info.treePath),
75+
Path: info.treePath,
76+
SHA: sha,
77+
LastCommitSHA: info.lastCommitSHA,
78+
LastCommitterWhen: info.lastCommitterWhen,
79+
LastAuthorWhen: 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,
7588
Links: &api.FileLinksResponse{
7689
Self: &selfURL,
7790
GitURL: &gitURL,
@@ -80,10 +93,10 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
8093
},
8194
Commit: &api.FileCommitResponse{
8295
CommitMeta: api.CommitMeta{
83-
URL: setting.AppURL + "api/v1/repos/" + repoFullName + "/git/commits/" + commitID,
84-
SHA: commitID,
96+
URL: setting.AppURL + "api/v1/repos/" + info.repoFullName + "/git/commits/" + info.commitID,
97+
SHA: info.commitID,
8598
},
86-
HTMLURL: setting.AppURL + repoFullName + "/commit/" + commitID,
99+
HTMLURL: setting.AppURL + info.repoFullName + "/commit/" + info.commitID,
87100
Author: &api.CommitUser{
88101
Identity: api.Identity{
89102
Name: "Anne Doe",
@@ -107,6 +120,8 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
107120
Payload: "",
108121
},
109122
}
123+
normalizeFileContentResponseCommitTime(ret.Content)
124+
return ret
110125
}
111126

112127
func BenchmarkAPICreateFileSmall(b *testing.B) {
@@ -168,16 +183,20 @@ func TestAPICreateFile(t *testing.T) {
168183
AddTokenAuth(token2)
169184
resp := MakeRequest(t, req, http.StatusCreated)
170185
gitRepo, _ := gitrepo.OpenRepository(t.Context(), repo1)
186+
defer gitRepo.Close()
171187
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
172-
latestCommit, _ := gitRepo.GetCommitByPath(treePath)
173-
expectedFileResponse := getExpectedFileResponseForCreate("user2/repo1", commitID, treePath, latestCommit.ID.String(), latestCommit.Committer.When)
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+
})
174197
var fileResponse api.FileResponse
175198
DecodeJSON(t, resp, &fileResponse)
176-
177-
// FIXME: This is a workaround to compare time.Time values. This maybe a bug of Golang,
178-
// assume your local timezone is UTC, but a location with zero offset is not equal to UTC but they should be.
179-
expectedFileResponse.Content.LastCommitWhen, _ = time.Parse(time.RFC3339, expectedFileResponse.Content.LastCommitWhen.Format(time.RFC3339))
180-
199+
normalizeFileContentResponseCommitTime(fileResponse.Content)
181200
assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
182201
assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
183202
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
@@ -187,7 +206,6 @@ func TestAPICreateFile(t *testing.T) {
187206
assert.Equal(t, expectedFileResponse.Commit.Committer.Email, fileResponse.Commit.Committer.Email)
188207
assert.Equal(t, expectedFileResponse.Commit.Committer.Name, fileResponse.Commit.Committer.Name)
189208
assert.Equal(t, expectedFileResponse.Commit.Committer.Date, fileResponse.Commit.Committer.Date)
190-
gitRepo.Close()
191209
}
192210

193211
// Test creating a file in a new branch
@@ -291,15 +309,19 @@ func TestAPICreateFile(t *testing.T) {
291309
resp = MakeRequest(t, req, http.StatusCreated)
292310
emptyRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "empty-repo"}) // public repo
293311
gitRepo, _ := gitrepo.OpenRepository(t.Context(), emptyRepo)
312+
defer gitRepo.Close()
294313
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
295314
latestCommit, _ := gitRepo.GetCommitByPath(treePath)
296-
expectedFileResponse := getExpectedFileResponseForCreate("user2/empty-repo", commitID, treePath, latestCommit.ID.String(), latestCommit.Committer.When)
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+
})
297323
DecodeJSON(t, resp, &fileResponse)
298-
299-
// FIXME: This is a workaround to compare time.Time values. This maybe a bug of Golang,
300-
// assume your local timezone is UTC, but a location with zero offset is not equal to UTC but they should be.
301-
expectedFileResponse.Content.LastCommitWhen, _ = time.Parse(time.RFC3339, expectedFileResponse.Content.LastCommitWhen.Format(time.RFC3339))
302-
324+
normalizeFileContentResponseCommitTime(fileResponse.Content)
303325
assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
304326
assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
305327
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
@@ -309,6 +331,5 @@ func TestAPICreateFile(t *testing.T) {
309331
assert.Equal(t, expectedFileResponse.Commit.Committer.Email, fileResponse.Commit.Committer.Email)
310332
assert.Equal(t, expectedFileResponse.Commit.Committer.Name, fileResponse.Commit.Committer.Name)
311333
assert.Equal(t, expectedFileResponse.Commit.Committer.Date, fileResponse.Commit.Committer.Date)
312-
gitRepo.Close()
313334
})
314335
}

0 commit comments

Comments
 (0)