woodpecker/server/handler/branch.go

53 lines
1.3 KiB
Go
Raw Normal View History

2014-06-04 21:25:38 +00:00
package handler
import (
"encoding/json"
"net/http"
"github.com/drone/drone/server/database"
2014-06-04 21:25:38 +00:00
"github.com/drone/drone/server/session"
"github.com/gorilla/pat"
)
type BranchHandler struct {
perms database.PermManager
repos database.RepoManager
commits database.CommitManager
2014-06-04 21:25:38 +00:00
sess session.Session
}
func NewBranchHandler(repos database.RepoManager, commits database.CommitManager, perms database.PermManager, sess session.Session) *BranchHandler {
2014-06-04 21:25:38 +00:00
return &BranchHandler{perms, repos, commits, sess}
}
// GetBranches gets a list of all branches and their most recent commits.
// GET /v1/repos/{host}/{owner}/{name}/branches
func (h *BranchHandler) GetBranches(w http.ResponseWriter, r *http.Request) error {
var host, owner, name = parseRepo(r)
// get the user form the session.
user := h.sess.User(r)
// get the repository from the database.
repo, err := h.repos.FindName(host, owner, name)
if err != nil {
return notFound{err}
}
// user must have read access to the repository.
if ok, _ := h.perms.Read(user, repo); !ok {
return notFound{err}
}
branches, err := h.commits.ListBranches(repo.ID)
if err != nil {
return notFound{err}
}
return json.NewEncoder(w).Encode(branches)
}
func (h *BranchHandler) Register(r *pat.Router) {
r.Get("/v1/repos/{host}/{owner}/{name}/branches", errorHandler(h.GetBranches))
}