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, https://github.com/go-gitea/gitea/pull/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 <earl-warren@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
Gusted 2025-04-18 09:31:40 +00:00 committed by Earl Warren
parent 813eabc340
commit 23cc1fdbbe
10 changed files with 98 additions and 70 deletions

View file

@ -178,12 +178,13 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref
// All content types have these fields in populated
contentsResponse := &api.ContentsResponse{
Name: entry.Name(),
Path: treePath,
SHA: entry.ID.String(),
LastCommitSHA: lastCommit.ID.String(),
Size: entry.Size(),
URL: &selfURLString,
Name: entry.Name(),
Path: treePath,
SHA: entry.ID.String(),
LastCommitSHA: lastCommit.ID.String(),
LastCommitWhen: lastCommit.Committer.When,
Size: entry.Size(),
URL: &selfURLString,
Links: &api.FileLinksResponse{
Self: &selfURLString,
},

View file

@ -5,6 +5,7 @@ package files
import (
"testing"
"time"
"forgejo.org/models/db"
repo_model "forgejo.org/models/repo"
@ -33,18 +34,19 @@ func getExpectedReadmeContentsResponse() *api.ContentsResponse {
gitURL := "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/" + sha
downloadURL := "https://try.gitea.io/user2/repo1/raw/branch/master/" + treePath
return &api.ContentsResponse{
Name: treePath,
Path: treePath,
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
LastCommitSHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
Type: "file",
Size: 30,
Encoding: &encoding,
Content: &content,
URL: &selfURL,
HTMLURL: &htmlURL,
GitURL: &gitURL,
DownloadURL: &downloadURL,
Name: treePath,
Path: treePath,
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
LastCommitSHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
LastCommitWhen: time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400)),
Type: "file",
Size: 30,
Encoding: &encoding,
Content: &content,
URL: &selfURL,
HTMLURL: &htmlURL,
GitURL: &gitURL,
DownloadURL: &downloadURL,
Links: &api.FileLinksResponse{
Self: &selfURL,
GitURL: &gitURL,