Skip to content

Commit 475b482

Browse files
committed
fix
1 parent 84b6ae4 commit 475b482

9 files changed

+178
-155
lines changed

modules/structs/repo_file.go

+3-1
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

+12-8
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)

templates/swagger/v1_json.tmpl

+7-2
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

+46-36
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,35 @@ func getCreateFileOptions() api.CreateFileOptions {
4949
}
5050
}
5151

52-
func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCommitSHA string, latestCommitWhen time.Time) *api.FileResponse {
52+
type apiFileResponseInfo struct {
53+
repoFullName, commitID, treePath, lastCommitSHA string
54+
lastCommitterWhen, lastAuthorWhen time.Time
55+
}
56+
57+
func getExpectedFileResponseForCreate(info apiFileResponseInfo) *api.FileResponse {
5358
sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
5459
encoding := "base64"
5560
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
61+
selfURL := setting.AppURL + "api/v1/repos/" + info.repoFullName + "/contents/" + info.treePath + "?ref=master"
62+
htmlURL := setting.AppURL + info.repoFullName + "/src/branch/master/" + info.treePath
63+
gitURL := setting.AppURL + "api/v1/repos/" + info.repoFullName + "/git/blobs/" + sha
64+
downloadURL := setting.AppURL + info.repoFullName + "/raw/branch/master/" + info.treePath
6065
return &api.FileResponse{
6166
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,
67+
Name: path.Base(info.treePath),
68+
Path: info.treePath,
69+
SHA: sha,
70+
LastCommitSHA: info.lastCommitSHA,
71+
LastCommitterWhen: info.lastCommitterWhen,
72+
LastAuthorWhen: info.lastAuthorWhen,
73+
Size: 16,
74+
Type: "file",
75+
Encoding: &encoding,
76+
Content: &content,
77+
URL: &selfURL,
78+
HTMLURL: &htmlURL,
79+
GitURL: &gitURL,
80+
DownloadURL: &downloadURL,
7581
Links: &api.FileLinksResponse{
7682
Self: &selfURL,
7783
GitURL: &gitURL,
@@ -80,10 +86,10 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
8086
},
8187
Commit: &api.FileCommitResponse{
8288
CommitMeta: api.CommitMeta{
83-
URL: setting.AppURL + "api/v1/repos/" + repoFullName + "/git/commits/" + commitID,
84-
SHA: commitID,
89+
URL: setting.AppURL + "api/v1/repos/" + info.repoFullName + "/git/commits/" + info.commitID,
90+
SHA: info.commitID,
8591
},
86-
HTMLURL: setting.AppURL + repoFullName + "/commit/" + commitID,
92+
HTMLURL: setting.AppURL + info.repoFullName + "/commit/" + info.commitID,
8793
Author: &api.CommitUser{
8894
Identity: api.Identity{
8995
Name: "Anne Doe",
@@ -168,16 +174,19 @@ func TestAPICreateFile(t *testing.T) {
168174
AddTokenAuth(token2)
169175
resp := MakeRequest(t, req, http.StatusCreated)
170176
gitRepo, _ := gitrepo.OpenRepository(t.Context(), repo1)
177+
defer gitRepo.Close()
171178
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
172179
latestCommit, _ := gitRepo.GetCommitByPath(treePath)
173-
expectedFileResponse := getExpectedFileResponseForCreate("user2/repo1", commitID, treePath, latestCommit.ID.String(), latestCommit.Committer.When)
180+
expectedFileResponse := getExpectedFileResponseForCreate(apiFileResponseInfo{
181+
repoFullName: "user2/repo1",
182+
commitID: commitID,
183+
treePath: treePath,
184+
lastCommitSHA: latestCommit.ID.String(),
185+
lastCommitterWhen: latestCommit.Committer.When,
186+
lastAuthorWhen: latestCommit.Author.When,
187+
})
174188
var fileResponse api.FileResponse
175189
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-
181190
assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
182191
assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
183192
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
@@ -187,7 +196,6 @@ func TestAPICreateFile(t *testing.T) {
187196
assert.Equal(t, expectedFileResponse.Commit.Committer.Email, fileResponse.Commit.Committer.Email)
188197
assert.Equal(t, expectedFileResponse.Commit.Committer.Name, fileResponse.Commit.Committer.Name)
189198
assert.Equal(t, expectedFileResponse.Commit.Committer.Date, fileResponse.Commit.Committer.Date)
190-
gitRepo.Close()
191199
}
192200

193201
// Test creating a file in a new branch
@@ -291,15 +299,18 @@ func TestAPICreateFile(t *testing.T) {
291299
resp = MakeRequest(t, req, http.StatusCreated)
292300
emptyRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "empty-repo"}) // public repo
293301
gitRepo, _ := gitrepo.OpenRepository(t.Context(), emptyRepo)
302+
defer gitRepo.Close()
294303
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
295304
latestCommit, _ := gitRepo.GetCommitByPath(treePath)
296-
expectedFileResponse := getExpectedFileResponseForCreate("user2/empty-repo", commitID, treePath, latestCommit.ID.String(), latestCommit.Committer.When)
305+
expectedFileResponse := getExpectedFileResponseForCreate(apiFileResponseInfo{
306+
repoFullName: "user2/empty-repo",
307+
commitID: commitID,
308+
treePath: treePath,
309+
lastCommitSHA: latestCommit.ID.String(),
310+
lastCommitterWhen: latestCommit.Committer.When,
311+
lastAuthorWhen: latestCommit.Author.When,
312+
})
297313
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-
303314
assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
304315
assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
305316
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
@@ -309,6 +320,5 @@ func TestAPICreateFile(t *testing.T) {
309320
assert.Equal(t, expectedFileResponse.Commit.Committer.Email, fileResponse.Commit.Committer.Email)
310321
assert.Equal(t, expectedFileResponse.Commit.Committer.Name, fileResponse.Commit.Committer.Name)
311322
assert.Equal(t, expectedFileResponse.Commit.Committer.Date, fileResponse.Commit.Committer.Date)
312-
gitRepo.Close()
313323
})
314324
}

tests/integration/api_repo_file_update_test.go

+29-29
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import (
88
"fmt"
99
"net/http"
1010
"net/url"
11-
"path/filepath"
11+
"path"
1212
"testing"
13-
"time"
1413

1514
auth_model "code.gitea.io/gitea/models/auth"
1615
repo_model "code.gitea.io/gitea/models/repo"
@@ -48,29 +47,29 @@ func getUpdateFileOptions() *api.UpdateFileOptions {
4847
}
4948
}
5049

51-
func getExpectedFileResponseForUpdate(commitID, treePath, lastCommitSHA string, lastCommitWhen time.Time) *api.FileResponse {
50+
func getExpectedFileResponseForUpdate(info apiFileResponseInfo) *api.FileResponse {
5251
sha := "08bd14b2e2852529157324de9c226b3364e76136"
5352
encoding := "base64"
5453
content := "VGhpcyBpcyB1cGRhdGVkIHRleHQ="
55-
selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master"
56-
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
5756
gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha
58-
downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + treePath
57+
downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + info.treePath
5958
return &api.FileResponse{
6059
Content: &api.ContentsResponse{
61-
Name: filepath.Base(treePath),
62-
Path: treePath,
63-
SHA: sha,
64-
LastCommitSHA: lastCommitSHA,
65-
LastCommitWhen: lastCommitWhen,
66-
Type: "file",
67-
Size: 20,
68-
Encoding: &encoding,
69-
Content: &content,
70-
URL: &selfURL,
71-
HTMLURL: &htmlURL,
72-
GitURL: &gitURL,
73-
DownloadURL: &downloadURL,
60+
Name: path.Base(info.treePath),
61+
Path: info.treePath,
62+
SHA: sha,
63+
LastCommitSHA: info.lastCommitSHA,
64+
LastCommitterWhen: info.lastCommitterWhen,
65+
Type: "file",
66+
Size: 20,
67+
Encoding: &encoding,
68+
Content: &content,
69+
URL: &selfURL,
70+
HTMLURL: &htmlURL,
71+
GitURL: &gitURL,
72+
DownloadURL: &downloadURL,
7473
Links: &api.FileLinksResponse{
7574
Self: &selfURL,
7675
GitURL: &gitURL,
@@ -79,10 +78,10 @@ func getExpectedFileResponseForUpdate(commitID, treePath, lastCommitSHA string,
7978
},
8079
Commit: &api.FileCommitResponse{
8180
CommitMeta: api.CommitMeta{
82-
URL: setting.AppURL + "api/v1/repos/user2/repo1/git/commits/" + commitID,
83-
SHA: commitID,
81+
URL: setting.AppURL + "api/v1/repos/user2/repo1/git/commits/" + info.commitID,
82+
SHA: info.commitID,
8483
},
85-
HTMLURL: setting.AppURL + "user2/repo1/commit/" + commitID,
84+
HTMLURL: setting.AppURL + "user2/repo1/commit/" + info.commitID,
8685
Author: &api.CommitUser{
8786
Identity: api.Identity{
8887
Name: "John Doe",
@@ -137,22 +136,23 @@ func TestAPIUpdateFile(t *testing.T) {
137136
AddTokenAuth(token2)
138137
resp := MakeRequest(t, req, http.StatusOK)
139138
gitRepo, _ := gitrepo.OpenRepository(t.Context(), repo1)
139+
defer gitRepo.Close()
140140
commitID, _ := gitRepo.GetBranchCommitID(updateFileOptions.NewBranchName)
141141
lasCommit, _ := gitRepo.GetCommitByPath(treePath)
142-
expectedFileResponse := getExpectedFileResponseForUpdate(commitID, treePath, lasCommit.ID.String(), lasCommit.Committer.When)
142+
expectedFileResponse := getExpectedFileResponseForUpdate(apiFileResponseInfo{
143+
commitID: commitID,
144+
treePath: treePath,
145+
lastCommitSHA: lasCommit.ID.String(),
146+
lastCommitterWhen: lasCommit.Committer.When,
147+
lastAuthorWhen: lasCommit.Author.When,
148+
})
143149
var fileResponse api.FileResponse
144150
DecodeJSON(t, resp, &fileResponse)
145-
146-
// FIXME: This is a workaround to compare time.Time values. This maybe a bug of Golang,
147-
// assume your local timezone is UTC, but a location with zero offset is not equal to UTC but they should be.
148-
expectedFileResponse.Content.LastCommitWhen, _ = time.Parse(time.RFC3339, expectedFileResponse.Content.LastCommitWhen.Format(time.RFC3339))
149-
150151
assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
151152
assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
152153
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
153154
assert.Equal(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
154155
assert.Equal(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
155-
gitRepo.Close()
156156
}
157157

158158
// Test updating a file in a new branch

tests/integration/api_repo_files_change_test.go

+21-23
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"net/http"
1010
"net/url"
1111
"testing"
12-
"time"
1312

1413
auth_model "code.gitea.io/gitea/models/auth"
1514
repo_model "code.gitea.io/gitea/models/repo"
@@ -87,8 +86,8 @@ func TestAPIChangeFiles(t *testing.T) {
8786
createTreePath := fmt.Sprintf("new/file%d.txt", fileID)
8887
updateTreePath := fmt.Sprintf("update/file%d.txt", fileID)
8988
deleteTreePath := fmt.Sprintf("delete/file%d.txt", fileID)
90-
createFile(user2, repo1, updateTreePath)
91-
createFile(user2, repo1, deleteTreePath)
89+
_, _ = createFile(user2, repo1, updateTreePath)
90+
_, _ = createFile(user2, repo1, deleteTreePath)
9291
changeFilesOptions := getChangeFilesOptions()
9392
changeFilesOptions.BranchName = branch
9493
changeFilesOptions.Files[0].Path = createTreePath
@@ -98,37 +97,36 @@ func TestAPIChangeFiles(t *testing.T) {
9897
AddTokenAuth(token2)
9998
resp := MakeRequest(t, req, http.StatusCreated)
10099
gitRepo, _ := gitrepo.OpenRepository(t.Context(), repo1)
100+
defer gitRepo.Close()
101101
commitID, _ := gitRepo.GetBranchCommitID(changeFilesOptions.NewBranchName)
102102
createLasCommit, _ := gitRepo.GetCommitByPath(createTreePath)
103103
updateLastCommit, _ := gitRepo.GetCommitByPath(updateTreePath)
104-
expectedCreateFileResponse := getExpectedFileResponseForCreate(fmt.Sprintf("%v/%v", user2.Name, repo1.Name), commitID, createTreePath, createLasCommit.ID.String(), createLasCommit.Committer.When)
105-
expectedUpdateFileResponse := getExpectedFileResponseForUpdate(commitID, updateTreePath, updateLastCommit.ID.String(), updateLastCommit.Committer.When)
104+
expectedCreateFileResponse := getExpectedFileResponseForCreate(apiFileResponseInfo{
105+
repoFullName: fmt.Sprintf("%s/%s", user2.Name, repo1.Name),
106+
commitID: commitID,
107+
treePath: createTreePath,
108+
lastCommitSHA: createLasCommit.ID.String(),
109+
lastCommitterWhen: createLasCommit.Committer.When,
110+
lastAuthorWhen: createLasCommit.Author.When,
111+
})
112+
expectedUpdateFileResponse := getExpectedFileResponseForUpdate(apiFileResponseInfo{
113+
commitID: commitID,
114+
treePath: updateTreePath,
115+
lastCommitSHA: updateLastCommit.ID.String(),
116+
lastCommitterWhen: updateLastCommit.Committer.When,
117+
lastAuthorWhen: updateLastCommit.Author.When,
118+
})
106119
var filesResponse api.FilesResponse
107120
DecodeJSON(t, resp, &filesResponse)
108-
109-
// FIXME: This is a workaround to compare time.Time values. This maybe a bug of Golang,
110-
// assume your local timezone is UTC, but a location with zero offset is not equal to UTC but they should be.
111-
expectedCreateFileResponse.Content.LastCommitWhen, _ = time.Parse(time.RFC3339, expectedCreateFileResponse.Content.LastCommitWhen.Format(time.RFC3339))
112-
expectedUpdateFileResponse.Content.LastCommitWhen, _ = time.Parse(time.RFC3339, expectedUpdateFileResponse.Content.LastCommitWhen.Format(time.RFC3339))
113-
114-
// check create file
115-
assert.Equal(t, expectedCreateFileResponse.Content, filesResponse.Files[0])
116-
117-
// check update file
118-
assert.Equal(t, expectedUpdateFileResponse.Content, filesResponse.Files[1])
119-
120-
// test commit info
121+
assert.Equal(t, expectedCreateFileResponse.Content, filesResponse.Files[0]) // check create file
122+
assert.Equal(t, expectedUpdateFileResponse.Content, filesResponse.Files[1]) // check update file
121123
assert.Equal(t, expectedCreateFileResponse.Commit.SHA, filesResponse.Commit.SHA)
122124
assert.Equal(t, expectedCreateFileResponse.Commit.HTMLURL, filesResponse.Commit.HTMLURL)
123125
assert.Equal(t, expectedCreateFileResponse.Commit.Author.Email, filesResponse.Commit.Author.Email)
124126
assert.Equal(t, expectedCreateFileResponse.Commit.Author.Name, filesResponse.Commit.Author.Name)
125127
assert.Equal(t, expectedCreateFileResponse.Commit.Committer.Email, filesResponse.Commit.Committer.Email)
126128
assert.Equal(t, expectedCreateFileResponse.Commit.Committer.Name, filesResponse.Commit.Committer.Name)
127-
128-
// test delete file
129-
assert.Nil(t, filesResponse.Files[2])
130-
131-
gitRepo.Close()
129+
assert.Nil(t, filesResponse.Files[2]) // test delete file
132130
})
133131
}
134132

0 commit comments

Comments
 (0)