-
Notifications
You must be signed in to change notification settings - Fork 2.2k
refactor(source/engine): move link generation and update from engine to source specific functions #4671
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
Closed
refactor(source/engine): move link generation and update from engine to source specific functions #4671
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package giturl | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| // TrimGitSuffix removes the .git suffix from a repository URL. | ||
| func TrimGitSuffix(repo string) string { | ||
| return strings.TrimSuffix(repo, ".git") | ||
| } | ||
|
|
||
| // EncodeSpecialChars encodes special characters in file paths that would | ||
| // break URL parsing. Specifically handles %, [, and ]. | ||
| func EncodeSpecialChars(path string) string { | ||
| path = strings.ReplaceAll(path, "%", "%25") | ||
| path = strings.ReplaceAll(path, "[", "%5B") | ||
| path = strings.ReplaceAll(path, "]", "%5D") | ||
| return path | ||
| } | ||
|
|
||
| // IsGistURL returns true if the repository URL is a GitHub gist. | ||
| func IsGistURL(repo string) bool { | ||
| return strings.Contains(repo, "gist.github.com") | ||
| } | ||
|
|
||
| // IsWikiURL returns true if the repository URL is a GitHub wiki. | ||
| func IsWikiURL(repo string) bool { | ||
| return strings.HasSuffix(repo, ".wiki.git") | ||
| } | ||
|
|
||
| // TrimWikiSuffix removes the .wiki.git suffix from a repository URL. | ||
| func TrimWikiSuffix(repo string) string { | ||
| return strings.TrimSuffix(repo, ".wiki.git") | ||
| } | ||
|
|
||
|
Comment on lines
+22
to
+36
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if it makes sense to have these Github-specific helpers in the |
||
| // CleanGistFilename converts a filename to the format used in gist URLs. | ||
| // Dots in filenames are replaced with hyphens. | ||
| func CleanGistFilename(filename string) string { | ||
| return strings.ReplaceAll(filename, ".", "-") | ||
| } | ||
|
|
||
| // TrimFileExtension removes the file extension from a path. | ||
| func TrimFileExtension(path string) string { | ||
| return strings.TrimSuffix(path, filepath.Ext(path)) | ||
| } | ||
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,140 @@ | ||
| package giturl | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestTrimGitSuffix(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| repo string | ||
| want string | ||
| }{ | ||
| {"with .git suffix", "https://github.com/owner/repo.git", "https://github.com/owner/repo"}, | ||
| {"without .git suffix", "https://github.com/owner/repo", "https://github.com/owner/repo"}, | ||
| {"empty string", "", ""}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := TrimGitSuffix(tt.repo); got != tt.want { | ||
| t.Errorf("TrimGitSuffix() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestEncodeSpecialChars(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| path string | ||
| want string | ||
| }{ | ||
| {"no special chars", "path/to/file.go", "path/to/file.go"}, | ||
| {"percent sign", "path/with%percent.go", "path/with%25percent.go"}, | ||
| {"square brackets", "path/with[brackets].go", "path/with%5Bbrackets%5D.go"}, | ||
| {"all special chars", "path%[test].go", "path%25%5Btest%5D.go"}, | ||
| {"empty string", "", ""}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := EncodeSpecialChars(tt.path); got != tt.want { | ||
| t.Errorf("EncodeSpecialChars() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestIsGistURL(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| repo string | ||
| want bool | ||
| }{ | ||
| {"github gist", "https://gist.github.com/user/abc123.git", true}, | ||
| {"github repo", "https://github.com/owner/repo.git", false}, | ||
| {"empty string", "", false}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := IsGistURL(tt.repo); got != tt.want { | ||
| t.Errorf("IsGistURL() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestIsWikiURL(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| repo string | ||
| want bool | ||
| }{ | ||
| {"github wiki", "https://github.com/owner/repo.wiki.git", true}, | ||
| {"github repo", "https://github.com/owner/repo.git", false}, | ||
| {"empty string", "", false}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := IsWikiURL(tt.repo); got != tt.want { | ||
| t.Errorf("IsWikiURL() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestTrimWikiSuffix(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| repo string | ||
| want string | ||
| }{ | ||
| {"with .wiki.git suffix", "https://github.com/owner/repo.wiki.git", "https://github.com/owner/repo"}, | ||
| {"without suffix", "https://github.com/owner/repo.git", "https://github.com/owner/repo.git"}, | ||
| {"empty string", "", ""}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := TrimWikiSuffix(tt.repo); got != tt.want { | ||
| t.Errorf("TrimWikiSuffix() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCleanGistFilename(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| filename string | ||
| want string | ||
| }{ | ||
| {"single extension", "config.yaml", "config-yaml"}, | ||
| {"multiple extensions", "config.yaml.example", "config-yaml-example"}, | ||
| {"no extension", "README", "README"}, | ||
| {"empty string", "", ""}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := CleanGistFilename(tt.filename); got != tt.want { | ||
| t.Errorf("CleanGistFilename() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestTrimFileExtension(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| path string | ||
| want string | ||
| }{ | ||
| {"with extension", "docs/README.md", "docs/README"}, | ||
| {"without extension", "docs/README", "docs/README"}, | ||
| {"empty string", "", ""}, | ||
| {"hidden file", ".gitignore", ""}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := TrimFileExtension(tt.path); got != tt.want { | ||
| t.Errorf("TrimFileExtension() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of duplicating the logic for this
else ifblock, can we add an||in the firstifcondition? Then when calling the link update function, we can add a smaller if else check.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah the duplication is messy but I was just trying to avoid nested ifs and making as few changes as possible in the previous flow (since it's temporary code if this gets approved)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplication like this can cause maintenance issues. This block already has one nested if so another one wouldn't increases the complexity level IMO