Skip to content

Commit

Permalink
Merge pull request #82 from fluxcd/fix/test-repo-id
Browse files Browse the repository at this point in the history
Verify repository id in git notifiers
  • Loading branch information
Philip Laine authored Nov 17, 2020
2 parents 97184e8 + bec9f9a commit 21b45da
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 13 deletions.
11 changes: 6 additions & 5 deletions internal/notifier/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package notifier

import (
"errors"
"fmt"
"strings"

"github.com/fluxcd/pkg/recorder"
Expand All @@ -32,7 +33,7 @@ type Bitbucket struct {

func NewBitbucket(addr string, token string) (*Bitbucket, error) {
if len(token) == 0 {
return nil, errors.New("Bitbucket token cannot be empty")
return nil, errors.New("bitbucket token cannot be empty")
}

_, id, err := parseGitAddress(addr)
Expand All @@ -42,14 +43,14 @@ func NewBitbucket(addr string, token string) (*Bitbucket, error) {

comp := strings.Split(token, ":")
if len(comp) != 2 {
return nil, errors.New("Invalid token format, expected to be <user>:<password>")
return nil, errors.New("invalid token format, expected to be <user>:<password>")
}
username := comp[0]
password := comp[1]

comp = strings.Split(id, "/")
if len(comp) != 2 {
return nil, errors.New("Invalid bitbucket repository id")
return nil, fmt.Errorf("invalid repository id %q", id)
}
owner := comp[0]
repo := comp[1]
Expand All @@ -70,7 +71,7 @@ func (b Bitbucket) Post(event recorder.Event) error {

revString, ok := event.Metadata["revision"]
if !ok {
return errors.New("Missing revision metadata")
return errors.New("missing revision metadata")
}
rev, err := parseRevision(revString)
if err != nil {
Expand Down Expand Up @@ -110,6 +111,6 @@ func toBitbucketState(severity string) (string, error) {
case recorder.EventSeverityError:
return "FAILED", nil
default:
return "", errors.New("Can't convert to GitHub state")
return "", errors.New("can't convert to bitbucket state")
}
}
1 change: 0 additions & 1 deletion internal/notifier/bitbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
13 changes: 9 additions & 4 deletions internal/notifier/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package notifier
import (
"context"
"errors"
"fmt"
"strings"
"time"

Expand All @@ -35,20 +36,24 @@ 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)
if err != nil {
return nil, err
}

comp := strings.Split(id, "/")
if len(comp) != 2 {
return nil, fmt.Errorf("invalid repository id %q", 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],
Expand All @@ -65,7 +70,7 @@ func (g *GitHub) Post(event recorder.Event) error {

revString, ok := event.Metadata["revision"]
if !ok {
return errors.New("Missing revision metadata")
return errors.New("missing revision metadata")
}
rev, err := parseRevision(revString)
if err != nil {
Expand Down Expand Up @@ -100,6 +105,6 @@ func toGitHubState(severity string) (string, error) {
case recorder.EventSeverityError:
return "failure", nil
default:
return "", errors.New("Can't convert to GitHub state")
return "", errors.New("can't convert to github state")
}
}
40 changes: 40 additions & 0 deletions internal/notifier/github_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 3 additions & 3 deletions internal/notifier/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -61,7 +61,7 @@ func (g *GitLab) Post(event recorder.Event) error {

revString, ok := event.Metadata["revision"]
if !ok {
return errors.New("Missing revision metadata")
return errors.New("missing revision metadata")
}
rev, err := parseRevision(revString)
if err != nil {
Expand Down Expand Up @@ -94,6 +94,6 @@ func toGitLabState(severity string) (gitlab.BuildStateValue, error) {
case recorder.EventSeverityError:
return gitlab.Failed, nil
default:
return "", errors.New("Can't convert to GitLab state")
return "", errors.New("can't convert to gitlab state")
}
}
47 changes: 47 additions & 0 deletions internal/notifier/gitlab_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 21b45da

Please sign in to comment.