Add repo branches endpoint (#481)

* add repo branches endpoint

* add branches func to remotes
This commit is contained in:
Anbraten 2021-10-27 02:47:55 +02:00 committed by GitHub
parent 63a93087a1
commit d35f5158bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 125 additions and 0 deletions

View file

@ -170,6 +170,20 @@ func GetRepoPermissions(c *gin.Context) {
c.JSON(http.StatusOK, perm)
}
func GetRepoBranches(c *gin.Context) {
repo := session.Repo(c)
user := session.User(c)
r := remote.FromContext(c)
branches, err := r.Branches(c, user, repo)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, branches)
}
func DeleteRepo(c *gin.Context) {
remove, _ := strconv.ParseBool(c.Query("remove"))
r := remote.FromContext(c)

View file

@ -275,6 +275,12 @@ func (c *config) Netrc(u *model.User, r *model.Repo) (*model.Netrc, error) {
}, nil
}
// Branches returns the names of all branches for the named repository.
func (c *config) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]string, error) {
// TODO: fetch all branches
return []string{r.Branch}, nil
}
// Hook parses the incoming Bitbucket hook and returns the Repository and
// Build details. If the hook is unsupported nil values are returned.
func (c *config) Hook(req *http.Request) (*model.Repo, *model.Build, error) {

View file

@ -225,6 +225,12 @@ func (c *Config) Activate(ctx context.Context, u *model.User, r *model.Repo, lin
return client.CreateHook(r.Owner, r.Name, link)
}
// Branches returns the names of all branches for the named repository.
func (c *Config) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]string, error) {
// TODO: fetch all branches
return []string{r.Branch}, nil
}
func (c *Config) Deactivate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
client := internal.NewClientWithToken(ctx, c.URL, c.Consumer, u.Token)
return client.DeleteHook(r.Owner, r.Name, link)

View file

@ -276,6 +276,12 @@ func (c *Coding) Deactivate(ctx context.Context, u *model.User, r *model.Repo, l
return c.newClient(ctx, u).RemoveWebhook(r.Owner, r.Name, link)
}
// Branches returns the names of all branches for the named repository.
func (c *Coding) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]string, error) {
// TODO: fetch all branches
return []string{r.Branch}, nil
}
// Hook parses the post-commit hook from the Request body and returns the
// required data in a standard format.
func (c *Coding) Hook(r *http.Request) (*model.Repo, *model.Build, error) {

View file

@ -424,6 +424,25 @@ func (c *Gitea) Deactivate(ctx context.Context, u *model.User, r *model.Repo, li
return nil
}
// Branches returns the names of all branches for the named repository.
func (c *Gitea) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]string, error) {
client, err := c.newClientToken(ctx, u.Token)
if err != nil {
return nil, err
}
giteaBranches, _, err := client.ListRepoBranches(r.Owner, r.Name, gitea.ListRepoBranchesOptions{})
if err != nil {
return nil, err
}
branches := make([]string, 0)
for _, branch := range giteaBranches {
branches = append(branches, branch.Name)
}
return branches, nil
}
// Hook parses the incoming Gitea hook and returns the Repository and Build
// details. If the hook is unsupported nil values are returned.
func (c *Gitea) Hook(r *http.Request) (*model.Repo, *model.Build, error) {

View file

@ -505,6 +505,22 @@ func (c *client) Activate(ctx context.Context, u *model.User, r *model.Repo, lin
return err
}
// Branches returns the names of all branches for the named repository.
func (c *client) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]string, error) {
client := c.newClientToken(ctx, u.Token)
githubBranches, _, err := client.Repositories.ListBranches(ctx, r.Owner, r.Name, &github.BranchListOptions{})
if err != nil {
return nil, err
}
branches := make([]string, 0)
for _, branch := range githubBranches {
branches = append(branches, *branch.Name)
}
return branches, nil
}
// Hook parses the post-commit hook from the Request body
// and returns the required data in a standard format.
func (c *client) Hook(r *http.Request) (*model.Repo, *model.Build, error) {

View file

@ -434,6 +434,30 @@ func (g *Gitlab) Deactivate(ctx context.Context, user *model.User, repo *model.R
return err
}
// Branches returns the names of all branches for the named repository.
func (g *Gitlab) Branches(ctx context.Context, user *model.User, repo *model.Repo) ([]string, error) {
client, err := newClient(g.URL, user.Token, g.SkipVerify)
if err != nil {
return nil, err
}
repo_, err := g.getProject(ctx, client, repo.Owner, repo.Name)
if err != nil {
return nil, err
}
gitlabBranches, _, err := client.Branches.ListBranches(repo_.ID, &gitlab.ListBranchesOptions{}, gitlab.WithContext(ctx))
if err != nil {
return nil, err
}
branches := make([]string, 0)
for _, branch := range gitlabBranches {
branches = append(branches, branch.Name)
}
return branches, nil
}
// Hook parses the post-commit hook from the Request body
// and returns the required data in a standard format.
func (g *Gitlab) Hook(req *http.Request) (*model.Repo, *model.Build, error) {

View file

@ -256,6 +256,12 @@ func (c *client) Deactivate(ctx context.Context, u *model.User, r *model.Repo, l
return nil
}
// Branches returns the names of all branches for the named repository.
func (c *client) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]string, error) {
// TODO: fetch all branches
return []string{r.Branch}, nil
}
// Hook parses the incoming Gogs hook and returns the Repository and Build
// details. If the hook is unsupported nil values are returned.
func (c *client) Hook(r *http.Request) (*model.Repo, *model.Build, error) {

View file

@ -52,6 +52,29 @@ func (_m *Remote) Auth(ctx context.Context, token string, secret string) (string
return r0, r1
}
// Branches provides a mock function with given fields: ctx, u, r
func (_m *Remote) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]string, error) {
ret := _m.Called(ctx, u, r)
var r0 []string
if rf, ok := ret.Get(0).(func(context.Context, *model.User, *model.Repo) []string); ok {
r0 = rf(ctx, u, r)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]string)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, *model.User, *model.Repo) error); ok {
r1 = rf(ctx, u, r)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// Deactivate provides a mock function with given fields: ctx, u, r, link
func (_m *Remote) Deactivate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
ret := _m.Called(ctx, u, r, link)

View file

@ -70,6 +70,9 @@ type Remote interface {
// post-commit hooks matching the given link.
Deactivate(ctx context.Context, u *model.User, r *model.Repo, link string) error
// Branches returns the names of all branches for the named repository.
Branches(ctx context.Context, u *model.User, r *model.Repo) ([]string, error)
// Hook parses the post-commit hook from the Request body and returns the
// required data in a standard format.
Hook(r *http.Request) (*model.Repo, *model.Build, error)

View file

@ -57,6 +57,8 @@ func apiRoutes(e *gin.Engine) {
repo.POST("", session.MustRepoAdmin(), api.PostRepo)
repo.GET("", api.GetRepo)
repo.GET("/branches", api.GetRepoBranches)
repo.GET("/builds", api.GetBuilds)
repo.GET("/builds/:number", api.GetBuild)