diff --git a/module/gitea/main.go b/module/gitea/main.go index ae57004..330aad1 100644 --- a/module/gitea/main.go +++ b/module/gitea/main.go @@ -26,11 +26,11 @@ type Module struct { type giteaKeyType int const ( - GITEA_KEY_OAUTH_CLIENT = giteaKeyType(iota) - GITEA_KEY_USER - GITEA_KEY_STATE - GITEA_KEY_ACCESS_TOKEN - GITEA_KEY_REFRESH_TOKEN + KeyOauthClient = giteaKeyType(iota) + KeyUser + KeyState + KeyAccessToken + KeyRefreshToken ) func New() *Module { @@ -56,7 +56,7 @@ func (m *Module) SetupModule(router *mux.Router, cb func(cb func(http.ResponseWr func (m *Module) getClient(cb func(http.ResponseWriter, *http.Request, *utils.State, *gitea.Client)) func(rw http.ResponseWriter, req *http.Request) { return m.sessionWrapper(func(rw http.ResponseWriter, req *http.Request, state *utils.State) { - if v, ok := utils.GetStateValue[*gitea.Client](state, GITEA_KEY_OAUTH_CLIENT); ok { + if v, ok := utils.GetStateValue[*gitea.Client](state, KeyOauthClient); ok { cb(rw, req, state, v) return } @@ -67,22 +67,50 @@ func (m *Module) getClient(cb func(http.ResponseWriter, *http.Request, *utils.St func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *utils.State, giteaClient *gitea.Client) { myUser, _, err := giteaClient.GetMyUserInfo() if err != nil { - state.Del(GITEA_KEY_OAUTH_CLIENT) + state.Del(KeyOauthClient) http.Error(rw, err.Error(), http.StatusInternalServerError) return } - fmt.Println("Username:", myUser.UserName) orgs, _, err := giteaClient.ListMyOrgs(gitea.ListOrgsOptions{}) if err != nil { http.Error(rw, err.Error(), http.StatusInternalServerError) return } - fmt.Println("Orgs:", orgs) - orgSimple := make([]struct{ Name string }, len(orgs)) for i, j := range orgs { orgSimple[i] = struct{ Name string }{j.UserName} } + selOrg := "" + repoSimple := make([]struct{ Name string }, 0) + selRepo := "" + refSimple := make([]struct{ Name string }, 0) + + q := req.URL.Query() + if q.Has("org") { + selOrg = q.Get("org") + repos, _, err := giteaClient.ListOrgRepos(selOrg, gitea.ListOrgReposOptions{ListOptions: gitea.ListOptions{Page: 0, PageSize: 100}}) + if err != nil { + http.Error(rw, err.Error(), http.StatusInternalServerError) + return + } + repoSimple = make([]struct{ Name string }, len(repos)) + for i, j := range repos { + repoSimple[i] = struct{ Name string }{Name: j.Name} + } + + if q.Has("repo") { + selRepo = q.Get("repo") + refs, _, err := giteaClient.GetRepoRefs(selOrg, selRepo, "heads") + if err != nil { + http.Error(rw, err.Error(), http.StatusInternalServerError) + return + } + refSimple = make([]struct{ Name string }, len(refs)) + for i, ref := range refs { + refSimple[i] = struct{ Name string }{Name: ref.Ref} + } + } + } tmp, err := template.New("homepage").Parse(indexTemplate) if err != nil { @@ -91,12 +119,20 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util } err = tmp.Execute(rw, struct { Username string - Orgs []struct { - Name string - } + Orgs []struct{ Name string } + Repos []struct{ Name string } + SelOrg string + ShowOrg bool + SelRepo string + ShowRepo bool }{ Username: myUser.UserName, Orgs: orgSimple, + Repos: repoSimple, + SelOrg: selOrg, + SelRepo: selRepo, + ShowOrg: selOrg != "", + ShowRepo: selRepo != "", }) if err != nil { fmt.Println("Template execute error:", err) @@ -105,14 +141,14 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util } func (m *Module) loginPage(rw http.ResponseWriter, req *http.Request, state *utils.State) { - if myUser, ok := utils.GetStateValue[*string](state, GITEA_KEY_USER); ok { + if myUser, ok := utils.GetStateValue[*string](state, KeyUser); ok { if myUser != nil { http.Redirect(rw, req, "/gitea", http.StatusTemporaryRedirect) return } } - if flowState, ok := utils.GetStateValue[uuid.UUID](state, GITEA_KEY_STATE); ok { + if flowState, ok := utils.GetStateValue[uuid.UUID](state, KeyState); ok { q := req.URL.Query() if q.Has("code") && q.Has("state") { if q.Get("state") == flowState.String() { @@ -126,7 +162,9 @@ func (m *Module) loginPage(rw http.ResponseWriter, req *http.Request, state *uti fmt.Println("Create client error:", err) return } - state.Put(GITEA_KEY_OAUTH_CLIENT, c) + state.Put(KeyOauthClient, c) + state.Put(KeyAccessToken, exchange.AccessToken) + state.Put(KeyRefreshToken, exchange.RefreshToken) http.Redirect(rw, req, "/gitea", http.StatusTemporaryRedirect) return } @@ -136,7 +174,7 @@ func (m *Module) loginPage(rw http.ResponseWriter, req *http.Request, state *uti } flowState := uuid.New() - state.Put(GITEA_KEY_STATE, flowState) + state.Put(KeyState, flowState) http.Redirect(rw, req, m.oauthClient.AuthCodeURL(flowState.String(), oauth2.AccessTypeOffline), http.StatusTemporaryRedirect) } diff --git a/module/gitea/pages/index.go.html b/module/gitea/pages/index.go.html index f921286..cbb02f9 100644 --- a/module/gitea/pages/index.go.html +++ b/module/gitea/pages/index.go.html @@ -2,17 +2,48 @@ Gitea | Melon Tools +

Gitea | Melon Tools

-
-

Select repository source:

- +
+
+

Select repository source:

+ +
+ {{if .ShowOrg}} +
+

Select repository:

+ +
+ {{end}} + {{if .ShowRepo}} +
+

Repository details:

+

Go import: go get {{.SelRepo.Full}}

+
+ {{end}}