Skip to content

Commit 23cc1fd

Browse files
GustedEarl Warren
authored andcommitted
feat(api): add last_commit_when to contents response (#7418)
- Add a new field `last_commit_when` to the `ContentResponse` type, which is populated with the last commit's commiter date. This can be used to determine when the last edit of the content was. - This field is compatible with what Gitea will likely add, go-gitea/gitea#32921. There's no field for this information in the Github API, so no way to be compatible with that (this API endpoint is otherwise fully compatible with Github's API). - Ref: gitnex/GitNex#1225 - Integration test adjusted. The API tests cannot test the actual output, as `testify` tries to 'deep equal' the `time.Time` structs which will differ due how the `time.Time` struct is created. Unit tests still verify the output. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7418 Reviewed-by: Earl Warren <[email protected]> Co-authored-by: Gusted <[email protected]> Co-committed-by: Gusted <[email protected]>
1 parent 813eabc commit 23cc1fd

File tree

10 files changed

+98
-70
lines changed

10 files changed

+98
-70
lines changed

modules/structs/repo_file.go

Lines changed: 4 additions & 0 deletions
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,8 @@ 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+
LastCommitWhen time.Time `json:"last_commit_when"`
124128
// `type` will be `file`, `dir`, `symlink`, or `submodule`
125129
Type string `json:"type"`
126130
Size int64 `json:"size"`

services/repository/files/content.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,13 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref
178178

179179
// All content types have these fields in populated
180180
contentsResponse := &api.ContentsResponse{
181-
Name: entry.Name(),
182-
Path: treePath,
183-
SHA: entry.ID.String(),
184-
LastCommitSHA: lastCommit.ID.String(),
185-
Size: entry.Size(),
186-
URL: &selfURLString,
181+
Name: entry.Name(),
182+
Path: treePath,
183+
SHA: entry.ID.String(),
184+
LastCommitSHA: lastCommit.ID.String(),
185+
LastCommitWhen: lastCommit.Committer.When,
186+
Size: entry.Size(),
187+
URL: &selfURLString,
187188
Links: &api.FileLinksResponse{
188189
Self: &selfURLString,
189190
},

services/repository/files/content_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package files
55

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

910
"forgejo.org/models/db"
1011
repo_model "forgejo.org/models/repo"
@@ -33,18 +34,19 @@ func getExpectedReadmeContentsResponse() *api.ContentsResponse {
3334
gitURL := "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/" + sha
3435
downloadURL := "https://try.gitea.io/user2/repo1/raw/branch/master/" + treePath
3536
return &api.ContentsResponse{
36-
Name: treePath,
37-
Path: treePath,
38-
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
39-
LastCommitSHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
40-
Type: "file",
41-
Size: 30,
42-
Encoding: &encoding,
43-
Content: &content,
44-
URL: &selfURL,
45-
HTMLURL: &htmlURL,
46-
GitURL: &gitURL,
47-
DownloadURL: &downloadURL,
37+
Name: treePath,
38+
Path: treePath,
39+
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
40+
LastCommitSHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
41+
LastCommitWhen: time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400)),
42+
Type: "file",
43+
Size: 30,
44+
Encoding: &encoding,
45+
Content: &content,
46+
URL: &selfURL,
47+
HTMLURL: &htmlURL,
48+
GitURL: &gitURL,
49+
DownloadURL: &downloadURL,
4850
Links: &api.FileLinksResponse{
4951
Self: &selfURL,
5052
GitURL: &gitURL,

templates/swagger/v1_json.tmpl

Lines changed: 5 additions & 0 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ func TestAPICreateFile(t *testing.T) {
177177
expectedFileResponse := getExpectedFileResponseForCreate("user2/repo1", commitID, treePath, latestCommit.ID.String())
178178
var fileResponse api.FileResponse
179179
DecodeJSON(t, resp, &fileResponse)
180+
// Testify cannot assert time.Time correctly.
181+
expectedFileResponse.Content.LastCommitWhen = fileResponse.Content.LastCommitWhen
180182
assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
181183
assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
182184
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
@@ -296,6 +298,8 @@ func TestAPICreateFile(t *testing.T) {
296298
latestCommit, _ := gitRepo.GetCommitByPath(treePath)
297299
expectedFileResponse := getExpectedFileResponseForCreate("user2/"+reponame, commitID, treePath, latestCommit.ID.String())
298300
DecodeJSON(t, resp, &fileResponse)
301+
// Testify cannot assert time.Time correctly.
302+
expectedFileResponse.Content.LastCommitWhen = fileResponse.Content.LastCommitWhen
299303
assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
300304
assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
301305
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)

tests/integration/api_repo_file_update_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ func TestAPIUpdateFile(t *testing.T) {
140140
expectedFileResponse := getExpectedFileResponseForUpdate(commitID, treePath, lasCommit.ID.String())
141141
var fileResponse api.FileResponse
142142
DecodeJSON(t, resp, &fileResponse)
143+
// Testify cannot assert time.Time correctly.
144+
expectedFileResponse.Content.LastCommitWhen = fileResponse.Content.LastCommitWhen
143145
assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
144146
assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
145147
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)

tests/integration/api_repo_files_change_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ func TestAPIChangeFiles(t *testing.T) {
104104
var filesResponse api.FilesResponse
105105
DecodeJSON(t, resp, &filesResponse)
106106

107+
// Testify cannot assert time.Time correctly.
108+
expectedCreateFileResponse.Content.LastCommitWhen = filesResponse.Files[0].LastCommitWhen
109+
expectedUpdateFileResponse.Content.LastCommitWhen = filesResponse.Files[1].LastCommitWhen
110+
107111
// check create file
108112
assert.Equal(t, expectedCreateFileResponse.Content, filesResponse.Files[0])
109113

tests/integration/api_repo_get_contents_list_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/url"
99
"path/filepath"
1010
"testing"
11+
"time"
1112

1213
auth_model "forgejo.org/models/auth"
1314
repo_model "forgejo.org/models/repo"
@@ -32,16 +33,17 @@ func getExpectedContentsListResponseForContents(ref, refType, lastCommitSHA stri
3233
downloadURL := setting.AppURL + "user2/repo1/raw/" + refType + "/" + ref + "/" + treePath
3334
return []*api.ContentsResponse{
3435
{
35-
Name: filepath.Base(treePath),
36-
Path: treePath,
37-
SHA: sha,
38-
LastCommitSHA: lastCommitSHA,
39-
Type: "file",
40-
Size: 30,
41-
URL: &selfURL,
42-
HTMLURL: &htmlURL,
43-
GitURL: &gitURL,
44-
DownloadURL: &downloadURL,
36+
Name: filepath.Base(treePath),
37+
Path: treePath,
38+
SHA: sha,
39+
LastCommitSHA: lastCommitSHA,
40+
LastCommitWhen: time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400)),
41+
Type: "file",
42+
Size: 30,
43+
URL: &selfURL,
44+
HTMLURL: &htmlURL,
45+
GitURL: &gitURL,
46+
DownloadURL: &downloadURL,
4547
Links: &api.FileLinksResponse{
4648
Self: &selfURL,
4749
GitURL: &gitURL,

tests/integration/api_repo_get_contents_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"net/url"
1010
"testing"
11+
"time"
1112

1213
auth_model "forgejo.org/models/auth"
1314
repo_model "forgejo.org/models/repo"
@@ -33,18 +34,19 @@ func getExpectedContentsResponseForContents(ref, refType, lastCommitSHA string)
3334
gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha
3435
downloadURL := setting.AppURL + "user2/repo1/raw/" + refType + "/" + ref + "/" + treePath
3536
return &api.ContentsResponse{
36-
Name: treePath,
37-
Path: treePath,
38-
SHA: sha,
39-
LastCommitSHA: lastCommitSHA,
40-
Type: "file",
41-
Size: 30,
42-
Encoding: &encoding,
43-
Content: &content,
44-
URL: &selfURL,
45-
HTMLURL: &htmlURL,
46-
GitURL: &gitURL,
47-
DownloadURL: &downloadURL,
37+
Name: treePath,
38+
Path: treePath,
39+
SHA: sha,
40+
LastCommitSHA: lastCommitSHA,
41+
LastCommitWhen: time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400)),
42+
Type: "file",
43+
Size: 30,
44+
Encoding: &encoding,
45+
Content: &content,
46+
URL: &selfURL,
47+
HTMLURL: &htmlURL,
48+
GitURL: &gitURL,
49+
DownloadURL: &downloadURL,
4850
Links: &api.FileLinksResponse{
4951
Self: &selfURL,
5052
GitURL: &gitURL,

tests/integration/repofiles_change_test.go

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func getExpectedFileResponseForRepofilesDelete() *api.FileResponse {
108108
}
109109
}
110110

111-
func getExpectedFileResponseForRepofilesCreate(commitID, lastCommitSHA string) *api.FileResponse {
111+
func getExpectedFileResponseForRepofilesCreate(commitID, lastCommitSHA string, lastCommitWhen time.Time) *api.FileResponse {
112112
treePath := "new/file.txt"
113113
encoding := "base64"
114114
content := "VGhpcyBpcyBhIE5FVyBmaWxl"
@@ -118,18 +118,19 @@ func getExpectedFileResponseForRepofilesCreate(commitID, lastCommitSHA string) *
118118
downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + treePath
119119
return &api.FileResponse{
120120
Content: &api.ContentsResponse{
121-
Name: filepath.Base(treePath),
122-
Path: treePath,
123-
SHA: "103ff9234cefeee5ec5361d22b49fbb04d385885",
124-
LastCommitSHA: lastCommitSHA,
125-
Type: "file",
126-
Size: 18,
127-
Encoding: &encoding,
128-
Content: &content,
129-
URL: &selfURL,
130-
HTMLURL: &htmlURL,
131-
GitURL: &gitURL,
132-
DownloadURL: &downloadURL,
121+
Name: filepath.Base(treePath),
122+
Path: treePath,
123+
SHA: "103ff9234cefeee5ec5361d22b49fbb04d385885",
124+
LastCommitSHA: lastCommitSHA,
125+
LastCommitWhen: lastCommitWhen,
126+
Type: "file",
127+
Size: 18,
128+
Encoding: &encoding,
129+
Content: &content,
130+
URL: &selfURL,
131+
HTMLURL: &htmlURL,
132+
GitURL: &gitURL,
133+
DownloadURL: &downloadURL,
133134
Links: &api.FileLinksResponse{
134135
Self: &selfURL,
135136
GitURL: &gitURL,
@@ -177,7 +178,7 @@ func getExpectedFileResponseForRepofilesCreate(commitID, lastCommitSHA string) *
177178
}
178179
}
179180

180-
func getExpectedFileResponseForRepofilesUpdate(commitID, filename, lastCommitSHA string) *api.FileResponse {
181+
func getExpectedFileResponseForRepofilesUpdate(commitID, filename, lastCommitSHA string, lastCommitWhen time.Time) *api.FileResponse {
181182
encoding := "base64"
182183
content := "VGhpcyBpcyBVUERBVEVEIGNvbnRlbnQgZm9yIHRoZSBSRUFETUUgZmlsZQ=="
183184
selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + filename + "?ref=master"
@@ -186,18 +187,19 @@ func getExpectedFileResponseForRepofilesUpdate(commitID, filename, lastCommitSHA
186187
downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + filename
187188
return &api.FileResponse{
188189
Content: &api.ContentsResponse{
189-
Name: filename,
190-
Path: filename,
191-
SHA: "dbf8d00e022e05b7e5cf7e535de857de57925647",
192-
LastCommitSHA: lastCommitSHA,
193-
Type: "file",
194-
Size: 43,
195-
Encoding: &encoding,
196-
Content: &content,
197-
URL: &selfURL,
198-
HTMLURL: &htmlURL,
199-
GitURL: &gitURL,
200-
DownloadURL: &downloadURL,
190+
Name: filename,
191+
Path: filename,
192+
SHA: "dbf8d00e022e05b7e5cf7e535de857de57925647",
193+
LastCommitSHA: lastCommitSHA,
194+
LastCommitWhen: lastCommitWhen,
195+
Type: "file",
196+
Size: 43,
197+
Encoding: &encoding,
198+
Content: &content,
199+
URL: &selfURL,
200+
HTMLURL: &htmlURL,
201+
GitURL: &gitURL,
202+
DownloadURL: &downloadURL,
201203
Links: &api.FileLinksResponse{
202204
Self: &selfURL,
203205
GitURL: &gitURL,
@@ -264,7 +266,7 @@ func TestChangeRepoFiles(t *testing.T) {
264266
require.NoError(t, err)
265267
lastCommit, err := gitRepo.GetCommitByPath("new/file.txt")
266268
require.NoError(t, err)
267-
expectedFileResponse := getExpectedFileResponseForRepofilesCreate(commitID, lastCommit.ID.String())
269+
expectedFileResponse := getExpectedFileResponseForRepofilesCreate(commitID, lastCommit.ID.String(), lastCommit.Committer.When)
268270
assert.Equal(t, expectedFileResponse.Content, filesResponse.Files[0])
269271
assert.Equal(t, expectedFileResponse.Commit.SHA, filesResponse.Commit.SHA)
270272
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, filesResponse.Commit.HTMLURL)
@@ -282,7 +284,7 @@ func TestChangeRepoFiles(t *testing.T) {
282284
require.NoError(t, err)
283285
lastCommit, err := commit.GetCommitByPath(opts.Files[0].TreePath)
284286
require.NoError(t, err)
285-
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String())
287+
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String(), lastCommit.Committer.When)
286288
assert.Equal(t, expectedFileResponse.Content, filesResponse.Files[0])
287289
assert.Equal(t, expectedFileResponse.Commit.SHA, filesResponse.Commit.SHA)
288290
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, filesResponse.Commit.HTMLURL)
@@ -303,7 +305,7 @@ func TestChangeRepoFiles(t *testing.T) {
303305
require.NoError(t, err)
304306
lastCommit, err := commit.GetCommitByPath(opts.Files[0].TreePath)
305307
require.NoError(t, err)
306-
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String())
308+
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String(), lastCommit.Committer.When)
307309

308310
// assert that the old file no longer exists in the last commit of the branch
309311
fromEntry, err := commit.GetTreeEntryByPath(opts.Files[0].FromTreePath)
@@ -339,7 +341,7 @@ func TestChangeRepoFiles(t *testing.T) {
339341

340342
commit, _ := gitRepo.GetBranchCommit(repo.DefaultBranch)
341343
lastCommit, _ := commit.GetCommitByPath(opts.Files[0].TreePath)
342-
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String())
344+
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String(), lastCommit.Committer.When)
343345
assert.Equal(t, expectedFileResponse.Content, filesResponse.Files[0])
344346
})
345347

0 commit comments

Comments
 (0)