diff --git a/.circleci/config.yml b/.circleci/config.yml index 2969bf0..51a19fb 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,7 +3,7 @@ jobs: build: docker: - image: circleci/golang:1.13-node - working_directory: /go/src/github.com/Brightscout/mattermost-plugin-confluence + working_directory: /go/src/github.com/mattermost/mattermost-plugin-confluence steps: - checkout - restore_cache: @@ -27,7 +27,7 @@ jobs: release: docker: - image: cibuilds/github:0.12 - working_directory: /go/src/github.com/Brightscout/mattermost-plugin-confluence + working_directory: /go/src/github.com/mattermost/mattermost-plugin-confluence steps: - checkout - attach_workspace: diff --git a/README.md b/README.md index b8178d1..41292a0 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Mattermost Plugin Confluence [![CircleCI branch](https://img.shields.io/circleci/project/github/Brightscout/mattermost-plugin-confluence/master.svg)](https://circleci.com/gh/Brightscout/mattermost-plugin-confluence) +# Mattermost Plugin Confluence [![CircleCI branch](https://img.shields.io/circleci/project/github/mattermost/mattermost-plugin-confluence/master.svg)](https://circleci.com/gh/mattermost/mattermost-plugin-confluence) A Mattermost plugin for Confluence. Supports Confluence Cloud, Server and Data Center versions. @@ -25,7 +25,7 @@ Set up CircleCI to run the build job for each branch and build-and-release for e ## Installation -1. Go to the [releases page of this GitHub repository](https://github.com/Brightscout/mattermost-plugin-confluence/releases/latest) and download the latest release for your Mattermost server. +1. Go to the [releases page of this GitHub repository](https://github.com/mattermost/mattermost-plugin-confluence/releases/latest) and download the latest release for your Mattermost server. 2. Upload this file in the Mattermost **System Console > Plugins > Management** page to install the plugin. To learn more about how to upload a plugin, [see the documentation](https://docs.mattermost.com/administration/plugins.html#plugin-uploads). 3. You can configure the Plugin from **System Console > Plugins > Confluence**. diff --git a/go.mod b/go.mod index 80417d6..d5d8f0e 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/Brightscout/mattermost-plugin-confluence +module github.com/mattermost/mattermost-plugin-confluence go 1.13 diff --git a/plugin.json b/plugin.json index 29bb571..1dd3817 100644 --- a/plugin.json +++ b/plugin.json @@ -1,8 +1,8 @@ { "id": "com.mattermost.confluence", "name": "Confluence", - "description": "", - "version": "0.1.0", + "description": "Atlassian Confluence plugin for Mattermost.", + "version": "1.1.0", "min_server_version": "5.12.0", "server": { "executables": { diff --git a/server/command/command.go b/server/command/command.go index f1f8b23..1c49944 100644 --- a/server/command/command.go +++ b/server/command/command.go @@ -6,10 +6,10 @@ import ( "github.com/mattermost/mattermost-server/model" - "github.com/Brightscout/mattermost-plugin-confluence/server/config" - "github.com/Brightscout/mattermost-plugin-confluence/server/serializer" - "github.com/Brightscout/mattermost-plugin-confluence/server/service" - "github.com/Brightscout/mattermost-plugin-confluence/server/util" + "github.com/mattermost/mattermost-plugin-confluence/server/config" + "github.com/mattermost/mattermost-plugin-confluence/server/serializer" + "github.com/mattermost/mattermost-plugin-confluence/server/service" + "github.com/mattermost/mattermost-plugin-confluence/server/util" ) type HandlerFunc func(context *model.CommandArgs, args ...string) *model.CommandResponse diff --git a/server/controller/atlassian_connect.go b/server/controller/atlassian_connect.go index cb8dc5b..c4a2cc7 100644 --- a/server/controller/atlassian_connect.go +++ b/server/controller/atlassian_connect.go @@ -7,8 +7,8 @@ import ( "path" "path/filepath" - "github.com/Brightscout/mattermost-plugin-confluence/server/config" - "github.com/Brightscout/mattermost-plugin-confluence/server/util" + "github.com/mattermost/mattermost-plugin-confluence/server/config" + "github.com/mattermost/mattermost-plugin-confluence/server/util" ) var atlassianConnectJSON = &Endpoint{ diff --git a/server/controller/confluence_cloud.go b/server/controller/confluence_cloud.go index 8f3876b..4088ff0 100644 --- a/server/controller/confluence_cloud.go +++ b/server/controller/confluence_cloud.go @@ -5,9 +5,9 @@ import ( "github.com/gorilla/mux" - "github.com/Brightscout/mattermost-plugin-confluence/server/config" - "github.com/Brightscout/mattermost-plugin-confluence/server/serializer" - "github.com/Brightscout/mattermost-plugin-confluence/server/service" + "github.com/mattermost/mattermost-plugin-confluence/server/config" + "github.com/mattermost/mattermost-plugin-confluence/server/serializer" + "github.com/mattermost/mattermost-plugin-confluence/server/service" ) var confluenceCloudWebhook = &Endpoint{ diff --git a/server/controller/confluence_server.go b/server/controller/confluence_server.go index 6082248..17a23fd 100644 --- a/server/controller/confluence_server.go +++ b/server/controller/confluence_server.go @@ -3,9 +3,9 @@ package controller import ( "net/http" - "github.com/Brightscout/mattermost-plugin-confluence/server/config" - "github.com/Brightscout/mattermost-plugin-confluence/server/serializer" - "github.com/Brightscout/mattermost-plugin-confluence/server/service" + "github.com/mattermost/mattermost-plugin-confluence/server/config" + "github.com/mattermost/mattermost-plugin-confluence/server/serializer" + "github.com/mattermost/mattermost-plugin-confluence/server/service" ) var confluenceServerWebhook = &Endpoint{ diff --git a/server/controller/edit_subscription.go b/server/controller/edit_subscription.go index a11cadc..096abe7 100644 --- a/server/controller/edit_subscription.go +++ b/server/controller/edit_subscription.go @@ -6,9 +6,9 @@ import ( "github.com/gorilla/mux" "github.com/mattermost/mattermost-server/model" - "github.com/Brightscout/mattermost-plugin-confluence/server/config" - "github.com/Brightscout/mattermost-plugin-confluence/server/serializer" - "github.com/Brightscout/mattermost-plugin-confluence/server/service" + "github.com/mattermost/mattermost-plugin-confluence/server/config" + "github.com/mattermost/mattermost-plugin-confluence/server/serializer" + "github.com/mattermost/mattermost-plugin-confluence/server/service" ) var editChannelSubscription = &Endpoint{ diff --git a/server/controller/get_subscription.go b/server/controller/get_subscription.go index df02c5d..a4bf9f2 100644 --- a/server/controller/get_subscription.go +++ b/server/controller/get_subscription.go @@ -6,7 +6,7 @@ import ( "github.com/gorilla/mux" - "github.com/Brightscout/mattermost-plugin-confluence/server/service" + "github.com/mattermost/mattermost-plugin-confluence/server/service" ) var getChannelSubscription = &Endpoint{ diff --git a/server/controller/main.go b/server/controller/main.go index ea4f112..9714ba0 100644 --- a/server/controller/main.go +++ b/server/controller/main.go @@ -10,8 +10,8 @@ import ( "github.com/mattermost/mattermost-server/model" "github.com/pkg/errors" - "github.com/Brightscout/mattermost-plugin-confluence/server/config" - "github.com/Brightscout/mattermost-plugin-confluence/server/util" + "github.com/mattermost/mattermost-plugin-confluence/server/config" + "github.com/mattermost/mattermost-plugin-confluence/server/util" ) type Endpoint struct { diff --git a/server/controller/save_subscription.go b/server/controller/save_subscription.go index eb091bf..3f34863 100644 --- a/server/controller/save_subscription.go +++ b/server/controller/save_subscription.go @@ -6,9 +6,9 @@ import ( "github.com/gorilla/mux" "github.com/mattermost/mattermost-server/model" - "github.com/Brightscout/mattermost-plugin-confluence/server/config" - "github.com/Brightscout/mattermost-plugin-confluence/server/serializer" - "github.com/Brightscout/mattermost-plugin-confluence/server/service" + "github.com/mattermost/mattermost-plugin-confluence/server/config" + "github.com/mattermost/mattermost-plugin-confluence/server/serializer" + "github.com/mattermost/mattermost-plugin-confluence/server/service" ) const subscriptionSaveSuccess = "Your subscription has been saved." @@ -34,11 +34,6 @@ func handleSaveSubscription(w http.ResponseWriter, r *http.Request) { http.Error(w, "Could not decode request body", http.StatusBadRequest) return } - if errCode, vsErr := service.ValidateSpaceSubscription(subscription.(serializer.SpaceSubscription)); vsErr != nil { - config.Mattermost.LogError(vsErr.Error()) - http.Error(w, vsErr.Error(), errCode) - return - } } else if subscriptionType == serializer.SubscriptionTypePage { subscription, err = serializer.PageSubscriptionFromJSON(r.Body) if err != nil { @@ -46,15 +41,10 @@ func handleSaveSubscription(w http.ResponseWriter, r *http.Request) { http.Error(w, "Could not decode request body", http.StatusBadRequest) return } - if errCode, psErr := service.ValidatePageSubscription(subscription.(serializer.PageSubscription)); psErr != nil { - config.Mattermost.LogError(psErr.Error()) - http.Error(w, psErr.Error(), errCode) - return - } } - if sErr := service.SaveSubscription(subscription); sErr != nil { + if statusCode, sErr := service.SaveSubscription(subscription); sErr != nil { config.Mattermost.LogError(sErr.Error()) - http.Error(w, sErr.Error(), http.StatusBadRequest) + http.Error(w, sErr.Error(), statusCode) return } post := &model.Post{ diff --git a/server/plugin.go b/server/plugin.go index 4cd306d..20fadc9 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -10,11 +10,11 @@ import ( "github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/plugin" - "github.com/Brightscout/mattermost-plugin-confluence/server/command" - "github.com/Brightscout/mattermost-plugin-confluence/server/util" + "github.com/mattermost/mattermost-plugin-confluence/server/command" + "github.com/mattermost/mattermost-plugin-confluence/server/util" - "github.com/Brightscout/mattermost-plugin-confluence/server/config" - "github.com/Brightscout/mattermost-plugin-confluence/server/controller" + "github.com/mattermost/mattermost-plugin-confluence/server/config" + "github.com/mattermost/mattermost-plugin-confluence/server/controller" ) const ( diff --git a/server/plugin_test.go b/server/plugin_test.go index 149ee01..8b76446 100644 --- a/server/plugin_test.go +++ b/server/plugin_test.go @@ -11,9 +11,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" - "github.com/Brightscout/mattermost-plugin-confluence/server/config" - "github.com/Brightscout/mattermost-plugin-confluence/server/service" - "github.com/Brightscout/mattermost-plugin-confluence/server/util" + "github.com/mattermost/mattermost-plugin-confluence/server/config" + "github.com/mattermost/mattermost-plugin-confluence/server/service" ) const ( @@ -41,6 +40,7 @@ func TestExecuteCommand(t *testing.T) { for name, val := range map[string]struct { commandArgs *model.CommandArgs ephemeralMessage string + patchAPICalls func() }{ "empty command ": { commandArgs: &model.CommandArgs{Command: "/confluence", UserId: "abcdabcdabcdabcd", ChannelId: "testtesttesttest"}, @@ -53,6 +53,11 @@ func TestExecuteCommand(t *testing.T) { "unsubscribe command ": { commandArgs: &model.CommandArgs{Command: "/confluence unsubscribe \"abc\"", UserId: "abcdabcdabcdabcd", ChannelId: "testtesttesttest"}, ephemeralMessage: fmt.Sprintf(subscriptionDeleteSuccess, "abc"), + patchAPICalls: func() { + monkey.Patch(service.DeleteSubscription, func(channelID, alias string) error { + return nil + }) + }, }, "unsubscribe command no alias": { commandArgs: &model.CommandArgs{Command: "/confluence unsubscribe", UserId: "abcdabcdabcdabcd", ChannelId: "testtesttesttest"}, @@ -69,12 +74,10 @@ func TestExecuteCommand(t *testing.T) { post := args.Get(1).(*model.Post) assert.Equal(t, val.ephemeralMessage, post.Message) }).Once().Return(&model.Post{}) - monkey.Patch(service.DeleteSubscription, func(channelID, alias string) error { - return nil - }) - monkey.Patch(util.IsSystemAdmin, func(userID string) bool { - return false - }) + mockAPI.On("GetUser", mock.AnythingOfType("string")).Return(&model.User{Id: "123", Roles: ""}, nil) + if val.patchAPICalls != nil { + val.patchAPICalls() + } res, err := p.ExecuteCommand(&plugin.Context{}, val.commandArgs) assert.Nil(t, err) assert.NotNil(t, res) diff --git a/server/serializer/channel_subscription.go b/server/serializer/channel_subscription.go index 37293bf..aea8bed 100644 --- a/server/serializer/channel_subscription.go +++ b/server/serializer/channel_subscription.go @@ -17,6 +17,10 @@ const ( PageRemovedEvent = "page_removed" SubscriptionTypeSpace = "space_subscription" SubscriptionTypePage = "page_subscription" + + aliasAlreadyExist = "A subscription with the same alias already exists." + urlSpaceKeyAlreadyExist = "A subscription with the same url and space key already exists." + urlPageIDAlreadyExist = "A subscription with the same url and page id already exists." ) var eventDisplayName = map[string]string{ @@ -37,6 +41,7 @@ type Subscription interface { Name() string GetFormattedSubscription() string IsValid() error + ValidateSubscription(*Subscriptions) error } type BaseSubscription struct { diff --git a/server/serializer/confluence_cloud.go b/server/serializer/confluence_cloud.go index 7fd496d..cb80eea 100644 --- a/server/serializer/confluence_cloud.go +++ b/server/serializer/confluence_cloud.go @@ -8,7 +8,7 @@ import ( "github.com/mattermost/mattermost-server/model" - "github.com/Brightscout/mattermost-plugin-confluence/server/config" + "github.com/mattermost/mattermost-plugin-confluence/server/config" ) const ( diff --git a/server/serializer/confluence_server.go b/server/serializer/confluence_server.go index c5dbeb0..7af84d3 100644 --- a/server/serializer/confluence_server.go +++ b/server/serializer/confluence_server.go @@ -8,7 +8,7 @@ import ( "github.com/mattermost/mattermost-server/model" - "github.com/Brightscout/mattermost-plugin-confluence/server/config" + "github.com/mattermost/mattermost-plugin-confluence/server/config" ) const ( diff --git a/server/serializer/page_subscription.go b/server/serializer/page_subscription.go index 152ec7f..92cbb0d 100644 --- a/server/serializer/page_subscription.go +++ b/server/serializer/page_subscription.go @@ -8,7 +8,7 @@ import ( url2 "net/url" "strings" - "github.com/Brightscout/mattermost-plugin-confluence/server/store" + "github.com/mattermost/mattermost-plugin-confluence/server/store" ) type PageSubscription struct { @@ -52,7 +52,6 @@ func (ps PageSubscription) GetFormattedSubscription() string { } func (ps PageSubscription) IsValid() error { - // TODO : Clean subscription data if ps.Alias == "" { return errors.New("alias can not be empty") } @@ -65,6 +64,9 @@ func (ps PageSubscription) IsValid() error { if ps.PageID == "" { return errors.New("page id can not be empty") } + if ps.ChannelID == "" { + return errors.New("channel id can not be empty") + } return nil } @@ -73,3 +75,21 @@ func PageSubscriptionFromJSON(data io.Reader) (PageSubscription, error) { err := json.NewDecoder(data).Decode(&ps) return ps, err } + +func (ps PageSubscription) ValidateSubscription(subs *Subscriptions) error { + if err := ps.IsValid(); err != nil { + return err + } + if channelSubscriptions, valid := subs.ByChannelID[ps.ChannelID]; valid { + if _, ok := channelSubscriptions[ps.Alias]; ok { + return errors.New(aliasAlreadyExist) + } + } + key := store.GetURLPageIDCombinationKey(ps.BaseURL, ps.PageID) + if urlPageIDSubscriptions, valid := subs.ByURLPagID[key]; valid { + if _, ok := urlPageIDSubscriptions[ps.ChannelID]; ok { + return errors.New(urlPageIDAlreadyExist) + } + } + return nil +} diff --git a/server/serializer/space_subscription.go b/server/serializer/space_subscription.go index 41f222f..35d69ec 100644 --- a/server/serializer/space_subscription.go +++ b/server/serializer/space_subscription.go @@ -8,7 +8,7 @@ import ( url2 "net/url" "strings" - "github.com/Brightscout/mattermost-plugin-confluence/server/store" + "github.com/mattermost/mattermost-plugin-confluence/server/store" ) type SpaceSubscription struct { @@ -52,7 +52,6 @@ func (ss SpaceSubscription) GetFormattedSubscription() string { } func (ss SpaceSubscription) IsValid() error { - // TODO : Clean subscription data if ss.Alias == "" { return errors.New("alias can not be empty") } @@ -65,6 +64,9 @@ func (ss SpaceSubscription) IsValid() error { if ss.SpaceKey == "" { return errors.New("space key can not be empty") } + if ss.ChannelID == "" { + return errors.New("channel id can not be empty") + } return nil } @@ -73,3 +75,21 @@ func SpaceSubscriptionFromJSON(data io.Reader) (SpaceSubscription, error) { err := json.NewDecoder(data).Decode(&ps) return ps, err } + +func (ss SpaceSubscription) ValidateSubscription(subs *Subscriptions) error { + if err := ss.IsValid(); err != nil { + return err + } + if channelSubscriptions, valid := subs.ByChannelID[ss.ChannelID]; valid { + if _, ok := channelSubscriptions[ss.Alias]; ok { + return errors.New(aliasAlreadyExist) + } + } + key := store.GetURLSpaceKeyCombinationKey(ss.BaseURL, ss.SpaceKey) + if urlSpaceKeySubscriptions, valid := subs.ByURLSpaceKey[key]; valid { + if _, ok := urlSpaceKeySubscriptions[ss.ChannelID]; ok { + return errors.New(urlSpaceKeyAlreadyExist) + } + } + return nil +} diff --git a/server/service/delete_subscription.go b/server/service/delete_subscription.go index a5799db..9ec1d5c 100644 --- a/server/service/delete_subscription.go +++ b/server/service/delete_subscription.go @@ -4,8 +4,8 @@ import ( "encoding/json" "fmt" - "github.com/Brightscout/mattermost-plugin-confluence/server/serializer" - "github.com/Brightscout/mattermost-plugin-confluence/server/store" + "github.com/mattermost/mattermost-plugin-confluence/server/serializer" + "github.com/mattermost/mattermost-plugin-confluence/server/store" ) const ( diff --git a/server/service/delete_subscription_test.go b/server/service/delete_subscription_test.go index 84fe7bf..5c78318 100644 --- a/server/service/delete_subscription_test.go +++ b/server/service/delete_subscription_test.go @@ -7,8 +7,8 @@ import ( "bou.ke/monkey" "github.com/stretchr/testify/assert" - "github.com/Brightscout/mattermost-plugin-confluence/server/serializer" - "github.com/Brightscout/mattermost-plugin-confluence/server/store" + "github.com/mattermost/mattermost-plugin-confluence/server/serializer" + "github.com/mattermost/mattermost-plugin-confluence/server/store" ) func TestDeleteSubscription(t *testing.T) { diff --git a/server/service/edit_subscription.go b/server/service/edit_subscription.go index d7b05fd..395ed15 100644 --- a/server/service/edit_subscription.go +++ b/server/service/edit_subscription.go @@ -3,8 +3,8 @@ package service import ( "encoding/json" - "github.com/Brightscout/mattermost-plugin-confluence/server/serializer" - "github.com/Brightscout/mattermost-plugin-confluence/server/store" + "github.com/mattermost/mattermost-plugin-confluence/server/serializer" + "github.com/mattermost/mattermost-plugin-confluence/server/store" ) func EditSubscription(subscription serializer.Subscription) error { diff --git a/server/service/get_subscription.go b/server/service/get_subscription.go index 138b80b..77be196 100644 --- a/server/service/get_subscription.go +++ b/server/service/get_subscription.go @@ -5,7 +5,7 @@ import ( "fmt" "net/http" - "github.com/Brightscout/mattermost-plugin-confluence/server/serializer" + "github.com/mattermost/mattermost-plugin-confluence/server/serializer" ) const generalError = "Some error occurred. Please try again after sometime." diff --git a/server/service/get_subscription_list.go b/server/service/get_subscription_list.go index 37a5840..96d648e 100644 --- a/server/service/get_subscription_list.go +++ b/server/service/get_subscription_list.go @@ -3,9 +3,9 @@ package service import ( "github.com/pkg/errors" - "github.com/Brightscout/mattermost-plugin-confluence/server/config" - "github.com/Brightscout/mattermost-plugin-confluence/server/serializer" - "github.com/Brightscout/mattermost-plugin-confluence/server/store" + "github.com/mattermost/mattermost-plugin-confluence/server/config" + "github.com/mattermost/mattermost-plugin-confluence/server/serializer" + "github.com/mattermost/mattermost-plugin-confluence/server/store" ) const getChannelSubscriptionsError = " Error getting channel subscriptions." diff --git a/server/service/get_subscription_list_test.go b/server/service/get_subscription_list_test.go index 33ca14a..a2f233c 100644 --- a/server/service/get_subscription_list_test.go +++ b/server/service/get_subscription_list_test.go @@ -7,7 +7,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/Brightscout/mattermost-plugin-confluence/server/serializer" + "github.com/mattermost/mattermost-plugin-confluence/server/serializer" ) func TestGetSubscriptionsByChannelID(t *testing.T) { diff --git a/server/service/get_subscription_test.go b/server/service/get_subscription_test.go index aa9c467..38d8fb6 100644 --- a/server/service/get_subscription_test.go +++ b/server/service/get_subscription_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/Brightscout/mattermost-plugin-confluence/server/serializer" + "github.com/mattermost/mattermost-plugin-confluence/server/serializer" ) func TestGetChannelSubscription(t *testing.T) { diff --git a/server/service/notification.go b/server/service/notification.go index f039c87..589c0fc 100644 --- a/server/service/notification.go +++ b/server/service/notification.go @@ -3,9 +3,9 @@ package service import ( "github.com/thoas/go-funk" - "github.com/Brightscout/mattermost-plugin-confluence/server/config" - "github.com/Brightscout/mattermost-plugin-confluence/server/serializer" - "github.com/Brightscout/mattermost-plugin-confluence/server/util" + "github.com/mattermost/mattermost-plugin-confluence/server/config" + "github.com/mattermost/mattermost-plugin-confluence/server/serializer" + "github.com/mattermost/mattermost-plugin-confluence/server/util" ) func SendConfluenceNotifications(event serializer.ConfluenceEvent, eventType string) { diff --git a/server/service/notification_test.go b/server/service/notification_test.go index e4ad542..756acdf 100644 --- a/server/service/notification_test.go +++ b/server/service/notification_test.go @@ -9,8 +9,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" - "github.com/Brightscout/mattermost-plugin-confluence/server/config" - "github.com/Brightscout/mattermost-plugin-confluence/server/serializer" + "github.com/mattermost/mattermost-plugin-confluence/server/config" + "github.com/mattermost/mattermost-plugin-confluence/server/serializer" ) func baseMock() *plugintest.API { diff --git a/server/service/save_subscription.go b/server/service/save_subscription.go index facdc87..91d9437 100644 --- a/server/service/save_subscription.go +++ b/server/service/save_subscription.go @@ -5,8 +5,8 @@ import ( "errors" "net/http" - "github.com/Brightscout/mattermost-plugin-confluence/server/serializer" - "github.com/Brightscout/mattermost-plugin-confluence/server/store" + "github.com/mattermost/mattermost-plugin-confluence/server/serializer" + "github.com/mattermost/mattermost-plugin-confluence/server/store" ) const ( @@ -16,9 +16,16 @@ const ( urlPageIDAlreadyExist = "A subscription with the same url and page id already exists." ) -func SaveSubscription(subscription serializer.Subscription) error { +func SaveSubscription(subscription serializer.Subscription) (int, error) { + subs, gErr := GetSubscriptions() + if gErr != nil { + return http.StatusInternalServerError, errors.New(generalSaveError) + } + if vErr := subscription.ValidateSubscription(&subs); vErr != nil { + return http.StatusBadRequest, vErr + } key := store.GetSubscriptionKey() - err := store.AtomicModify(key, func(initialBytes []byte) ([]byte, error) { + if err := store.AtomicModify(key, func(initialBytes []byte) ([]byte, error) { subscriptions, err := serializer.SubscriptionsFromJSON(initialBytes) if err != nil { return nil, err @@ -29,50 +36,8 @@ func SaveSubscription(subscription serializer.Subscription) error { return nil, marshalErr } return modifiedBytes, nil - }) - return err -} - -func ValidatePageSubscription(s serializer.PageSubscription) (int, error) { - if err := s.IsValid(); err != nil { - return http.StatusBadRequest, err - } - subs, gErr := GetSubscriptions() - if gErr != nil { - return http.StatusInternalServerError, errors.New(generalSaveError) - } - if channelSubscriptions, valid := subs.ByChannelID[s.ChannelID]; valid { - if _, ok := channelSubscriptions[s.Alias]; ok { - return http.StatusBadRequest, errors.New(aliasAlreadyExist) - } - } - key := store.GetURLPageIDCombinationKey(s.BaseURL, s.PageID) - if urlPageIDSubscriptions, valid := subs.ByURLPagID[key]; valid { - if _, ok := urlPageIDSubscriptions[s.ChannelID]; ok { - return http.StatusBadRequest, errors.New(urlPageIDAlreadyExist) - } - } - return http.StatusOK, nil -} - -func ValidateSpaceSubscription(s serializer.SpaceSubscription) (int, error) { - if err := s.IsValid(); err != nil { - return http.StatusBadRequest, err - } - subs, gErr := GetSubscriptions() - if gErr != nil { - return http.StatusInternalServerError, errors.New(generalSaveError) - } - if channelSubscriptions, valid := subs.ByChannelID[s.ChannelID]; valid { - if _, ok := channelSubscriptions[s.Alias]; ok { - return http.StatusBadRequest, errors.New(aliasAlreadyExist) - } - } - key := store.GetURLSpaceKeyCombinationKey(s.BaseURL, s.SpaceKey) - if urlSpaceKeySubscriptions, valid := subs.ByURLSpaceKey[key]; valid { - if _, ok := urlSpaceKeySubscriptions[s.ChannelID]; ok { - return http.StatusBadRequest, errors.New(urlSpaceKeyAlreadyExist) - } + }); err != nil { + return http.StatusInternalServerError, err } return http.StatusOK, nil } diff --git a/server/service/save_subscription_test.go b/server/service/save_subscription_test.go index 9627616..f586c5e 100644 --- a/server/service/save_subscription_test.go +++ b/server/service/save_subscription_test.go @@ -7,7 +7,8 @@ import ( "bou.ke/monkey" "github.com/stretchr/testify/assert" - "github.com/Brightscout/mattermost-plugin-confluence/server/serializer" + "github.com/mattermost/mattermost-plugin-confluence/server/serializer" + "github.com/mattermost/mattermost-plugin-confluence/server/store" ) func TestSaveSpaceSubscription(t *testing.T) { @@ -110,8 +111,11 @@ func TestSaveSpaceSubscription(t *testing.T) { monkey.Patch(GetSubscriptions, func() (serializer.Subscriptions, error) { return subscriptions, nil }) - errCode, err := ValidateSpaceSubscription(val.newSubscription) - assert.Equal(t, val.statusCode, errCode) + monkey.Patch(store.AtomicModify, func(key string, modify func(initialValue []byte) ([]byte, error)) error { + return nil + }) + statusCode, err := SaveSubscription(val.newSubscription) + assert.Equal(t, val.statusCode, statusCode) if err != nil { assert.Equal(t, val.errorMessage, err.Error()) } @@ -219,7 +223,10 @@ func TestSavePageSubscription(t *testing.T) { monkey.Patch(GetSubscriptions, func() (serializer.Subscriptions, error) { return subscriptions, nil }) - errCode, err := ValidatePageSubscription(val.newSubscription) + monkey.Patch(store.AtomicModify, func(key string, modify func(initialValue []byte) ([]byte, error)) error { + return nil + }) + errCode, err := SaveSubscription(val.newSubscription) assert.Equal(t, val.statusCode, errCode) if err != nil { assert.Equal(t, val.errorMessage, err.Error()) diff --git a/server/store/store.go b/server/store/store.go index 71ab821..7dd2d5f 100644 --- a/server/store/store.go +++ b/server/store/store.go @@ -9,8 +9,8 @@ import ( "github.com/mattermost/mattermost-server/model" "github.com/pkg/errors" - "github.com/Brightscout/mattermost-plugin-confluence/server/config" - "github.com/Brightscout/mattermost-plugin-confluence/server/util" + "github.com/mattermost/mattermost-plugin-confluence/server/config" + "github.com/mattermost/mattermost-plugin-confluence/server/util" ) const ConfluenceSubscriptionKeyPrefix = "confluence_subs" diff --git a/server/util/util.go b/server/util/util.go index 4229ad4..deffa37 100644 --- a/server/util/util.go +++ b/server/util/util.go @@ -10,7 +10,7 @@ import ( "github.com/mattermost/mattermost-server/model" - "github.com/Brightscout/mattermost-plugin-confluence/server/config" + "github.com/mattermost/mattermost-plugin-confluence/server/config" ) // GetKeyHash can be used to create a hash from a string