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

Feature - Pagination for git tree API #5838

Merged
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
508f0c2
Feature - Pagination for git tree API
richmahn Jan 25, 2019
2134a5b
Handles case when page is negative
richmahn Jan 25, 2019
12b8269
Does a for loop over the start and end rather than all entries
richmahn Jan 25, 2019
7a305a9
Removed redundent logic
richmahn Jan 25, 2019
6983876
Adds per_page as a query parameter
richmahn Jan 25, 2019
6948d77
Adds DEFAULT_GIT_TREES_PER_PAGE for settings, ran make fmt
richmahn Jan 25, 2019
9d1a420
Fix typo in cheat-sheet en
richmahn Jan 25, 2019
78fb089
Makes page start at 1, generated swagger
richmahn Jan 25, 2019
667d705
Merge branch 'master' into feature/pagination-for-git-tree-api
techknowlogick Jan 28, 2019
10947d1
Merge branch 'master' into feature/pagination-for-git-tree-api
techknowlogick Jan 30, 2019
c681922
Use updates to SDK
richmahn Jan 31, 2019
33244f5
Merge branch 'feature/pagination-for-git-tree-api' of github.com:rich…
richmahn Jan 31, 2019
28d6fe2
Merge remote-tracking branch 'upstream/master' into feature/paginatio…
richmahn Jan 31, 2019
c203b1a
Updates to use latest sdk
richmahn Feb 5, 2019
50dd3cb
Merge remote-tracking branch 'upstream/master' into feature/paginatio…
richmahn Feb 5, 2019
63cde32
Updates swagger for tree api
richmahn Feb 5, 2019
83b1ddb
Adds test for GetTreeBySHA
richmahn Feb 5, 2019
8e40525
Updates per PR reviews
richmahn Feb 5, 2019
c374bca
Updates per PR reviews
richmahn Feb 5, 2019
65eb658
Remove file
richmahn Feb 5, 2019
55dec09
Formatting
richmahn Feb 5, 2019
cc94c88
Merge remote-tracking branch 'upstream/master' into feature/paginatio…
richmahn Feb 5, 2019
a22c12c
Fix to swagger file
richmahn Feb 5, 2019
c57703b
Fix to swagger
richmahn Feb 5, 2019
db5ebdf
Update v1_json.tmpl
richmahn Feb 5, 2019
a473edf
Fix to swagger file
richmahn Feb 6, 2019
64b3858
Merge branch 'master' into feature/pagination-for-git-tree-api
richmahn Feb 6, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions custom/conf/app.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,8 @@ ENABLE_SWAGGER = true
MAX_RESPONSE_ITEMS = 50
; Default paging number of api
DEFAULT_PAGING_NUM = 30
; Default number of items per page for git trees api
DEFAULT_GIT_TREES_PER_PAGE = 1000

[i18n]
LANGS = en-US,zh-CN,zh-HK,zh-TW,de-DE,fr-FR,nl-NL,lv-LV,ru-RU,uk-UA,ja-JP,es-ES,pt-BR,pl-PL,bg-BG,it-IT,fi-FI,tr-TR,cs-CZ,sr-SP,sv-SE,ko-KR
Expand Down
1 change: 1 addition & 0 deletions docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
- `ENABLE_SWAGGER`: **true**: Enables /api/swagger, /api/v1/swagger etc. endpoints. True or false; default is true.
- `MAX_RESPONSE_ITEMS`: **50**: Max number of items in a page.
- `DEFAULT_PAGING_NUM`: **30**: Default paging number of api.
- `DEFAULT_GIT_TREES_PER_PAGE`: **1000**: Default number of items per page for git trees api.
richmahn marked this conversation as resolved.
Show resolved Hide resolved

## i18n (`i18n`)

Expand Down
1 change: 1 addition & 0 deletions docs/content/doc/advanced/config-cheat-sheet.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ menu:
- `ENABLE_SWAGGER`: **true**: 是否启用swagger路由 /api/swagger, /api/v1/swagger etc. endpoints. True 或 false; 默认是 true.
- `MAX_RESPONSE_ITEMS`: **50**: 一个页面最大的项目数。
- `DEFAULT_PAGING_NUM`: **30**: API中默认分页条数。
- `DEFAULT_GIT_TREES_PER_PAGE`: **1000**: GIT TREES API每页的默认项目数.

## Markup (`markup`)

Expand Down
14 changes: 8 additions & 6 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,15 @@ var (

// API settings
API = struct {
EnableSwagger bool
MaxResponseItems int
DefaultPagingNum int
EnableSwagger bool
MaxResponseItems int
DefaultPagingNum int
DefaultGitTreesPerPage int
}{
EnableSwagger: true,
MaxResponseItems: 50,
DefaultPagingNum: 30,
EnableSwagger: true,
MaxResponseItems: 50,
DefaultPagingNum: 30,
DefaultGitTreesPerPage: 1000,
}

U2F = struct {
Expand Down
72 changes: 54 additions & 18 deletions routers/api/v1/repo/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,42 @@ func GetTree(ctx *context.APIContext) {
// description: sha of the commit
// type: string
// required: true
// - name: recursive
// in: query
// description: show all directories and files
// required: false
// type: boolean
// - name: page
// in: query
// description: page number; the 'truncated' field in the response will be true if there are still more items after this page, false if the last page
// required: false
// type: integer
// - name: per_page
// in: query
// description: number of items per page; default is 1000 or what is set in app.ini as DEFAULT_GIT_TREES_PER_PAGE
// required: false
// type: integer
// responses:
// "200":
// "$ref": "#/responses/GitTreeResponse"
sha := ctx.Params("sha")
if len(sha) == 0 {
ctx.Error(400, "sha not provided", nil)
ctx.Error(400, "", "sha not provided")
return
}
tree := GetTreeBySHA(ctx, sha)
if tree != nil {
ctx.JSON(200, tree)
} else {
ctx.Error(400, "sha invalid", nil)
ctx.Error(400, "", "sha invalid")
}
}

// GetTreeBySHA get the GitTreeResponse of a repository using a sha hash.
func GetTreeBySHA(ctx *context.APIContext, sha string) *gitea.GitTreeResponse {
fmt.Printf("HEREEEEER1\n")
gitTree, err := ctx.Repo.GitRepo.GetTree(sha)
fmt.Printf("Error: %v\n\n", err)
if err != nil || gitTree == nil {
return nil
}
Expand All @@ -69,6 +86,8 @@ func GetTreeBySHA(ctx *context.APIContext, sha string) *gitea.GitTreeResponse {
} else {
entries, err = gitTree.ListEntries()
}
fmt.Printf("Error: %v", err)
fmt.Printf("HEREEEEE: %v\n", tree)
if err != nil {
return tree
}
Expand All @@ -87,30 +106,47 @@ func GetTreeBySHA(ctx *context.APIContext, sha string) *gitea.GitTreeResponse {
// 40 is the size of the sha1 hash in hexadecimal format.
copyPos := len(treeURL) - 40

if len(entries) > 1000 {
tree.Entries = make([]gitea.GitEntry, 1000)
page := ctx.QueryInt("page")
perPage := ctx.QueryInt("per_page")
if perPage <= 0 || perPage > setting.API.DefaultGitTreesPerPage {
perPage = setting.API.DefaultGitTreesPerPage
}
if page <= 0 {
page = 1
}
tree.Page = page
tree.TotalCount = len(entries)
rangeStart := perPage * (page - 1)
if rangeStart >= len(entries) {
return tree
}
var rangeEnd int
if len(entries) > perPage {
tree.Truncated = true
}
if rangeStart+perPage < len(entries) {
rangeEnd = rangeStart + perPage
} else {
tree.Entries = make([]gitea.GitEntry, len(entries))
rangeEnd = len(entries)
}
for e := range entries {
if e > 1000 {
tree.Truncated = true
break
}

tree.Entries[e].Path = entries[e].Name()
tree.Entries[e].Mode = fmt.Sprintf("%06x", entries[e].Mode())
tree.Entries[e].Type = string(entries[e].Type)
tree.Entries[e].Size = entries[e].Size()
tree.Entries[e].SHA = entries[e].ID.String()
tree.Entries = make([]gitea.GitEntry, rangeEnd-rangeStart)
for e := rangeStart; e < rangeEnd; e++ {
i := e - rangeStart
tree.Entries[i].Path = entries[e].Name()
tree.Entries[i].Mode = fmt.Sprintf("%06x", entries[e].Mode())
tree.Entries[i].Type = string(entries[e].Type)
tree.Entries[i].Size = entries[e].Size()
tree.Entries[i].SHA = entries[e].ID.String()

if entries[e].IsDir() {
copy(treeURL[copyPos:], entries[e].ID.String())
tree.Entries[e].URL = string(treeURL[:])
tree.Entries[i].URL = string(treeURL[:])
} else {
copy(blobURL[copyPos:], entries[e].ID.String())
tree.Entries[e].URL = string(blobURL[:])
tree.Entries[i].URL = string(blobURL[:])
}
}
fmt.Printf("HEREEEEE:\n")
richmahn marked this conversation as resolved.
Show resolved Hide resolved
fmt.Printf("%v\n", tree)
return tree
}
48 changes: 48 additions & 0 deletions routers/api/v1/repo/tree_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2019 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 repo

import (
"github.com/stretchr/testify/assert"
"testing"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/sdk/gitea"
)

func TestGetTreeBySHA(t *testing.T) {
models.PrepareTestEnv(t)
sha := "master"
ctx := test.MockContext(t, "user2/repo1")
ctx.SetParams(":id", "1")
ctx.SetParams(":sha", sha)
test.LoadRepo(t, ctx, 1)
test.LoadRepoCommit(t, ctx)
test.LoadUser(t, ctx, 2)
test.LoadGitRepo(t, ctx)

tree := GetTreeBySHA(&context.APIContext{Context: ctx, Org: nil}, ctx.Params("sha"))
expectedTree := &gitea.GitTreeResponse{
SHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
URL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/trees/65f1bf27bc3bf70f64657658635e66094edbcb4d",
Entries: []gitea.GitEntry {
{
Path: "README.md",
Mode: "100644",
Type: "blob",
Size: 30,
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
URL: "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/4b4851ad51df6a7d9f25c979345979eaeb5b349f",
},
},
Truncated: false,
Page: 1,
TotalCount:1,
}

assert.EqualValues(t, tree, expectedTree)
}
8 changes: 8 additions & 0 deletions routers/repo/authorized_keys
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# gitea public key
command="/var/folders/n0/4qx0k8414mndry6fjtm06v140000gp/T/tmp.mnDo3LUU/go-build937563667/b548/repo.test serv key-1 --config=''",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAu7tvIvX6ZHrRXuZNfkR3XLHSsuCK9Zn3X58lxBcQzuo5xZgB6vRwwm/QtJuF+zZPtY5hsQILBLmF+BZ5WpKZp1jBeSjH2G7lxet9kbcH+kIVj0tPFEoyKI9wvWqIwC4prx/WVk2wLTJjzBAhyNxfEq7C9CeiX9pQEbEqJfkKCQ== nocomment
# gitea public key
command="/var/folders/n0/4qx0k8414mndry6fjtm06v140000gp/T/tmp.mnDo3LUU/go-build937563667/b548/repo.test serv key-2 --config=''",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAu7tvIvX6ZHrRXuZNfkR3XLHSsuCK9Zn3X58lxBcQzuo5xZgB6vRwwm/QtJuF+zZPtY5hsQILBLmF+BZ5WpKZp1jBeSjH2G7lxet9kbcH+kIVj0tPFEoyKI9wvWqIwC4prx/WVk2wLTJjzBAhyNxfEq7C9CeiX9pQEbEqJfkKCQ== nocomment
# gitea public key
command="/var/folders/n0/4qx0k8414mndry6fjtm06v140000gp/T/tmp.JMhL8PJw/go-build307601641/b548/repo.test serv key-1 --config=''",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAu7tvIvX6ZHrRXuZNfkR3XLHSsuCK9Zn3X58lxBcQzuo5xZgB6vRwwm/QtJuF+zZPtY5hsQILBLmF+BZ5WpKZp1jBeSjH2G7lxet9kbcH+kIVj0tPFEoyKI9wvWqIwC4prx/WVk2wLTJjzBAhyNxfEq7C9CeiX9pQEbEqJfkKCQ== nocomment
# gitea public key
command="/var/folders/n0/4qx0k8414mndry6fjtm06v140000gp/T/tmp.JMhL8PJw/go-build307601641/b548/repo.test serv key-2 --config=''",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAu7tvIvX6ZHrRXuZNfkR3XLHSsuCK9Zn3X58lxBcQzuo5xZgB6vRwwm/QtJuF+zZPtY5hsQILBLmF+BZ5WpKZp1jBeSjH2G7lxet9kbcH+kIVj0tPFEoyKI9wvWqIwC4prx/WVk2wLTJjzBAhyNxfEq7C9CeiX9pQEbEqJfkKCQ== nocomment
32 changes: 30 additions & 2 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"version": "1.1.1"
},
"basePath": "{{AppSubUrl}}/api/v1",
"basePath": "/api/v1",
richmahn marked this conversation as resolved.
Show resolved Hide resolved
"paths": {
"/admin/orgs": {
"get": {
Expand Down Expand Up @@ -1775,6 +1775,24 @@
"name": "sha",
"in": "path",
"required": true
},
{
"type": "boolean",
"description": "show all directories and files",
"name": "recursive",
"in": "query"
},
{
"type": "integer",
"description": "page number; the 'truncated' field in the response will be true if there are still more items after this page, false if the last page",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "number of items per page; default is 1000 or what is set in app.ini as DEFAULT_GIT_TREES_PER_PAGE",
"name": "per_page",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -7352,10 +7370,20 @@
"description": "GitTreeResponse returns a git tree",
"type": "object",
"properties": {
"page": {
"type": "integer",
"format": "int64",
"x-go-name": "Page"
},
"sha": {
"type": "string",
"x-go-name": "SHA"
},
"total_count": {
"type": "integer",
"format": "int64",
"x-go-name": "TotalCount"
},
"tree": {
"type": "array",
"items": {
Expand Down Expand Up @@ -8925,4 +8953,4 @@
"SudoHeader": []
}
]
}
}
6 changes: 6 additions & 0 deletions vendor/code.gitea.io/git/repo_tree.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions vendor/code.gitea.io/sdk/gitea/repo_tree.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.