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
91 changes: 47 additions & 44 deletions application/badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,56 @@ import (
"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
}
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))
p := filepath.Join(a.storage.GetVolumePath(t), "/data", fmt.Sprintf("%s.badge", bdg))

_, err = os.Stat(p)
if err != nil {
l.Warn("Task get error", zap.Error(err))
if os.IsNotExist(err) {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusBadRequest)
_, err = os.Stat(p)
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
}
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
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
}
37 changes: 0 additions & 37 deletions volumes/volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package volumes

import (
"os"
"strings"
"testing"
"time"

"github.com/factorysh/microdensity/task"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -42,36 +38,3 @@ func TestNewVolumes(t *testing.T) {
})
}
}

func TestListByProjectByBranch(t *testing.T) {
defer cleanDir()

v, err := New(testRootDir)
assert.NoError(t, err)
var first uuid.UUID
var last uuid.UUID
for i := 0; i < 10; i++ {
id, err := uuid.NewUUID()
assert.NoError(t, err)
c := time.Now()
if first == uuid.Nil {
first = id
}
last = id
err = v.Create(&task.Task{
Project: "group%2Fproject",
Branch: "master",
Id: id,
Creation: c,
Commit: "70ea687225ea4869311c500a954e8cc5e687e608",
})
assert.NoError(t, err)
}

dirs, err := v.ByProjectByBranch("group%2Fproject", "master")
assert.NoError(t, err)
assert.Len(t, dirs, 10, "one folder should be found")
assert.True(t, strings.HasSuffix(dirs[0], first.String()), dirs[0])
assert.True(t, strings.HasSuffix(dirs[9], last.String()))
cleanDir()
}