mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-08-01 17:38:33 +00:00
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:
parent
813eabc340
commit
23cc1fdbbe
10 changed files with 98 additions and 70 deletions
|
@ -177,6 +177,8 @@ func TestAPICreateFile(t *testing.T) {
|
|||
expectedFileResponse := getExpectedFileResponseForCreate("user2/repo1", commitID, treePath, latestCommit.ID.String())
|
||||
var fileResponse api.FileResponse
|
||||
DecodeJSON(t, resp, &fileResponse)
|
||||
// Testify cannot assert time.Time correctly.
|
||||
expectedFileResponse.Content.LastCommitWhen = fileResponse.Content.LastCommitWhen
|
||||
assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
|
||||
assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
|
||||
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
|
||||
|
@ -296,6 +298,8 @@ func TestAPICreateFile(t *testing.T) {
|
|||
latestCommit, _ := gitRepo.GetCommitByPath(treePath)
|
||||
expectedFileResponse := getExpectedFileResponseForCreate("user2/"+reponame, commitID, treePath, latestCommit.ID.String())
|
||||
DecodeJSON(t, resp, &fileResponse)
|
||||
// Testify cannot assert time.Time correctly.
|
||||
expectedFileResponse.Content.LastCommitWhen = fileResponse.Content.LastCommitWhen
|
||||
assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
|
||||
assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
|
||||
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
|
||||
|
|
|
@ -140,6 +140,8 @@ func TestAPIUpdateFile(t *testing.T) {
|
|||
expectedFileResponse := getExpectedFileResponseForUpdate(commitID, treePath, lasCommit.ID.String())
|
||||
var fileResponse api.FileResponse
|
||||
DecodeJSON(t, resp, &fileResponse)
|
||||
// Testify cannot assert time.Time correctly.
|
||||
expectedFileResponse.Content.LastCommitWhen = fileResponse.Content.LastCommitWhen
|
||||
assert.Equal(t, expectedFileResponse.Content, fileResponse.Content)
|
||||
assert.Equal(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
|
||||
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
|
||||
|
|
|
@ -104,6 +104,10 @@ func TestAPIChangeFiles(t *testing.T) {
|
|||
var filesResponse api.FilesResponse
|
||||
DecodeJSON(t, resp, &filesResponse)
|
||||
|
||||
// Testify cannot assert time.Time correctly.
|
||||
expectedCreateFileResponse.Content.LastCommitWhen = filesResponse.Files[0].LastCommitWhen
|
||||
expectedUpdateFileResponse.Content.LastCommitWhen = filesResponse.Files[1].LastCommitWhen
|
||||
|
||||
// check create file
|
||||
assert.Equal(t, expectedCreateFileResponse.Content, filesResponse.Files[0])
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"net/url"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
auth_model "forgejo.org/models/auth"
|
||||
repo_model "forgejo.org/models/repo"
|
||||
|
@ -32,16 +33,17 @@ func getExpectedContentsListResponseForContents(ref, refType, lastCommitSHA stri
|
|||
downloadURL := setting.AppURL + "user2/repo1/raw/" + refType + "/" + ref + "/" + treePath
|
||||
return []*api.ContentsResponse{
|
||||
{
|
||||
Name: filepath.Base(treePath),
|
||||
Path: treePath,
|
||||
SHA: sha,
|
||||
LastCommitSHA: lastCommitSHA,
|
||||
Type: "file",
|
||||
Size: 30,
|
||||
URL: &selfURL,
|
||||
HTMLURL: &htmlURL,
|
||||
GitURL: &gitURL,
|
||||
DownloadURL: &downloadURL,
|
||||
Name: filepath.Base(treePath),
|
||||
Path: treePath,
|
||||
SHA: sha,
|
||||
LastCommitSHA: lastCommitSHA,
|
||||
LastCommitWhen: time.Date(2017, time.March, 19, 16, 47, 59, 0, time.FixedZone("", -14400)),
|
||||
Type: "file",
|
||||
Size: 30,
|
||||
URL: &selfURL,
|
||||
HTMLURL: &htmlURL,
|
||||
GitURL: &gitURL,
|
||||
DownloadURL: &downloadURL,
|
||||
Links: &api.FileLinksResponse{
|
||||
Self: &selfURL,
|
||||
GitURL: &gitURL,
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
auth_model "forgejo.org/models/auth"
|
||||
repo_model "forgejo.org/models/repo"
|
||||
|
@ -33,18 +34,19 @@ func getExpectedContentsResponseForContents(ref, refType, lastCommitSHA string)
|
|||
gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha
|
||||
downloadURL := setting.AppURL + "user2/repo1/raw/" + refType + "/" + ref + "/" + treePath
|
||||
return &api.ContentsResponse{
|
||||
Name: treePath,
|
||||
Path: treePath,
|
||||
SHA: sha,
|
||||
LastCommitSHA: lastCommitSHA,
|
||||
Type: "file",
|
||||
Size: 30,
|
||||
Encoding: &encoding,
|
||||
Content: &content,
|
||||
URL: &selfURL,
|
||||
HTMLURL: &htmlURL,
|
||||
GitURL: &gitURL,
|
||||
DownloadURL: &downloadURL,
|
||||
Name: treePath,
|
||||
Path: treePath,
|
||||
SHA: sha,
|
||||
LastCommitSHA: lastCommitSHA,
|
||||
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,
|
||||
|
|
|
@ -108,7 +108,7 @@ func getExpectedFileResponseForRepofilesDelete() *api.FileResponse {
|
|||
}
|
||||
}
|
||||
|
||||
func getExpectedFileResponseForRepofilesCreate(commitID, lastCommitSHA string) *api.FileResponse {
|
||||
func getExpectedFileResponseForRepofilesCreate(commitID, lastCommitSHA string, lastCommitWhen time.Time) *api.FileResponse {
|
||||
treePath := "new/file.txt"
|
||||
encoding := "base64"
|
||||
content := "VGhpcyBpcyBhIE5FVyBmaWxl"
|
||||
|
@ -118,18 +118,19 @@ func getExpectedFileResponseForRepofilesCreate(commitID, lastCommitSHA string) *
|
|||
downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + treePath
|
||||
return &api.FileResponse{
|
||||
Content: &api.ContentsResponse{
|
||||
Name: filepath.Base(treePath),
|
||||
Path: treePath,
|
||||
SHA: "103ff9234cefeee5ec5361d22b49fbb04d385885",
|
||||
LastCommitSHA: lastCommitSHA,
|
||||
Type: "file",
|
||||
Size: 18,
|
||||
Encoding: &encoding,
|
||||
Content: &content,
|
||||
URL: &selfURL,
|
||||
HTMLURL: &htmlURL,
|
||||
GitURL: &gitURL,
|
||||
DownloadURL: &downloadURL,
|
||||
Name: filepath.Base(treePath),
|
||||
Path: treePath,
|
||||
SHA: "103ff9234cefeee5ec5361d22b49fbb04d385885",
|
||||
LastCommitSHA: lastCommitSHA,
|
||||
LastCommitWhen: lastCommitWhen,
|
||||
Type: "file",
|
||||
Size: 18,
|
||||
Encoding: &encoding,
|
||||
Content: &content,
|
||||
URL: &selfURL,
|
||||
HTMLURL: &htmlURL,
|
||||
GitURL: &gitURL,
|
||||
DownloadURL: &downloadURL,
|
||||
Links: &api.FileLinksResponse{
|
||||
Self: &selfURL,
|
||||
GitURL: &gitURL,
|
||||
|
@ -177,7 +178,7 @@ func getExpectedFileResponseForRepofilesCreate(commitID, lastCommitSHA string) *
|
|||
}
|
||||
}
|
||||
|
||||
func getExpectedFileResponseForRepofilesUpdate(commitID, filename, lastCommitSHA string) *api.FileResponse {
|
||||
func getExpectedFileResponseForRepofilesUpdate(commitID, filename, lastCommitSHA string, lastCommitWhen time.Time) *api.FileResponse {
|
||||
encoding := "base64"
|
||||
content := "VGhpcyBpcyBVUERBVEVEIGNvbnRlbnQgZm9yIHRoZSBSRUFETUUgZmlsZQ=="
|
||||
selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + filename + "?ref=master"
|
||||
|
@ -186,18 +187,19 @@ func getExpectedFileResponseForRepofilesUpdate(commitID, filename, lastCommitSHA
|
|||
downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + filename
|
||||
return &api.FileResponse{
|
||||
Content: &api.ContentsResponse{
|
||||
Name: filename,
|
||||
Path: filename,
|
||||
SHA: "dbf8d00e022e05b7e5cf7e535de857de57925647",
|
||||
LastCommitSHA: lastCommitSHA,
|
||||
Type: "file",
|
||||
Size: 43,
|
||||
Encoding: &encoding,
|
||||
Content: &content,
|
||||
URL: &selfURL,
|
||||
HTMLURL: &htmlURL,
|
||||
GitURL: &gitURL,
|
||||
DownloadURL: &downloadURL,
|
||||
Name: filename,
|
||||
Path: filename,
|
||||
SHA: "dbf8d00e022e05b7e5cf7e535de857de57925647",
|
||||
LastCommitSHA: lastCommitSHA,
|
||||
LastCommitWhen: lastCommitWhen,
|
||||
Type: "file",
|
||||
Size: 43,
|
||||
Encoding: &encoding,
|
||||
Content: &content,
|
||||
URL: &selfURL,
|
||||
HTMLURL: &htmlURL,
|
||||
GitURL: &gitURL,
|
||||
DownloadURL: &downloadURL,
|
||||
Links: &api.FileLinksResponse{
|
||||
Self: &selfURL,
|
||||
GitURL: &gitURL,
|
||||
|
@ -264,7 +266,7 @@ func TestChangeRepoFiles(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
lastCommit, err := gitRepo.GetCommitByPath("new/file.txt")
|
||||
require.NoError(t, err)
|
||||
expectedFileResponse := getExpectedFileResponseForRepofilesCreate(commitID, lastCommit.ID.String())
|
||||
expectedFileResponse := getExpectedFileResponseForRepofilesCreate(commitID, lastCommit.ID.String(), lastCommit.Committer.When)
|
||||
assert.Equal(t, expectedFileResponse.Content, filesResponse.Files[0])
|
||||
assert.Equal(t, expectedFileResponse.Commit.SHA, filesResponse.Commit.SHA)
|
||||
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, filesResponse.Commit.HTMLURL)
|
||||
|
@ -282,7 +284,7 @@ func TestChangeRepoFiles(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
lastCommit, err := commit.GetCommitByPath(opts.Files[0].TreePath)
|
||||
require.NoError(t, err)
|
||||
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String())
|
||||
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String(), lastCommit.Committer.When)
|
||||
assert.Equal(t, expectedFileResponse.Content, filesResponse.Files[0])
|
||||
assert.Equal(t, expectedFileResponse.Commit.SHA, filesResponse.Commit.SHA)
|
||||
assert.Equal(t, expectedFileResponse.Commit.HTMLURL, filesResponse.Commit.HTMLURL)
|
||||
|
@ -303,7 +305,7 @@ func TestChangeRepoFiles(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
lastCommit, err := commit.GetCommitByPath(opts.Files[0].TreePath)
|
||||
require.NoError(t, err)
|
||||
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String())
|
||||
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String(), lastCommit.Committer.When)
|
||||
|
||||
// assert that the old file no longer exists in the last commit of the branch
|
||||
fromEntry, err := commit.GetTreeEntryByPath(opts.Files[0].FromTreePath)
|
||||
|
@ -339,7 +341,7 @@ func TestChangeRepoFiles(t *testing.T) {
|
|||
|
||||
commit, _ := gitRepo.GetBranchCommit(repo.DefaultBranch)
|
||||
lastCommit, _ := commit.GetCommitByPath(opts.Files[0].TreePath)
|
||||
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String())
|
||||
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String(), lastCommit.Committer.When)
|
||||
assert.Equal(t, expectedFileResponse.Content, filesResponse.Files[0])
|
||||
})
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue