Get repo go import

This commit is contained in:
Melon 2022-03-26 11:43:55 +00:00
parent d217b4d53b
commit 7ac73b86e1
Signed by: melon
GPG Key ID: B0ADD5395BCDAAB6
2 changed files with 48 additions and 20 deletions

View File

@ -13,6 +13,7 @@ import (
"html/template"
"net/http"
"os"
"strings"
)
//go:embed pages/index.go.html
@ -83,7 +84,9 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
selOrg := ""
repoSimple := make([]struct{ Name string }, 0)
selRepo := ""
refSimple := make([]struct{ Name string }, 0)
selModule := ""
selCommitTime := ""
selCommitHash := ""
q := req.URL.Query()
if q.Has("org") {
@ -100,14 +103,35 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
if q.Has("repo") {
selRepo = q.Get("repo")
refs, _, err := giteaClient.GetRepoRefs(selOrg, selRepo, "heads")
repo, _, err := giteaClient.GetRepo(selOrg, selRepo)
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}
refs, _, err := giteaClient.GetRepoRefs(selOrg, selRepo, "heads/"+repo.DefaultBranch)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
if len(refs) == 1 {
ref := refs[0]
commit, _, err := giteaClient.GetSingleCommit(selOrg, selRepo, ref.Object.SHA)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
selCommitTime = commit.CommitMeta.Created.Format("20060102150405")
selCommitHash = commit.CommitMeta.SHA[:12]
goMod, _, err := giteaClient.GetFile(selOrg, selRepo, ref.Object.SHA, "go.mod")
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
goModStr := string(goMod)
goModIdx := strings.Index(goModStr, "\n")
goModLine := goModStr[:goModIdx]
goModSpace := strings.Index(goModLine, " ")
selModule = goModLine[goModSpace+1:]
}
}
}
@ -118,21 +142,25 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
return
}
err = tmp.Execute(rw, struct {
Username string
Orgs []struct{ Name string }
Repos []struct{ Name string }
SelOrg string
ShowOrg bool
SelRepo string
ShowRepo bool
Username string
Orgs []struct{ Name string }
Repos []struct{ Name string }
SelOrg string
ShowOrg bool
SelModule string
ShowRepo bool
CommitTime string
CommitHash string
}{
Username: myUser.UserName,
Orgs: orgSimple,
Repos: repoSimple,
SelOrg: selOrg,
SelRepo: selRepo,
ShowOrg: selOrg != "",
ShowRepo: selRepo != "",
Username: myUser.UserName,
Orgs: orgSimple,
Repos: repoSimple,
SelOrg: selOrg,
SelModule: selModule,
ShowOrg: selOrg != "",
ShowRepo: selModule != "",
CommitTime: selCommitTime,
CommitHash: selCommitHash,
})
if err != nil {
fmt.Println("Template execute error:", err)

View File

@ -41,7 +41,7 @@
{{if .ShowRepo}}
<div>
<p>Repository details:</p>
<p>Go import: go get {{.SelRepo.Full}}</p>
<p>Go import: go get {{.SelModule}}@v0.0.0-{{.CommitTime}}-{{.CommitHash}}</p>
</div>
{{end}}
</div>