Skip to content

Commit

Permalink
Changed the way to store assignee, mention, reporter and watching in …
Browse files Browse the repository at this point in the history
…struct so that the code can be easily tested (#6)

* [MI-2088]:Add Support for DM subscriptions for watchers and reporters

* [MI-2088]: Review Fixes
1. Improved the name of certain variable
2. Improved spacing

* [MI-2088]:Revieww fixes
1.Changes the names of few variables
2.Improved code complexity

* [MI-2088]: Review fixes done
1.Changed the name of few variables

* [MI-2088]:fixed review fixes

* [MI-2088]:fixed review fixes

* [MI-2088]:fixed review fixes

* [MI-2088]:Review fixes done
1.Changed the way to store assignee, mention, reporter and watching in struct so that the code can be easily tested

* [MI-2088]: Review fixes done
1. Changed the name of various variables
2. Used constants everywhere

Co-authored-by: Kshitij Katiyar <kshitij.katiyar@brightscout.com>
  • Loading branch information
Nityanand13 and Kshitij-Katiyar authored Sep 30, 2022
1 parent ba9dfb2 commit 2ef95e2
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 106 deletions.
12 changes: 7 additions & 5 deletions server/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ func getMockUserStoreKV() mockUserStoreKV {
trueValue := true
withNotifications := connection // copy
withNotifications.Settings = &ConnectionSettings{
Notifications: trueValue,
SendNotificationsForMention: &trueValue,
SendNotificationsForAssignee: &trueValue,
SendNotificationsForReporter: &trueValue,
SendNotificationsForWatching: &trueValue,
Notifications: trueValue,
RolesForDMNotification: map[string]*bool{
subCommandAssignee: &trueValue,
subCommandMention: &trueValue,
subCommandReporter: &trueValue,
subCommandWatching: &trueValue,
},
}

return mockUserStoreKV{
Expand Down
32 changes: 4 additions & 28 deletions server/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ func (p *Plugin) applyReporterNotification(wh *webhook, instanceID types.ID, rep
commentMessage := fmt.Sprintf("%s **commented** on %s:\n> %s", commentAuthor, jwhook.mdKeySummaryLink(), jwhook.Comment.Body)

connection, err := p.GetUserSetting(wh, instanceID, reporter.Name, reporter.AccountID)
if err != nil || connection.Settings == nil || !connection.Settings.ShouldReceiveNotificationsForReporter() {
if err != nil || connection.Settings == nil || !connection.Settings.ShouldReceiveNotification(notificationTypeReporter) {
return
}

Expand Down Expand Up @@ -1131,39 +1131,15 @@ func (p *Plugin) GetUserSetting(wh *webhook, instanceID types.ID, jiraAccountID,
return connection, nil
}

func (s *ConnectionSettings) ShouldReceiveNotificationsForAssignee() bool {
if s.SendNotificationsForAssignee != nil {
return *s.SendNotificationsForAssignee
func (s *ConnectionSettings) ShouldReceiveNotification(role string) bool {
if val, ok := s.RolesForDMNotification[role]; ok {
return *val
}

// Check old setting for backwards compatibility
return s.Notifications
}

func (s *ConnectionSettings) ShouldReceiveNotificationsForReporter() bool {
if s.SendNotificationsForReporter != nil {
return *s.SendNotificationsForReporter
}

return s.Notifications
}

func (s *ConnectionSettings) ShouldReceiveNotificationsForMention() bool {
if s.SendNotificationsForMention != nil {
return *s.SendNotificationsForMention
}

return s.Notifications
}

func (s *ConnectionSettings) ShouldReceiveNotificationsForWatching() bool {
if s.SendNotificationsForWatching != nil {
return *s.SendNotificationsForWatching
}

return s.Notifications
}

func (wh *webhook) fetchConnectedUser(p *Plugin, instanceID types.ID) (Client, *Connection, error) {
var accountInformation []map[string]string

Expand Down
37 changes: 10 additions & 27 deletions server/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ const (
subCommandWatching = "watching"
)

func (connection *Connection) sendNotification(role string, hasNotification bool) bool {
if role != subCommandAssignee && role != subCommandMention && role != subCommandReporter && role != subCommandWatching {
return false
}
connection.Settings.RolesForDMNotification[role] = &hasNotification
return true
}
func (p *Plugin) settingsNotifications(header *model.CommandArgs, instanceID, mattermostUserID types.ID, connection *Connection, args []string) *model.CommandResponse {
const helpText = "`/jira settings notifications [assignee|mention|reporter|watching] [value]`\n* Invalid value. Accepted values are: `on` or `off`."

Expand All @@ -40,16 +47,7 @@ func (p *Plugin) settingsNotifications(header *model.CommandArgs, instanceID, ma
if connection.Settings == nil {
connection.Settings = &ConnectionSettings{}
}
switch args[1] {
case subCommandAssignee:
connection.Settings.SendNotificationsForAssignee = &value
case subCommandMention:
connection.Settings.SendNotificationsForMention = &value
case subCommandReporter:
connection.Settings.SendNotificationsForReporter = &value
case subCommandWatching:
connection.Settings.SendNotificationsForWatching = &value
default:
if !connection.sendNotification(args[1], value) {
return p.responsef(header, helpText)
}

Expand All @@ -64,23 +62,8 @@ func (p *Plugin) settingsNotifications(header *model.CommandArgs, instanceID, ma
return p.responsef(header, errConnectToJira, err)
}
notifications := settingOff
switch args[1] {
case subCommandAssignee:
if *updatedConnection.Settings.SendNotificationsForAssignee {
notifications = settingOn
}
case subCommandMention:
if *updatedConnection.Settings.SendNotificationsForMention {
notifications = settingOn
}
case subCommandReporter:
if *updatedConnection.Settings.SendNotificationsForReporter {
notifications = settingOn
}
case subCommandWatching:
if *updatedConnection.Settings.SendNotificationsForWatching {
notifications = settingOn
}
if *updatedConnection.Settings.RolesForDMNotification[args[1]] {
notifications = settingOn
}

return p.responsef(header, "Settings updated.\n\t%s notifications %s.", strings.Title(args[1]), notifications)
Expand Down
15 changes: 6 additions & 9 deletions server/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ func (c *Connection) JiraAccountID() types.ID {
}

type ConnectionSettings struct {
Notifications bool `json:"notifications"`
SendNotificationsForMention *bool `json:"send_notifications_for_mention"`
SendNotificationsForAssignee *bool `json:"send_notifications_for_assignee"`
SendNotificationsForReporter *bool `json:"send_notifications_for_reporter"`
SendNotificationsForWatching *bool `json:"send_notifications_for_watching"`
Notifications bool `json:"notifications"`
RolesForDMNotification map[string]*bool
}

func (s *ConnectionSettings) String() string {
Expand All @@ -56,19 +53,19 @@ func (s *ConnectionSettings) String() string {
reporterNotifications := "Notifications for reporter: off"
watchingNotifications := "Notifications for watching: off"

if s != nil && s.ShouldReceiveNotificationsForAssignee() {
if s != nil && s.ShouldReceiveNotification(subCommandAssignee) {
assigneeNotifications = "Notifications for assignee: on"
}

if s != nil && s.ShouldReceiveNotificationsForMention() {
if s != nil && s.ShouldReceiveNotification(subCommandMention) {
mentionNotifications = "Notifications for mention: on"
}

if s != nil && s.ShouldReceiveNotificationsForReporter() {
if s != nil && s.ShouldReceiveNotification(subCommandReporter) {
reporterNotifications = "Notifications for reporter: on"
}

if s != nil && s.ShouldReceiveNotificationsForWatching() {
if s != nil && s.ShouldReceiveNotification(subCommandWatching) {
watchingNotifications = "Notifications for watching: on"
}

Expand Down
12 changes: 7 additions & 5 deletions server/user_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ func (p *Plugin) httpACUserInteractive(w http.ResponseWriter, r *http.Request, i
},
// Set default settings the first time a user connects
Settings: &ConnectionSettings{
Notifications: trueValue,
SendNotificationsForWatching: &trueValue,
SendNotificationsForMention: &trueValue,
SendNotificationsForAssignee: &trueValue,
SendNotificationsForReporter: &trueValue,
Notifications: trueValue,
RolesForDMNotification: map[string]*bool{
subCommandMention: &trueValue,
subCommandAssignee: &trueValue,
subCommandReporter: &trueValue,
subCommandWatching: &trueValue,
},
},
}

Expand Down
12 changes: 7 additions & 5 deletions server/user_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ func (p *Plugin) httpOAuth1aComplete(w http.ResponseWriter, r *http.Request, ins
trueValue := true
// Set default settings the first time a user connects
connection.Settings = &ConnectionSettings{
Notifications: trueValue,
SendNotificationsForWatching: &trueValue,
SendNotificationsForMention: &trueValue,
SendNotificationsForAssignee: &trueValue,
SendNotificationsForReporter: &trueValue,
Notifications: trueValue,
RolesForDMNotification: map[string]*bool{
subCommandMention: &trueValue,
subCommandAssignee: &trueValue,
subCommandReporter: &trueValue,
subCommandWatching: &trueValue,
},
}

err = p.connectUser(instance, types.ID(mattermostUserID), connection)
Expand Down
24 changes: 14 additions & 10 deletions server/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@ func TestUserSettings_String(t *testing.T) {
}{
"notifications on": {
settings: ConnectionSettings{
Notifications: valueTrue,
SendNotificationsForMention: nil,
SendNotificationsForAssignee: nil,
SendNotificationsForReporter: nil,
SendNotificationsForWatching: nil,
Notifications: valueTrue,
RolesForDMNotification: map[string]*bool{
subCommandAssignee: nil,
subCommandMention: nil,
subCommandReporter: nil,
subCommandWatching: nil,
},
},
expectedOutput: "\t- Notifications for assignee: on \n\t- Notifications for mention: on \n\t- Notifications for reporter: on \n\t- Notifications for watching: on",
},
"notifications off": {
settings: ConnectionSettings{
Notifications: valueFalse,
SendNotificationsForMention: nil,
SendNotificationsForAssignee: nil,
SendNotificationsForReporter: nil,
SendNotificationsForWatching: nil,
Notifications: valueFalse,
RolesForDMNotification: map[string]*bool{
subCommandAssignee: nil,
subCommandMention: nil,
subCommandReporter: nil,
subCommandWatching: nil,
},
},
expectedOutput: "\t- Notifications for assignee: off \n\t- Notifications for mention: off \n\t- Notifications for reporter: off \n\t- Notifications for watching: off",
},
Expand Down
19 changes: 2 additions & 17 deletions server/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,8 @@ func (wh *webhook) PostNotifications(p *Plugin, instanceID types.ID) ([]*model.P
}
// If this is a comment-related webhook, we need to check if they have permissions to read that.
// Otherwise, check if they can view the issue.
switch notification.notificationType {
case subCommandAssignee:
if !c.Settings.ShouldReceiveNotificationsForAssignee() {
continue
}
case subCommandMention:
if !c.Settings.ShouldReceiveNotificationsForMention() {
continue
}
case subCommandReporter:
if !c.Settings.ShouldReceiveNotificationsForReporter() {
continue
}
case subCommandWatching:
if !c.Settings.ShouldReceiveNotificationsForWatching() {
continue
}
if !c.Settings.ShouldReceiveNotification(notification.notificationType) {
continue
}
if _, ok := mapForNotification[mattermostUserID]; ok {
continue
Expand Down

0 comments on commit 2ef95e2

Please sign in to comment.