From 7cab7b3a78dadf286418a9a549085ab6bf44c336 Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Sat, 17 Apr 2021 13:10:31 -0400 Subject: [PATCH 01/10] fix: url encoding of url-unsafe characters --- package.json | 2 ++ src/extension.ts | 7 ++++--- src/providers.ts | 35 +++++++++++++++++++++-------------- src/test/providers.test.ts | 16 ++++++++++++++++ yarn.lock | 10 ++++++++++ 5 files changed, 53 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 52081b3..1ddd703 100644 --- a/package.json +++ b/package.json @@ -301,6 +301,7 @@ }, "devDependencies": { "@types/ini": "^1.3.30", + "@types/lodash": "^4.14.168", "@types/mocha": "^2.2.42", "@types/mz": "^0.0.32", "@types/node": "^10.12.21", @@ -312,6 +313,7 @@ "dependencies": { "gitconfiglocal": "^2.1.0", "ini": "^1.3.5", + "lodash": "^4.17.21", "mz": "^2.7.0" } } diff --git a/src/extension.ts b/src/extension.ts index 1b92af3..d7cdc7d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -242,10 +242,11 @@ async function githubinator({ return err("could not find url") } if (openUrl) { - vscode.env.openExternal(vscode.Uri.parse(url)) + // @ts-ignore This works. Using vscode.Uri.parse double encodes characters which breaks URLs. + await vscode.env.openExternal(url) } if (copyToClipboard) { - vscode.env.clipboard.writeText(url) - vscode.window.showInformationMessage("URL copied to clipboard.") + await vscode.env.clipboard.writeText(url) + await vscode.window.showInformationMessage("URL copied to clipboard.") } } diff --git a/src/providers.ts b/src/providers.ts index 40c1b9b..e34a1cd 100644 --- a/src/providers.ts +++ b/src/providers.ts @@ -2,6 +2,7 @@ import * as path from "path" import * as url from "url" import { IProviderConfig } from "./extension" import { cleanHostname } from "./utils" +import { flatten } from "lodash" interface IBaseGetUrls { readonly selection: [number | null, number | null] @@ -102,6 +103,12 @@ abstract class BaseProvider { abstract getUrls(params: IBaseGetUrls): Promise } +function pathJoin(...args: string[]): string { + return path.join( + ...flatten(args.map(x => x.split("/"))).map(encodeURIComponent), + ) +} + export class Github extends BaseProvider { DEFAULT_HOSTNAMES = ["github.com"] PROVIDER_NAME = "github" @@ -132,7 +139,7 @@ export class Github extends BaseProvider { return null } const u = new url.URL( - path.join( + pathJoin( repoInfo.org, repoInfo.repo, mode, @@ -150,11 +157,11 @@ export class Github extends BaseProvider { const blameUrl = createUrl("blame") const historyUrl = createUrl("commits", false) const compareUrl = new url.URL( - path.join(repoInfo.org, repoInfo.repo, "compare", head.value), + pathJoin(repoInfo.org, repoInfo.repo, "compare", head.value), rootUrl, ).toString() const prUrl = new url.URL( - path.join(repoInfo.org, repoInfo.repo, "pull", "new", head.value), + pathJoin(repoInfo.org, repoInfo.repo, "pull", "new", head.value), rootUrl, ).toString() return { @@ -190,7 +197,7 @@ export class Gitlab extends BaseProvider { const lines = start != null && end != null ? `L${start + 1}-${end + 1}` : null const repoUrl = new url.URL( - path.join(repoInfo.org, repoInfo.repo), + pathJoin(repoInfo.org, repoInfo.repo), rootUrl, ).toString() const createUrl = (mode: string, hash = true) => { @@ -198,7 +205,7 @@ export class Gitlab extends BaseProvider { return null } const u = new url.URL( - path.join( + pathJoin( repoInfo.org, repoInfo.repo, mode, @@ -216,12 +223,12 @@ export class Gitlab extends BaseProvider { const blameUrl = createUrl("blame") const historyUrl = createUrl("commits", false) const compareUrl = new url.URL( - path.join(repoInfo.org, repoInfo.repo, "compare", head.value), + pathJoin(repoInfo.org, repoInfo.repo, "compare", head.value), rootUrl, ).toString() // https://gitlab.com/recipeyak/recipeyak/merge_requests/new?merge_request%5Bsource_branch%5D=master const prUrl = new url.URL( - path.join(repoInfo.org, repoInfo.repo, "merge_requests", "new"), + pathJoin(repoInfo.org, repoInfo.repo, "merge_requests", "new"), rootUrl, ) prUrl.search = `merge_request%5Bsource_branch%5D=${head.value}` @@ -258,7 +265,7 @@ export class Bitbucket extends BaseProvider { const lines = start != null && end != null ? `lines-${start + 1}:${end + 1}` : null const repoUrl = new url.URL( - path.join(repoInfo.org, repoInfo.repo), + pathJoin(repoInfo.org, repoInfo.repo), rootUrl, ).toString() const createUrl = (mode: string, hash = true) => { @@ -266,7 +273,7 @@ export class Bitbucket extends BaseProvider { return null } const u = new url.URL( - path.join( + pathJoin( repoInfo.org, repoInfo.repo, mode, @@ -283,7 +290,7 @@ export class Bitbucket extends BaseProvider { const blobUrl = createUrl("blob") const blameUrl = createUrl("annotate") const compareUrl = new url.URL( - path.join( + pathJoin( repoInfo.org, repoInfo.repo, "branches", @@ -295,7 +302,7 @@ export class Bitbucket extends BaseProvider { const historyUrl = createUrl("history-node", false) // "https://bitbucket.org/recipeyak/recipeyak/pull-requests/new?source=db99a912f5c4bffe11d91e163cd78ed96589611b" const prUrl = new url.URL( - path.join(repoInfo.org, repoInfo.repo, "pull-requests", "new"), + pathJoin(repoInfo.org, repoInfo.repo, "pull-requests", "new"), rootUrl, ) prUrl.search = `source=${head.value}` @@ -337,7 +344,7 @@ export class VisualStudio extends BaseProvider { ? `&line=${start + 1}&lineEnd=${end + 1}` : null const repoUrl = new url.URL( - path.join(repoInfo.org, "_git", repoInfo.repo), + pathJoin(repoInfo.org, "_git", repoInfo.repo), rootUrl, ) let filePath = relativeFilePath @@ -359,12 +366,12 @@ export class VisualStudio extends BaseProvider { historyUrl.search = baseSearch + "&_a=history" } const compareUrl = new url.URL( - path.join(repoInfo.org, "_git", repoInfo.repo, "branches"), + pathJoin(repoInfo.org, "_git", repoInfo.repo, "branches"), rootUrl, ) compareUrl.search = `targetVersion=${version}&_a=commits` const prUrl = new url.URL( - path.join(repoInfo.org, "_git", repoInfo.repo, "pullrequestcreate"), + pathJoin(repoInfo.org, "_git", repoInfo.repo, "pullrequestcreate"), rootUrl, ) prUrl.search = `sourceRef=${head.value}` diff --git a/src/test/providers.test.ts b/src/test/providers.test.ts index 72205bc..eef7c54 100644 --- a/src/test/providers.test.ts +++ b/src/test/providers.test.ts @@ -5,9 +5,25 @@ import { createSha, createBranch, Github, + pathJoin, } from "../providers" import * as assert from "assert" +suite("utils", async () => { + test("pathJoin", () => { + assert.strictEqual( + pathJoin( + "ghost", + "ghost.github.io", + "blob", + "fix-#123✅", + "C#/C#.Package", + ), + "ghost/ghost.github.io/blob/fix/fix-%23123%E2%9C%85/C%23/C%23.Package", + ) + }) +}) + suite("Github", async () => { test("ssh", async () => { for (let url of [ diff --git a/yarn.lock b/yarn.lock index 7fe8134..d19b268 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12,6 +12,11 @@ resolved "https://registry.yarnpkg.com/@types/ini/-/ini-1.3.30.tgz#d1485459c9fad84e937414b832a2adb649eab379" integrity sha512-2+iF8zPSbpU83UKE+PNd4r/MhwNAdyGpk3H+VMgEH3EhjFZq1kouLgRoZrmIcmoGX97xFvqdS44DkICR5Nz3tQ== +"@types/lodash@^4.14.168": + version "4.14.168" + resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.168.tgz#fe24632e79b7ade3f132891afff86caa5e5ce008" + integrity sha512-oVfRvqHV/V6D1yifJbVRU3TMp8OT6o6BG+U9MkwuJ3U8/CsDHvalRpsxBqivn71ztOFZBTfJMvETbqHiaNSj7Q== + "@types/mocha@^2.2.42": version "2.2.48" resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-2.2.48.tgz#3523b126a0b049482e1c3c11877460f76622ffab" @@ -336,6 +341,11 @@ js-yaml@^3.7.0: argparse "^1.0.7" esprima "^4.0.0" +lodash@^4.17.21: + version "4.17.21" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c" + integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg== + minimatch@3.0.4, minimatch@^3.0.4: version "3.0.4" resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" From 3308e79ace509ab9f2cdeafbe36b6a5e920d6e27 Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Sat, 17 Apr 2021 13:11:31 -0400 Subject: [PATCH 02/10] whoops --- src/providers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/providers.ts b/src/providers.ts index e34a1cd..b762a3f 100644 --- a/src/providers.ts +++ b/src/providers.ts @@ -103,7 +103,7 @@ abstract class BaseProvider { abstract getUrls(params: IBaseGetUrls): Promise } -function pathJoin(...args: string[]): string { +export function pathJoin(...args: string[]): string { return path.join( ...flatten(args.map(x => x.split("/"))).map(encodeURIComponent), ) From dcbe6118714251d018d3109757b6058c95801528 Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Sat, 17 Apr 2021 13:17:29 -0400 Subject: [PATCH 03/10] . --- src/test/providers.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/providers.test.ts b/src/test/providers.test.ts index eef7c54..a08a5ea 100644 --- a/src/test/providers.test.ts +++ b/src/test/providers.test.ts @@ -16,10 +16,10 @@ suite("utils", async () => { "ghost", "ghost.github.io", "blob", - "fix-#123✅", + "fixit/-#123✅", "C#/C#.Package", ), - "ghost/ghost.github.io/blob/fix/fix-%23123%E2%9C%85/C%23/C%23.Package", + "ghost/ghost.github.io/blob/fixit--%23123%E2%9C%85/C%23/C%23.Package", ) }) }) From a8e803a77c58a3ef85c3f93ce0c41d9aed541601 Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Sat, 17 Apr 2021 15:27:10 -0400 Subject: [PATCH 04/10] . --- src/test/providers.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/providers.test.ts b/src/test/providers.test.ts index a08a5ea..b647fc9 100644 --- a/src/test/providers.test.ts +++ b/src/test/providers.test.ts @@ -19,7 +19,7 @@ suite("utils", async () => { "fixit/-#123✅", "C#/C#.Package", ), - "ghost/ghost.github.io/blob/fixit--%23123%E2%9C%85/C%23/C%23.Package", + "ghost/ghost.github.io/blob/fixit/-%23123%E2%9C%85/C%23/C%23.Package", ) }) }) From 65d0f350014f7c7bd29389b905ce3163f373d947 Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Sat, 17 Apr 2021 15:37:39 -0400 Subject: [PATCH 05/10] doc --- CHANGELOG.md | 6 ++++++ README.md | 4 ++++ package.json | 2 +- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bca8f49..37fc5eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## 1.0.1 - 2021-04-17 + +### Fixed + +- Fixed URL escaping of branch and file names. + ## 1.0.0 - 2021-04-17 ### Added diff --git a/README.md b/README.md index 227b407..f6305ba 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,10 @@ The "main" branch is configured via `githubinator.mainBranches` (see "Extension ## Release Notes +## 1.0.1 + +- Fixed URL escaping of branch and file names. + ## 1.0.0 - support multiple default branches. vscode-githubinator now attempts to open `main`, then `master`, `trunk`, `develop`, and `dev`. Configure these branches with the `githubinator.mainBranches` option. diff --git a/package.json b/package.json index 1ddd703..ba9626f 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "githubinator", "displayName": "Githubinator", "description": "Quickly open files on Github and other providers. View blame information, copy permalinks and more. See the \"commands\" section of the README for more details.", - "version": "1.0.0", + "version": "1.0.1", "publisher": "chdsbd", "license": "SEE LICENSE IN LICENSE", "icon": "images/logo256.png", From fed8c5ee02e6a00e893a4b0ff5f3bc2e9d934f0c Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Sat, 17 Apr 2021 15:39:55 -0400 Subject: [PATCH 06/10] test --- ...4\267\360\237\217\273\342\200\215\342\231\202\357\270\217.txt" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "blah-#\360\237\244\267\360\237\217\273\342\200\215\342\231\202\357\270\217.txt" diff --git "a/blah-#\360\237\244\267\360\237\217\273\342\200\215\342\231\202\357\270\217.txt" "b/blah-#\360\237\244\267\360\237\217\273\342\200\215\342\231\202\357\270\217.txt" new file mode 100644 index 0000000..e69de29 From 86db4dc96498bb8b5a71e64b527605cc737f0882 Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Sat, 17 Apr 2021 15:44:11 -0400 Subject: [PATCH 07/10] teset --- src/test/providers.test.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/providers.test.ts b/src/test/providers.test.ts index b647fc9..d26504c 100644 --- a/src/test/providers.test.ts +++ b/src/test/providers.test.ts @@ -21,6 +21,16 @@ suite("utils", async () => { ), "ghost/ghost.github.io/blob/fixit/-%23123%E2%9C%85/C%23/C%23.Package", ) + assert.strictEqual( + pathJoin( + "ghost", + "ghost.github.io", + "blob", + "chris/fix%23123-✅", + "blah-#🤷🏻‍♂️.txt", + ), + "ghost/ghost.github.io/blob/chris/fix%23123-✅/blah-%23🤷🏻%E2%80%8D♂%EF%B8%8F.txt", + ) }) }) From a4678db4eb2602c0ee6259a7bb957cbe3e67e12d Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Sat, 17 Apr 2021 15:47:12 -0400 Subject: [PATCH 08/10] . --- src/test/providers.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/providers.test.ts b/src/test/providers.test.ts index d26504c..ad2d01c 100644 --- a/src/test/providers.test.ts +++ b/src/test/providers.test.ts @@ -26,7 +26,7 @@ suite("utils", async () => { "ghost", "ghost.github.io", "blob", - "chris/fix%23123-✅", + "chris/fix#123-✅", "blah-#🤷🏻‍♂️.txt", ), "ghost/ghost.github.io/blob/chris/fix%23123-✅/blah-%23🤷🏻%E2%80%8D♂%EF%B8%8F.txt", From a5eeac95457abd4e7a6e2c4322264e92732be46c Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Sat, 17 Apr 2021 15:52:26 -0400 Subject: [PATCH 09/10] . --- src/test/providers.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/providers.test.ts b/src/test/providers.test.ts index ad2d01c..3bca8ce 100644 --- a/src/test/providers.test.ts +++ b/src/test/providers.test.ts @@ -29,7 +29,7 @@ suite("utils", async () => { "chris/fix#123-✅", "blah-#🤷🏻‍♂️.txt", ), - "ghost/ghost.github.io/blob/chris/fix%23123-✅/blah-%23🤷🏻%E2%80%8D♂%EF%B8%8F.txt", + "ghost/ghost.github.io/blob/chris/fix%23123-%E2%9C%85/blah-%23%F0%9F%A4%B7%F0%9F%8F%BB%E2%80%8D%E2%99%82%EF%B8%8F.txt", ) }) }) From 8e923cb243dc5cbefc66519069d242e06a72bcee Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Sat, 17 Apr 2021 15:54:16 -0400 Subject: [PATCH 10/10] . --- ...4\267\360\237\217\273\342\200\215\342\231\202\357\270\217.txt" | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 "blah-#\360\237\244\267\360\237\217\273\342\200\215\342\231\202\357\270\217.txt" diff --git "a/blah-#\360\237\244\267\360\237\217\273\342\200\215\342\231\202\357\270\217.txt" "b/blah-#\360\237\244\267\360\237\217\273\342\200\215\342\231\202\357\270\217.txt" deleted file mode 100644 index e69de29..0000000