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

Conversation

raghavaggarwal2308
Copy link
Contributor

Summary

  • Update code to show code preview for permalink with branch names

What to test?

  • Getting code preview for permalinks with branch names
Steps to reproduce:
  1. Connect your GitHub account
  2. Open a file on a GitHub repo
  3. Click on any line number in the file
  4. Copy the browser URL
  5. Post the link in the channel
Expected behavior:
  • You should get a preview of the code near that permalink.

Ticket Link

Fixes #670

@raghavaggarwal2308 raghavaggarwal2308 self-assigned this Apr 9, 2024
@hanzei hanzei added the 2: Dev Review Requires review by a core committer label Apr 9, 2024
@hanzei
Copy link
Contributor

hanzei commented Apr 9, 2024

@raghavaggarwal2308 Can you please share a example permalink that now works?

@hanzei hanzei requested a review from ayusht2810 April 9, 2024 10:14
@raghavaggarwal2308
Copy link
Contributor Author

raghavaggarwal2308 commented Apr 9, 2024

@raghavaggarwal2308 Can you please share a example permalink that now works?

@hanzei Here is an example: https://github.com/mattermost/mattermost/blob/master/server/public/Makefile#L7
image

Copy link
Contributor

@hanzei hanzei left a comment

Choose a reason for hiding this comment

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

The feature doesn't seem to work with branches that are not master. e.g. https://github.com/mattermost/mattermost-plugin-github/blob/playwright-shared-repo/.gitmodules#L2 doesn't create a preview.

Can you please check what is going on?

@raghavaggarwal2308
Copy link
Contributor Author

The feature doesn't seem to work with branches that are not master. e.g. https://github.com/mattermost/mattermost-plugin-github/blob/playwright-shared-repo/.gitmodules#L2 doesn't create a preview.

Can you please check what is going on?

@hanzei I checked and found about the regex we are using to compare with the link and extract data is not accepting characters other than alphabets and numbers in the branch name.
We can add any character in the branch on Github including /, due to which its very difficult to write a regex for this as the URL also contains forward slashes.
The only solution we could think for this is to make some trade of and no support some characters like \ here. We can mentions the same in the readme that the permalink preview will not work for branch names containing these characters.
Please let me your thoughts on this

@mickmister
Copy link
Member

@raghavaggarwal2308 Let's support dashes and underscores here. It's okay if we don't support slashes because of the complications. We'll want to make sure there is no UX impact when the branch has a slash though. It should be as if it's not a link to a file, rather than failing to load it

Comment on lines -97 to -101
// 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
}
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

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/master/app/authentication.go#L15-L22 lorem ipsum",
output: "start \n[mattermost/mattermost-server/app/authentication.go](https://github.com/mattermost/mattermost-server/blob/master/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",
Copy link
Member

Choose a reason for hiding this comment

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

Does this test require connecting to the internet? Ideally this isn't the case, though idk if we have any sort of mocking in place for the requests to fetch code from GitHub. The other tests have a valid URL that points to this code, though this master branch one is a broken link. How does this test end up gathering this TokenLocation code for the test here?

Copy link
Contributor

@Kshitij-Katiyar Kshitij-Katiyar Jun 21, 2024

Choose a reason for hiding this comment

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

@mickmister This test does not require internet connectivity. we have to mock in the place of request. mocking

the link is used in the mock function to return the intended value so it works on a broken link too.

@Kshitij-Katiyar
Copy link
Contributor

Kshitij-Katiyar commented Jun 21, 2024

@raghavaggarwal2308 Let's support dashes and underscores here. It's okay if we don't support slashes because of the complications. We'll want to make sure there is no UX impact when the branch has a slash though. It should be as if it's not a link to a file, rather than failing to load it

@mickmister Added the support for hyphen, Hope it aligns with your suggestion.

@Kshitij-Katiyar
Copy link
Contributor

The feature doesn't seem to work with branches that are not master. e.g. https://github.com/mattermost/mattermost-plugin-github/blob/playwright-shared-repo/.gitmodules#L2 doesn't create a preview.

Can you please check what is going on?

@hanzei @mickmister Working fine now.

@mickmister
Copy link
Member

@raghavaggarwal2308 Let's support dashes and underscores here. It's okay if we don't support slashes because of the complications. We'll want to make sure there is no UX impact when the branch has a slash though. It should be as if it's not a link to a file, rather than failing to load it

@mickmister Added the support for hyphen, Hope it aligns with your suggestion.

@Kshitij-Katiyar I think underscores are commonly used in branch names too. Can we support branch names with underscores? Does it support capital letters as well? Maybe a unit test that tests using them together

@Kshitij-Katiyar
Copy link
Contributor

@raghavaggarwal2308 Let's support dashes and underscores here. It's okay if we don't support slashes because of the complications. We'll want to make sure there is no UX impact when the branch has a slash though. It should be as if it's not a link to a file, rather than failing to load it

@mickmister Added the support for hyphen, Hope it aligns with your suggestion.

@Kshitij-Katiyar I think underscores are commonly used in branch names too. Can we support branch names with underscores? Does it support capital letters as well? Maybe a unit test that tests using them together

@raghavaggarwal2308 Let's support dashes and underscores here. It's okay if we don't support slashes because of the complications. We'll want to make sure there is no UX impact when the branch has a slash though. It should be as if it's not a link to a file, rather than failing to load it

@mickmister Added the support for hyphen, Hope it aligns with your suggestion.

@Kshitij-Katiyar I think underscores are commonly used in branch names too. Can we support branch names with underscores? Does it support capital letters as well? Maybe a unit test that tests using them together

@mickmister the regex now in use supports lowercase, uppercase, numeric, hyphen and underscore characters.
Updated the existing testcase for code preview with branch name to use a branch with name including all the supported characters.
Let me know if we want to add another testcase or update the existing one.(as done)

@mickmister mickmister added the 3: QA Review Requires review by a QA tester label Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2: Dev Review Requires review by a core committer 3: QA Review Requires review by a QA tester
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Code previews only work for commit hashes, not for branches
5 participants