Skip to content

Commit

Permalink
fix: invalid error on empty repo collabs (#466)
Browse files Browse the repository at this point in the history
Propagate a more informative error when adding an existing collaborator.
Ignore webhooks default branch git reference not found errors because
the repo won't have a default branch when it's empty.

Fixes: #464
  • Loading branch information
aymanbagabas authored Feb 5, 2024
1 parent d119d53 commit 047b6a7
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 11 deletions.
4 changes: 4 additions & 0 deletions pkg/backend/collab.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (d *Backend) AddCollaborator(ctx context.Context, repo string, username str
return d.store.AddCollabByUsernameAndRepo(ctx, tx, username, repo, level)
}),
); err != nil {
if errors.Is(err, db.ErrDuplicateKey) {
return proto.ErrCollaboratorExist
}

return err
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/proto/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ var (
ErrTokenExpired = errors.New("token expired")
// ErrCollaboratorNotFound is returned when a collaborator is not found.
ErrCollaboratorNotFound = errors.New("collaborator not found")
// ErrCollaboratorExist is returned when a collaborator already exists.
ErrCollaboratorExist = errors.New("collaborator already exists")
)
2 changes: 1 addition & 1 deletion pkg/webhook/branch_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func NewBranchTagEvent(ctx context.Context, user proto.User, repo proto.Reposito

payload.Repository.Owner.ID = owner.ID
payload.Repository.Owner.Username = owner.Username
payload.Repository.DefaultBranch, err = proto.RepositoryDefaultBranch(repo)
payload.Repository.DefaultBranch, err = getDefaultBranch(repo)
if err != nil {
return BranchTagEvent{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhook/collaborator.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func NewCollaboratorEvent(ctx context.Context, user proto.User, repo proto.Repos

payload.Repository.Owner.ID = owner.ID
payload.Repository.Owner.Username = owner.Username
payload.Repository.DefaultBranch, err = proto.RepositoryDefaultBranch(repo)
payload.Repository.DefaultBranch, err = getDefaultBranch(repo)
if err != nil {
return CollaboratorEvent{}, err
}
Expand Down
9 changes: 2 additions & 7 deletions pkg/webhook/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package webhook

import (
"context"
"errors"
"fmt"

gitm "github.com/aymanbagabas/git-module"
Expand Down Expand Up @@ -75,12 +74,8 @@ func NewPushEvent(ctx context.Context, user proto.User, repo proto.Repository, r
return PushEvent{}, err
}

payload.Repository.DefaultBranch, err = proto.RepositoryDefaultBranch(repo)
// XXX: we check for ErrReferenceNotExist here because we don't want to
// return an error if the repo is an empty repo.
// This means that the repo doesn't have a default branch yet and this is
// the first push to it.
if err != nil && !errors.Is(err, git.ErrReferenceNotExist) {
payload.Repository.DefaultBranch, err = getDefaultBranch(repo)
if err != nil {
return PushEvent{}, err
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/webhook/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ func NewRepositoryEvent(ctx context.Context, user proto.User, repo proto.Reposit

payload.Repository.Owner.ID = owner.ID
payload.Repository.Owner.Username = owner.Username
payload.Repository.DefaultBranch, _ = proto.RepositoryDefaultBranch(repo)
payload.Repository.DefaultBranch, err = getDefaultBranch(repo)
if err != nil {
return RepositoryEvent{}, err
}

return payload, nil
}
16 changes: 16 additions & 0 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"

"github.com/charmbracelet/soft-serve/git"
"github.com/charmbracelet/soft-serve/pkg/db"
"github.com/charmbracelet/soft-serve/pkg/db/models"
"github.com/charmbracelet/soft-serve/pkg/proto"
"github.com/charmbracelet/soft-serve/pkg/store"
"github.com/charmbracelet/soft-serve/pkg/utils"
"github.com/charmbracelet/soft-serve/pkg/version"
Expand Down Expand Up @@ -142,3 +145,16 @@ func SendEvent(ctx context.Context, payload EventPayload) error {
func repoURL(publicURL string, repo string) string {
return fmt.Sprintf("%s/%s.git", publicURL, utils.SanitizeRepo(repo))
}

func getDefaultBranch(repo proto.Repository) (string, error) {
branch, err := proto.RepositoryDefaultBranch(repo)
// XXX: we check for ErrReferenceNotExist here because we don't want to
// return an error if the repo is an empty repo.
// This means that the repo doesn't have a default branch yet and this is
// the first push to it.
if err != nil && !errors.Is(err, git.ErrReferenceNotExist) {
return "", err
}

return branch, nil
}
12 changes: 12 additions & 0 deletions testscript/testdata/repo-collab.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ soft repo collab remove test foo
soft repo collab list test
! stdout .

# create empty repo
soft repo create empty '-d "empty repo"'

# add collab
soft repo collab add empty foo
# add collab again
# test issue #464 https://github.com/charmbracelet/soft-serve/issues/464
! soft repo collab add empty foo
stderr '.*already exists.*'
# a placeholder to reset stderr
soft help

# stop the server
[windows] stopserver
[windows] ! stderr .
2 changes: 1 addition & 1 deletion testscript/testdata/soft-browse.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[windows] skip

# clone repo
git clone https://github.com/charmbracelet/catwalk.git catwalk
#git clone https://github.com/charmbracelet/catwalk.git catwalk

# run soft browse
# disable this temporarily
Expand Down

0 comments on commit 047b6a7

Please sign in to comment.