Branch list enhancements (#808)

* Allow users not logged in to access branches page
  (fixes a nil pointer derefernce)

* Add Gogs support for branches
This commit is contained in:
qwerty287 2022-02-26 17:36:00 +01:00 committed by GitHub
parent 40b5c6a320
commit ecc25395aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 5 deletions

View file

@ -423,7 +423,11 @@ func (c *Gitea) Deactivate(ctx context.Context, u *model.User, r *model.Repo, li
// 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)
token := ""
if u != nil {
token = u.Token
}
client, err := c.newClientToken(ctx, token)
if err != nil {
return nil, err
}

View file

@ -458,7 +458,11 @@ func (c *client) Activate(ctx context.Context, u *model.User, r *model.Repo, lin
// 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)
token := ""
if u != nil {
token = u.Token
}
client := c.newClientToken(ctx, token)
githubBranches, _, err := client.Repositories.ListBranches(ctx, r.Owner, r.Name, &github.BranchListOptions{})
if err != nil {

View file

@ -488,7 +488,11 @@ func (g *Gitlab) Deactivate(ctx context.Context, user *model.User, repo *model.R
// 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)
token := ""
if user != nil {
token = user.Token
}
client, err := newClient(g.URL, token, g.SkipVerify)
if err != nil {
return nil, err
}

View file

@ -262,8 +262,21 @@ func (c *client) Deactivate(ctx context.Context, u *model.User, r *model.Repo, l
// 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
token := ""
if u != nil {
token = u.Token
}
client := c.newClientToken(token)
gogsBranches, err := client.ListRepoBranches(r.Owner, r.Name)
if err != nil {
return nil, err
}
branches := make([]string, 0)
for _, branch := range gogsBranches {
branches = append(branches, branch.Name)
}
return branches, nil
}
// Hook parses the incoming Gogs hook and returns the Repository and Build