Skip to content

MM-8880: Show code block previews of Github permalinks - #137

Merged
marianunez merged 9 commits into
mattermost:masterfrom
agnivade:MM-18880
Nov 19, 2019
Merged

marianunez merged 9 commits into
mattermost:masterfrom
agnivade:MM-18880

Conversation

@agnivade

@agnivade agnivade commented Oct 7, 2019 •

Copy link
Copy Markdown
Contributor

Summary

For every message we now hook into the MessageWillBePosted event, and check
if it contains a permalink to a Github file.

If yes, we parse out the link, and make an API call to the Github contents API.
Get the file contents and filter out the required lines given in the permalink anchor.

Then we construct a code block in markdown and replace the original string.

Added a config key to gate this feature.

Screenshots:

With config disabled:
before-mm

After enabling it:
after-mm
The newly formed hyperlink is a link to the original permalink

Config setting:
codepreview

Ticket Link

MM-18880

Fixes #128

@hanzei
hanzei requested a review from marianunez October 7, 2019 10:13
@hanzei hanzei added 2: Dev Review Requires review by a core committer Hacktoberfest labels Oct 7, 2019
@hanzei hanzei added this to the v0.12.0 milestone Oct 7, 2019
@marianunez

Copy link
Copy Markdown
Member

@agnivade Could you include a screenshot of an example of how it looks to the description so we can get a quick PM review? cc @aaronrothschild

@marianunez marianunez added the 1: PM Review Requires review by a product manager label Oct 7, 2019
@agnivade

agnivade commented Oct 7, 2019

Copy link
Copy Markdown
Contributor Author

Yep, done !

Comment thread Makefile Resolved
@aaronrothschild

Copy link
Copy Markdown
Contributor

LGTM. Do we cut off the code at a certain number of characters?

@aaronrothschild aaronrothschild removed the 1: PM Review Requires review by a product manager label Oct 7, 2019
@agnivade

agnivade commented Oct 8, 2019

Copy link
Copy Markdown
Contributor Author

Good point actually. I think number of lines is better than number of characters. How about a max of 10 lines with an ellipsis at end if there are more lines to indicate that lines have been cut off in the preview ?

@marianunez
marianunez requested a review from alifarooq0 October 9, 2019 20:16

@marianunez marianunez left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@agnivade Could we handle the case for a single code line https://github.com/mattermost/mattermost-server/blob/cbb25838a61872b624ac512556d7bc932486a64c/app/authentication.go#L20 ? The original concept was to show the given line and include 3 lines before/after to show the surrounding code block (much like github does in code review changes screen)

@agnivade

Copy link
Copy Markdown
Contributor Author

@agnivade

Copy link
Copy Markdown
Contributor Author

gentle ping @marianunez

@marianunez

Copy link
Copy Markdown
Member

@agnivade sorry for the delay. Will be getting to this PR today.

Feel free to re-requets review when necessary given that the PR falls out of our review list and sometimes gets out of our radar.

@hanzei
hanzei requested a review from marianunez October 15, 2019 14:56

@marianunez marianunez left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall LGTM. Some suggested changes below - will review the tests next

Comment thread server/plugin.go Resolved
Comment thread server/plugin.go Resolved
Comment thread server/permalinks.go Resolved
Comment thread server/permalinks.go Resolved
Comment thread server/permalinks.go Resolved
Comment thread server/permalinks.go Resolved
Comment thread server/permalinks.go Resolved
Comment thread server/utils.go Resolved

@alifarooq0 alifarooq0 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks @agnivade for your contribution! 🎉 Had a few comments. Your test looks great! I was wondering if you can also add tests for makeReplacements as that is where all the magic happen :)

Comment thread Makefile Resolved
Comment thread server/plugin.go Resolved
Comment thread server/plugin.go Resolved
Comment thread server/permalinks.go Resolved
Comment thread server/permalinks.go Resolved
Comment thread server/permalinks.go Resolved
Comment thread server/utils.go Resolved
Comment thread server/utils.go Resolved
Comment thread server/utils.go Resolved
Comment thread server/permalinks.go Resolved
Comment thread server/permalinks.go Resolved
Comment thread go.mod Resolved
Comment thread server/plugin.go Resolved
@agnivade

Copy link
Copy Markdown
Contributor Author

@ali-farooq0 - Thanks for the wonderful review.

Regarding testing makeReplacements, I had thought about it too. It is hard to do that because it actually makes a network request to get the file. So either we need to pass an interface which means adding mocks and other complications including changes in the entire codebase. Or we need to make an actual network request, which means somehow having a dummy test github account credentials. Both look cumbersome to me.

But I have tests for getLineNumbers and insideLink which perform the tasks after the file is fetched. So we have some coverage there. It's only the file fetching part and skipping the bad commit hash which is not tested.

@agnivade

Copy link
Copy Markdown
Contributor Author

Oops my bad ! I mistakenly amended to the previous commit. So you won't see the exact changes done for this review. You will also see Maria's review changes. Sorry about that :(

@levb levb added the Help Wanted Community help wanted label Oct 20, 2019
@hanzei hanzei added this to the v0.13.0 milestone Nov 5, 2019
- Added a mock github client to test makeReplacements.
- Some minor nits on changing no. to number and adding periods
at end of sentences.
- Changing the captureMap from a map to a struct.
@agnivade
agnivade requested a review from alifarooq0 November 5, 2019 18:34

@alifarooq0 alifarooq0 left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice @agnivade! Still feel like getReplacements should be the one that fetches all the details from github and puts it together in nice little struct, but the way it is currently set up is fine as well, especially since we've added tests now 👍

Just one minor fix as per https://github.com/mattermost/mattermost-plugin-github/pull/137/files#r344789251 and this PR should be good to go 👍

Comment thread server/permalinks.go Resolved
Comment thread server/permalinks_test.go Resolved
@agnivade

Copy link
Copy Markdown
Contributor Author

@ali-farooq0 - It looks like the build was cancelled ? Can you restart it ?

@marianunez marianunez removed the 2: Dev Review Requires review by a core committer label Nov 11, 2019
@alifarooq0

Copy link
Copy Markdown

@DHaussermann this is ready for QA.

@alifarooq0 alifarooq0 assigned DHaussermann and unassigned agnivade Nov 12, 2019
@DHaussermann

Copy link
Copy Markdown

Hey @agnivade I'm not seeing this work as expected. Once I enable Enable Code Previews the next subscription event does get delivered but I see no preview and the the logs show

{"level":"info","ts":1574089720.0073502,"caller":"go-plugin/stream.go:15","msg":"2019/11/18 15:08:40 RPC call to KVGet API failed: connection is shut down","plugin_id":"github","source":"plugin_stderr"}
{"level":"info","ts":1574089720.00742,"caller":"go-plugin/stream.go:15","msg":"2019/11/18 15:08:40 RPC call to KVGet API failed: connection is shut down","plugin_id":"github","source":"plugin_stderr"}

After this subscriptions and @ mentions stop working.

Once I disable Enable Code Previews It looks like the plugin crashes and restarts

{"level":"error","ts":1574090255.8316064,"caller":"plugin/health_check.go:91","msg":"Health check failed for plugin github, error: Plugin RPC connection is not responding"}
{"level":"warn","ts":1574090255.831667,"caller":"plugin/hclog_adapter.go:51","msg":"error closing client during Kill","plugin_id":"github","wrapped_extras":"errconnection is shut down"}
{"level":"warn","ts":1574090255.8316815,"caller":"plugin/hclog_adapter.go:53","msg":"plugin failed to exit gracefully","plugin_id":"github"}

Subscriptions and @ mentions then start working again.

Can you help me isolate this behavior? Can you confirm if these errors are related to the new feature and if I need to collect more info somehow?

@agnivade

Copy link
Copy Markdown
Contributor Author

Hi @DHaussermann - Yes I have seen the " RPC call to KVGet API failed: connection is shut down," several times. But I worked around it by just restarting the plugin process. Everything starts working again. I thought it was an unrelated issue and didn't have time to look deeper, because everything works fine with a restart.

On a deeper look, the error comes from the p.API.KVGet(userID + GITHUB_TOKEN_KEY) call in (*Plugin).getGitHubUserInfo method. I am calling this method in my code, but this is also used in a lot of places. If it is failing, I believe there is a central reason this is failing. Does this help ?

Please let me know how can I help further.

@DHaussermann

DHaussermann commented Nov 18, 2019 •

Copy link
Copy Markdown

Thanks @agnivade can you also give me some guidance on the functionality?
Once I enable the feature I do get the 1st event delivered but I don't see the file preview. I'm not sure if it's related to the error or I'm trying to use this correctly. Maybe it does not work with all events or the link I'm using? Should these steps work?

  1. Create a subscription to a repo that includes issues
  2. Enable the feature
  3. In the target repo, create an issue where the description contains a file like such as:
Link Test Issue
https://github.com/DHaussermann/hello-world/blob/master/hello.go

Expected: In the channel where the event was delivered, link included in GitHub post shows a preview.

@agnivade

Copy link
Copy Markdown
Contributor Author

Ah no, you are using it wrong. You don't need any subscriptions or delivery of any event whatsoever. And secondly, that link is not a permalink. It's just a URL to a file.

Here is what I would suggest:

Once you enable the feature, just copy paste any text containing a github permalink. That's all. Then you should be able to see the preview.

As an example type https://github.com/mattermost/mattermost-server/blob/4d1c6cd446ba5a40fb90d530ceadc4ed7a387424/store/constants.go#L7 in the text box.

You can get a permalink by clicking on a line number of a file in github. Then click on the 3 dots on the left, click on copy permalink. You can also test a line range, by pressing the shift key and clicking on a close-by line to get a line range. Then do the same to get its permalink.

Let me know if that helps.

@DHaussermann

DHaussermann commented Nov 18, 2019 •

Copy link
Copy Markdown

Thanks @agnivade this does help. I think I mistakenly focused on the subscription events because I noticed they stop working :) . With a proper link I see the functionality working.

I also notice the same behavior with the preview. After it is generated once, the plugin stops working. I then have to disable and re-enable the plugin to see a preview again.

To try and isolate this I deployed the plugin from the master branch of the Mattermost repo and I can't reproduce the crash or log errors.

Is there anyway to determine conclusively if the issue is introduced by this PR?

This causes failures in the RPC calls, and without the userID,
we cannot do anything either.
@agnivade

Copy link
Copy Markdown
Contributor Author

@DHaussermann - It was a corner case where an empty userID was being passed. Apparently, restarting the plugin did not send these erroneous messages.

Please check again with the latest patch.

@DHaussermann DHaussermann added the 2: Dev Review Requires review by a core committer label Nov 19, 2019

@DHaussermann DHaussermann left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for the fix @agnivade !

Tested and passed.

  • Tested new feature is working
  • Tested ranges for link
  • Tested code link in between other text
  • Regression tested plugin
    No issues found
    LGTM!

@DHaussermann DHaussermann removed the 3: QA Review Requires review by a QA tester label Nov 19, 2019
@marianunez marianunez added 4: Reviews Complete All reviewers have approved the pull request and removed 2: Dev Review Requires review by a core committer labels Nov 19, 2019
@marianunez
marianunez merged commit b77d863 into mattermost:master Nov 19, 2019
@agnivade
agnivade deleted the MM-18880 branch November 19, 2019 16:35
@hanzei hanzei removed the Help Wanted Community help wanted label Mar 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4: Reviews Complete All reviewers have approved the pull request Hacktoberfest

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Show code block preview of Github code line permalink

9 participants