From 133f527b4105fa7d2015f9f8ce1d92ac28a611a5 Mon Sep 17 00:00:00 2001 From: Raghav Aggarwal Date: Mon, 29 Jul 2024 12:07:28 +0530 Subject: [PATCH] [GH-670] Fix issue: Code previews not working for branches (#767) * [MM-362] Fix issue: Code previews not working for branches * [MM-670]: added support for hyphen in branch name for code preview of permalink * [MM-670]: fixed the testcase for permalink.go * [MM-670]: Updated the testcase for code preview using branch name with various charecter --------- Co-authored-by: kshitij katiyar --- server/plugin/permalinks.go | 6 ------ server/plugin/permalinks_test.go | 13 ++++++------- server/plugin/plugin.go | 2 +- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/server/plugin/permalinks.go b/server/plugin/permalinks.go index 70094eb82..b5e5f80db 100644 --- a/server/plugin/permalinks.go +++ b/server/plugin/permalinks.go @@ -2,7 +2,6 @@ package plugin import ( "context" - "encoding/hex" "path" "strings" "time" @@ -94,11 +93,6 @@ func (p *Plugin) makeReplacements(msg string, replacements []replacement, ghClie // iterating the slice in reverse to preserve the replacement indices. for i := len(replacements) - 1; i >= 0; i-- { r := replacements[i] - // quick bailout if the commit hash is not proper. - if _, err := hex.DecodeString(r.permalinkInfo.commit); err != nil { - p.client.Log.Warn("Bad git commit hash in permalink", "error", err.Error(), "hash", r.permalinkInfo.commit) - continue - } ctx, cancel := context.WithTimeout(context.Background(), permalinkReqTimeout) defer cancel() diff --git a/server/plugin/permalinks_test.go b/server/plugin/permalinks_test.go index 881cf9fac..89b9ec81b 100644 --- a/server/plugin/permalinks_test.go +++ b/server/plugin/permalinks_test.go @@ -284,7 +284,7 @@ func TestMakeReplacements(t *testing.T) { replacements []replacement }{ { - name: "basic one link", + name: "basic one link with commit hash", input: "start https://github.com/mattermost/mattermost-server/blob/cbb25838a61872b624ac512556d7bc932486a64c/app/authentication.go#L15-L22 lorem ipsum", output: "start \n[mattermost/mattermost-server/app/authentication.go](https://github.com/mattermost/mattermost-server/blob/cbb25838a61872b624ac512556d7bc932486a64c/app/authentication.go#L15-L22)\n```go\ntype TokenLocation int\n\nconst (\n\tTokenLocationNotFound TokenLocation = iota\n\tTokenLocationHeader\n\tTokenLocationCookie\n\tTokenLocationQueryString\n)\n```\n lorem ipsum", replacements: []replacement{ @@ -354,13 +354,13 @@ func TestMakeReplacements(t *testing.T) { }, }, { - name: "bad commit hash", - input: "start https://github.com/mattermost/mattermost-server/blob/badhash/app/authentication.go#L15-L22 lorem ipsum", - output: "start https://github.com/mattermost/mattermost-server/blob/badhash/app/authentication.go#L15-L22 lorem ipsum", + name: "link with branch name", + input: "start https://github.com/mattermost/mattermost-server/blob/TEST-branch_1/app/authentication.go#L15-L22 lorem ipsum", + output: "start \n[mattermost/mattermost-server/app/authentication.go](https://github.com/mattermost/mattermost-server/blob/TEST-branch_1/app/authentication.go#L15-L22)\n```go\ntype TokenLocation int\n\nconst (\n\tTokenLocationNotFound TokenLocation = iota\n\tTokenLocationHeader\n\tTokenLocationCookie\n\tTokenLocationQueryString\n)\n```\n lorem ipsum", replacements: []replacement{ { index: 6, - word: "https://github.com/mattermost/mattermost-server/blob/badhash/app/authentication.go#L15-L22", + word: "https://github.com/mattermost/mattermost-server/blob/TEST-branch_1/app/authentication.go#L15-L22", permalinkInfo: struct { haswww string commit string @@ -370,7 +370,7 @@ func TestMakeReplacements(t *testing.T) { line string }{ haswww: "", - commit: "badhash", + commit: "master", line: "L15-L22", path: "app/authentication.go", user: "mattermost", @@ -442,7 +442,6 @@ func TestMakeReplacements(t *testing.T) { }) } - mockPluginAPI.AssertCalled(t, "LogWarn", "Bad git commit hash in permalink", "error", "encoding/hex: invalid byte: U+0068 'h'", "hash", "badhash") mockPluginAPI.AssertCalled(t, "LogWarn", "Error while fetching file contents", "error", "unmarshalling failed for both file and directory content: unexpected end of JSON input and unexpected end of JSON input", "path", "path/file.go") } diff --git a/server/plugin/plugin.go b/server/plugin/plugin.go index d360be6e4..0f092ee76 100644 --- a/server/plugin/plugin.go +++ b/server/plugin/plugin.go @@ -109,7 +109,7 @@ type Plugin struct { // NewPlugin returns an instance of a Plugin. func NewPlugin() *Plugin { p := &Plugin{ - githubPermalinkRegex: regexp.MustCompile(`https?://(?Pwww\.)?github\.com/(?P[\w-]+)/(?P[\w-.]+)/blob/(?P\w+)/(?P[\w-/.]+)#(?P[\w-]+)?`), + githubPermalinkRegex: regexp.MustCompile(`https?://(?Pwww\.)?github\.com/(?P[\w-]+)/(?P[\w-.]+)/blob/(?P[\w-]+)/(?P[\w-/.]+)#(?P[\w-]+)?`), } p.CommandHandlers = map[string]CommandHandleFunc{