Skip to content

Commit

Permalink
[GH-670] Fix issue: Code previews not working for branches (#767)
Browse files Browse the repository at this point in the history
* [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 <kshitij.katiyar@brightscout.com>
  • Loading branch information
raghavaggarwal2308 and Kshitij-Katiyar authored Jul 29, 2024
1 parent a7e16de commit 133f527
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
6 changes: 0 additions & 6 deletions server/plugin/permalinks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package plugin

import (
"context"
"encoding/hex"
"path"
"strings"
"time"
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 6 additions & 7 deletions server/plugin/permalinks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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")
}

Expand Down
2 changes: 1 addition & 1 deletion server/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type Plugin struct {
// NewPlugin returns an instance of a Plugin.
func NewPlugin() *Plugin {
p := &Plugin{
githubPermalinkRegex: regexp.MustCompile(`https?://(?P<haswww>www\.)?github\.com/(?P<user>[\w-]+)/(?P<repo>[\w-.]+)/blob/(?P<commit>\w+)/(?P<path>[\w-/.]+)#(?P<line>[\w-]+)?`),
githubPermalinkRegex: regexp.MustCompile(`https?://(?P<haswww>www\.)?github\.com/(?P<user>[\w-]+)/(?P<repo>[\w-.]+)/blob/(?P<commit>[\w-]+)/(?P<path>[\w-/.]+)#(?P<line>[\w-]+)?`),
}

p.CommandHandlers = map[string]CommandHandleFunc{
Expand Down

0 comments on commit 133f527

Please sign in to comment.