2014-06-04 21:25:38 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
"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 {
|
2014-06-12 23:41:04 +00:00
|
|
|
perms database.PermManager
|
|
|
|
repos database.RepoManager
|
|
|
|
commits database.CommitManager
|
2014-06-04 21:25:38 +00:00
|
|
|
sess session.Session
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:41:04 +00:00
|
|
|
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))
|
|
|
|
}
|