Add myOrg to store username or org name

This commit is contained in:
Melon 2022-04-12 09:58:39 +01:00
parent ccf4318310
commit d0c7561530
Signed by: melon
GPG Key ID: B0ADD5395BCDAAB6
2 changed files with 14 additions and 9 deletions

View File

@ -85,6 +85,7 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
orgSimple[i] = struct{ Name string }{j.UserName} orgSimple[i] = struct{ Name string }{j.UserName}
} }
selOrg := "" selOrg := ""
myOrg := ""
repoSimple := make([]struct{ Name string }, 0) repoSimple := make([]struct{ Name string }, 0)
selRepo := "" selRepo := ""
selModule := "" selModule := ""
@ -95,10 +96,12 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
if q.Has("org") { if q.Has("org") {
selOrg = q.Get("org") selOrg = q.Get("org")
var repos []*gitea.Repository var repos []*gitea.Repository
if selOrg == "@me" { if selOrg == "$me" {
myOrg = myUser.UserName
repos, _, err = giteaClient.ListMyRepos(gitea.ListReposOptions{ListOptions: gitea.ListOptions{Page: 0, PageSize: 100}}) repos, _, err = giteaClient.ListMyRepos(gitea.ListReposOptions{ListOptions: gitea.ListOptions{Page: 0, PageSize: 100}})
} else { } else {
repos, _, err = giteaClient.ListOrgRepos(selOrg, gitea.ListOrgReposOptions{ListOptions: gitea.ListOptions{Page: 0, PageSize: 100}}) myOrg = selOrg
repos, _, err = giteaClient.ListOrgRepos(myOrg, gitea.ListOrgReposOptions{ListOptions: gitea.ListOptions{Page: 0, PageSize: 100}})
} }
if err != nil { if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
@ -111,26 +114,26 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
if q.Has("repo") { if q.Has("repo") {
selRepo = q.Get("repo") selRepo = q.Get("repo")
repo, _, err := giteaClient.GetRepo(selOrg, selRepo) repo, _, err := giteaClient.GetRepo(myOrg, selRepo)
if err != nil { if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
} }
refs, _, err := giteaClient.GetRepoRefs(selOrg, selRepo, "heads/"+repo.DefaultBranch) refs, _, err := giteaClient.GetRepoRefs(myOrg, selRepo, "heads/"+repo.DefaultBranch)
if err != nil { if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
} }
if len(refs) == 1 { if len(refs) == 1 {
ref := refs[0] ref := refs[0]
commit, _, err := giteaClient.GetSingleCommit(selOrg, selRepo, ref.Object.SHA) commit, _, err := giteaClient.GetSingleCommit(myOrg, selRepo, ref.Object.SHA)
if err != nil { if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
} }
selCommitTime = commit.CommitMeta.Created.UTC().Format("20060102150405") selCommitTime = commit.CommitMeta.Created.UTC().Format("20060102150405")
selCommitHash = commit.CommitMeta.SHA[:12] selCommitHash = commit.CommitMeta.SHA[:12]
goMod, _, err := giteaClient.GetFile(selOrg, selRepo, ref.Object.SHA, "go.mod") goMod, _, err := giteaClient.GetFile(myOrg, selRepo, ref.Object.SHA, "go.mod")
if err != nil { if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
@ -153,6 +156,7 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
Username string Username string
Orgs []struct{ Name string } Orgs []struct{ Name string }
Repos []struct{ Name string } Repos []struct{ Name string }
MyOrg string
SelOrg string SelOrg string
ShowOrg bool ShowOrg bool
SelModule string SelModule string
@ -163,9 +167,10 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
Username: myUser.UserName, Username: myUser.UserName,
Orgs: orgSimple, Orgs: orgSimple,
Repos: repoSimple, Repos: repoSimple,
MyOrg: myOrg,
SelOrg: selOrg, SelOrg: selOrg,
SelModule: selModule, SelModule: selModule,
ShowOrg: selOrg != "", ShowOrg: myOrg != "",
ShowRepo: selModule != "", ShowRepo: selModule != "",
CommitTime: selCommitTime, CommitTime: selCommitTime,
CommitHash: selCommitHash, CommitHash: selCommitHash,

View File

@ -28,7 +28,7 @@
<p>Select repository source:</p> <p>Select repository source:</p>
<ul> <ul>
<li> <li>
<a href="?org=@me">{{.Username}} (my user account)</a> <a href="?org=$me">{{.Username}} (my user account)</a>
</li> </li>
{{range .Orgs}} {{range .Orgs}}
<li> <li>
@ -43,7 +43,7 @@
<ul> <ul>
{{range .Repos}} {{range .Repos}}
<li> <li>
<a href="?org={{$.SelOrg}}&repo={{.Name}}">{{$.SelOrg}}/{{.Name}}</a> <a href="?org={{$.MyOrg}}&repo={{.Name}}">{{$.SelOrg}}/{{.Name}}</a>
</li> </li>
{{end}} {{end}}
</ul> </ul>