Skip to content

Commit ed14f93

Browse files
Gustedzeripathwxiaoguang
authored and
Sysoev, Vladimir
committed
Add latest commit's SHA to content response (go-gitea#20398)
* Add latest commit's SHA to content response - When requesting the contents of a filepath, add the latest commit's SHA to the requested file. - Resolves go-gitea#12840 * Add swagger * Fix NPE * Fix tests * Hook into LastCommitCache * Move AddLastCommitCache to a common nogogit and gogit file Signed-off-by: Andrew Thornton <[email protected]> * Prevent NPE Co-authored-by: Andrew Thornton <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent 8a14807 commit ed14f93

File tree

12 files changed

+187
-131
lines changed

12 files changed

+187
-131
lines changed

integrations/api_repo_file_create_test.go

+17-14
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func getCreateFileOptions() api.CreateFileOptions {
5050
}
5151
}
5252

53-
func getExpectedFileResponseForCreate(repoFullName, commitID, treePath string) *api.FileResponse {
53+
func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCommitSHA string) *api.FileResponse {
5454
sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
5555
encoding := "base64"
5656
content := "VGhpcyBpcyBuZXcgdGV4dA=="
@@ -60,17 +60,18 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath string) *
6060
downloadURL := setting.AppURL + repoFullName + "/raw/branch/master/" + treePath
6161
return &api.FileResponse{
6262
Content: &api.ContentsResponse{
63-
Name: filepath.Base(treePath),
64-
Path: treePath,
65-
SHA: sha,
66-
Size: 16,
67-
Type: "file",
68-
Encoding: &encoding,
69-
Content: &content,
70-
URL: &selfURL,
71-
HTMLURL: &htmlURL,
72-
GitURL: &gitURL,
73-
DownloadURL: &downloadURL,
63+
Name: filepath.Base(treePath),
64+
Path: treePath,
65+
SHA: sha,
66+
LastCommitSHA: latestCommitSHA,
67+
Size: 16,
68+
Type: "file",
69+
Encoding: &encoding,
70+
Content: &content,
71+
URL: &selfURL,
72+
HTMLURL: &htmlURL,
73+
GitURL: &gitURL,
74+
DownloadURL: &downloadURL,
7475
Links: &api.FileLinksResponse{
7576
Self: &selfURL,
7677
GitURL: &gitURL,
@@ -170,7 +171,8 @@ func TestAPICreateFile(t *testing.T) {
170171
resp := session.MakeRequest(t, req, http.StatusCreated)
171172
gitRepo, _ := git.OpenRepository(stdCtx.Background(), repo1.RepoPath())
172173
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
173-
expectedFileResponse := getExpectedFileResponseForCreate("user2/repo1", commitID, treePath)
174+
latestCommit, _ := gitRepo.GetCommitByPath(treePath)
175+
expectedFileResponse := getExpectedFileResponseForCreate("user2/repo1", commitID, treePath, latestCommit.ID.String())
174176
var fileResponse api.FileResponse
175177
DecodeJSON(t, resp, &fileResponse)
176178
assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
@@ -289,7 +291,8 @@ func TestAPICreateFile(t *testing.T) {
289291
emptyRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "empty-repo"}).(*repo_model.Repository) // public repo
290292
gitRepo, _ := git.OpenRepository(stdCtx.Background(), emptyRepo.RepoPath())
291293
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
292-
expectedFileResponse := getExpectedFileResponseForCreate("user2/empty-repo", commitID, treePath)
294+
latestCommit, _ := gitRepo.GetCommitByPath(treePath)
295+
expectedFileResponse := getExpectedFileResponseForCreate("user2/empty-repo", commitID, treePath, latestCommit.ID.String())
293296
DecodeJSON(t, resp, &fileResponse)
294297
assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
295298
assert.EqualValues(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)

integrations/api_repo_file_update_test.go

+15-13
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func getUpdateFileOptions() *api.UpdateFileOptions {
4848
}
4949
}
5050

51-
func getExpectedFileResponseForUpdate(commitID, treePath string) *api.FileResponse {
51+
func getExpectedFileResponseForUpdate(commitID, treePath, lastCommitSHA string) *api.FileResponse {
5252
sha := "08bd14b2e2852529157324de9c226b3364e76136"
5353
encoding := "base64"
5454
content := "VGhpcyBpcyB1cGRhdGVkIHRleHQ="
@@ -58,17 +58,18 @@ func getExpectedFileResponseForUpdate(commitID, treePath string) *api.FileRespon
5858
downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + treePath
5959
return &api.FileResponse{
6060
Content: &api.ContentsResponse{
61-
Name: filepath.Base(treePath),
62-
Path: treePath,
63-
SHA: sha,
64-
Type: "file",
65-
Size: 20,
66-
Encoding: &encoding,
67-
Content: &content,
68-
URL: &selfURL,
69-
HTMLURL: &htmlURL,
70-
GitURL: &gitURL,
71-
DownloadURL: &downloadURL,
61+
Name: filepath.Base(treePath),
62+
Path: treePath,
63+
SHA: sha,
64+
LastCommitSHA: lastCommitSHA,
65+
Type: "file",
66+
Size: 20,
67+
Encoding: &encoding,
68+
Content: &content,
69+
URL: &selfURL,
70+
HTMLURL: &htmlURL,
71+
GitURL: &gitURL,
72+
DownloadURL: &downloadURL,
7273
Links: &api.FileLinksResponse{
7374
Self: &selfURL,
7475
GitURL: &gitURL,
@@ -137,7 +138,8 @@ func TestAPIUpdateFile(t *testing.T) {
137138
resp := session.MakeRequest(t, req, http.StatusOK)
138139
gitRepo, _ := git.OpenRepository(stdCtx.Background(), repo1.RepoPath())
139140
commitID, _ := gitRepo.GetBranchCommitID(updateFileOptions.NewBranchName)
140-
expectedFileResponse := getExpectedFileResponseForUpdate(commitID, treePath)
141+
lasCommit, _ := gitRepo.GetCommitByPath(treePath)
142+
expectedFileResponse := getExpectedFileResponseForUpdate(commitID, treePath, lasCommit.ID.String())
141143
var fileResponse api.FileResponse
142144
DecodeJSON(t, resp, &fileResponse)
143145
assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)

integrations/api_repo_get_contents_list_test.go

+28-16
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/stretchr/testify/assert"
2222
)
2323

24-
func getExpectedContentsListResponseForContents(ref, refType string) []*api.ContentsResponse {
24+
func getExpectedContentsListResponseForContents(ref, refType, lastCommitSHA string) []*api.ContentsResponse {
2525
treePath := "README.md"
2626
sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
2727
selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=" + ref
@@ -30,15 +30,16 @@ func getExpectedContentsListResponseForContents(ref, refType string) []*api.Cont
3030
downloadURL := setting.AppURL + "user2/repo1/raw/" + refType + "/" + ref + "/" + treePath
3131
return []*api.ContentsResponse{
3232
{
33-
Name: filepath.Base(treePath),
34-
Path: treePath,
35-
SHA: sha,
36-
Type: "file",
37-
Size: 30,
38-
URL: &selfURL,
39-
HTMLURL: &htmlURL,
40-
GitURL: &gitURL,
41-
DownloadURL: &downloadURL,
33+
Name: filepath.Base(treePath),
34+
Path: treePath,
35+
SHA: sha,
36+
LastCommitSHA: lastCommitSHA,
37+
Type: "file",
38+
Size: 30,
39+
URL: &selfURL,
40+
HTMLURL: &htmlURL,
41+
GitURL: &gitURL,
42+
DownloadURL: &downloadURL,
4243
Links: &api.FileLinksResponse{
4344
Self: &selfURL,
4445
GitURL: &gitURL,
@@ -94,7 +95,9 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
9495
var contentsListResponse []*api.ContentsResponse
9596
DecodeJSON(t, resp, &contentsListResponse)
9697
assert.NotNil(t, contentsListResponse)
97-
expectedContentsListResponse := getExpectedContentsListResponseForContents(ref, refType)
98+
lastCommit, err := gitRepo.GetCommitByPath("README.md")
99+
assert.NoError(t, err)
100+
expectedContentsListResponse := getExpectedContentsListResponseForContents(ref, refType, lastCommit.ID.String())
98101
assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
99102

100103
// No ref
@@ -103,17 +106,22 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
103106
resp = session.MakeRequest(t, req, http.StatusOK)
104107
DecodeJSON(t, resp, &contentsListResponse)
105108
assert.NotNil(t, contentsListResponse)
106-
expectedContentsListResponse = getExpectedContentsListResponseForContents(repo1.DefaultBranch, refType)
109+
110+
expectedContentsListResponse = getExpectedContentsListResponseForContents(repo1.DefaultBranch, refType, lastCommit.ID.String())
107111
assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
108112

109-
// ref is the branch we created above in setup
113+
// ref is the branch we created above in setup
110114
ref = newBranch
111115
refType = "branch"
112116
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
113117
resp = session.MakeRequest(t, req, http.StatusOK)
114118
DecodeJSON(t, resp, &contentsListResponse)
115119
assert.NotNil(t, contentsListResponse)
116-
expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType)
120+
branchCommit, err := gitRepo.GetBranchCommit(ref)
121+
assert.NoError(t, err)
122+
lastCommit, err = branchCommit.GetCommitByPath("README.md")
123+
assert.NoError(t, err)
124+
expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType, lastCommit.ID.String())
117125
assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
118126

119127
// ref is the new tag we created above in setup
@@ -123,7 +131,11 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
123131
resp = session.MakeRequest(t, req, http.StatusOK)
124132
DecodeJSON(t, resp, &contentsListResponse)
125133
assert.NotNil(t, contentsListResponse)
126-
expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType)
134+
tagCommit, err := gitRepo.GetTagCommit(ref)
135+
assert.NoError(t, err)
136+
lastCommit, err = tagCommit.GetCommitByPath("README.md")
137+
assert.NoError(t, err)
138+
expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType, lastCommit.ID.String())
127139
assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
128140

129141
// ref is a commit
@@ -133,7 +145,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
133145
resp = session.MakeRequest(t, req, http.StatusOK)
134146
DecodeJSON(t, resp, &contentsListResponse)
135147
assert.NotNil(t, contentsListResponse)
136-
expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType)
148+
expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType, commitID)
137149
assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
138150

139151
// Test file contents a file with a bad ref

integrations/api_repo_get_contents_test.go

+23-17
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/stretchr/testify/assert"
2121
)
2222

23-
func getExpectedContentsResponseForContents(ref, refType string) *api.ContentsResponse {
23+
func getExpectedContentsResponseForContents(ref, refType, lastCommitSHA string) *api.ContentsResponse {
2424
treePath := "README.md"
2525
sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
2626
encoding := "base64"
@@ -30,17 +30,18 @@ func getExpectedContentsResponseForContents(ref, refType string) *api.ContentsRe
3030
gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha
3131
downloadURL := setting.AppURL + "user2/repo1/raw/" + refType + "/" + ref + "/" + treePath
3232
return &api.ContentsResponse{
33-
Name: treePath,
34-
Path: treePath,
35-
SHA: sha,
36-
Type: "file",
37-
Size: 30,
38-
Encoding: &encoding,
39-
Content: &content,
40-
URL: &selfURL,
41-
HTMLURL: &htmlURL,
42-
GitURL: &gitURL,
43-
DownloadURL: &downloadURL,
33+
Name: treePath,
34+
Path: treePath,
35+
SHA: sha,
36+
LastCommitSHA: lastCommitSHA,
37+
Type: "file",
38+
Size: 30,
39+
Encoding: &encoding,
40+
Content: &content,
41+
URL: &selfURL,
42+
HTMLURL: &htmlURL,
43+
GitURL: &gitURL,
44+
DownloadURL: &downloadURL,
4445
Links: &api.FileLinksResponse{
4546
Self: &selfURL,
4647
GitURL: &gitURL,
@@ -96,7 +97,8 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
9697
var contentsResponse api.ContentsResponse
9798
DecodeJSON(t, resp, &contentsResponse)
9899
assert.NotNil(t, contentsResponse)
99-
expectedContentsResponse := getExpectedContentsResponseForContents(ref, refType)
100+
lastCommit, _ := gitRepo.GetCommitByPath("README.md")
101+
expectedContentsResponse := getExpectedContentsResponseForContents(ref, refType, lastCommit.ID.String())
100102
assert.EqualValues(t, *expectedContentsResponse, contentsResponse)
101103

102104
// No ref
@@ -105,7 +107,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
105107
resp = session.MakeRequest(t, req, http.StatusOK)
106108
DecodeJSON(t, resp, &contentsResponse)
107109
assert.NotNil(t, contentsResponse)
108-
expectedContentsResponse = getExpectedContentsResponseForContents(repo1.DefaultBranch, refType)
110+
expectedContentsResponse = getExpectedContentsResponseForContents(repo1.DefaultBranch, refType, lastCommit.ID.String())
109111
assert.EqualValues(t, *expectedContentsResponse, contentsResponse)
110112

111113
// ref is the branch we created above in setup
@@ -115,7 +117,9 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
115117
resp = session.MakeRequest(t, req, http.StatusOK)
116118
DecodeJSON(t, resp, &contentsResponse)
117119
assert.NotNil(t, contentsResponse)
118-
expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType)
120+
branchCommit, _ := gitRepo.GetBranchCommit(ref)
121+
lastCommit, _ = branchCommit.GetCommitByPath("README.md")
122+
expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType, lastCommit.ID.String())
119123
assert.EqualValues(t, *expectedContentsResponse, contentsResponse)
120124

121125
// ref is the new tag we created above in setup
@@ -125,7 +129,9 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
125129
resp = session.MakeRequest(t, req, http.StatusOK)
126130
DecodeJSON(t, resp, &contentsResponse)
127131
assert.NotNil(t, contentsResponse)
128-
expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType)
132+
tagCommit, _ := gitRepo.GetTagCommit(ref)
133+
lastCommit, _ = tagCommit.GetCommitByPath("README.md")
134+
expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType, lastCommit.ID.String())
129135
assert.EqualValues(t, *expectedContentsResponse, contentsResponse)
130136

131137
// ref is a commit
@@ -135,7 +141,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
135141
resp = session.MakeRequest(t, req, http.StatusOK)
136142
DecodeJSON(t, resp, &contentsResponse)
137143
assert.NotNil(t, contentsResponse)
138-
expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType)
144+
expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType, commitID)
139145
assert.EqualValues(t, *expectedContentsResponse, contentsResponse)
140146

141147
// Test file contents a file with a bad ref

0 commit comments

Comments
 (0)