Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-773]: Fixed Issue Jira's autolink should support issue links that contain a comment link in the URL #871

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ endif
## Ensures NPM dependencies are installed without having to run this all the time.
webapp/.npminstall:
ifneq ($(HAS_WEBAPP),)
cd webapp && $(NPM) install
cd webapp && $(NPM) install --verbose
touch $@
endif

Expand Down
40 changes: 28 additions & 12 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/gorilla/mux"
"github.com/pkg/errors"

jira "github.com/andygrunwald/go-jira"
pluginapi "github.com/mattermost/mattermost-plugin-api"
"github.com/mattermost/mattermost-plugin-api/experimental/flow"
"github.com/mattermost/mattermost-plugin-api/experimental/telemetry"
Expand All @@ -45,6 +46,15 @@ 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)](`
)

var BuildHash = ""
Expand Down Expand Up @@ -356,32 +366,38 @@ func (p *Plugin) AddAutolinksForCloudInstance(ci *cloudInstance) error {
return fmt.Errorf("unable to get project keys: %w", err)
}

for _, proj := range plist {
key := proj.Key
err = p.AddAutolinks(key, ci.BaseURL)
}
if err != nil {
if err = p.AddAutolinks(plist, ci.BaseURL); err != nil {
return fmt.Errorf("some keys were not installed: %w", err)
}

return nil
}

func (p *Plugin) AddAutolinks(key, baseURL string) error {
func (p *Plugin) AddAutolinks(projectList jira.ProjectList, baseURL string) error {
baseURL = strings.TrimRight(baseURL, "/")
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide 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: "Jump to comment for " + baseURL,
Pattern: replacedBaseURL + patternCommentLinkEndpoint,
Template: templateViewIssueWithComment + baseURL + templateCommentLinkEndpoint,
},
{
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,
},
}

for _, project := range projectList {
key := project.Key
installList = append(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})`,
})
}

client := autolinkclient.NewClientPlugin(p.API)
if err := client.Add(installList...); err != nil {
return fmt.Errorf("unable to add autolinks: %w", err)
Expand Down