feat: add /repos/{owner}/{repo}/git/trees/{tree_sha endpoint

This commit is contained in:
Derrick Hammer 2024-02-12 01:48:49 -05:00
parent c607400951
commit d132946e40
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 67 additions and 0 deletions

View File

@ -214,6 +214,42 @@ func convertCommitFile(file *gitea.ChangedFile) *github.CommitFile {
}
}
func convertGitTree(tree *gitea.GitTreeResponse) *github.Tree {
if tree == nil {
return &github.Tree{}
}
return &github.Tree{
SHA: stringPtr(tree.SHA),
Truncated: boolPtr(tree.Truncated),
Entries: convertGitEntries(tree.Entries),
}
}
func convertGitEntries(entries []gitea.GitEntry) []*github.TreeEntry {
if entries == nil {
return make([]*github.TreeEntry, 0)
}
var ghEntries []*github.TreeEntry
for _, entry := range entries {
ghEntries = append(ghEntries, convertGitEntry(&entry))
}
return ghEntries
}
func convertGitEntry(s *gitea.GitEntry) *github.TreeEntry {
if s == nil {
return &github.TreeEntry{}
}
return &github.TreeEntry{
Path: stringPtr(s.Path),
Mode: stringPtr(s.Mode),
Type: stringPtr(s.Type),
Size: intPtr(int(s.Size)),
SHA: stringPtr(s.SHA),
URL: stringPtr(s.URL),
}
}
func translatePrAction(action structs.HookIssueAction) string {
translatedAction := ""

View File

@ -134,6 +134,36 @@ func (r restApi) getClientOrError(w http.ResponseWriter) *gitea.Client {
return client
}
func (r restApi) handlerGetTree(w http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
owner := vars["owner"]
repo := vars["repo"]
treeSha := vars["tree_sha"]
recursive := false
if request.URL.Query().Has("recursive") {
recursive = true
}
client := r.getClientOrError(w)
if client == nil {
return
}
tree, r2, err := client.GetTrees(owner, repo, treeSha, recursive)
if err != nil {
http.Error(w, "Failed to get tree", http.StatusInternalServerError)
r.logger.Error("Failed to get tree", zap.Error(err))
return
}
treeResponse := convertGitTree(tree)
r.sendPagingHeaders(w, r2)
r.respond(w, http.StatusOK, treeResponse)
}
func setupRestRoutes(params RouteParams) {
logger := params.Logger
cfg := params.Config
@ -143,6 +173,7 @@ func setupRestRoutes(params RouteParams) {
setupRoutes := func(r *mux.Router) {
r.HandleFunc("/repos/{owner}/{repo}/pulls/{pull_number}/files", restApi.handlerGetPullRequestFiles).Methods("GET")
r.HandleFunc("/repos/{owner}/{repo}/git/trees/{tree_sha}", restApi.handlerGetTree).Methods("GET")
}
restRouter := r.PathPrefix("/api").Subrouter()