Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func New(cfg *conf.Conf) (*Application, error) {
r.Use(authMiddleware.Middleware())
r.Use(a.ServiceMiddleware)
r.Get("/", a.ReadmeHandler)
r.Get("/-{taskID}", a.TaskHandler)
r.Get("/-{taskID}", a.TaskIDHandler)
})
r.Route("/service/{serviceID}/{project}", func(r chi.Router) {
r.Use(authMiddleware.Middleware())
Expand All @@ -135,13 +135,17 @@ func New(cfg *conf.Conf) (*Application, error) {
r.Route("/{branch}", func(r chi.Router) {
r.Route("/{commit}", func(r chi.Router) {
r.Post("/", a.PostTaskHandler)
r.Get("/", a.TaskHandler)
r.Get("/", a.TaskHandler(false))
r.Get("/status", badge.StatusBadge(a.storage, false))
r.Get("/badge/{badge}", a.TaskMyBadgeHandler)
r.Get("/volumes/*", a.VolumesHandler(6))
r.Get("/badge/{badge}", a.BadgeMyTaskHandler(false))
r.Get("/volumes/*", a.VolumesHandler(6, false))
})
r.Route("/latest", func(r chi.Router) {
r.Get("/", a.TaskHandler(true))
r.Get("/status", badge.StatusBadge(a.storage, true))
r.Get("/badge/{badge}", a.BadgeMyTaskHandler(true))
r.Get("/volumes/*", a.VolumesHandler(6, true))
})
r.Get("/latest", nil)
r.Get("/latest/status", badge.StatusBadge(a.storage, true))
})
})
})
Expand Down
94 changes: 51 additions & 43 deletions application/badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,66 @@ import (
"os"
"path/filepath"

"github.com/factorysh/microdensity/badge"
_badge "github.com/factorysh/microdensity/badge"
"github.com/go-chi/chi/v5"
"go.uber.org/zap"
)

func (a *Application) TaskMyBadgeHandler(w http.ResponseWriter, r *http.Request) {
l := a.logger.With(
zap.String("url", r.URL.String()),
zap.String("service", chi.URLParam(r, "serviceID")),
zap.String("project", chi.URLParam(r, "project")),
zap.String("branch", chi.URLParam(r, "branch")),
zap.String("commit", chi.URLParam(r, "commit")),
zap.String("branch", chi.URLParam(r, "branch")),
)
service := chi.URLParam(r, "serviceID")
project := chi.URLParam(r, "project")
branch := chi.URLParam(r, "branch")
commit := chi.URLParam(r, "commit")
bdg := chi.URLParam(r, "badge")
// BadgeMyTaskHandler generates a badge for a given task
func (a *Application) BadgeMyTaskHandler(latest bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := a.logger.With(
zap.String("url", r.URL.String()),
zap.String("service", chi.URLParam(r, "serviceID")),
zap.String("project", chi.URLParam(r, "project")),
zap.String("branch", chi.URLParam(r, "branch")),
zap.String("commit", chi.URLParam(r, "commit")),
zap.String("branch", chi.URLParam(r, "branch")),
)
service := chi.URLParam(r, "serviceID")
project := chi.URLParam(r, "project")
branch := chi.URLParam(r, "branch")
commit := chi.URLParam(r, "commit")
bdg := chi.URLParam(r, "badge")

t, err := a.storage.GetByCommit(service, project, branch, commit, false)
if err != nil {
l.Warn("Task get error", zap.Error(err))
w.WriteHeader(http.StatusNotFound)
return
}
// get the task
t, err := a.storage.GetByCommit(service, project, branch, commit, latest)
if err != nil {
l.Warn("Task get error", zap.Error(err))
w.WriteHeader(http.StatusNotFound)
return
}

p := filepath.Join(a.storage.GetVolumePath(t), "/data", fmt.Sprintf("%s.badge", bdg))
// try to get the output badge for this task in this service
p := filepath.Join(a.storage.GetVolumePath(t), "/data", fmt.Sprintf("%s.badge", bdg))
_, err = os.Stat(p)

_, err = os.Stat(p)
if err != nil {
l.Warn("Task get error", zap.Error(err))
if os.IsNotExist(err) {
w.WriteHeader(http.StatusNotFound)
} else {
// if not found
if err != nil {
// fallback to status badge
if os.IsNotExist(err) {
// use the service name, task status and colors from badge pkg
badge.WriteBadge(service, t.State.String(), _badge.Colors.Get(t.State), w)
return
}
w.WriteHeader(http.StatusBadRequest)
return
}
return
}
l = l.With(zap.String("path", p))
b, err := ioutil.ReadFile(p)
if err != nil {
l.Error("reading file", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
var badge _badge.Badge
err = json.Unmarshal(b, &badge)
if err != nil {
l.Error("JSON unmarshal", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
l = l.With(zap.String("path", p))
b, err := ioutil.ReadFile(p)
if err != nil {
l.Error("reading file", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
var badge _badge.Badge
err = json.Unmarshal(b, &badge)
if err != nil {
l.Error("JSON unmarshal", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
badge.Render(w, r)
}
badge.Render(w, r)
}
53 changes: 41 additions & 12 deletions application/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,48 @@ func (a *Application) PostTaskHandler(w http.ResponseWriter, r *http.Request) {
}

// TaskHandler show a Task
func (a *Application) TaskHandler(w http.ResponseWriter, r *http.Request) {
func (a *Application) TaskHandler(latest bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

l := a.logger.With(
zap.String("url", r.URL.String()),
zap.String("service", chi.URLParam(r, "serviceID")),
zap.String("project", chi.URLParam(r, "project")),
zap.String("branch", chi.URLParam(r, "branch")),
zap.String("commit", chi.URLParam(r, "commit")),
)
t, err := a.storage.GetByCommit(
chi.URLParam(r, "serviceID"),
chi.URLParam(r, "project"),
chi.URLParam(r, "branch"),
chi.URLParam(r, "commit"),
latest,
)
if err != nil {
l.Warn("Task get error", zap.Error(err))
if os.IsNotExist(err) {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusBadRequest)
}
return
}
err = json.NewEncoder(w).Encode(t)
if err != nil {
l.Error("Json encoding error", zap.Error(err))
panic(err)
}
}
}

// TaskIDHandler get a task using it's ID
func (a *Application) TaskIDHandler(w http.ResponseWriter, r *http.Request) {
l := a.logger.With(
zap.String("url", r.URL.String()),
zap.String("service", chi.URLParam(r, "serviceID")),
zap.String("project", chi.URLParam(r, "project")),
zap.String("branch", chi.URLParam(r, "branch")),
zap.String("commit", chi.URLParam(r, "commit")),
)
t, err := a.volumes.Get(
chi.URLParam(r, "serviceID"),
chi.URLParam(r, "project"),
chi.URLParam(r, "branch"),
chi.URLParam(r, "commit"),
zap.String("task ID", chi.URLParam(r, "taskID")),
)

t, err := a.storage.Get(chi.URLParam(r, "taskID"))
if err != nil {
l.Warn("Task get error", zap.Error(err))
if os.IsNotExist(err) {
Expand All @@ -134,9 +162,10 @@ func (a *Application) TaskHandler(w http.ResponseWriter, r *http.Request) {
}
return
}

err = json.NewEncoder(w).Encode(t)
if err != nil {
l.Error("Json encoding error", zap.Error(err))
panic(err)
return
}
}
4 changes: 2 additions & 2 deletions application/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// VolumesHandler expose volumes of a task
func (a *Application) VolumesHandler(basePathLen int) http.HandlerFunc {
func (a *Application) VolumesHandler(basePathLen int, latest bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := a.logger.With(
zap.String("url", r.URL.String()),
Expand All @@ -28,7 +28,7 @@ func (a *Application) VolumesHandler(basePathLen int) http.HandlerFunc {
branch := chi.URLParam(r, "branch")
commit := chi.URLParam(r, "commit")

t, err := a.storage.GetByCommit(service, project, branch, commit, false)
t, err := a.storage.GetByCommit(service, project, branch, commit, latest)
if err != nil {
l.Error("Get task", zap.Error(err))
w.WriteHeader(http.StatusNotFound)
Expand Down
15 changes: 8 additions & 7 deletions badge/badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"github.com/narqo/go-badge"
)

var colors = statusToColors{
// Colors is used to harmonize status colors for all badges
var Colors = statusToColors{
c: map[task.State]badge.Color{
// blue - lapis
task.Ready: "#2832C2",
Expand All @@ -20,8 +21,8 @@ var colors = statusToColors{
task.Running: "#DD571C",
// red - ruby
task.Failed: "#900603",
// green - emerald
task.Done: "#028A0F",
// green
task.Done: "#4ec820",
},
// blue
Default: "#527284",
Expand Down Expand Up @@ -67,7 +68,7 @@ func StatusBadge(s storage.Storage, latest bool) func(http.ResponseWriter, *http
t, err := s.GetByCommit(service, project, branch, commit, latest)

if t == nil || err != nil {
err = writeBadge("status", "?!", colors.Default, w)
err = WriteBadge("status", "?!", Colors.Default, w)
if err != nil {
panic(err)
}
Expand All @@ -79,15 +80,15 @@ func StatusBadge(s storage.Storage, latest bool) func(http.ResponseWriter, *http
return
}

writeBadge(fmt.Sprintf("status : %s", service), t.State.String(), colors.Get(t.State), w)
WriteBadge(fmt.Sprintf("status : %s", service), t.State.String(), Colors.Get(t.State), w)
if err != nil {
panic(err)
}
}
}

// writeBadge is a wrapper use to write a badge into an http response
func writeBadge(label string, content string, color badge.Color, w http.ResponseWriter) error {
// WriteBadge is a wrapper use to write a badge into an http response
func WriteBadge(label string, content string, color badge.Color, w http.ResponseWriter) error {
w.Header().Set("content-type", "image/svg+xml")
return badge.Render(label, content, color, w)
}
75 changes: 0 additions & 75 deletions volumes/volumes.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package volumes

import (
"encoding/json"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"sort"

"github.com/factorysh/microdensity/task"
"go.uber.org/zap"
Expand Down Expand Up @@ -60,79 +57,7 @@ func (v *Volumes) Create(t *task.Task) error {
return err
}

func (v *Volumes) Get(service, project, branch, commit string) (*task.Task, error) {
p := v.Path(service, project, branch)
l := v.logger.With(
zap.String("root", v.root),
zap.String("service", service),
zap.String("project", project),
zap.String("branch", branch),
zap.String("commit", commit),
zap.String("path", p),
)
_, err := os.Stat(p)
if err != nil {
if os.IsNotExist(err) {
l.Warn("Volume not found")
} else {
l.Error("Stat error", zap.Error(err))
}
return nil, err
}

commits, err := ioutil.ReadDir(p)
if err != nil {
l.Error("Readall error", zap.Error(err))
return nil, err
}
for _, com := range commits {
l = l.With(zap.String("commit", com.Name()))
f, err := os.OpenFile(v.Path(service, project, branch, com.Name(), "task.json"), os.O_RDONLY, 0)
if err != nil {
l.Error("open commit file", zap.Error(err))
return nil, err
}
var t task.Task
err = json.NewDecoder(f).Decode(&t)
if err != nil {
l.Error("JSON decode", zap.Error(err))
return nil, err
}
if t.Commit == commit {
return &t, nil
}
}
return nil, nil
}

// Path will return the full path for this volume
func (v *Volumes) Path(elem ...string) string {
return filepath.Join(v.root, filepath.Join(elem...))
}

// ByProjectByBranch returns all subvolumes for a specific branch of a project
func (v *Volumes) ByProjectByBranch(project string, branch string) ([]string, error) {
vols, err := ioutil.ReadDir(v.Path(project, branch))
if err != nil || !containsFiles(vols) {
v.logger.Error("by project by branch",
zap.String("project", project),
zap.String("brnach", branch),
zap.Error(err),
)
return nil, err
}

var res []string
for _, run := range vols {
if run.IsDir() {
res = append(res, v.Path(project, branch, run.Name()))
}
}
sort.Strings(res)

return res, nil
}

func containsFiles(files []fs.FileInfo) bool {
return len(files) > 0
}
Loading