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-670] Fix issue: Code previews not working for branches #767

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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
}
Comment on lines -97 to -101
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the implications of removing this? Can we instead be a bit more permissive with the restriction to allow for the new kinds of links? I'm concerned that removing this may be too permissive

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this as it is commit specific and our regex will handle all the other possible cases. I don't think there will be any implications here. @mickmister


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
Loading