Verify repository id in git notifiers
The different git notifiers need repository ids in different formats. On top of that some git providers support repositories in subgroups while others do not support nesting. Each notifier needs test to verify that they support their specific functionality. Signed-off-by: Philip Laine <philip.laine@xenit.se>
This commit is contained in:
parent
97184e8131
commit
e812068c63
|
|
@ -18,6 +18,7 @@ package notifier
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/fluxcd/pkg/recorder"
|
||||
|
|
@ -49,7 +50,7 @@ func NewBitbucket(addr string, token string) (*Bitbucket, error) {
|
|||
|
||||
comp = strings.Split(id, "/")
|
||||
if len(comp) != 2 {
|
||||
return nil, errors.New("Invalid bitbucket repository id")
|
||||
return nil, fmt.Errorf("Invalid repository id '%s'", id)
|
||||
}
|
||||
owner := comp[0]
|
||||
repo := comp[1]
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ import (
|
|||
|
||||
func TestNewBitbucketBasic(t *testing.T) {
|
||||
b, err := NewBitbucket("https://bitbucket.org/foo/bar", "foo:bar")
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, b.Owner, "foo")
|
||||
assert.Equal(t, b.Repo, "bar")
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ package notifier
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
|
@ -35,7 +36,7 @@ type GitHub struct {
|
|||
|
||||
func NewGitHub(addr string, token string) (*GitHub, error) {
|
||||
if len(token) == 0 {
|
||||
return nil, errors.New("GitHub token cannot be empty")
|
||||
return nil, errors.New("GitHub token cannot be empty")
|
||||
}
|
||||
|
||||
_, id, err := parseGitAddress(addr)
|
||||
|
|
@ -43,12 +44,16 @@ func NewGitHub(addr string, token string) (*GitHub, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
comp := strings.Split(id, "/")
|
||||
if len(comp) != 2 {
|
||||
return nil, fmt.Errorf("Invalid repository id '%s'", id)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
|
||||
tc := oauth2.NewClient(ctx, ts)
|
||||
client := github.NewClient(tc)
|
||||
|
||||
comp := strings.Split(id, "/")
|
||||
return &GitHub{
|
||||
Owner: comp[0],
|
||||
Repo: comp[1],
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
Copyright 2020 The Flux authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package notifier
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewGitHubBasic(t *testing.T) {
|
||||
g, err := NewGitHub("https://github.com/foo/bar", "foobar")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, g.Owner, "foo")
|
||||
assert.Equal(t, g.Repo, "bar")
|
||||
}
|
||||
|
||||
func TestNewGitHubInvalidUrl(t *testing.T) {
|
||||
_, err := NewGitHub("https://github.com/foo/bar/baz", "foobar")
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestNewGitHubEmptyToken(t *testing.T) {
|
||||
_, err := NewGitHub("https://github.com/foo/bar", "")
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
|
@ -30,7 +30,7 @@ type GitLab struct {
|
|||
|
||||
func NewGitLab(addr string, token string) (*GitLab, error) {
|
||||
if len(token) == 0 {
|
||||
return nil, errors.New("GitLab token cannot be empty")
|
||||
return nil, errors.New("GitLab token cannot be empty")
|
||||
}
|
||||
|
||||
host, id, err := parseGitAddress(addr)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
Copyright 2020 The Flux authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package notifier
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewGitLabBasic(t *testing.T) {
|
||||
g, err := NewGitLab("https://gitlab.com/foo/bar", "foobar")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, g.Id, "foo/bar")
|
||||
}
|
||||
|
||||
func TestNewGitLabSubgroups(t *testing.T) {
|
||||
g, err := NewGitLab("https://gitlab.com/foo/bar/baz", "foobar")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, g.Id, "foo/bar/baz")
|
||||
}
|
||||
|
||||
func TestNewGitLabSelfHosted(t *testing.T) {
|
||||
g, err := NewGitLab("https://example.com/foo/bar", "foo:bar")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, g.Id, "foo/bar")
|
||||
assert.Equal(t, g.Client.BaseURL().Host, "example.com")
|
||||
}
|
||||
|
||||
func TestNewGitLabEmptyToken(t *testing.T) {
|
||||
_, err := NewGitLab("https://gitlab.com/foo/bar", "")
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
Loading…
Reference in New Issue