From 304fa4a99e7101110893f71b9db550b79b42aa3f Mon Sep 17 00:00:00 2001 From: MrMelon Date: Fri, 9 Sep 2022 16:45:10 +0100 Subject: [PATCH] Update swagger renderer to load external files properly --- module/gitea/main.go | 157 +++++++++++++---------------- module/gitea/pages/index.go.html | 4 +- module/gitea/pages/swagger.go.html | 14 +-- 3 files changed, 80 insertions(+), 95 deletions(-) diff --git a/module/gitea/main.go b/module/gitea/main.go index e5acbed..b5924b5 100644 --- a/module/gitea/main.go +++ b/module/gitea/main.go @@ -67,6 +67,7 @@ func (m *Module) SetupModule(router *mux.Router, f func(cb func(http.ResponseWri } router.HandleFunc("/", m.getClient(m.homepage)) router.HandleFunc("/login", m.sessionWrapper(m.loginPage)) + router.PathPrefix("/spec/{org}/{repo}/{sha}/{spec:.+}").HandlerFunc(m.getClient(m.specPage)) router.PathPrefix("/swagger").HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { p := filepath.Join("swagger", filepath.Base(req.URL.Path)) open, err := assets.SwaggerAssets.Open(p) @@ -140,6 +141,7 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util selModule := "" selCommitTime := "" selCommitHash := "" + selCommitHashShort := "" mySpecs := make([]struct { Name string Code int @@ -196,54 +198,9 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util return } - if q.Has("spec") { - spec := q.Get("spec") - if q.Has("raw") && q.Get("raw") == "true" { - open, _, err := giteaClient.GetFile(myOrg, selRepo, ref.Object.SHA, spec) - if err != nil { - http.Error(rw, "OpenAPI spec raw: "+err.Error(), http.StatusInternalServerError) - return - } - http.ServeContent(rw, req, spec, repo.Updated, bytes.NewReader(open)) - return - } - contents, _, err := giteaClient.GetContents(myOrg, selRepo, ref.Object.SHA, spec) - if err != nil { - http.Error(rw, "OpenAPI spec: "+err.Error(), http.StatusInternalServerError) - return - } - - tmp, err := template.New("swagger").Parse(swaggerTemplate) - if err != nil { - fmt.Println("Template parse error:", err) - return - } - q2 := q - q2.Set("raw", "true") - err = tmp.Execute(rw, struct { - LoadUrls []struct { - Url string `json:"url"` - Name string `json:"name"` - } - LoadMain string - }{ - LoadUrls: []struct { - Url string `json:"url"` - Name string `json:"name"` - }{ - {Url: "/gitea/?" + q2.Encode(), Name: contents.Name}, - }, - LoadMain: contents.Name, - }) - if err != nil { - fmt.Println("Template execute error:", err) - return - } - return - } - selCommitTime = commit.CommitMeta.Created.UTC().Format("20060102150405") - selCommitHash = commit.CommitMeta.SHA[:12] + selCommitHash = commit.CommitMeta.SHA + selCommitHashShort = commit.CommitMeta.SHA[:12] goMod, resp, err := giteaClient.GetFile(myOrg, selRepo, ref.Object.SHA, "go.mod") if err != nil { if resp.StatusCode != http.StatusNotFound { @@ -310,33 +267,35 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util Name string Private bool } - MyOrg string - SelOrg string - SelRepo string - ShowOrg bool - SelModule string - ShowGoMod bool - CommitTime string - CommitHash string - ShowSpec bool - Specs []struct { + MyOrg string + SelOrg string + SelRepo string + ShowOrg bool + SelModule string + ShowGoMod bool + CommitTime string + CommitHash string + CommitHashShort string + ShowSpec bool + Specs []struct { Name string Code int } }{ - Username: myUser.UserName, - Orgs: orgSimple, - Repos: repoSimple, - MyOrg: myOrg, - SelOrg: selOrg, - SelRepo: selRepo, - SelModule: selModule, - ShowOrg: myOrg != "", - ShowGoMod: selModule != "", - CommitTime: selCommitTime, - CommitHash: selCommitHash, - ShowSpec: len(mySpecs) > 0, - Specs: mySpecs, + Username: myUser.UserName, + Orgs: orgSimple, + Repos: repoSimple, + MyOrg: myOrg, + SelOrg: selOrg, + SelRepo: selRepo, + SelModule: selModule, + ShowOrg: myOrg != "", + ShowGoMod: selModule != "", + CommitTime: selCommitTime, + CommitHash: selCommitHash, + CommitHashShort: selCommitHashShort, + ShowSpec: len(mySpecs) > 0, + Specs: mySpecs, }) if err != nil { fmt.Println("Template execute error:", err) @@ -383,25 +342,51 @@ func (m *Module) loginPage(rw http.ResponseWriter, req *http.Request, state *uti http.Redirect(rw, req, m.oauthClient.AuthCodeURL(flowState.String(), oauth2.AccessTypeOffline), http.StatusTemporaryRedirect) } -func (m *Module) fetchRepos(giteaClient *gitea.Client) { - repos, _, err := giteaClient.ListOrgRepos("snow", gitea.ListOrgReposOptions{ListOptions: gitea.ListOptions{Page: 0, PageSize: 100}}) - if err != nil { - fmt.Println(err) - return - } - for _, myRepo := range repos { - refs, _, err := giteaClient.GetRepoRefs("snow", myRepo.Name, "heads") +func (m *Module) specPage(rw http.ResponseWriter, req *http.Request, state *utils.State, giteaClient *gitea.Client) { + vars := mux.Vars(req) + myOrg := vars["org"] + myRepo := vars["repo"] + mySha := vars["sha"] + mySpec := vars["spec"] + + q := req.URL.Query() + if q.Has("raw") && q.Get("raw") == "true" { + open, _, err := giteaClient.GetFile(myOrg, myRepo, mySha, mySpec) if err != nil { - fmt.Println(err) + http.Error(rw, "OpenAPI spec raw: "+err.Error(), http.StatusInternalServerError) return } - fmt.Println(len(refs)) - for _, myRef := range refs { - if myRef.Ref == "refs/heads/"+myRepo.DefaultBranch { - fmt.Println(myRef.Ref) - return - } + http.ServeContent(rw, req, mySpec, time.Now(), bytes.NewReader(open)) + return + } + contents, _, err := giteaClient.GetContents(myOrg, myRepo, mySha, mySpec) + if err != nil { + http.Error(rw, "OpenAPI spec: "+err.Error(), http.StatusInternalServerError) + return + } + + tmp, err := template.New("swagger").Parse(swaggerTemplate) + if err != nil { + fmt.Println("Template parse error:", err) + return + } + err = tmp.Execute(rw, struct { + LoadUrls []struct { + Url string `json:"url"` + Name string `json:"name"` } - fmt.Println("Can't find default branch") + LoadMain string + }{ + LoadUrls: []struct { + Url string `json:"url"` + Name string `json:"name"` + }{ + {Url: fmt.Sprintf("/gitea/spec/%s/%s/%s/%s?raw=true", myOrg, myRepo, mySha, mySpec), Name: contents.Name}, + }, + LoadMain: contents.Name, + }) + if err != nil { + fmt.Println("Template execute error:", err) + return } } diff --git a/module/gitea/pages/index.go.html b/module/gitea/pages/index.go.html index 28aebd2..c0b57f8 100644 --- a/module/gitea/pages/index.go.html +++ b/module/gitea/pages/index.go.html @@ -57,7 +57,7 @@
{{if .ShowGoMod}}

Repository details:

-

Go import: go get {{.SelModule}}@v0.0.0-{{.CommitTime}}-{{.CommitHash}}

+

Go import: go get {{.SelModule}}@v0.0.0-{{.CommitTime}}-{{.CommitHashShort}}

{{end}} {{if .ShowSpec}}
@@ -68,7 +68,7 @@ {{if not (eq .Code 200)}} {{.Name}} ({{.Code}}) {{else}} - {{.Name}} + {{.Name}} {{end}} {{end}} diff --git a/module/gitea/pages/swagger.go.html b/module/gitea/pages/swagger.go.html index 26d2ae2..d89cff2 100644 --- a/module/gitea/pages/swagger.go.html +++ b/module/gitea/pages/swagger.go.html @@ -4,16 +4,16 @@ Swagger | Gitea | Melon Tools - - - - + + + +
- - - + + +