From d53a11d1395c010d8b7f85d9a3c6c08f7b157e34 Mon Sep 17 00:00:00 2001 From: kshitij katiyar Date: Wed, 28 May 2025 18:29:58 +0530 Subject: [PATCH 1/5] comment_autolink_support --- server/plugin.go | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/server/plugin.go b/server/plugin.go index 84f18feca..18f1aeacc 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -25,6 +25,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" @@ -40,7 +41,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 @@ -84,13 +86,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"` } const defaultMaxAttachmentSize = types.ByteSize(100 * 1024 * 1024) // 100Mb @@ -149,6 +151,9 @@ type Plugin struct { // telemetry Tracker tracker telemetry.Tracker + + // autolinkClusterMutex to lock operations for autolink + autolinkClusterMutex *cluster.Mutex } func (p *Plugin) getConfig() config { @@ -338,6 +343,13 @@ func (p *Plugin) OnActivate() error { p.enterpriseChecker = enterprise.NewEnterpriseChecker(p.API) + autolinkClusterMutex, err := cluster.NewMutex(p.API, autolinkClusterMutexKey) + if err != nil { + return err + } + + p.autolinkClusterMutex = autolinkClusterMutex + go func() { p.SetupAutolink(instances) }() @@ -434,6 +446,7 @@ func (p *Plugin) AddAutoLinkForProjects(plist jira.ProjectList, baseURL string) func (p *Plugin) AddAutolinks(key, baseURL string) error { baseURL = strings.TrimRight(baseURL, "/") + replacedBaseURL := `(` + strings.ReplaceAll(baseURL, ".", `\.`) installList := []autolink.Autolink{ { Name: key + " key to link for " + baseURL, @@ -445,8 +458,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: replacedBaseURL + `/browse/)(?P\w+)(-)(?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). From a5d86e7e5e00806796d2a59430a070c9a8222266 Mon Sep 17 00:00:00 2001 From: kshitij katiyar Date: Thu, 29 May 2025 15:39:37 +0530 Subject: [PATCH 2/5] review fixes --- server/plugin.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/plugin.go b/server/plugin.go index 18f1aeacc..45babc2b8 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -345,7 +345,7 @@ func (p *Plugin) OnActivate() error { autolinkClusterMutex, err := cluster.NewMutex(p.API, autolinkClusterMutexKey) if err != nil { - return err + return errors.Wrap(err, "Error creating cluster mutex for autolink") } p.autolinkClusterMutex = autolinkClusterMutex @@ -446,7 +446,7 @@ func (p *Plugin) AddAutoLinkForProjects(plist jira.ProjectList, baseURL string) func (p *Plugin) AddAutolinks(key, baseURL string) error { baseURL = strings.TrimRight(baseURL, "/") - replacedBaseURL := `(` + strings.ReplaceAll(baseURL, ".", `\.`) + replacedBaseURL := `(` + strings.ReplaceAll(baseURL, ".", `\.`) + `)` installList := []autolink.Autolink{ { Name: key + " key to link for " + baseURL, @@ -455,7 +455,7 @@ func (p *Plugin) AddAutolinks(key, baseURL string) error { }, { Name: key + " link to key for " + baseURL, - Pattern: `(` + strings.ReplaceAll(baseURL, ".", `\.`) + `/browse/)(` + key + `)(-)(?P\d+)`, + Pattern: replacedBaseURL + `/browse/)(` + key + `)(-)(?P\d+)`, Template: `[` + key + `-${jira_id}](` + baseURL + `/browse/` + key + `-${jira_id})`, }, { From 08ed6f7fed8814eb488034a3445688218ea3fc90 Mon Sep 17 00:00:00 2001 From: kshitij katiyar Date: Mon, 9 Jun 2025 18:18:48 +0530 Subject: [PATCH 3/5] fixed regex --- server/plugin.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/server/plugin.go b/server/plugin.go index 781ae9508..0234ed4cb 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -515,7 +515,6 @@ func (p *Plugin) AddAutoLinkForProjects(plist jira.ProjectList, baseURL string) func (p *Plugin) AddAutolinks(key, baseURL string) error { baseURL = strings.TrimRight(baseURL, "/") - replacedBaseURL := `(` + strings.ReplaceAll(baseURL, ".", `\.`) + `)` installList := []autolink.Autolink{ { Name: key + " key to link for " + baseURL, @@ -524,12 +523,12 @@ func (p *Plugin) AddAutolinks(key, baseURL string) error { }, { Name: key + " link to key for " + baseURL, - Pattern: replacedBaseURL + `/browse/)(` + key + `)(-)(?P\d+)`, + 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: replacedBaseURL + `/browse/)(?P\w+)(-)(?P\d+)[?](focusedCommentId)(=)(?P\d+)`, + Pattern: `(` + strings.ReplaceAll(baseURL, ".", `\.`) + `/browse/)(?P\w+)(-)(?P\d+)[?](focusedCommentId)(=)(?P\d+)`, Template: `[${project_id}-${jira_id} (comment)](` + baseURL + `/browse/${project_id}-${jira_id}?focusedCommentId=${comment_id})`, }, } From 3c9388fd1a9f1e78534c04277ae5a5b79694aafe Mon Sep 17 00:00:00 2001 From: Abbas Naqvi Date: Fri, 20 Jun 2025 17:04:24 +0530 Subject: [PATCH 4/5] Fix autolink for Jira --- server/plugin.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/plugin.go b/server/plugin.go index 0234ed4cb..6194ad063 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -527,8 +527,8 @@ func (p *Plugin) AddAutolinks(key, baseURL string) error { Template: `[` + key + `-${jira_id}](` + baseURL + `/browse/` + key + `-${jira_id})`, }, { - Name: key + "Jump to comment for " + baseURL, - Pattern: `(` + strings.ReplaceAll(baseURL, ".", `\.`) + `/browse/)(?P\w+)(-)(?P\d+)[?](focusedCommentId)(=)(?P\d+)`, + 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})`, }, } From 072211122f5a58e8c9009184b2c7258e01183058 Mon Sep 17 00:00:00 2001 From: raghavaggarwal2308 Date: Sat, 30 Aug 2025 19:30:10 +0530 Subject: [PATCH 5/5] [MM-1440] Fix the autolink pattern for comments --- server/plugin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/plugin.go b/server/plugin.go index f8660dd09..c7d838118 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -540,7 +540,7 @@ func (p *Plugin) AddAutolinks(key, baseURL string) error { }, { Name: key + " Jump to comment for " + baseURL, - Pattern: `(` + strings.ReplaceAll(baseURL, ".", `\\.`) + `/browse/)(?P[A-Z][A-Z0-9]+)-(?P\d+)\?focusedCommentId=(?P\d+)`, + 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})`, }, }