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" "html/template"
"net/http" "net/http"
"os" "os"
"strings"
) )
//go:embed pages/index.go.html //go:embed pages/index.go.html
@ -83,7 +84,9 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
selOrg := "" selOrg := ""
repoSimple := make([]struct{ Name string }, 0) repoSimple := make([]struct{ Name string }, 0)
selRepo := "" selRepo := ""
refSimple := make([]struct{ Name string }, 0) selModule := ""
selCommitTime := ""
selCommitHash := ""
q := req.URL.Query() q := req.URL.Query()
if q.Has("org") { if q.Has("org") {
@ -100,14 +103,35 @@ 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")
refs, _, err := giteaClient.GetRepoRefs(selOrg, selRepo, "heads") repo, _, err := giteaClient.GetRepo(selOrg, selRepo)
if err != nil { if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError) http.Error(rw, err.Error(), http.StatusInternalServerError)
return return
} }
refSimple = make([]struct{ Name string }, len(refs)) refs, _, err := giteaClient.GetRepoRefs(selOrg, selRepo, "heads/"+repo.DefaultBranch)
for i, ref := range refs { if err != nil {
refSimple[i] = struct{ Name string }{Name: ref.Ref} 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:]
} }
} }
} }
@ -123,16 +147,20 @@ func (m *Module) homepage(rw http.ResponseWriter, req *http.Request, state *util
Repos []struct{ Name string } Repos []struct{ Name string }
SelOrg string SelOrg string
ShowOrg bool ShowOrg bool
SelRepo string SelModule string
ShowRepo bool ShowRepo bool
CommitTime string
CommitHash string
}{ }{
Username: myUser.UserName, Username: myUser.UserName,
Orgs: orgSimple, Orgs: orgSimple,
Repos: repoSimple, Repos: repoSimple,
SelOrg: selOrg, SelOrg: selOrg,
SelRepo: selRepo, SelModule: selModule,
ShowOrg: selOrg != "", ShowOrg: selOrg != "",
ShowRepo: selRepo != "", ShowRepo: selModule != "",
CommitTime: selCommitTime,
CommitHash: selCommitHash,
}) })
if err != nil { if err != nil {
fmt.Println("Template execute error:", err) fmt.Println("Template execute error:", err)

View File

@ -41,7 +41,7 @@
{{if .ShowRepo}} {{if .ShowRepo}}
<div> <div>
<p>Repository details:</p> <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> </div>
{{end}} {{end}}
</div> </div>