Author | Jakob Wakeling <[email protected]> |
Date | 2023-08-03 08:35:23 |
Commit | 1f376a942c45404d3fdf74160d09a12b61a23a79 |
Parent | 7ba1b21b11fb9266c475104434c93714b2b67af9 |
Add last commit date to repository index
Diffstat
M | res/admin/users.html | | | 2 | +- |
M | res/index.html | | | 2 | +- |
A | src/index.go | | | 83 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | src/repo.go | | | 55 | ------------------------------------------------------- |
4 files changed, 85 insertions, 57 deletions
diff --git a/res/admin/users.html b/res/admin/users.html index acc0f0d..7839d2e 100644 --- a/res/admin/users.html +++ b/res/admin/users.html @@ -30,7 +30,7 @@ {{range .Users}} <tr> <td>{{.Id}}</td> - <td><a href="/?user={{.Name}}">{{.Name}}</a></td> + <td><a href="/?u={{.Name}}">{{.Name}}</a></td> <td>{{.FullName}}</td> <td>{{.IsAdmin}}</td> <td><a href="/admin/user/edit?user={{.Id}}">edit</a></td> diff --git a/res/index.html b/res/index.html index 6ad6a4c..b8ea778 100644 --- a/res/index.html +++ b/res/index.html @@ -44,7 +44,7 @@ <tr> <td><a href="/{{.Name}}/">{{.Name}}</a></td> <td>{{.Description}}</td> - <td>{{.Owner}}</td> + <td><a href="/?u={{.Owner}}">{{.Owner}}</a></td> <td>{{.Visibility}}</td> <td>{{.LastCommit}}</td> </tr> diff --git a/src/index.go b/src/index.go new file mode 100644 index 0000000..4ac76d5 --- /dev/null +++ b/src/index.go @@ -0,0 +1,83 @@ +package goit + +import ( + "errors" + "log" + "net/http" + "time" + + "github.com/Jamozed/Goit/src/util" + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" +) + +func HandleIndex(w http.ResponseWriter, r *http.Request) { + auth, admin, uid := AuthCookieAdmin(w, r, true) + + user, err := GetUser(uid) + if err != nil { + log.Println("[/]", err.Error()) + HttpError(w, http.StatusInternalServerError) + return + } + + type row struct{ Name, Description, Owner, Visibility, LastCommit string } + data := struct { + Title, Username string + Admin, Auth bool + Repos []row + }{Title: "Repositories", Admin: admin, Auth: auth} + + if user != nil { + data.Username = user.Name + } + + rows, err := db.Query("SELECT id, owner_id, name, description, is_private FROM repos") + if err != nil { + log.Println("[/]", err.Error()) + HttpError(w, http.StatusInternalServerError) + return + } + defer rows.Close() + + for rows.Next() { + repo := Repo{} + + if err := rows.Scan(&repo.Id, &repo.OwnerId, &repo.Name, &repo.Description, &repo.IsPrivate); err != nil { + log.Println("[/]", err.Error()) + } else if !repo.IsPrivate || (auth && uid == repo.OwnerId) { + owner, err := GetUser(repo.OwnerId) + if err != nil { + log.Println("[/]", err.Error()) + } + + var lastCommit string + if gr, err := git.PlainOpen(RepoPath(repo.Name)); err != nil { + log.Println("[/]", err.Error()) + } else if ref, err := gr.Head(); err != nil { + if !errors.Is(err, plumbing.ErrReferenceNotFound) { + log.Println("[/]", err.Error()) + } + } else if commit, err := gr.CommitObject(ref.Hash()); err != nil { + log.Println("[/]", err.Error()) + } else { + lastCommit = commit.Author.When.UTC().Format(time.DateTime) + } + + data.Repos = append(data.Repos, row{ + Name: repo.Name, Description: repo.Description, Owner: owner.Name, + Visibility: util.If(repo.IsPrivate, "private", "public"), LastCommit: lastCommit, + }) + } + } + + if err := rows.Err(); err != nil { + log.Println("[/]", err.Error()) + HttpError(w, http.StatusInternalServerError) + return + } + + if err := Tmpl.ExecuteTemplate(w, "index", data); err != nil { + log.Println("[/]", err.Error()) + } +} diff --git a/src/repo.go b/src/repo.go index 7cc9780..0200eb1 100644 --- a/src/repo.go +++ b/src/repo.go @@ -27,61 +27,6 @@ type Repo struct { IsPrivate bool } -func HandleIndex(w http.ResponseWriter, r *http.Request) { - auth, admin, uid := AuthCookieAdmin(w, r, true) - - user, err := GetUser(uid) - if err != nil { - log.Println("[/]", err.Error()) - HttpError(w, http.StatusInternalServerError) - return - } - - if rows, err := db.Query("SELECT id, owner_id, name, description, is_private FROM repos"); err != nil { - log.Println("[/]", err.Error()) - HttpError(w, http.StatusInternalServerError) - } else { - defer rows.Close() - - type row struct{ Name, Description, Owner, Visibility, LastCommit string } - data := struct { - Title, Username string - Admin, Auth bool - Repos []row - }{Title: "Repositories", Admin: admin, Auth: auth} - - if user != nil { - data.Username = user.Name - } - - for rows.Next() { - d := Repo{} - if err := rows.Scan(&d.Id, &d.OwnerId, &d.Name, &d.Description, &d.IsPrivate); err != nil { - log.Println("[/]", err.Error()) - } else if !d.IsPrivate || (auth && uid == d.OwnerId) { - owner, err := GetUser(d.OwnerId) - if err != nil { - log.Println("[/]", err.Error()) - } - - data.Repos = append(data.Repos, row{ - d.Name, d.Description, owner.Name, util.If(d.IsPrivate, "private", "public"), "", - }) - } - } - - if err := rows.Err(); err != nil { - log.Println("[/]", err.Error()) - HttpError(w, http.StatusInternalServerError) - return - } - - if err := Tmpl.ExecuteTemplate(w, "index", data); err != nil { - log.Println("[/]", err.Error()) - } - } -} - func HandleRepoRefs(w http.ResponseWriter, r *http.Request) { reponame := mux.Vars(r)["repo"]