From 6f83adf934b235fd12f2682c3cb4e2434e648247 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Sat, 4 Apr 2020 17:06:39 +0800 Subject: [PATCH 1/4] feat: support enterprise --- package.json | 2 ++ src/background.ts | 7 ++++++ src/manifest.json | 9 ++++++- webpack.config.js | 3 ++- yarn.lock | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 src/background.ts diff --git a/package.json b/package.json index 464eda1..72d3d7f 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,8 @@ "react-use": "^13.8.0", "react-window": "^1.8.5", "styled-components": "^5.0.1", + "webext-domain-permission-toggle": "^1.0.0", + "webext-dynamic-content-scripts": "^6.0.3", "webextension-polyfill": "^0.5.0" }, "devDependencies": { diff --git a/src/background.ts b/src/background.ts new file mode 100644 index 0000000..179d229 --- /dev/null +++ b/src/background.ts @@ -0,0 +1,7 @@ +import addDomainPermissionToggle from 'webext-domain-permission-toggle' +import 'webext-dynamic-content-scripts' + +addDomainPermissionToggle({ + title: 'Enable Gitako on this domain', + reloadOnSuccess: 'Refresh to activate Gitako?', +}) diff --git a/src/manifest.json b/src/manifest.json index aefade9..6f0c91d 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -6,8 +6,15 @@ "128": "icons/Gitako-128.png", "256": "icons/Gitako-256.png" }, - "permissions": ["storage", "*://*.github.com/*", "*://*.sentry.io/*"], + "permissions": ["storage", "contextMenus", "activeTab", "*://*.github.com/*"], + "optional_permissions": ["http://*/*", "https://*/*"], "web_accessible_resources": ["icons/vscode/*", "content.css"], + "background": { + "scripts": ["background.js"] + }, + "browser_action": { + "default_icon": "icons/Gitako.png" + }, "content_scripts": [ { "matches": ["https://github.com/*"], diff --git a/webpack.config.js b/webpack.config.js index 1483add..1a57d65 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -61,8 +61,9 @@ plugins.push( module.exports = { entry: { content: './src/content.tsx', + background: './src/background.ts', }, - devtool: IN_PRODUCTION_MODE ? 'source-map' : 'eval-source-map', + devtool: IN_PRODUCTION_MODE ? 'source-map' : 'inline-source-map', mode: IN_PRODUCTION_MODE ? 'production' : 'development', output: { path: path.resolve(__dirname, 'dist'), diff --git a/yarn.lock b/yarn.lock index 06f2cd1..3b10667 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1265,11 +1265,35 @@ "@testing-library/dom" "^6.11.0" "@types/testing-library__react" "^9.1.2" +"@types/chrome@0.0.86": + version "0.0.86" + resolved "https://registry.yarnpkg.com/@types/chrome/-/chrome-0.0.86.tgz#1026ef8e76db4fde1859cae858d73236fe670d69" + integrity sha512-7ehebPf/5IR64SYdD2Vig0q0oUCNbWMQ29kASlNJaHVVtA5/J/DnrxnYVPCID70o7LgHdYsav6I4/XdqAxjTLQ== + dependencies: + "@types/filesystem" "*" + "@types/color-name@^1.1.1": version "1.1.1" resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0" integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ== +"@types/filesystem@*": + version "0.0.29" + resolved "https://registry.yarnpkg.com/@types/filesystem/-/filesystem-0.0.29.tgz#ee3748eb5be140dcf980c3bd35f11aec5f7a3748" + integrity sha512-85/1KfRedmfPGsbK8YzeaQUyV1FQAvMPMTuWFQ5EkLd2w7szhNO96bk3Rh/SKmOfd9co2rCLf0Voy4o7ECBOvw== + dependencies: + "@types/filewriter" "*" + +"@types/filewriter@*": + version "0.0.28" + resolved "https://registry.yarnpkg.com/@types/filewriter/-/filewriter-0.0.28.tgz#c054e8af4d9dd75db4e63abc76f885168714d4b3" + integrity sha1-wFTor02d11205jq8dviFFocU1LM= + +"@types/firefox-webext-browser@^67.0.2": + version "67.0.2" + resolved "https://registry.yarnpkg.com/@types/firefox-webext-browser/-/firefox-webext-browser-67.0.2.tgz#04625d3294fbca54bccf42dae43f636c5b6452a2" + integrity sha512-tXR5THtH5Fqwfmi4y/2dTxCTebqHsKCH2bif/VdE5CPcB/S5x5fu+N+wBuMENzN41agovHMU/LQbtWNV+Aux+w== + "@types/firefox-webext-browser@^70.0.1": version "70.0.1" resolved "https://registry.yarnpkg.com/@types/firefox-webext-browser/-/firefox-webext-browser-70.0.1.tgz#53a3915bfbe25e2c5ec439dbdbe6e303610012bb" @@ -2939,6 +2963,13 @@ content-disposition@0.5.3: dependencies: safe-buffer "5.1.2" +content-scripts-register-polyfill@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/content-scripts-register-polyfill/-/content-scripts-register-polyfill-1.0.0.tgz#21bc2df475ea48aa83737d24675a6a44e4a006a7" + integrity sha512-DiHscMTyL5evEg6C8GSkkd5Kfkma726zxqIt+zv6L2W/gVqYOUZUw2F27BpAUp9nQZnIrky5Gx1eYA9Fw4+aeA== + dependencies: + "@types/firefox-webext-browser" "^67.0.2" + content-type@~1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.4.tgz#e138cc75e040c727b1966fe5e5f8c9aee256fe3b" @@ -9159,6 +9190,35 @@ web-ext@^3.2.0: yargs "13.3.0" zip-dir "1.0.2" +webext-additional-permissions@^0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/webext-additional-permissions/-/webext-additional-permissions-0.1.0.tgz#bc5a2a6cb8e60098c81a6004c446bfae2aefd85a" + integrity sha512-eA1o6BdRbpU5ClL6GcmwhGtVVzNTbQSqX9esakpurWYVt9ykpGQ1ZNe5l30Yt19GW0eRC8VdroOeJSumP5cMpQ== + dependencies: + "@types/chrome" "0.0.86" + +webext-domain-permission-toggle@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/webext-domain-permission-toggle/-/webext-domain-permission-toggle-1.0.0.tgz#6f9a6d3234ffe596921d1be5ae8823ef5b02ebd2" + integrity sha512-K6RAOYtVCMAmqjQPopMbGOIoxpFU05ERGgaLkUNvFCdpPsXLB5y4m1+Cq1dezyfx6FU0lLEqxoL/5/49+EY5zA== + dependencies: + webext-additional-permissions "^0.1.0" + +webext-dynamic-content-scripts@^6.0.3: + version "6.0.3" + resolved "https://registry.yarnpkg.com/webext-dynamic-content-scripts/-/webext-dynamic-content-scripts-6.0.3.tgz#729a5cf4408dca589af889cb403943ab414c2ecd" + integrity sha512-MIH4UOnkUl4hapl83uMQT5t2bbYXTPO/mxhFFBcesm2mGvnG3OrNKNiaPRFL6AdcPvqDVae8UcWcWHSflptLTQ== + dependencies: + "@types/chrome" "0.0.86" + content-scripts-register-polyfill "^1.0.0" + webext-additional-permissions "^0.1.0" + webext-permissions-events-polyfill "^1.0.1" + +webext-permissions-events-polyfill@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/webext-permissions-events-polyfill/-/webext-permissions-events-polyfill-1.0.1.tgz#c5108d140b03efbd06b34c7211694b8cde928177" + integrity sha512-dPsU7KtDn+OG+mPy0LLc9UhMXdVeQ8ywD5kK1GFtvceur4IWOnpI5D4uTjMPrKm8+re/8cbHKwVjnviIA9wZ3Q== + webextension-polyfill@^0.5.0: version "0.5.0" resolved "https://registry.yarnpkg.com/webextension-polyfill/-/webextension-polyfill-0.5.0.tgz#795e0bf6a2b8eadcdb6edaecd169e9228c747519" From 749af84cbbcb13e6357c483adb906c08f5d46f78 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Fri, 1 May 2020 18:54:43 +0800 Subject: [PATCH 2/4] feat: update API endpoint for enterprise --- src/platforms/GitHub/API.ts | 9 ++++++--- src/platforms/GitHub/index.ts | 7 ++++++- src/platforms/Gitee/index.ts | 3 +++ src/platforms/dummyPlatformForTypeSafety.ts | 3 +++ src/platforms/platform.d.ts | 1 + src/utils/gitSubmodule.ts | 2 +- 6 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/platforms/GitHub/API.ts b/src/platforms/GitHub/API.ts index e5e0272..0a15427 100644 --- a/src/platforms/GitHub/API.ts +++ b/src/platforms/GitHub/API.ts @@ -1,4 +1,5 @@ import { errors } from 'platforms' +import { isEnterprise } from '.' function apiRateLimitExceeded(content: any /* safe any */) { return content?.['documentation_url'] === 'https://developer.github.com/v3/#rate-limiting' @@ -55,12 +56,14 @@ async function request( } } +const API_ENDPOINT = isEnterprise() ? `${window.location.host}/api/v3` : 'api.github.com' + export async function getRepoMeta( userName: string, repoName: string, accessToken?: string, ): Promise { - const url = `https://api.github.com/repos/${userName}/${repoName}` + const url = `https://${API_ENDPOINT}/repos/${userName}/${repoName}` return await request(url, { accessToken }) } @@ -70,7 +73,7 @@ export async function getTreeData( branchName: string, accessToken?: string, ): Promise { - const url = `https://api.github.com/repos/${userName}/${repoName}/git/trees/${branchName}?recursive=1` + const url = `https://${API_ENDPOINT}/repos/${userName}/${repoName}/git/trees/${branchName}?recursive=1` return await request(url, { accessToken }) } @@ -80,7 +83,7 @@ export async function getBlobData( sha: string, accessToken?: string, ): Promise { - const url = `https://api.github.com/repos/${userName}/${repoName}/git/blobs/${sha}` + const url = `https://${API_ENDPOINT}/repos/${userName}/${repoName}/git/blobs/${sha}` return await request(url, { accessToken }) } diff --git a/src/platforms/GitHub/index.ts b/src/platforms/GitHub/index.ts index 9f88a7a..08b00a7 100644 --- a/src/platforms/GitHub/index.ts +++ b/src/platforms/GitHub/index.ts @@ -76,10 +76,15 @@ function getUrlForRedirect( type = 'blob', path = '', ) { - return `https://github.com/${userName}/${repoName}/${type}/${branchName}/${path}` + return `https://${window.location.host}/${userName}/${repoName}/${type}/${branchName}/${path}` +} + +export function isEnterprise() { + return !window.location.host.endsWith('github.com') } export const GitHub: Platform = { + isEnterprise, resolveMeta() { if (!URLHelper.isInRepoPage()) { return null diff --git a/src/platforms/Gitee/index.ts b/src/platforms/Gitee/index.ts index cb2916c..68075ba 100644 --- a/src/platforms/Gitee/index.ts +++ b/src/platforms/Gitee/index.ts @@ -78,6 +78,9 @@ function getUrlForRedirect( } export const Gitee: Platform = { + isEnterprise() { + return !window.location.host.endsWith('gitee.com') + }, resolveMeta() { if (!DOMHelper.isInRepoPage()) { return null diff --git a/src/platforms/dummyPlatformForTypeSafety.ts b/src/platforms/dummyPlatformForTypeSafety.ts index 9852e99..8f40194 100644 --- a/src/platforms/dummyPlatformForTypeSafety.ts +++ b/src/platforms/dummyPlatformForTypeSafety.ts @@ -1,4 +1,7 @@ export const dummyPlatformForTypeSafety: Platform = { + isEnterprise() { + return false + }, resolveMeta() { return null }, diff --git a/src/platforms/platform.d.ts b/src/platforms/platform.d.ts index 0746e30..cbf60cc 100644 --- a/src/platforms/platform.d.ts +++ b/src/platforms/platform.d.ts @@ -1,4 +1,5 @@ type Platform = { + isEnterprise(): boolean resolveMeta(): MetaData | null getMetaData( metaData: Pick, diff --git a/src/utils/gitSubmodule.ts b/src/utils/gitSubmodule.ts index b1fa06d..be78173 100644 --- a/src/utils/gitSubmodule.ts +++ b/src/utils/gitSubmodule.ts @@ -11,7 +11,7 @@ function transformModuleGitURL(node: TreeNode, URL: string) { const matched = URL.match(subModuleURLRegex.git) if (!matched) return const [_, userName, repoName] = matched - return appendCommitPath(`https://github.com/${userName}/${repoName}`, node) + return appendCommitPath(`https://${window.location.host}/${userName}/${repoName}`, node) } function cutDotGit(URL: string) { From aaf64667ea45c85b6441eb6e4e47490b7729f67c Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Tue, 5 May 2020 20:39:07 +0800 Subject: [PATCH 3/4] build: generate assets for release tags --- .github/workflows/release-assets.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/release-assets.yml b/.github/workflows/release-assets.yml index 67da144..155f623 100644 --- a/.github/workflows/release-assets.yml +++ b/.github/workflows/release-assets.yml @@ -4,6 +4,7 @@ on: push: tags: - v[0-9]+.* # roughly matching version numbers + - release-* jobs: release: From 36e7509c60d1932307fcca8bb333de2642e775d1 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Wed, 6 May 2020 12:09:51 +0800 Subject: [PATCH 4/4] fix: add domains to manifest --- src/manifest.json | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/manifest.json b/src/manifest.json index 6f0c91d..da6114f 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -6,7 +6,14 @@ "128": "icons/Gitako-128.png", "256": "icons/Gitako-256.png" }, - "permissions": ["storage", "contextMenus", "activeTab", "*://*.github.com/*"], + "permissions": [ + "storage", + "contextMenus", + "activeTab", + "*://*.github.com/*", + "*://gitako.now.sh/*", + "*://*.sentry.io/*" + ], "optional_permissions": ["http://*/*", "https://*/*"], "web_accessible_resources": ["icons/vscode/*", "content.css"], "background": {