1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-18 08:26:45 +02:00
woodpecker/server/handler/branch.go
2014-06-12 16:41:04 -07:00

53 lines
1.3 KiB
Go

package handler
import (
"encoding/json"
"net/http"
"github.com/drone/drone/server/database"
"github.com/drone/drone/server/session"
"github.com/gorilla/pat"
)
type BranchHandler struct {
perms database.PermManager
repos database.RepoManager
commits database.CommitManager
sess session.Session
}
func NewBranchHandler(repos database.RepoManager, commits database.CommitManager, perms database.PermManager, sess session.Session) *BranchHandler {
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))
}