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-61]: Show code block previews of Gitlab permalinks #299

Merged
merged 29 commits into from
Jun 19, 2023

Conversation

shivamjosh
Copy link
Contributor

Summary

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

If yes, we parse out the link and make an API call to the Gitlab files 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:
image

After enabling it:
image

The newly formed hyperlink is a link to the original permalink

Config setting:
image

Ticket Link

Fixes #61

@shivamjosh shivamjosh requested a review from mickmister as a code owner June 2, 2022 09:39
@mattermod
Copy link
Contributor

Hello @shivamjosh,

Thanks for your pull request! A Core Committer will review your pull request soon. For code contributions, you can learn more about the review process here.

Per the Mattermost Contribution Guide, we need to add you to the list of approved contributors for the Mattermost project.

Please help complete the Mattermost contribution license agreement?
Once you have signed the CLA, please comment with /check-cla and confirm that the CLA check is green.

This is a standard procedure for many open source projects.

Please let us know if you have any questions.

We are very happy to have you join our growing community! If you're not yet a member, please consider joining our Contributors community channel to meet other contributors and discuss new opportunities with the core team.

@codecov
Copy link

codecov bot commented Jun 6, 2022

Codecov Report

Patch coverage: 64.12% and project coverage change: +1.84 🎉

Comparison is base (4a527b4) 31.20% compared to head (9fc71e6) 33.05%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #299      +/-   ##
==========================================
+ Coverage   31.20%   33.05%   +1.84%     
==========================================
  Files          21       22       +1     
  Lines        3778     4000     +222     
==========================================
+ Hits         1179     1322     +143     
- Misses       2479     2547      +68     
- Partials      120      131      +11     
Impacted Files Coverage Δ
server/configuration.go 33.33% <ø> (ø)
server/main.go 0.00% <0.00%> (ø)
server/plugin.go 14.52% <5.40%> (-0.55%) ⬇️
server/permalinks.go 67.92% <67.92%> (ø)
server/utils.go 78.02% <87.34%> (+7.14%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@mickmister mickmister added 2: Dev Review Requires review by a core committer 3: QA Review Requires review by a QA tester labels Jun 12, 2022
@mattermod
Copy link
Contributor

This PR has been automatically labelled "stale" because it hasn't had recent activity.
A core team member will check in on the status of the PR to help with questions.
Thank you for your contribution!

Copy link
Contributor

@levb levb left a comment

Choose a reason for hiding this comment

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

Mostly LGTM, with nits.

The big question I have if we should be using MessageHasBeenPosted here instead, to rewrite the post asynchronously.

server/plugin.go Outdated Show resolved Hide resolved
server/plugin.go Show resolved Hide resolved
server/permalinks.go Outdated Show resolved Hide resolved
server/permalinks.go Outdated Show resolved Hide resolved
server/permalinks.go Outdated Show resolved Hide resolved
Copy link
Contributor

@mickmister mickmister left a comment

Choose a reason for hiding this comment

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

Thanks for this @shivamjosh 👍

I have a few requests including one regarding performance, and a few questions.

I also have a request that when there has been prior review on the work, to please link to the existing PR in the new PR's description: Brightscout#4. GitHub isn't allowing me to reply on conversations there so I'm copying this discussion here Brightscout#4 (comment)

Have you checked if there's a difference between Github and Gitlab permalinks or just copied this regex from Github plugin?

I have copied it from Github plugin and then made changes according to the GitLab plugin.

How confident are we that the URL formats between GitHub and GitLab are generally identical?

server/plugin.go Outdated Show resolved Hide resolved
server/plugin.go Outdated Show resolved Hide resolved
server/utils.go Outdated
Comment on lines 211 to 222
// isInsideLink reports whether the given index in a string is preceded
// by zero or more space, then (, then ].
//
// It is a poor man's version of checking markdown hyperlinks without
// using a full-blown markdown parser. The idea is to quickly confirm
// whether a permalink is inside a markdown link or not. Something like
// "text ]( permalink" is rare enough. Even then, it is okay if
// there are false positives, but there cannot be any false negatives.
//
// Note: it is fine to go one byte at a time instead of one rune because
// we are anyways looking for ASCII chars.
func isInsideLink(msg string, index int) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this implementation or explanation from another source?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, this implementation is taken from the Github plugin.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we link to the code here using a GitHub permalink?

Copy link
Contributor

Choose a reason for hiding this comment

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

Done

server/permalinks.go Outdated Show resolved Hide resolved

// makeReplacements performs the given replacements on the msg and returns
// the new msg. The replacements slice needs to be sorted by the index in ascending order.
func (p *Plugin) makeReplacements(msg string, replacements []replacement, glClient *gitlab.Client) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Was this code adapted from the GitHub plugin's implementation of this feature?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we put a comment linking to the original function using a GitHub permalink?

Copy link
Contributor

Choose a reason for hiding this comment

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

Done

server/permalinks.go Outdated Show resolved Hide resolved
server/permalinks_test.go Outdated Show resolved Hide resolved
server/permalinks_test.go Outdated Show resolved Hide resolved
server/plugin.go Outdated Show resolved Hide resolved
manojmalik20 and others added 3 commits July 25, 2022 15:05
* [MI-1956] Review fixes
1.Removed nested struct
2.Made a global variable for "gitlabPermalinkRegex"
3.Made replacement before interacting with gitlab API
4. Used LogDebug instead of LogError

* Run checkstyle
1. Added new line at the end of the file
2. Run check-style

* Review Fixes
1.Made requests concurrently

* Review Fixes
1. Added New line at the end of file

* Review fixes
1.Made requests concurrently

* Review fixes
1.Made requests concurrently

* Review fixes
1.Made first letter copital of comments
2.Made requests concurrently with proper order

* Review Fixes
1.used gofmt and golint

* Merge branch 'permalink_preview' of /~https://github.com/Brightscout/mattermost-plugin-gitlab into permalink_preview_fixes

* Review fixes
1.Used better name of myMap

* Review Fixes
1.Used proper name of myMap

* Review fixes
1.Used make check-style

* MI-1840: Show code block previews of Github permalinks

* MI-1840: Minor fix

* MI-1840: Done review fixes

* MI-1840: Added unit test for permalink

* Fixed lint errors

* [MI-1956] Review fixes
1.Removed nested struct
2.Made a global variable for "gitlabPermalinkRegex"
3.Made replacement before interacting with gitlab API
4. Used LogDebug instead of LogError

* Run checkstyle
1. Added new line at the end of the file
2. Run check-style

* Review Fixes
1.Made requests concurrently

* Review Fixes
1. Added New line at the end of file

* Review fixes
1.Made requests concurrently

* Review fixes
1.Made requests concurrently

* Review fixes
1.Made first letter copital of comments
2.Made requests concurrently with proper order

* Review Fixes
1.used gofmt and golint

* Merge branch 'permalink_preview' of /~https://github.com/Brightscout/mattermost-plugin-gitlab into permalink_preview_fixes

* Review fixes
1.Used better name of myMap

* Review Fixes
1.Used proper name of myMap

* Review fixes
1.Used make check-style

* Review Fixes
1.Removed one of the empty line

* Review Fixes
1.Revert all changes unrelated to the permalink preview feature.
2.Added checkAndRefreshToken method

* Review fixes
1.Changed the name of a function

* Unit tests started working

* Removed Package-lock.json

* [MI-1956] Added Package-lock.json

Co-authored-by: raghavaggarwal2308 <raghav.aggarwal@brightscout.com>
Co-authored-by: Nityanand Rai <nityanand.rai@brightscout.com>
@raghavaggarwal2308
Copy link
Contributor

@mickmister @levb Fixed the review fixes you mentioned.

Copy link
Contributor

@levb levb left a comment

Choose a reason for hiding this comment

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

LGTM (upon resolving @mickmister's comments)

@DHaussermann
Copy link

Sorry @raghavaggarwal2308 I missed that this was ready due to the label.

I'll look shortly.

@DHaussermann DHaussermann added Do Not Merge Should not be merged until this label is removed and removed Awaiting Submitter Action Blocked on the author labels Apr 26, 2023
@DHaussermann
Copy link

/update-branch

@mattermost-build
Copy link
Contributor

We don't have permissions to update this PR, please contact the submitter to apply the update.

@DHaussermann
Copy link

@raghavaggarwal2308 I have retested this and this is working as expected. Thank you 👍

I have only 1 small ask. Now that there is a separate piece of functionality involving private repos, the exiting filed no longer affects anything related to private repos.
image

For this PR - Can you please add some text to the description of the existing filed that will clarify the scope.
(Optional) Allow the plugin to work with private repositories. could be changed to (Optional) Allow the plugin to work with private repositories for subscriptions.

This will add context that the code previews are not covered by the existing switch.

@raghavaggarwal2308
Copy link
Contributor

raghavaggarwal2308 commented May 7, 2023

@DHaussermann Updated the description of EnablePrivateRepos

@hanzei hanzei requested a review from DHaussermann May 16, 2023 21:03
@hanzei hanzei removed the Do Not Merge Should not be merged until this label is removed label May 24, 2023
@hanzei
Copy link
Collaborator

hanzei commented May 24, 2023

@DHaussermann Heads up that the PR is ready for your review.

Copy link

@DHaussermann DHaussermann left a comment

Choose a reason for hiding this comment

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

Tested and passed

The last point has now been addressed. The label on the exiting radio button has been updated to clarify the scope.

LGTM!

@raghavaggarwal2308 thanks for the effort on this. Sorry it took me so long to circle back.

@DHaussermann DHaussermann added 4: Reviews Complete All reviewers have approved the pull request and removed Lifecycle/1:stale 3: QA Review Requires review by a QA tester labels May 25, 2023
@DHaussermann
Copy link

@hanzei I had applied the Do Not Merge label for an unrelated reason. We still have not cut a GitLab release for v1.7.0 which has the oAuth token fix.

I am reluctant to merge new features into master until we get this cut. Do you have any thoughts on this? I'll start a thread in the channel on Community.

@hanzei hanzei added the Do Not Merge Should not be merged until this label is removed label May 26, 2023
@mattermost-build
Copy link
Contributor

This PR has been automatically labelled "stale" because it hasn't had recent activity.
A core team member will check in on the status of the PR to help with questions.
Thank you for your contribution!

@DHaussermann DHaussermann removed the Do Not Merge Should not be merged until this label is removed label Jun 19, 2023
@DHaussermann
Copy link

@hanzei I'm okay with including this into 1.7.0 is we can merge is soon and it doesn't add significant delays.

@hanzei hanzei modified the milestones: v1.8.0, v1.7.0 Jun 19, 2023
@hanzei hanzei merged commit 842c6b3 into mattermost:master Jun 19, 2023
@raghavaggarwal2308 raghavaggarwal2308 deleted the permalink_preview branch April 9, 2024 07:25
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Backport github plugin improvements