added utf-8 support for branch name check and minor improvements

This commit is contained in:
Enver Bisevac 2023-08-15 18:46:56 +02:00
parent 77aad698dc
commit ef1f04eb5b
4 changed files with 18 additions and 12 deletions

View File

@ -10,6 +10,7 @@ import (
"fmt"
"io"
"github.com/harness/gitness/gitrpc/check"
"github.com/harness/gitness/gitrpc/rpc"
"github.com/rs/zerolog/log"
@ -75,6 +76,11 @@ func (c *Client) CreateBranch(ctx context.Context, params *CreateBranchParams) (
if params == nil {
return nil, ErrNoParamsProvided
}
if err := check.BranchName(params.BranchName); err != nil {
return nil, ErrInvalidArgumentf(err.Error())
}
resp, err := c.refService.CreateBranch(ctx, &rpc.CreateBranchRequest{
Base: mapToRPCWriteRequest(params.WriteParams),
Target: params.Target,

View File

@ -32,11 +32,11 @@ var refnameDisposition = [256]byte{
}
func BranchName(branch string) error {
lock := ".lock"
last := '\x00'
const lock = ".lock"
last := byte('\x00')
for i, c := range branch {
ch := c & 255
for i := 0; i < len(branch); i++ {
ch := branch[i] & 255
disp := refnameDisposition[ch]
switch disp {
@ -80,7 +80,7 @@ out:
return fmt.Errorf("branch '%s' cannot start with '.'", branch)
}
if strings.HasSuffix(branch, lock) {
return fmt.Errorf("branch '%s' cannot ends with '%s'", branch, lock)
return fmt.Errorf("branch '%s' cannot end with '%s'", branch, lock)
}
return nil
}

View File

@ -29,6 +29,13 @@ func TestBranchName(t *testing.T) {
},
wantErr: false,
},
{
name: "happy path, test utf-8 chars",
args: args{
branch: "eb/new\u2318branch",
},
wantErr: false,
},
{
name: "branch name empty should return error",
args: args{

View File

@ -9,9 +9,7 @@ import (
"fmt"
"github.com/harness/gitness/gitrpc"
"github.com/harness/gitness/gitrpc/check"
apiauth "github.com/harness/gitness/internal/api/auth"
"github.com/harness/gitness/internal/api/usererror"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/types/enum"
)
@ -42,11 +40,6 @@ func (c *Controller) CreateBranch(ctx context.Context, session *auth.Session,
in.Target = repo.DefaultBranch
}
err = check.BranchName(in.Name)
if err != nil {
return nil, usererror.BadRequest(err.Error())
}
writeParams, err := CreateRPCWriteParams(ctx, c.urlProvider, session, repo)
if err != nil {
return nil, fmt.Errorf("failed to create RPC write params: %w", err)