-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Register code repository file tree api
add notes fix tree types Add the function of parsing user information CreateGitRepoFile add authorinfo delete fmt print add commit author info add notes, agg GetUserBasicInfoFromReq errors add userinfo filter add filter unittest fix userinfo filter fix log fix context key type add exception request filter, parallel page tool add userinfo filter fix notes add notes fix jwt unittest fix unittest TestAboutGitRepositoryFileTreeTable
- Loading branch information
krli
committed
May 27, 2022
1 parent
d893005
commit f3e3513
Showing
22 changed files
with
892 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,69 @@ | ||
/* | ||
Copyright 2022 The Katanomi Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
var ( | ||
GitRepositoryFileTreeGVK = GroupVersion.WithKind("GitRepositoryFileTree") | ||
) | ||
|
||
// GitRepoFile object for plugins | ||
type GitRepositoryFileTree struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
||
Spec GitRepositoryFileTreeSpec `json:"spec"` | ||
} | ||
|
||
// GitRepoFileSpec spec for repository's file | ||
type GitRepositoryFileTreeSpec struct { | ||
Tree []GitRepositoryFileTreeNode `json:"tree"` | ||
} | ||
|
||
type GitRepositoryFileTreeNodeType string | ||
|
||
const ( | ||
// TreeNodeBlobType represents a file | ||
TreeNodeBlobType GitRepositoryFileTreeNodeType = "blob" | ||
// TreeNodeTreeType represents a folder | ||
TreeNodeTreeType GitRepositoryFileTreeNodeType = "tree" | ||
) | ||
|
||
// GitRepositoryFileTreeNode represents a node in the file system | ||
type GitRepositoryFileTreeNode struct { | ||
// Sha is the ID of the node | ||
Sha string `json:"sha"` | ||
// Name is the name of the node | ||
Name string `json:"name"` | ||
// Path is the path of the node | ||
Path string `json:"path"` | ||
// Type is the type of the node | ||
Type GitRepositoryFileTreeNodeType `json:"type"` | ||
// Mode indicates the permission level of the file | ||
Mode string `json:"mode"` | ||
} | ||
|
||
// Requesting parameters for the File Tree API | ||
type GitRepoFileTreeOption struct { | ||
GitRepo | ||
Path string `json:"path"` | ||
TreeSha string `json:"tree_sha"` | ||
Recursive bool `json:"recursive"` | ||
} |
This file contains 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 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 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 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,72 @@ | ||
/* | ||
Copyright 2022 The Katanomi Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package logging | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/emicklei/go-restful/v3" | ||
"knative.dev/pkg/logging" | ||
) | ||
|
||
// Is it an abnormal status code | ||
func isExceptionStatusCode(resp *restful.Response) bool { | ||
if resp.StatusCode() == http.StatusOK || resp.StatusCode() == http.StatusCreated { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
// ExceptionRequestFilter is used to catch requests with exceptions | ||
func ExceptionRequestFilter(serviceName string, ignorePaths []string, ignoreStatusCodes []int) restful.FilterFunction { | ||
|
||
isIgnoredPath := func(req *restful.Request) bool { | ||
routePath := strings.TrimPrefix(req.SelectedRoutePath(), "/") | ||
for _, _item := range ignorePaths { | ||
item := strings.TrimPrefix(_item, "/") | ||
if item == routePath { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
isIgnoredStatusCode := func(resp *restful.Response) bool { | ||
for _, code := range ignoreStatusCodes { | ||
if resp.StatusCode() == code { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
return func(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) { | ||
if isIgnoredPath(req) { | ||
chain.ProcessFilter(req, resp) | ||
return | ||
} | ||
logger := logging.FromContext(req.Request.Context()) | ||
logger.Debugw("received requests", "req", req) | ||
chain.ProcessFilter(req, resp) | ||
if isIgnoredStatusCode(resp) { | ||
return | ||
} | ||
if isExceptionStatusCode(resp) { | ||
logger.Debugw("status code of the response to the exception caught", "code", resp.StatusCode(), "req", req, "resp", resp) | ||
return | ||
} | ||
} | ||
} |
This file contains 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,91 @@ | ||
/* | ||
Copyright 2022 The Katanomi Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package parallel | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"k8s.io/utils/trace" | ||
"knative.dev/pkg/logging" | ||
) | ||
|
||
// PageRequestFunc is a tool for concurrent processing of pagination | ||
type PageRequestFunc struct { | ||
// RequestPage for concurrent request paging | ||
RequestPage func(ctx context.Context, pageSize int, page int) (interface{}, error) | ||
// PageResult for get paging information | ||
PageResult func(items interface{}) (total int, currentPageLen int, err error) | ||
} | ||
|
||
// Concurrent request paging | ||
func PageRequest(ctx context.Context, logName string, concurrency int, pageSize int, f PageRequestFunc) ([]interface{}, error) { | ||
log := trace.New("PageRequest", trace.Field{Key: "name", Value: logName}) | ||
logger := logging.FromContext(ctx) | ||
|
||
defer func() { | ||
log.LogIfLong(3 * time.Second) | ||
}() | ||
|
||
items, err := f.RequestPage(ctx, pageSize, 1) | ||
if err != nil { | ||
return nil, err | ||
} | ||
log.Step("requested page 1") | ||
total, firstPageLen, err := f.PageResult(items) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if firstPageLen < pageSize { | ||
return []interface{}{items}, nil | ||
} | ||
|
||
if total == firstPageLen { | ||
return []interface{}{items}, nil | ||
} | ||
|
||
var request = func(i int) func() (interface{}, error) { | ||
return func() (interface{}, error) { | ||
items, err := f.RequestPage(ctx, pageSize, i) | ||
log.Step(fmt.Sprintf("requested page %d", i)) | ||
return items, err | ||
} | ||
} | ||
|
||
totalPage := total / pageSize | ||
if total%pageSize != 0 { | ||
totalPage = totalPage + 1 | ||
} | ||
|
||
if totalPage-1 < concurrency { // first page we have requested, so skip first page | ||
concurrency = totalPage - 1 | ||
} | ||
|
||
p := P(logger, "PageRequest").FailFast().SetConcurrent(concurrency).Context(ctx) | ||
for i := 2; i <= totalPage; i++ { | ||
p.Add(request(i)) | ||
} | ||
|
||
results, err := p.Do().Wait() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return append([]interface{}{items}, results...), nil | ||
|
||
} |
This file contains 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,35 @@ | ||
/* | ||
Copyright 2022 The Katanomi Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package parallel | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestPage(t *testing.T) { | ||
PageRequest(context.Background(), "TestPageResult", 2, 10, PageRequestFunc{ | ||
RequestPage: func(ctx context.Context, pageSize int, page int) (interface{}, error) { | ||
fmt.Printf("request -> page: %d, pagesize: %d\n", page, pageSize) | ||
return nil, nil | ||
}, | ||
PageResult: func(items interface{}) (total int, currentPageLen int, err error) { | ||
return 8, 6, nil | ||
}, | ||
}) | ||
} |
This file contains 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
Oops, something went wrong.