Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add latest commit's SHA to content response #20398

Merged
merged 12 commits into from
Jul 30, 2022
31 changes: 17 additions & 14 deletions integrations/api_repo_file_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func getCreateFileOptions() api.CreateFileOptions {
}
}

func getExpectedFileResponseForCreate(repoFullName, commitID, treePath string) *api.FileResponse {
func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCommitSHA string) *api.FileResponse {
sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
encoding := "base64"
content := "VGhpcyBpcyBuZXcgdGV4dA=="
Expand All @@ -60,17 +60,18 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath string) *
downloadURL := setting.AppURL + repoFullName + "/raw/branch/master/" + treePath
return &api.FileResponse{
Content: &api.ContentsResponse{
Name: filepath.Base(treePath),
Path: treePath,
SHA: sha,
Size: 16,
Type: "file",
Encoding: &encoding,
Content: &content,
URL: &selfURL,
HTMLURL: &htmlURL,
GitURL: &gitURL,
DownloadURL: &downloadURL,
Name: filepath.Base(treePath),
Path: treePath,
SHA: sha,
LastCommitSHA: latestCommitSHA,
Size: 16,
Type: "file",
Encoding: &encoding,
Content: &content,
URL: &selfURL,
HTMLURL: &htmlURL,
GitURL: &gitURL,
DownloadURL: &downloadURL,
Links: &api.FileLinksResponse{
Self: &selfURL,
GitURL: &gitURL,
Expand Down Expand Up @@ -170,7 +171,8 @@ func TestAPICreateFile(t *testing.T) {
resp := session.MakeRequest(t, req, http.StatusCreated)
gitRepo, _ := git.OpenRepository(stdCtx.Background(), repo1.RepoPath())
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
expectedFileResponse := getExpectedFileResponseForCreate("user2/repo1", commitID, treePath)
latestCommit, _ := gitRepo.GetCommitByPath(treePath)
expectedFileResponse := getExpectedFileResponseForCreate("user2/repo1", commitID, treePath, latestCommit.ID.String())
var fileResponse api.FileResponse
DecodeJSON(t, resp, &fileResponse)
assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
Expand Down Expand Up @@ -289,7 +291,8 @@ func TestAPICreateFile(t *testing.T) {
emptyRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "empty-repo"}).(*repo_model.Repository) // public repo
gitRepo, _ := git.OpenRepository(stdCtx.Background(), emptyRepo.RepoPath())
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
expectedFileResponse := getExpectedFileResponseForCreate("user2/empty-repo", commitID, treePath)
latestCommit, _ := gitRepo.GetCommitByPath(treePath)
expectedFileResponse := getExpectedFileResponseForCreate("user2/empty-repo", commitID, treePath, latestCommit.ID.String())
DecodeJSON(t, resp, &fileResponse)
assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
assert.EqualValues(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
Expand Down
28 changes: 15 additions & 13 deletions integrations/api_repo_file_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func getUpdateFileOptions() *api.UpdateFileOptions {
}
}

func getExpectedFileResponseForUpdate(commitID, treePath string) *api.FileResponse {
func getExpectedFileResponseForUpdate(commitID, treePath, lastCommitSHA string) *api.FileResponse {
sha := "08bd14b2e2852529157324de9c226b3364e76136"
encoding := "base64"
content := "VGhpcyBpcyB1cGRhdGVkIHRleHQ="
Expand All @@ -58,17 +58,18 @@ func getExpectedFileResponseForUpdate(commitID, treePath string) *api.FileRespon
downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + treePath
return &api.FileResponse{
Content: &api.ContentsResponse{
Name: filepath.Base(treePath),
Path: treePath,
SHA: sha,
Type: "file",
Size: 20,
Encoding: &encoding,
Content: &content,
URL: &selfURL,
HTMLURL: &htmlURL,
GitURL: &gitURL,
DownloadURL: &downloadURL,
Name: filepath.Base(treePath),
Path: treePath,
SHA: sha,
LastCommitSHA: lastCommitSHA,
Type: "file",
Size: 20,
Encoding: &encoding,
Content: &content,
URL: &selfURL,
HTMLURL: &htmlURL,
GitURL: &gitURL,
DownloadURL: &downloadURL,
Links: &api.FileLinksResponse{
Self: &selfURL,
GitURL: &gitURL,
Expand Down Expand Up @@ -137,7 +138,8 @@ func TestAPIUpdateFile(t *testing.T) {
resp := session.MakeRequest(t, req, http.StatusOK)
gitRepo, _ := git.OpenRepository(stdCtx.Background(), repo1.RepoPath())
commitID, _ := gitRepo.GetBranchCommitID(updateFileOptions.NewBranchName)
expectedFileResponse := getExpectedFileResponseForUpdate(commitID, treePath)
lasCommit, _ := gitRepo.GetCommitByPath(treePath)
expectedFileResponse := getExpectedFileResponseForUpdate(commitID, treePath, lasCommit.ID.String())
var fileResponse api.FileResponse
DecodeJSON(t, resp, &fileResponse)
assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
Expand Down
44 changes: 28 additions & 16 deletions integrations/api_repo_get_contents_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/stretchr/testify/assert"
)

func getExpectedContentsListResponseForContents(ref, refType string) []*api.ContentsResponse {
func getExpectedContentsListResponseForContents(ref, refType, lastCommitSHA string) []*api.ContentsResponse {
treePath := "README.md"
sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=" + ref
Expand All @@ -30,15 +30,16 @@ func getExpectedContentsListResponseForContents(ref, refType string) []*api.Cont
downloadURL := setting.AppURL + "user2/repo1/raw/" + refType + "/" + ref + "/" + treePath
return []*api.ContentsResponse{
{
Name: filepath.Base(treePath),
Path: treePath,
SHA: sha,
Type: "file",
Size: 30,
URL: &selfURL,
HTMLURL: &htmlURL,
GitURL: &gitURL,
DownloadURL: &downloadURL,
Name: filepath.Base(treePath),
Path: treePath,
SHA: sha,
LastCommitSHA: lastCommitSHA,
Type: "file",
Size: 30,
URL: &selfURL,
HTMLURL: &htmlURL,
GitURL: &gitURL,
DownloadURL: &downloadURL,
Links: &api.FileLinksResponse{
Self: &selfURL,
GitURL: &gitURL,
Expand Down Expand Up @@ -94,7 +95,9 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
var contentsListResponse []*api.ContentsResponse
DecodeJSON(t, resp, &contentsListResponse)
assert.NotNil(t, contentsListResponse)
expectedContentsListResponse := getExpectedContentsListResponseForContents(ref, refType)
lastCommit, err := gitRepo.GetCommitByPath("README.md")
assert.NoError(t, err)
expectedContentsListResponse := getExpectedContentsListResponseForContents(ref, refType, lastCommit.ID.String())
assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)

// No ref
Expand All @@ -103,17 +106,22 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
resp = session.MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &contentsListResponse)
assert.NotNil(t, contentsListResponse)
expectedContentsListResponse = getExpectedContentsListResponseForContents(repo1.DefaultBranch, refType)

expectedContentsListResponse = getExpectedContentsListResponseForContents(repo1.DefaultBranch, refType, lastCommit.ID.String())
assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)

// ref is the branch we created above in setup
// ref is the branch we created above in setup
ref = newBranch
refType = "branch"
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/contents/%s?ref=%s", user2.Name, repo1.Name, treePath, ref)
resp = session.MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &contentsListResponse)
assert.NotNil(t, contentsListResponse)
expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType)
branchCommit, err := gitRepo.GetBranchCommit(ref)
assert.NoError(t, err)
lastCommit, err = branchCommit.GetCommitByPath("README.md")
assert.NoError(t, err)
expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType, lastCommit.ID.String())
assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)

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

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

// Test file contents a file with a bad ref
Expand Down
40 changes: 23 additions & 17 deletions integrations/api_repo_get_contents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/stretchr/testify/assert"
)

func getExpectedContentsResponseForContents(ref, refType string) *api.ContentsResponse {
func getExpectedContentsResponseForContents(ref, refType, lastCommitSHA string) *api.ContentsResponse {
treePath := "README.md"
sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
encoding := "base64"
Expand All @@ -30,17 +30,18 @@ func getExpectedContentsResponseForContents(ref, refType string) *api.ContentsRe
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,
Type: "file",
Size: 30,
Encoding: &encoding,
Content: &content,
URL: &selfURL,
HTMLURL: &htmlURL,
GitURL: &gitURL,
DownloadURL: &downloadURL,
Name: treePath,
Path: treePath,
SHA: sha,
LastCommitSHA: lastCommitSHA,
Type: "file",
Size: 30,
Encoding: &encoding,
Content: &content,
URL: &selfURL,
HTMLURL: &htmlURL,
GitURL: &gitURL,
DownloadURL: &downloadURL,
Links: &api.FileLinksResponse{
Self: &selfURL,
GitURL: &gitURL,
Expand Down Expand Up @@ -96,7 +97,8 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
var contentsResponse api.ContentsResponse
DecodeJSON(t, resp, &contentsResponse)
assert.NotNil(t, contentsResponse)
expectedContentsResponse := getExpectedContentsResponseForContents(ref, refType)
lastCommit, _ := gitRepo.GetCommitByPath("README.md")
expectedContentsResponse := getExpectedContentsResponseForContents(ref, refType, lastCommit.ID.String())
assert.EqualValues(t, *expectedContentsResponse, contentsResponse)

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

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

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

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

// Test file contents a file with a bad ref
Expand Down
Loading