diff --git a/server/plugin.go b/server/plugin.go index 1066ba159..c7d838118 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -26,6 +26,7 @@ import ( "github.com/mattermost/mattermost/server/public/model" "github.com/mattermost/mattermost/server/public/plugin" "github.com/mattermost/mattermost/server/public/pluginapi" + "github.com/mattermost/mattermost/server/public/pluginapi/cluster" "github.com/mattermost/mattermost/server/public/pluginapi/experimental/flow" "github.com/mattermost-community/mattermost-plugin-autolink/server/autolink" @@ -41,7 +42,8 @@ const ( botDisplayName = "Jira" botDescription = "Created by the Jira Plugin." - autolinkPluginID = "mattermost-autolink" + autolinkPluginID = "mattermost-autolink" + autolinkClusterMutexKey = "autolink_cluster_mutex" // Move these two to the plugin settings if admins need to adjust them. WebhookMaxProcsPerServer = 20 @@ -85,13 +87,13 @@ type externalConfig struct { DisplaySubscriptionNameInNotifications bool // The encryption key used to encrypt stored api tokens - EncryptionKey string + EncryptionKey string `json:"encryptiontoken"` // API token from Jira - AdminAPIToken string + AdminAPIToken string `json:"adminapitoken"` // Email of the admin - AdminEmail string + AdminEmail string `json:"adminemail"` // Number of days Jira comments will be posted as threaded replies instead of a new post ThreadedJiraCommentSubscriptionDuration string `json:"threadedjiracommentsubscriptionduration"` @@ -163,6 +165,9 @@ type Plugin struct { // telemetry Tracker tracker telemetry.Tracker + + // autolinkClusterMutex to lock operations for autolink + autolinkClusterMutex *cluster.Mutex } func (p *Plugin) getConfig() config { @@ -419,6 +424,13 @@ func (p *Plugin) OnActivate() error { p.enterpriseChecker = enterprise.NewEnterpriseChecker(p.API) + autolinkClusterMutex, err := cluster.NewMutex(p.API, autolinkClusterMutexKey) + if err != nil { + return errors.Wrap(err, "Error creating cluster mutex for autolink") + } + + p.autolinkClusterMutex = autolinkClusterMutex + go func() { p.SetupAutolink(instances) }() @@ -526,8 +538,16 @@ func (p *Plugin) AddAutolinks(key, baseURL string) error { Pattern: `(` + strings.ReplaceAll(baseURL, ".", `\.`) + `/browse/)(` + key + `)(-)(?P\d+)`, Template: `[` + key + `-${jira_id}](` + baseURL + `/browse/` + key + `-${jira_id})`, }, + { + Name: key + " Jump to comment for " + baseURL, + Pattern: `(` + strings.ReplaceAll(baseURL, ".", `\.`) + `/browse/)(?P[A-Z][A-Z0-9]+)-(?P\d+)\?focusedCommentId=(?P\d+)`, + Template: `[${project_id}-${jira_id} (comment)](` + baseURL + `/browse/${project_id}-${jira_id}?focusedCommentId=${comment_id})`, + }, } + p.autolinkClusterMutex.Lock() + defer p.autolinkClusterMutex.Unlock() + client := autolinkclient.NewClientPlugin(p.API) if err := client.Add(installList...); err != nil { // Do not return an error if the status code is 304 (indicating that the autolink for this project is already installed).