Repository navigation
MM-8880: Show code block previews of Github permalinks #137
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
62f05a6
Added code previews from permalink
agnivade 1947062
Incorporate review comments
agnivade 22fa562
Do not use githubClient as a struct field
agnivade ad4d384
Merge remote-tracking branch 'origin/master' into MM-18880
agnivade 6cfcbce
Next round of review
agnivade 578e65e
Fix minor grammatical typo
agnivade 5a3dc5a
Moving cancel into a defer clause
agnivade 5b2266e
Merge remote-tracking branch 'origin/master' into MM-18880
agnivade 2f39334
Ignore hook events where UserId is not set
agnivade File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,5 +5,5 @@ import ( | |
| ) | ||
|
|
||
| func main() { | ||
| plugin.ClientMain(&Plugin{}) | ||
| plugin.ClientMain(NewPlugin()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/hex" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/google/go-github/v25/github" | ||
| ) | ||
|
|
||
| // maxPermalinkReplacements sets the maximum limit to the number of | ||
| // permalink replacements that can be performed on a single message. | ||
| const maxPermalinkReplacements = 10 | ||
|
|
||
| const permalinkReqTimeout = 5 * time.Second | ||
|
|
||
| // maxPreviewLines sets the maximum number of preview lines that will be shown | ||
| // while replacing a permalink. | ||
| const maxPreviewLines = 10 | ||
|
|
||
| // permalinkLineContext shows the number of lines before and after to show | ||
| // if the link points to a single line. | ||
| const permalinkLineContext = 3 | ||
|
|
||
| // replacement holds necessary info to replace github permalinks | ||
| // in messages with a code preview block. | ||
| type replacement struct { | ||
| index int // index of the permalink in the string | ||
| word string // the permalink | ||
| permalinkInfo struct { // holds the necessary metadata of a permalink | ||
| haswww string | ||
| commit string | ||
| user string | ||
| repo string | ||
| path string | ||
| line string | ||
| } | ||
| } | ||
|
|
||
| // getReplacements returns the permalink replacements that needs to be performed | ||
| // on a message. The returned slice is sorted by the index in ascending order. | ||
| func (p *Plugin) getReplacements(msg string) []replacement { | ||
| // find the permalinks from the msg using a regex | ||
| matches := p.githubPermalinkRegex.FindAllStringSubmatch(msg, -1) | ||
| indices := p.githubPermalinkRegex.FindAllStringIndex(msg, -1) | ||
| var replacements []replacement | ||
| for i, m := range matches { | ||
| // have a limit on the number of replacements to do | ||
| if i > maxPermalinkReplacements { | ||
| break | ||
| } | ||
| word := m[0] | ||
| index := indices[i][0] | ||
| r := replacement{ | ||
| index: index, | ||
| word: word, | ||
| } | ||
| // ignore if the word is inside a link | ||
| if isInsideLink(msg, index) { | ||
| continue | ||
| } | ||
| // populate the permalinkInfo with the extracted groups of the regex | ||
| for j, name := range p.githubPermalinkRegex.SubexpNames() { | ||
| if j == 0 { | ||
| continue | ||
| } | ||
| switch name { | ||
| case "haswww": | ||
| r.permalinkInfo.haswww = m[j] | ||
| case "user": | ||
| r.permalinkInfo.user = m[j] | ||
| case "repo": | ||
| r.permalinkInfo.repo = m[j] | ||
| case "commit": | ||
| r.permalinkInfo.commit = m[j] | ||
| case "path": | ||
| r.permalinkInfo.path = m[j] | ||
| case "line": | ||
| r.permalinkInfo.line = m[j] | ||
| } | ||
| } | ||
| replacements = append(replacements, r) | ||
| } | ||
| return replacements | ||
| } | ||
|
|
||
| // makeReplacements perform 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, ghClient *github.Client) string { | ||
| // 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.API.LogError("bad git commit hash in permalink", "error", err.Error(), "hash", r.permalinkInfo.commit) | ||
| continue | ||
| } | ||
|
|
||
| // get the file contents | ||
| opts := github.RepositoryContentGetOptions{ | ||
| Ref: r.permalinkInfo.commit, | ||
| } | ||
| // TODO: make all of these requests concurrently. | ||
| reqctx, cancel := context.WithTimeout(context.Background(), permalinkReqTimeout) | ||
| fileContent, _, _, err := ghClient.Repositories.GetContents(reqctx, | ||
|
agnivade marked this conversation as resolved.
Resolved
|
||
| r.permalinkInfo.user, r.permalinkInfo.repo, r.permalinkInfo.path, &opts) | ||
| defer cancel() | ||
| if err != nil { | ||
| p.API.LogError("error while fetching file contents", "error", err.Error(), "path", r.permalinkInfo.path) | ||
| continue | ||
| } | ||
| // this is not a file, ignore. | ||
| if fileContent == nil { | ||
| p.API.LogWarn("permalink is not a file", "file", r.permalinkInfo.path) | ||
| continue | ||
| } | ||
| decoded, err := fileContent.GetContent() | ||
| if err != nil { | ||
| p.API.LogError("error while decoding file contents", "error", err.Error(), "path", r.permalinkInfo.path) | ||
| continue | ||
| } | ||
|
|
||
| // get the required lines. | ||
| start, end := getLineNumbers(r.permalinkInfo.line) | ||
| // bad anchor tag, ignore. | ||
| if start == -1 || end == -1 { | ||
| continue | ||
| } | ||
| isTruncated := false | ||
| if end-start > maxPreviewLines { | ||
| end = start + maxPreviewLines | ||
| isTruncated = true | ||
|
agnivade marked this conversation as resolved.
Resolved
|
||
| } | ||
| lines, err := filterLines(decoded, start, end) | ||
| if err != nil { | ||
| p.API.LogError("error while filtering lines", "error", err.Error(), "path", r.permalinkInfo.path) | ||
| } | ||
| if lines == "" { | ||
| p.API.LogError("line numbers out of range. Skipping.", "file", r.permalinkInfo.path, "start", start, "end", end) | ||
| continue | ||
| } | ||
| final := getCodeMarkdown(r.permalinkInfo.user, r.permalinkInfo.repo, r.permalinkInfo.path, r.word, lines, isTruncated) | ||
|
|
||
| // replace word in msg starting from r.index only once. | ||
| msg = msg[:r.index] + strings.Replace(msg[r.index:], r.word, final, 1) | ||
|
agnivade marked this conversation as resolved.
Resolved
|
||
| } | ||
| return msg | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.