init
This commit is contained in:
commit
c0f618a9db
133 files changed
+55621
No files matched your search
@@ -0,0 +1,100 @@
|
||||
package subscription
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var allFeatures = map[string]bool{
|
||||
"merges": true,
|
||||
"jobs": true,
|
||||
"issues": true,
|
||||
"pushes": true,
|
||||
"issue_comments": true,
|
||||
"merge_request_comments": true,
|
||||
"pipeline": true,
|
||||
"tag": true,
|
||||
"pull_reviews": true,
|
||||
"confidential_issues": true,
|
||||
// "label:": true,//particular case for label:XXX
|
||||
}
|
||||
|
||||
type Subscription struct {
|
||||
ChannelID string
|
||||
CreatorID string
|
||||
Features string
|
||||
Repository string
|
||||
}
|
||||
|
||||
func New(channelID, creatorID, features, repository string) (*Subscription, error) {
|
||||
if strings.Contains(features, "label:") && len(strings.Split(features, "\"")) < 3 {
|
||||
return nil, errors.New("the label is formatted incorrectly")
|
||||
}
|
||||
if strings.Contains(features, "label:") && len(strings.Split(features, "\"")) > 3 {
|
||||
return nil, errors.New("can't add multiple labels on the same subscription")
|
||||
}
|
||||
|
||||
badFeatures := make([]string, 0)
|
||||
for _, feature := range strings.Split(features, ",") {
|
||||
if _, ok := allFeatures[feature]; !strings.HasPrefix(feature, "label:") && !ok {
|
||||
badFeatures = append(badFeatures, feature)
|
||||
}
|
||||
}
|
||||
if len(badFeatures) > 0 {
|
||||
return nil, errors.Errorf("unknown features %s", strings.Join(badFeatures, ","))
|
||||
}
|
||||
return &Subscription{
|
||||
ChannelID: channelID,
|
||||
CreatorID: creatorID,
|
||||
Features: features,
|
||||
Repository: repository,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Subscription) Merges() bool {
|
||||
return strings.Contains(s.Features, "merges")
|
||||
}
|
||||
|
||||
func (s *Subscription) Jobs() bool {
|
||||
return strings.Contains(s.Features, "jobs")
|
||||
}
|
||||
|
||||
func (s *Subscription) Issues() bool {
|
||||
return strings.Contains(s.Features, "issues")
|
||||
}
|
||||
|
||||
func (s *Subscription) ConfidentialIssues() bool {
|
||||
return strings.Contains(s.Features, "confidential_issues")
|
||||
}
|
||||
|
||||
func (s *Subscription) Pushes() bool {
|
||||
return strings.Contains(s.Features, "pushes")
|
||||
}
|
||||
|
||||
func (s *Subscription) IssueComments() bool {
|
||||
return strings.Contains(s.Features, "issue_comments")
|
||||
}
|
||||
|
||||
func (s *Subscription) MergeRequestComments() bool {
|
||||
return strings.Contains(s.Features, "merge_request_comments")
|
||||
}
|
||||
|
||||
func (s *Subscription) Pipeline() bool {
|
||||
return strings.Contains(s.Features, "pipeline")
|
||||
}
|
||||
|
||||
func (s *Subscription) Tag() bool {
|
||||
return strings.Contains(s.Features, "tag")
|
||||
}
|
||||
|
||||
func (s *Subscription) PullReviews() bool {
|
||||
return strings.Contains(s.Features, "pull_reviews")
|
||||
}
|
||||
|
||||
func (s *Subscription) Label() string {
|
||||
if !strings.Contains(s.Features, "label:") {
|
||||
return ""
|
||||
}
|
||||
return strings.Split(s.Features, "\"")[1]
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package subscription
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewSubscriptionSimple(t *testing.T) {
|
||||
s, err := New("", "", "issues", "")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, &Subscription{Features: "issues"}, s)
|
||||
assert.True(t, s.Issues())
|
||||
assert.False(t, s.Merges())
|
||||
assert.False(t, s.Pushes())
|
||||
assert.False(t, s.IssueComments())
|
||||
assert.False(t, s.MergeRequestComments())
|
||||
assert.False(t, s.Pipeline())
|
||||
assert.False(t, s.Tag())
|
||||
assert.False(t, s.PullReviews())
|
||||
assert.Equal(t, "", s.Label())
|
||||
}
|
||||
|
||||
func TestNewSubscriptionMultiple(t *testing.T) {
|
||||
s, err := New("", "", "issues,merges", "")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, &Subscription{Features: "issues,merges"}, s)
|
||||
assert.True(t, s.Issues())
|
||||
assert.True(t, s.Merges())
|
||||
assert.False(t, s.Pushes())
|
||||
assert.False(t, s.IssueComments())
|
||||
assert.False(t, s.MergeRequestComments())
|
||||
assert.False(t, s.Pipeline())
|
||||
assert.False(t, s.Tag())
|
||||
assert.False(t, s.PullReviews())
|
||||
assert.Equal(t, "", s.Label())
|
||||
}
|
||||
|
||||
func TestNewSubscriptionAll(t *testing.T) {
|
||||
s, err := New("", "", "issues,merges,pushes,issue_comments,merge_request_comments,pipeline,tag,pull_reviews", "")
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, &Subscription{Features: "issues,merges,pushes,issue_comments,merge_request_comments,pipeline,tag,pull_reviews"}, s)
|
||||
assert.True(t, s.Issues())
|
||||
assert.True(t, s.Merges())
|
||||
assert.True(t, s.Pushes())
|
||||
assert.True(t, s.IssueComments())
|
||||
assert.True(t, s.MergeRequestComments())
|
||||
assert.True(t, s.Pipeline())
|
||||
assert.True(t, s.Tag())
|
||||
assert.True(t, s.PullReviews())
|
||||
assert.Equal(t, "", s.Label())
|
||||
}
|
||||
|
||||
func TestNewSubscriptionUnknown(t *testing.T) {
|
||||
s, err := New("", "", "unknown,merges,missing", "")
|
||||
assert.Nil(t, s)
|
||||
assert.Equal(t, err.Error(), "unknown features unknown,missing")
|
||||
}
|
||||
|
||||
func TestNewSubscriptionLabel(t *testing.T) {
|
||||
s, err := New("", "", `issues,label:"test",merges`, "")
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, s.Merges())
|
||||
assert.True(t, s.Issues())
|
||||
assert.Equal(t, "test", s.Label())
|
||||
}
|
||||
|
||||
func TestNewSubscriptionBadFormated(t *testing.T) {
|
||||
s, err := New("", "", `label:"`, "")
|
||||
assert.Nil(t, s)
|
||||
assert.Equal(t, err.Error(), "the label is formatted incorrectly")
|
||||
}
|
||||
|
||||
func TestNewSubscriptionMultipleLabel(t *testing.T) {
|
||||
s, err := New("", "", `label:"1",label:"2"`, "")
|
||||
assert.Nil(t, s)
|
||||
assert.Equal(t, err.Error(), "can't add multiple labels on the same subscription")
|
||||
}
|
||||
Reference in new issue
Block a user