From 258a8284d294d01b4228ca17d3b4d591692a890b Mon Sep 17 00:00:00 2001 From: Brian Douglas Date: Sat, 27 May 2023 07:58:00 -0700 Subject: [PATCH 01/17] fix: remove auto-take message from triage.yml --- .github/workflows/triage.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/triage.yml b/.github/workflows/triage.yml index 2c94a619..0313fd78 100644 --- a/.github/workflows/triage.yml +++ b/.github/workflows/triage.yml @@ -15,6 +15,5 @@ jobs: - name: disable take an issue uses: bdougie/take-action@main with: - message: The auto-take issues workflow is not enabled here, we can only assign issues to @open-sauced/triage, @open-sauced/committers, @open-sauced/maintainers in this repo for now. We suggest folks not on the team open issues and take a look at the [Bugs](https://github.com/orgs/open-sauced/projects/6) or [Papercuts](https://github.com/open-sauced/insights/issues?q=is%3Aopen+is%3Aissue+milestone%3APapercuts). If you would like to find good first issues, please check out the companion project to this, [open-sauced/hot](https://github.com/open-sauced/hot). issueCurrentlyAssignedMessage: Thanks for being interested in this issue. It looks like this ticket is already assigned to a contributor. token: ${{ secrets.GITHUB_TOKEN }} From 3b9a14bab991c739b9e57b1d0d829100695f47fb Mon Sep 17 00:00:00 2001 From: Brian Douglas Date: Sat, 27 May 2023 07:58:32 -0700 Subject: [PATCH 02/17] fix: rename triage.yml --- .github/workflows/triage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/triage.yml b/.github/workflows/triage.yml index 0313fd78..dc999dbe 100644 --- a/.github/workflows/triage.yml +++ b/.github/workflows/triage.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 10 steps: - - name: disable take an issue + - name: take an issue uses: bdougie/take-action@main with: issueCurrentlyAssignedMessage: Thanks for being interested in this issue. It looks like this ticket is already assigned to a contributor. From c9b4207df064bb1255ea23ad0aae37e1d0070977 Mon Sep 17 00:00:00 2001 From: Brian Douglas Date: Sat, 27 May 2023 14:59:51 +0000 Subject: [PATCH 03/17] chore(patch): release 1.3.1-beta.1 on beta channel [skip ci] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## [1.3.1-beta.1](https://github.com/open-sauced/ai/compare/v1.3.0...v1.3.1-beta.1) (2023-05-27) ### 🐛 Bug Fixes * remove auto-take message from triage.yml ([258a828](https://github.com/open-sauced/ai/commit/258a8284d294d01b4228ca17d3b4d591692a890b)) * rename triage.yml ([3b9a14b](https://github.com/open-sauced/ai/commit/3b9a14bab991c739b9e57b1d0d829100695f47fb)) --- CHANGELOG.md | 8 ++++++++ npm-shrinkwrap.json | 4 ++-- package.json | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1db2de5e..bbe29e1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ > All notable changes to this project will be documented in this file +## [1.3.1-beta.1](https://github.com/open-sauced/ai/compare/v1.3.0...v1.3.1-beta.1) (2023-05-27) + + +### 🐛 Bug Fixes + +* remove auto-take message from triage.yml ([258a828](https://github.com/open-sauced/ai/commit/258a8284d294d01b4228ca17d3b4d591692a890b)) +* rename triage.yml ([3b9a14b](https://github.com/open-sauced/ai/commit/3b9a14bab991c739b9e57b1d0d829100695f47fb)) + ## [1.3.0](https://github.com/open-sauced/ai/compare/v1.2.0...v1.3.0) (2023-05-27) diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index d5e8385d..635ae41e 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,12 +1,12 @@ { "name": "opensauced-browser-extension", - "version": "1.3.0", + "version": "1.3.1-beta.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "opensauced-browser-extension", - "version": "1.3.0", + "version": "1.3.1-beta.1", "dependencies": { "date-fns": "^2.30.0", "gpt-tokenizer": "^1.0.5", diff --git a/package.json b/package.json index dc75f8c9..fcacdeda 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "opensauced-browser-extension", "private": true, - "version": "1.3.0", + "version": "1.3.1-beta.1", "files": [ "dist" ], From 26702e689641138a791f3c8cb935ddc99540a19e Mon Sep 17 00:00:00 2001 From: Ozgur Sar Date: Sat, 27 May 2023 21:12:13 +0300 Subject: [PATCH 04/17] test: add tests for utils/urlMatchers.ts (#143) * add tests for urlMatchers * Update test/utils/urlMatcher.test.ts I agree Co-authored-by: Brian Douglas * change abcd to google.com --------- Co-authored-by: Brian Douglas --- test/utils/urlMatcher.test.ts | 51 ++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/test/utils/urlMatcher.test.ts b/test/utils/urlMatcher.test.ts index 2f4514de..cfe40882 100644 --- a/test/utils/urlMatcher.test.ts +++ b/test/utils/urlMatcher.test.ts @@ -20,7 +20,7 @@ test('getGithubUsername', () => { expect(getGithubUsername('https://www.github.com/username/repo/pulls')).toBe('username') expect(getGithubUsername('www.github.com/123')).toBe('123') expect(getGithubUsername('github.com/123/')).toBe('123') - expect(getGithubUsername('abcd')).toBe(undefined) + expect(getGithubUsername('https://google.com/')).toBe(undefined) }) test('getLinkedInUsername', () => { @@ -41,6 +41,55 @@ test('getTwitterUsername', () => { expect(getTwitterUsername('twitter.com/123/')).toBe('123') }) +test('isGithubPullRequestPage', () => { + expect(isGithubPullRequestPage('https://www.github.com/')).toBe(false) + expect(isGithubPullRequestPage('https://www.github.com/pull')).toBe(false) + expect(isGithubPullRequestPage('https://www.github.com/pull/')).toBe(false) + expect(isGithubPullRequestPage('https://www.github.com/username/repo/pull/123')).toBe(true) + expect(isGithubPullRequestPage('https://www.github.com/username/repo/pull/123/')).toBe(true) + expect(isGithubPullRequestPage('www.github.com/username/repo/pull/123')).toBe(true) + expect(isGithubPullRequestPage('github.com/username/repo/pull/123/')).toBe(true) + expect(isGithubPullRequestPage('https://google.com/')).toBe(false) +}) +test('isGithubProfilePage', () => { + expect(isGithubProfilePage('https://www.github.com/')).toBe(false) + expect(isGithubProfilePage('https://www.github.com/username')).toBe(true) + expect(isGithubProfilePage('https://www.github.com/username/')).toBe(false) + expect(isGithubProfilePage('https://www.github.com/username?tab=repositories')).toBe(true) + expect(isGithubProfilePage('www.github.com/username')).toBe(true) + expect(isGithubProfilePage('github.com/username')).toBe(true) + expect(isGithubProfilePage('github.com/username/')).toBe(false) + expect(isGithubProfilePage('https://google.com/')).toBe(false) +}) +test('isGithubRepoPage', () => { + expect(isGithubRepoPage('https://www.github.com/')).toBe(false) + expect(isGithubRepoPage('https://www.github.com/username/')).toBe(false) + expect(isGithubRepoPage('https://www.github.com/username/repo')).toBe(true) + expect(isGithubRepoPage('https://www.github.com/username/repo/')).toBe(false) + expect(isGithubRepoPage('https://www.github.com/username/repo/https://google.com/')).toBe(false) + expect(isGithubRepoPage('www.github.com/username/repo')).toBe(true) + expect(isGithubRepoPage('github.com/username/repo')).toBe(true) + expect(isGithubRepoPage('https://google.com/')).toBe(false) +}) + +test('isPullRequestCreatePage', () => { + expect(isPullRequestCreatePage('https://www.github.com/')).toBe(false) + expect(isPullRequestCreatePage('https://github.com/username/repo/compare/')).toBe(false) + expect(isPullRequestCreatePage('https://github.com/username/repo/compare/https://google.com/')).toBe(true) + expect(isPullRequestCreatePage('https://github.com/username/repo/compare/https://google.com//')).toBe(true) + expect(isPullRequestCreatePage('www.github.com/username/repo/compare/https://google.com//')).toBe(true) + expect(isPullRequestCreatePage('github.com/username/repo/compare/https://google.com//')).toBe(true) + expect(isPullRequestCreatePage('https://google.com/')).toBe(false) +}) +test('isPullRequestFilesChangedPage', () => { + expect(isPullRequestFilesChangedPage('https://www.github.com/')).toBe(false) + expect(isPullRequestFilesChangedPage('https://github.com/username/repo/pull/123/files')).toBe(true) + expect(isPullRequestFilesChangedPage('https://github.com/username/repo/pull/123/files/')).toBe(true) + expect(isPullRequestFilesChangedPage('www.github.com/username/repo/pull/123/files')).toBe(true) + expect(isPullRequestFilesChangedPage('github.com/username/repo/pull/123/files/')).toBe(true) + expect(isPullRequestFilesChangedPage('github.com/username/repo/pull/123/fil')).toBe(false) + expect(isPullRequestFilesChangedPage('https://google.com/')).toBe(false) +}) \ No newline at end of file From 77addd1d932971891328237e768aea0b18815f65 Mon Sep 17 00:00:00 2001 From: Ozgur Sar Date: Sat, 27 May 2023 18:13:50 +0000 Subject: [PATCH 05/17] chore(patch): release 1.3.1-beta.2 on beta channel [skip ci] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## [1.3.1-beta.2](https://github.com/open-sauced/ai/compare/v1.3.1-beta.1...v1.3.1-beta.2) (2023-05-27) ### ✅ Tests * add tests for utils/urlMatchers.ts ([#143](https://github.com/open-sauced/ai/issues/143)) ([26702e6](https://github.com/open-sauced/ai/commit/26702e689641138a791f3c8cb935ddc99540a19e)) --- CHANGELOG.md | 7 +++++++ npm-shrinkwrap.json | 4 ++-- package.json | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbe29e1b..40e99d8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ > All notable changes to this project will be documented in this file +## [1.3.1-beta.2](https://github.com/open-sauced/ai/compare/v1.3.1-beta.1...v1.3.1-beta.2) (2023-05-27) + + +### ✅ Tests + +* add tests for utils/urlMatchers.ts ([#143](https://github.com/open-sauced/ai/issues/143)) ([26702e6](https://github.com/open-sauced/ai/commit/26702e689641138a791f3c8cb935ddc99540a19e)) + ## [1.3.1-beta.1](https://github.com/open-sauced/ai/compare/v1.3.0...v1.3.1-beta.1) (2023-05-27) diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 635ae41e..2c9ee65f 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,12 +1,12 @@ { "name": "opensauced-browser-extension", - "version": "1.3.1-beta.1", + "version": "1.3.1-beta.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "opensauced-browser-extension", - "version": "1.3.1-beta.1", + "version": "1.3.1-beta.2", "dependencies": { "date-fns": "^2.30.0", "gpt-tokenizer": "^1.0.5", diff --git a/package.json b/package.json index fcacdeda..6706eed0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "opensauced-browser-extension", "private": true, - "version": "1.3.1-beta.1", + "version": "1.3.1-beta.2", "files": [ "dist" ], From 77c7f27a9596a54aa365ddf38b0a42859fb9d45b Mon Sep 17 00:00:00 2001 From: Abdurrahman Rajab Date: Sun, 28 May 2023 20:52:06 +0300 Subject: [PATCH 06/17] chore: display version on the extension. (#141) --- src/pages/help.tsx | 13 +++++++++++++ src/pages/home.tsx | 13 +++++++------ src/vite-env.d.ts | 1 + vite.config.ts | 3 +++ 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/pages/help.tsx b/src/pages/help.tsx index 0f384f3c..ef20c60e 100644 --- a/src/pages/help.tsx +++ b/src/pages/help.tsx @@ -7,6 +7,7 @@ import { } from "react-icons/hi2"; import { goBack } from "react-chrome-extension-router"; import { VscIssues } from "react-icons/vsc"; +const { version } = config; const Help = () => (
@@ -27,7 +28,19 @@ const Help = () => ( className="w-[100%]" src={OpenSaucedLogo} /> +
+ + +
diff --git a/src/pages/home.tsx b/src/pages/home.tsx index 8fc140d1..88b8940e 100644 --- a/src/pages/home.tsx +++ b/src/pages/home.tsx @@ -26,6 +26,7 @@ const Home = () => { src={OpenSaucedLogo} /> + {user && ( {currentTabIsOpensaucedUser && ( @@ -86,12 +87,12 @@ const Home = () => { }} > + View - View {" "} {checkedUser} - 's profile + 's profile )} @@ -105,7 +106,7 @@ const Home = () => { }} > - Help + Help diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index 11f02fe2..f12ad48d 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -1 +1,2 @@ /// +declare var config: Record; \ No newline at end of file diff --git a/vite.config.ts b/vite.config.ts index d87e27c9..346c10a8 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -6,4 +6,7 @@ import manifest from "./manifest.json" assert { type: "json" }; // https://vitejs.dev/config/ export default defineConfig({ plugins: [react(), crx({ manifest })], + define: { + 'config.version': JSON.stringify(process.env.npm_package_version), +} }); From 3a79facf2196537b4cfc0ce763ed74934c7b268f Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Mon, 29 May 2023 18:18:28 +0530 Subject: [PATCH 07/17] test: add tests for checkAuth.ts (#149) * test: add tests for checkAuth.ts * call check user auth once --- src/utils/checkAuthentication.ts | 31 ++++++++++++------- src/worker/background.ts | 19 +++++++++--- test/utils/checkAuthentication.test.ts | 43 ++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 15 deletions(-) create mode 100644 test/utils/checkAuthentication.test.ts diff --git a/src/utils/checkAuthentication.ts b/src/utils/checkAuthentication.ts index cdf07697..4fd3b94a 100644 --- a/src/utils/checkAuthentication.ts +++ b/src/utils/checkAuthentication.ts @@ -5,34 +5,42 @@ import { OPEN_SAUCED_INSIGHTS_DOMAIN, SUPABASE_LOGIN_URL, } from "../constants"; -import { checkTokenValidity } from "./fetchOpenSaucedApiData"; -import setAccessTokenInChromeStorage from "../utils/setAccessToken"; -export const checkAuthentication = async () => { +export const checkAuthentication = async ( + hasOptedLogOut: () => Promise, + getCookie: ( + details: chrome.cookies.Details, + callback: (cookie: chrome.cookies.Cookie | null) => void + ) => void, + checkTokenValidity: (authCookie: any) => Promise, + setAccessTokenInChromeStorage: (authCookie: any) => void, + removeAuthTokenFromStorage: () => void, + logError: (error: string) => void, +) => { if (await hasOptedLogOut()) { - return chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); + return removeAuthTokenFromStorage(); } - chrome.cookies.get( + getCookie( { name: SUPABASE_AUTH_COOKIE_NAME, url: `https://${OPEN_SAUCED_INSIGHTS_DOMAIN}`, }, async cookie => { if (!cookie) { - return chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); + return removeAuthTokenFromStorage(); } try { const authCookie = JSON.parse(decodeURIComponent(cookie.value))[0]; const isValidToken = await checkTokenValidity(authCookie); if (!isValidToken) { - return chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); + return removeAuthTokenFromStorage(); } - void setAccessTokenInChromeStorage(authCookie); + setAccessTokenInChromeStorage(authCookie); } catch (error) { - void chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); - console.error("Error processing cookie:", error); + removeAuthTokenFromStorage(); + logError(error as string); } }, ); @@ -55,5 +63,6 @@ export const optLogIn = () => { window.open(SUPABASE_LOGIN_URL, "_blank"); }; -const hasOptedLogOut = async (): Promise => (await chrome.storage.local.get(OPEN_SAUCED_OPTED_LOG_OUT_KEY))[OPEN_SAUCED_OPTED_LOG_OUT_KEY] === true; +export const hasOptedLogOut = async (): Promise => (await chrome.storage.local.get(OPEN_SAUCED_OPTED_LOG_OUT_KEY))[OPEN_SAUCED_OPTED_LOG_OUT_KEY] === true; +export const removeAuthTokenFromStorage = async (): Promise => chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); diff --git a/src/worker/background.ts b/src/worker/background.ts index 62cd846f..3bd0b63d 100644 --- a/src/worker/background.ts +++ b/src/worker/background.ts @@ -1,16 +1,27 @@ -import { checkAuthentication } from "../utils/checkAuthentication"; +import { checkAuthentication, hasOptedLogOut, removeAuthTokenFromStorage } from "../utils/checkAuthentication"; import { SUPABASE_AUTH_COOKIE_NAME, OPEN_SAUCED_INSIGHTS_DOMAIN } from "../constants"; import { setDefaultDescriptionConfig } from "../utils/aiprdescription/descriptionconfig"; +import { checkTokenValidity } from "../utils/fetchOpenSaucedApiData"; +import setAccessTokenInChromeStorage from "../utils/setAccessToken"; + + +const checkUserAuthentication = () => { + void checkAuthentication(hasOptedLogOut, chrome.cookies.get, checkTokenValidity, setAccessTokenInChromeStorage, removeAuthTokenFromStorage, console.error); +}; chrome.cookies.onChanged.addListener(changeInfo => { if ( changeInfo.cookie.name === SUPABASE_AUTH_COOKIE_NAME || changeInfo.cookie.domain === OPEN_SAUCED_INSIGHTS_DOMAIN ) { - void checkAuthentication(); + checkUserAuthentication(); } }); -chrome.runtime.onInstalled.addListener(checkAuthentication); +chrome.runtime.onInstalled.addListener(() => { + checkUserAuthentication(); +}); chrome.runtime.onInstalled.addListener(setDefaultDescriptionConfig); -chrome.runtime.onStartup.addListener(checkAuthentication); +chrome.runtime.onStartup.addListener(() => { + checkUserAuthentication(); +}); diff --git a/test/utils/checkAuthentication.test.ts b/test/utils/checkAuthentication.test.ts new file mode 100644 index 00000000..3cb92a3e --- /dev/null +++ b/test/utils/checkAuthentication.test.ts @@ -0,0 +1,43 @@ +import { describe, it, expect, beforeEach, vi } from 'vitest'; +import { checkAuthentication } from '../../src/utils/checkAuthentication'; + + +const hasOptedLogOut = vi.fn().mockResolvedValue(false); +const getCookie = vi.fn(); +const checkTokenValidity = vi.fn().mockResolvedValue(true); +const setAccessTokenInChromeStorage = vi.fn(); +const removeAuthTokenFromStorage = vi.fn(); +const logError = vi.fn(); + +describe('checkAuthentication', () => { + beforeEach(() => { + vi.resetAllMocks(); + }); + + it('should remove auth token from storage if user has opted out', async () => { + hasOptedLogOut.mockResolvedValue(true); + await checkAuthentication( + hasOptedLogOut, + getCookie, + checkTokenValidity, + setAccessTokenInChromeStorage, + removeAuthTokenFromStorage, + logError, + ); + expect(removeAuthTokenFromStorage).toHaveBeenCalled(); + }); + + it('should remove auth token from storage if cookie is not found', async () => { + getCookie.mockImplementation((details, callback) => callback(null)); + await checkAuthentication( + hasOptedLogOut, + getCookie, + checkTokenValidity, + setAccessTokenInChromeStorage, + removeAuthTokenFromStorage, + logError, + ); + expect(removeAuthTokenFromStorage).toHaveBeenCalled(); + }); +}) + \ No newline at end of file From cb539385069dddfb0ee20dcd475c5d3bd45a24ca Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Mon, 29 May 2023 12:49:51 +0000 Subject: [PATCH 08/17] chore(patch): release 1.3.1-beta.3 on beta channel [skip ci] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## [1.3.1-beta.3](https://github.com/open-sauced/ai/compare/v1.3.1-beta.2...v1.3.1-beta.3) (2023-05-29) ### ✅ Tests * add tests for checkAuth.ts ([#149](https://github.com/open-sauced/ai/issues/149)) ([3a79fac](https://github.com/open-sauced/ai/commit/3a79facf2196537b4cfc0ce763ed74934c7b268f)) --- CHANGELOG.md | 7 +++++++ npm-shrinkwrap.json | 4 ++-- package.json | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40e99d8a..100e097a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ > All notable changes to this project will be documented in this file +## [1.3.1-beta.3](https://github.com/open-sauced/ai/compare/v1.3.1-beta.2...v1.3.1-beta.3) (2023-05-29) + + +### ✅ Tests + +* add tests for checkAuth.ts ([#149](https://github.com/open-sauced/ai/issues/149)) ([3a79fac](https://github.com/open-sauced/ai/commit/3a79facf2196537b4cfc0ce763ed74934c7b268f)) + ## [1.3.1-beta.2](https://github.com/open-sauced/ai/compare/v1.3.1-beta.1...v1.3.1-beta.2) (2023-05-27) diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 2c9ee65f..bf472f1d 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,12 +1,12 @@ { "name": "opensauced-browser-extension", - "version": "1.3.1-beta.2", + "version": "1.3.1-beta.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "opensauced-browser-extension", - "version": "1.3.1-beta.2", + "version": "1.3.1-beta.3", "dependencies": { "date-fns": "^2.30.0", "gpt-tokenizer": "^1.0.5", diff --git a/package.json b/package.json index 6706eed0..5373fbf5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "opensauced-browser-extension", "private": true, - "version": "1.3.1-beta.2", + "version": "1.3.1-beta.3", "files": [ "dist" ], From e3b7051826f016bbd64c8d0b6a37ae2c48865879 Mon Sep 17 00:00:00 2001 From: Abdurrahman Rajab Date: Mon, 29 May 2023 17:05:44 +0300 Subject: [PATCH 09/17] fix: Post a highlight (#101) --- src/constants.ts | 12 +- .../DescriptionGeneratorButton.ts | 79 ++++++----- src/content-scripts/github.ts | 25 ++++ src/pages/home.tsx | 13 ++ src/pages/posthighlight.tsx | 134 ++++++++++++++++++ src/utils/fetchGithubAPIData.ts | 6 +- src/utils/fetchOpenSaucedApiData.ts | 16 +++ 7 files changed, 244 insertions(+), 41 deletions(-) create mode 100644 src/pages/posthighlight.tsx diff --git a/src/constants.ts b/src/constants.ts index 7328b3da..9cd94565 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -7,11 +7,13 @@ export const OPEN_SAUCED_INSIGHTS_DOMAIN = "insights.opensauced.pizza"; export const AI_PR_DESCRIPTION_CONFIG_KEY = "ai-pr-description-config"; // API endpoints -export const OPEN_SAUCED_USERS_ENDPOINT = "https://api.opensauced.pizza/v1/users"; -export const OPEN_SAUCED_REPOS_ENDPOINT = "https://api.opensauced.pizza/v1/repos"; -export const OPEN_SAUCED_SESSION_ENDPOINT = "https://api.opensauced.pizza/v1/auth/session"; -export const OPEN_SAUCED_USER_INSIGHTS_ENDPOINT = "https://api.opensauced.pizza/v1/user/insights"; -export const OPEN_SAUCED_AI_PR_DESCRIPTION_ENDPOINT = "https://api.opensauced.pizza/v1/prs/description/generate"; +export const OPEN_SAUCED_API_ENDPOINT = "https://api.opensauced.pizza/v1"; +export const OPEN_SAUCED_USERS_ENDPOINT = `${OPEN_SAUCED_API_ENDPOINT}/users`; +export const OPEN_SAUCED_REPOS_ENDPOINT = `${OPEN_SAUCED_API_ENDPOINT}/repos`; +export const OPEN_SAUCED_SESSION_ENDPOINT = `${OPEN_SAUCED_API_ENDPOINT}/auth/session`; +export const OPEN_SAUCED_USER_INSIGHTS_ENDPOINT = `${OPEN_SAUCED_API_ENDPOINT}/user/insights`; +export const OPEN_SAUCED_AI_PR_DESCRIPTION_ENDPOINT = `${OPEN_SAUCED_API_ENDPOINT}/prs/description/generate`; +export const OPEN_SAUCED_USER_HIGHLIGHTS_ENDPOINT = `${OPEN_SAUCED_API_ENDPOINT}/user/highlights`; export const OPEN_SAUCED_AI_CODE_REFACTOR_ENDPOINT = "https://api.opensauced.pizza/v1/prs/suggestion/generate"; // GitHub constants/selectors diff --git a/src/content-scripts/components/GenerateAIDescription/DescriptionGeneratorButton.ts b/src/content-scripts/components/GenerateAIDescription/DescriptionGeneratorButton.ts index b3f60926..7c468b3b 100644 --- a/src/content-scripts/components/GenerateAIDescription/DescriptionGeneratorButton.ts +++ b/src/content-scripts/components/GenerateAIDescription/DescriptionGeneratorButton.ts @@ -23,56 +23,69 @@ export const DescriptionGeneratorButton = () => { }; const handleSubmit = async () => { + const logo = document.getElementById("ai-description-button-logo") ?? null; + try { if (!(await isLoggedIn())) { return window.open(SUPABASE_LOGIN_URL, "_blank"); } - const logo = document.getElementById("ai-description-button-logo"); if (!logo) { return; } - const url = getPullRequestAPIURL(window.location.href); - const descriptionConfig = await getAIDescriptionConfig(); - - if (!descriptionConfig) { - return; - } - if (!descriptionConfig.enabled) { - return alert("AI PR description is disabled!"); - } logo.classList.toggle("animate-spin"); - const [diff, commitMessages] = await getDescriptionContext(url, descriptionConfig.config.source); - - if (!diff && !commitMessages) { - logo.classList.toggle("animate-spin"); - return alert(`No input context was generated.`); - } - if (isOutOfContextBounds([diff, commitMessages], descriptionConfig.config.maxInputLength)) { - logo.classList.toggle("animate-spin"); - return alert(`Max input length exceeded. Try setting the description source to commit-messages.`); - } - const token = await getAuthToken(); - const descriptionStream = await generateDescription( - token, - descriptionConfig.config.language, - descriptionConfig.config.length, - descriptionConfig.config.temperature / 10, - descriptionConfig.config.tone, - diff, - commitMessages, - ); + const descriptionStream = await getAiDescription(); logo.classList.toggle("animate-spin"); - if (!descriptionStream) { - return console.error("No description was generated!"); - } + const textArea = document.getElementsByName(GITHUB_PR_COMMENT_TEXT_AREA_SELECTOR)[0] as HTMLTextAreaElement; insertTextAtCursor(textArea, descriptionStream); } catch (error: unknown) { + logo?.classList.toggle("animate-spin"); + if (error instanceof Error) { + alert(error.message); console.error("Description generation error:", error.message); } } }; + +export const getAiDescription = async () => { + const url = getPullRequestAPIURL(window.location.href); + const descriptionConfig = await getAIDescriptionConfig(); + + if (!descriptionConfig) { + throw new Error("Configuration file is empty!"); + } + + if (!descriptionConfig.enabled) { + throw new Error("AI PR description is disabled!"); + } + + const [diff, commitMessages] = await getDescriptionContext(url, descriptionConfig.config.source); + + if (!diff && !commitMessages) { + throw new Error(`No input context was generated.`); + } + if (isOutOfContextBounds([diff, commitMessages], descriptionConfig.config.maxInputLength)) { + throw new Error(`Max input length exceeded. Try setting the description source to commit-messages.`); + } + const token = await getAuthToken(); + const descriptionStream = await generateDescription( + token, + descriptionConfig.config.language, + descriptionConfig.config.length, + descriptionConfig.config.temperature / 10, + descriptionConfig.config.tone, + diff, + commitMessages, + ); + + if (!descriptionStream) { + throw new Error("No description was generated!"); + } + + return descriptionStream; +}; + diff --git a/src/content-scripts/github.ts b/src/content-scripts/github.ts index a1e4f9e3..c154de7c 100644 --- a/src/content-scripts/github.ts +++ b/src/content-scripts/github.ts @@ -16,6 +16,7 @@ import domUpdateWatch from "../utils/dom-utils/domUpdateWatcher"; import injectDescriptionGeneratorButton from "../utils/dom-utils/addDescriptionGenerator"; import injectChangeSuggestorButton from "../utils/dom-utils/changeSuggestorButton"; import prEditWatch, { prReviewWatch } from "../utils/dom-utils/prWatcher"; +import { getAiDescription } from "./components/GenerateAIDescription/DescriptionGeneratorButton"; const processGithubPage = async () => { if (prefersDarkMode(document.cookie)) { @@ -55,3 +56,27 @@ const processGithubPage = async () => { }; void processGithubPage(); + +chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { + switch (msg.type) { + case "get_highlight": { + const title = (document.querySelector(".js-issue-title.markdown-title") as HTMLHeadingElement)?.innerText; + + sendResponse(title); + break; + } + case "get_ai_description": { + const asyncRequest = async () => { + const aiText = await getAiDescription(); + + sendResponse(aiText); + }; + + asyncRequest().catch((e: Error | undefined) => { + sendResponse(e?.toString()); + console.error(e); + }); + return true; + } + } +}); diff --git a/src/pages/home.tsx b/src/pages/home.tsx index 88b8940e..705d80d3 100644 --- a/src/pages/home.tsx +++ b/src/pages/home.tsx @@ -10,6 +10,9 @@ import { useOpensaucedUserCheck } from "../hooks/useOpensaucedUserCheck"; import { Profile } from "./profile"; import { goTo } from "react-chrome-extension-router"; import AIPRDescription from "./aiprdescription"; +import PostOnHighlight from "./posthighlight"; + + import Help from "./help"; const Home = () => { @@ -79,6 +82,16 @@ const Home = () => { AI Configuration + + {currentTabIsOpensaucedUser && ( + + OpenSauced logo + + + +
+ + setHighlightTitle(e.target.value)} + /> + +