Skip to content
Closed
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
51 changes: 43 additions & 8 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -46,6 +47,17 @@ const (
WebhookMaxProcsPerServer = 20
WebhookBufferSize = 10000
PluginRepo = "https://github.com/mattermost/mattermost-plugin-jira"

// Endpoints
patternCommentLinkEndpoint = `/browse/)(?P<project_id>\w+)(-)(?P<jira_id>\d+)[?](focusedCommentId)(=)(?P<comment_id>\d+)`
templateCommentLinkEndpoint = `/browse/${project_id}-${jira_id}?focusedCommentId=${comment_id})`
patternIssueLinkEndpoint = `/browse/)(?P<project_id>\w+)(-)(?P<jira_id>\d+)`
templateIssueLinkEndpoint = `/browse/${project_id}-${jira_id})`

templateViewIssue = `[${project_id}-${jira_id}](`
templateViewIssueWithComment = `[${project_id}-${jira_id} (comment)](`

autolinkClusterMutexKey = "autolink_cluster_mutex"
)

type externalConfig struct {
Expand Down Expand Up @@ -149,6 +161,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 @@ -338,6 +353,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)
}()
Expand Down Expand Up @@ -434,24 +456,37 @@ func (p *Plugin) AddAutoLinkForProjects(plist jira.ProjectList, baseURL string)

func (p *Plugin) AddAutolinks(key, baseURL string) error {
baseURL = strings.TrimRight(baseURL, "/")
Comment thread
Kshitij-Katiyar marked this conversation as resolved.
var replacedBaseURL = `(` + strings.ReplaceAll(baseURL, ".", `\.`)
installList := []autolink.Autolink{
{
Name: key + " key to link for " + baseURL,
Pattern: `(` + key + `)(-)(?P<jira_id>\d+)`,
Template: `[` + key + `-${jira_id}](` + baseURL + `/browse/` + key + `-${jira_id})`,
},
{
Name: key + " link to key for " + baseURL,
Pattern: `(` + strings.ReplaceAll(baseURL, ".", `\.`) + `/browse/)(` + key + `)(-)(?P<jira_id>\d+)`,
Template: `[` + key + `-${jira_id}](` + baseURL + `/browse/` + key + `-${jira_id})`,
Name: "Link to key for " + baseURL,
Pattern: replacedBaseURL + patternIssueLinkEndpoint,
Template: templateViewIssue + baseURL + templateIssueLinkEndpoint,
},
{
Name: "Jump to comment for " + baseURL,
Pattern: replacedBaseURL + patternCommentLinkEndpoint,
Template: templateViewIssueWithComment + baseURL + templateCommentLinkEndpoint,
},
}

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).
if !strings.Contains(err.Error(), `Error: 304, {"status": "OK"}`) {
return fmt.Errorf("unable to add autolinks: %w", err)
if len(installList) > 0 {
p.autolinkClusterMutex.Lock()
defer p.autolinkClusterMutex.Unlock()

client := autolinkclient.NewClientPlugin(p.API)

// Creating the new autolinks
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).
if !strings.Contains(err.Error(), `Error: 304, {"status": "OK"}`) {
return fmt.Errorf("unable to add autolinks: %w", err)
}
}
}

Expand Down
Loading