-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[WIP] feature: list files in tree #2749
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
83fa7a5
WIP
c01391f
Initial stab at repo listing
3ac0edb
Add repo listing path
21cefc8
Baby integration tests
ea55e32
More consistent routing
8532318
First cut at organized functions and types
33c8939
Add stub tests, support recursion
262d452
More groping around trying to find where to put tests
351a341
Move data structures into `models`
4ebbfd8
Remove redundant TODO comment
necaris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2017 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package integrations | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
api "code.gitea.io/sdk/gitea" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func testAPIGetTree(t *testing.T, treePath string, exists bool, entries []*api.TreeEntry) { | ||
prepareTestEnv(t) | ||
|
||
session := loginUser(t, "user2") | ||
req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo1/git/trees/%s", treePath) | ||
resp := session.MakeRequest(t, req, NoExpectedStatus) | ||
if !exists { | ||
assert.EqualValues(t, http.StatusNotFound, resp.HeaderCode) | ||
return | ||
} | ||
assert.EqualValues(t, http.StatusOK, resp.HeaderCode) | ||
fmt.Print(bytes.NewBuffer(resp.Body)) | ||
var trees []*api.TreeEntry | ||
DecodeJSON(t, resp, &trees) | ||
|
||
if assert.EqualValues(t, len(entries), len(trees)) { | ||
for i, tree := range trees { | ||
assert.EqualValues(t, entries[i], tree) | ||
} | ||
} | ||
} | ||
|
||
func TestAPIGetTree(t *testing.T) { | ||
for _, test := range []struct { | ||
TreePath string | ||
Exists bool | ||
Listing *api.RepoTreeListing | ||
// Entries []*api.TreeEntry | ||
}{ | ||
{"master", true, []*api.TreeEntry{ | ||
&api.TreeEntry{ | ||
Name: "README.md", | ||
ID: "4b4851ad51df6a7d9f25c979345979eaeb5b349f", | ||
Type: "blob", | ||
// Size: 30, | ||
}, | ||
}}, | ||
{"master/doesnotexist", false, []*api.TreeEntry{}}, | ||
{"feature/1", true, []*api.TreeEntry{ | ||
&api.TreeEntry{ | ||
Name: "README.md", | ||
ID: "4b4851ad51df6a7d9f25c979345979eaeb5b349f", | ||
Type: "blob", | ||
// Size: 30, | ||
}, | ||
}}, | ||
{"feature/1/doesnotexist", false, []*api.TreeEntry{}}, | ||
} { | ||
testAPIGetTree(t, test.TreePath, test.Exists, test.Entries) | ||
} | ||
} | ||
|
||
// func TestAPIListRepoEntries(t *testing.T) { | ||
// prepareTestEnv(t) | ||
|
||
// // TODO: Make this actually work!!! | ||
// req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/tree/thing/thing") | ||
// resp := MakeRequest(t, req, http.StatusOK) | ||
|
||
// var repo api.Repository | ||
// DecodeJSON(t, resp, &repo) | ||
// assert.EqualValues(t, 1, repo.ID) | ||
// assert.EqualValues(t, "repo1", repo.Name) | ||
// } | ||
|
||
// func TestVersion(t *testing.T) { | ||
// prepareTestEnv(t) | ||
|
||
// setting.AppVer = "test-version-1" | ||
// req := NewRequest(t, "GET", "/api/v1/version") | ||
// resp := MakeRequest(t, req, http.StatusOK) | ||
|
||
// var version gitea.ServerVersion | ||
// DecodeJSON(t, resp, &version) | ||
// assert.Equal(t, setting.AppVer, string(version.Version)) | ||
// } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// TODO: | ||
// With model objects here, we can write tests for them and for TreeListing? Or maybe | ||
// they should go elsewhere? | ||
|
||
// Then we can write fuller integration tests, with model objects that we're expecting and asserting | ||
// against | ||
import ( | ||
"path/filepath" | ||
|
||
"code.gitea.io/git" | ||
) | ||
|
||
// RepoFile represents a file blob contained in the repository | ||
type RepoFile struct { | ||
Path string `json:"path"` | ||
// Mode git.EntryMode `json:"mode"` // TODO: Do we include this? It'll require exporting the mode as public in the `git` module... | ||
Type git.ObjectType `json:"type"` | ||
// Size int64 `json:"size"` // TODO: Do we include this? It's expensive... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not though, since |
||
SHA string `json:"sha"` | ||
URL string `json:"url"` | ||
} | ||
|
||
// RepoTreeListing represents a tree (or subtree) listing in the repository | ||
type RepoTreeListing struct { | ||
SHA string `json:"sha"` | ||
Path string `json:"path"` | ||
Tree []*RepoFile `json:"tree"` | ||
} | ||
|
||
// NewRepoFile creates a new RepoFile from a Git tree entry and some metadata. | ||
func NewRepoFile(e *git.TreeEntry, parentPath string, rawLink string) *RepoFile { | ||
var filePath string | ||
if parentPath != "" { | ||
filePath = filepath.Join(parentPath, e.Name()) | ||
} else { | ||
filePath = e.Name() | ||
} | ||
return &RepoFile{ | ||
Path: filePath, | ||
// Mode: e.mode, // TODO: Not exported by `git.TreeEntry` | ||
Type: e.Type, | ||
// Size: e.Size(), // TODO: Expensive! | ||
SHA: e.ID.String(), | ||
URL: filepath.Join(rawLink, filePath), | ||
} | ||
} | ||
|
||
// NewRepoTreeListing creates a new RepoTreeListing from a Git tree and some metadata | ||
func NewRepoTreeListing(t *git.Tree, treePath, rawLink string, recursive bool) (*RepoTreeListing, error) { | ||
tree, err := t.SubTree(treePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var entries []*RepoFile | ||
treeEntries, err := tree.ListEntries() | ||
if err != nil { | ||
return nil, err | ||
} | ||
treeEntries.CustomSort(base.NaturalSortLess) | ||
for i := range treeEntries { | ||
entry := treeEntries[i] | ||
if entry.IsDir() && recursive { | ||
subListing, err := treeListing(t, filepath.Join(treePath, entry.Name()), rawLink, recursive) | ||
if err != nil { | ||
return nil, err | ||
} | ||
entries = append(entries, subListing.Tree...) | ||
} else { | ||
entries = append(entries, models.NewRepoFile(treeEntries[i], treePath, rawLink)) | ||
} | ||
} | ||
|
||
return &RepoTreeListing{ | ||
SHA: tree.ID.String(), | ||
Path: treePath, | ||
Tree: entries, | ||
}, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright, whatnot | ||
package repo | ||
|
||
func TestTreeListing() { | ||
|
||
} | ||
|
||
func TestRepoFile() { | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does GitHub API represent them? usually we create a
APIFormat
function for things like this