aboutsummaryrefslogtreecommitdiff
path: root/pkg/handler/git/handler.go
blob: e2f40422ed212156620190800a9ec6569bc4c11f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package git

import (
	"log/slog"
	"net/http"

	"git.gabrielgio.me/cerrado/pkg/service"
	"git.gabrielgio.me/cerrado/templates"
	"github.com/go-git/go-git/v5/plumbing"
	"github.com/go-git/go-git/v5/plumbing/object"
)

type (
	GitHandler struct {
		gitService gitService
	}

	gitService interface {
		ListRepositories() ([]*service.Repository, error)
		ListCommits(name string, ref string) ([]*object.Commit, error)
		GetHead(name string) (*plumbing.Reference, error)
		ListTags(name string) ([]*object.Tag, error)
		ListBranches(name string) ([]*plumbing.Reference, error)
	}
)

func NewGitHandler(gitService gitService) *GitHandler {
	return &GitHandler{
		gitService: gitService,
	}
}

func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) {
	repos, err := g.gitService.ListRepositories()
	if err != nil {
		slog.Error("Error listing repo", "error", err)
		return
	}

	gitList := &templates.GitListPage{repos}
	templates.WritePageTemplate(w, gitList)
}

func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) {
	name := r.PathValue("name")
	ref, err := g.gitService.GetHead(name)
	if err != nil {
		slog.Error("Error loading head", "error", err)
		return
	}

	gitList := &templates.GitItemPage{
		Name:        name,
		Ref:         ref.Name().Short(),
		GitItemBase: &templates.GitItemSummaryPage{},
	}
	templates.WritePageTemplate(w, gitList)
}

func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) {
	name := r.PathValue("name")
	ref, err := g.gitService.GetHead(name)
	if err != nil {
		slog.Error("Error loading head", "error", err)
		return
	}
	gitList := &templates.GitItemPage{
		Name:        name,
		Ref:         ref.Name().Short(),
		GitItemBase: &templates.GitItemAboutPage{},
	}
	templates.WritePageTemplate(w, gitList)
}

func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) {
	name := r.PathValue("name")

	tags, err := g.gitService.ListTags(name)
	if err != nil {
		slog.Error("Error loading tags", "error", err)
		return
	}

	branches, err := g.gitService.ListBranches(name)
	if err != nil {
		slog.Error("Error loading branches", "error", err)
		return
	}

	ref, err := g.gitService.GetHead(name)
	if err != nil {
		slog.Error("Error loading head", "error", err)
		return
	}

	gitList := &templates.GitItemPage{
		Name: name,
		Ref:  ref.Name().Short(),
		GitItemBase: &templates.GitItemRefsPage{
			Tags:     tags,
			Branches: branches,
		},
	}
	templates.WritePageTemplate(w, gitList)
}

func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) {
	name := r.PathValue("name")
	ref := r.PathValue("ref")
	gitList := &templates.GitItemPage{
		Name:        name,
		Ref:         ref,
		GitItemBase: &templates.GitItemTreePage{},
	}
	templates.WritePageTemplate(w, gitList)
}

func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) {
	name := r.PathValue("name")
	ref := r.PathValue("ref")

	commits, err := g.gitService.ListCommits(name, ref)
	if err != nil {
		slog.Error("Error loading commits", "error", err)
		return
	}

	gitList := &templates.GitItemPage{
		Name: name,
		Ref:  ref,
		GitItemBase: &templates.GitItemLogPage{
			Commits: commits,
		},
	}
	templates.WritePageTemplate(w, gitList)
}