From dd652e8f591f7194d9def7ff8432d08c168643b1 Mon Sep 17 00:00:00 2001 From: JKamsker Date: Thu, 9 Nov 2023 00:19:59 +0100 Subject: [PATCH 1/4] Added box for gh pr's and issues --- .../site-adapters/github/index.mjs | 105 ++++++++++++++++++ src/content-script/site-adapters/index.mjs | 4 +- 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/src/content-script/site-adapters/github/index.mjs b/src/content-script/site-adapters/github/index.mjs index bf1bb7fb..0fd1171d 100644 --- a/src/content-script/site-adapters/github/index.mjs +++ b/src/content-script/site-adapters/github/index.mjs @@ -16,6 +16,100 @@ const getPatchData = async (patchUrl) => { return patchData } +const isPull = () => { + return location.href.match(/\/pull\/\d+$/) +} + +const isIssue = () => { + return location.href.match(/\/issues\/\d+$/) +} + +function parseGitHubIssueData() { + // Function to parse a single comment + function parseComment(commentElement) { + // Parse the date + const dateElement = commentElement.querySelector('relative-time') + const date = dateElement.getAttribute('datetime') + + // Parse the author + const authorElement = + commentElement.querySelector('.author') || commentElement.querySelector('.author-name') + const author = authorElement.textContent.trim() + + // Parse the body + const bodyElement = commentElement.querySelector('.comment-body') + const body = bodyElement.textContent.trim() + + return { date, author, body } + } + + // Function to parse all messages on the page + function parseAllMessages() { + // Find all comment containers + const commentElements = document.querySelectorAll('.timeline-comment-group') + const messages = Array.from(commentElements).map(parseComment) + + // The initial post is not a ".timeline-comment-group", so we need to handle it separately + const initialPostElement = document.querySelector('.js-comment-container') + const initialPost = parseComment(initialPostElement) + + // Combine the initial post with the rest of the comments + return [initialPost, ...messages] + } + + // Function to get the content of the comment input box + function getCommentInputContent() { + const commentInput = document.querySelector('.js-new-comment-form textarea') + return commentInput ? commentInput.value : '' + } + + // Get all messages + const messages = parseAllMessages() + + // Get the content of the new comment box + const commentBoxContent = getCommentInputContent() + + // Return an object with both results + return { + messages: messages, + commentBoxContent: commentBoxContent, + } +} + +function createChatGPtSummaryPrompt(issueData, isIssue = true) { + // Destructure the issueData object into messages and commentBoxContent + const { messages, commentBoxContent } = issueData + + // Start crafting the prompt + let prompt = '' + + if (isIssue) { + prompt = + 'Please summarize the following GitHub issue thread.\nWhat is the main issue and key points discussed in this thread?\n\n' + } else { + prompt = + 'Please summarize the following GitHub pull request thread.\nWhat is the main issue this pull request is trying to solve?\n\n' + } + + prompt += '---\n\n' + + // Add each message to the prompt + messages.forEach((message, index) => { + prompt += `Message ${index + 1} by ${message.author} on ${message.date}:\n${message.body}\n\n` + }) + + // If there's content in the comment box, add it as a draft message + if (commentBoxContent) { + prompt += '---\n\n' + prompt += `Draft message in comment box:\n${commentBoxContent}\n\n` + } + + // Add a request for summary at the end of the prompt + // prompt += 'What is the main issue and key points discussed in this thread?' + + return prompt +} + export default { init: async (hostname, userConfig, getInput, mountComponent) => { try { @@ -23,6 +117,11 @@ export default { const checkUrlChange = async () => { if (location.href !== oldUrl) { oldUrl = location.href + if (isPull() || isIssue()) { + mountComponent(config.github, userConfig) + return + } + const patchUrl = await getPatchUrl() if (patchUrl) { mountComponent(config.github, userConfig) @@ -36,6 +135,12 @@ export default { }, inputQuery: async () => { try { + if (isPull() || isIssue()) { + const issueData = parseGitHubIssueData() + const summaryPrompt = createChatGPtSummaryPrompt(issueData, isIssue()) + + return await cropText(summaryPrompt) + } const patchUrl = await getPatchUrl() const patchData = await getPatchData(patchUrl) if (!patchData) return diff --git a/src/content-script/site-adapters/index.mjs b/src/content-script/site-adapters/index.mjs index 978ae753..52ef58c8 100644 --- a/src/content-script/site-adapters/index.mjs +++ b/src/content-script/site-adapters/index.mjs @@ -137,9 +137,9 @@ export const config = { }, github: { inputQuery: github.inputQuery, - sidebarContainerQuery: ['#diff', '.commit'], + sidebarContainerQuery: ['#diff', '.commit', '.Layout-main'], appendContainerQuery: [], - resultsContainerQuery: ['#diff', '.commit'], + resultsContainerQuery: ['#diff', '.commit', '.Layout-main'], action: { init: github.init, }, From fffb8b4d0eee95927e49aaeab80d24616ed82eb7 Mon Sep 17 00:00:00 2001 From: josc146 Date: Sun, 12 Nov 2023 23:32:25 +0800 Subject: [PATCH 2/4] only mount for pull and issue --- src/content-script/index.jsx | 5 +++-- src/content-script/site-adapters/baidu/index.mjs | 1 + src/content-script/site-adapters/bilibili/index.mjs | 1 + src/content-script/site-adapters/github/index.mjs | 1 + src/content-script/site-adapters/youtube/index.mjs | 1 + 5 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/content-script/index.jsx b/src/content-script/index.jsx index d2f93da7..3190de85 100644 --- a/src/content-script/index.jsx +++ b/src/content-script/index.jsx @@ -274,14 +274,15 @@ async function prepareForStaticCard() { ) return + let initSuccess = true if (siteName in siteConfig) { const siteAction = siteConfig[siteName].action if (siteAction && siteAction.init) { - await siteAction.init(location.hostname, userConfig, getInput, mountComponent) + initSuccess = await siteAction.init(location.hostname, userConfig, getInput, mountComponent) } } - mountComponent(siteConfig[siteName], userConfig) + if (initSuccess) mountComponent(siteConfig[siteName], userConfig) } } diff --git a/src/content-script/site-adapters/baidu/index.mjs b/src/content-script/site-adapters/baidu/index.mjs index 619fcb28..96ade831 100644 --- a/src/content-script/site-adapters/baidu/index.mjs +++ b/src/content-script/site-adapters/baidu/index.mjs @@ -22,5 +22,6 @@ export default { } catch (e) { /* empty */ } + return true }, } diff --git a/src/content-script/site-adapters/bilibili/index.mjs b/src/content-script/site-adapters/bilibili/index.mjs index 091a94ae..df05a976 100644 --- a/src/content-script/site-adapters/bilibili/index.mjs +++ b/src/content-script/site-adapters/bilibili/index.mjs @@ -15,6 +15,7 @@ export default { } catch (e) { /* empty */ } + return true }, inputQuery: async () => { try { diff --git a/src/content-script/site-adapters/github/index.mjs b/src/content-script/site-adapters/github/index.mjs index 0fd1171d..1566fdd4 100644 --- a/src/content-script/site-adapters/github/index.mjs +++ b/src/content-script/site-adapters/github/index.mjs @@ -132,6 +132,7 @@ export default { } catch (e) { /* empty */ } + return (await getPatchUrl()) || isPull() || isIssue() }, inputQuery: async () => { try { diff --git a/src/content-script/site-adapters/youtube/index.mjs b/src/content-script/site-adapters/youtube/index.mjs index 6d2b17d8..233a1bbf 100644 --- a/src/content-script/site-adapters/youtube/index.mjs +++ b/src/content-script/site-adapters/youtube/index.mjs @@ -21,6 +21,7 @@ export default { } catch (e) { /* empty */ } + return true }, inputQuery: async () => { try { From 0bb2cdc3229d1caa0a11efea00786f128df61b91 Mon Sep 17 00:00:00 2001 From: josc146 Date: Sun, 12 Nov 2023 23:42:12 +0800 Subject: [PATCH 3/4] add title to issue parser --- src/content-script/site-adapters/github/index.mjs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/content-script/site-adapters/github/index.mjs b/src/content-script/site-adapters/github/index.mjs index 1566fdd4..a1297bf6 100644 --- a/src/content-script/site-adapters/github/index.mjs +++ b/src/content-script/site-adapters/github/index.mjs @@ -63,6 +63,9 @@ function parseGitHubIssueData() { return commentInput ? commentInput.value : '' } + // Get the issue title + const title = document.querySelector('.js-issue-title').textContent.trim() + // Get all messages const messages = parseAllMessages() @@ -71,6 +74,7 @@ function parseGitHubIssueData() { // Return an object with both results return { + title: title, messages: messages, commentBoxContent: commentBoxContent, } @@ -78,7 +82,7 @@ function parseGitHubIssueData() { function createChatGPtSummaryPrompt(issueData, isIssue = true) { // Destructure the issueData object into messages and commentBoxContent - const { messages, commentBoxContent } = issueData + const { title, messages, commentBoxContent } = issueData // Start crafting the prompt let prompt = '' @@ -93,6 +97,8 @@ function createChatGPtSummaryPrompt(issueData, isIssue = true) { prompt += '---\n\n' + prompt += `Title:\n${title}\n\n` + // Add each message to the prompt messages.forEach((message, index) => { prompt += `Message ${index + 1} by ${message.author} on ${message.date}:\n${message.body}\n\n` From 7e9c3790248d537267899fc84e5dce0a5234efdf Mon Sep 17 00:00:00 2001 From: josc146 Date: Sun, 12 Nov 2023 23:44:53 +0800 Subject: [PATCH 4/4] due to allowing issue analysis, now getPatchUrl allows exceptions to occur and continue --- src/content-script/site-adapters/github/index.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content-script/site-adapters/github/index.mjs b/src/content-script/site-adapters/github/index.mjs index a1297bf6..d41a5695 100644 --- a/src/content-script/site-adapters/github/index.mjs +++ b/src/content-script/site-adapters/github/index.mjs @@ -3,7 +3,7 @@ import { config } from '../index.mjs' const getPatchUrl = async () => { const patchUrl = location.origin + location.pathname + '.patch' - const response = await fetch(patchUrl, { method: 'HEAD' }) + const response = await fetch(patchUrl, { method: 'HEAD' }).catch(() => ({})) if (response.ok) return patchUrl return '' }