Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -85,13 +87,13 @@ type externalConfig struct {
DisplaySubscriptionNameInNotifications bool

// The encryption key used to encrypt stored api tokens
EncryptionKey string
EncryptionKey string `json:"encryptiontoken"`
Comment thread
Kshitij-Katiyar marked this conversation as resolved.

// 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"`
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}()
Expand Down Expand Up @@ -526,8 +538,16 @@ func (p *Plugin) AddAutolinks(key, baseURL string) error {
Pattern: `(` + strings.ReplaceAll(baseURL, ".", `\.`) + `/browse/)(` + key + `)(-)(?P<jira_id>\d+)`,
Template: `[` + key + `-${jira_id}](` + baseURL + `/browse/` + key + `-${jira_id})`,
},
{
Name: key + " Jump to comment for " + baseURL,
Pattern: `(` + strings.ReplaceAll(baseURL, ".", `\.`) + `/browse/)(?P<project_id>[A-Z][A-Z0-9]+)-(?P<jira_id>\d+)\?focusedCommentId=(?P<comment_id>\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).
Expand Down
Loading