From d132946e4073564b22f20c149065551c9faf5a4e Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 12 Feb 2024 01:48:49 -0500 Subject: [PATCH] feat: add /repos/{owner}/{repo}/git/trees/{tree_sha endpoint --- api/convert.go | 36 ++++++++++++++++++++++++++++++++++++ api/routes_rest_api.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/api/convert.go b/api/convert.go index fcb993e..85b7e55 100644 --- a/api/convert.go +++ b/api/convert.go @@ -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 := "" diff --git a/api/routes_rest_api.go b/api/routes_rest_api.go index bf09cd2..dba0523 100644 --- a/api/routes_rest_api.go +++ b/api/routes_rest_api.go @@ -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()