Skip to content

Commit e82f62e

Browse files
JKamskerjosStorer
andauthored
Added box for gh pr's and issues (#558)
* Added box for gh pr's and issues * only mount for pull and issue * add title to issue parser * due to allowing issue analysis, now getPatchUrl allows exceptions to occur and continue --------- Co-authored-by: josc146 <[email protected]>
1 parent 524b39f commit e82f62e

File tree

6 files changed

+121
-5
lines changed

6 files changed

+121
-5
lines changed

src/content-script/index.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,15 @@ async function prepareForStaticCard() {
274274
)
275275
return
276276

277+
let initSuccess = true
277278
if (siteName in siteConfig) {
278279
const siteAction = siteConfig[siteName].action
279280
if (siteAction && siteAction.init) {
280-
await siteAction.init(location.hostname, userConfig, getInput, mountComponent)
281+
initSuccess = await siteAction.init(location.hostname, userConfig, getInput, mountComponent)
281282
}
282283
}
283284

284-
mountComponent(siteConfig[siteName], userConfig)
285+
if (initSuccess) mountComponent(siteConfig[siteName], userConfig)
285286
}
286287
}
287288

src/content-script/site-adapters/baidu/index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ export default {
2222
} catch (e) {
2323
/* empty */
2424
}
25+
return true
2526
},
2627
}

src/content-script/site-adapters/bilibili/index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default {
1515
} catch (e) {
1616
/* empty */
1717
}
18+
return true
1819
},
1920
inputQuery: async () => {
2021
try {

src/content-script/site-adapters/github/index.mjs

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { config } from '../index.mjs'
33

44
const getPatchUrl = async () => {
55
const patchUrl = location.origin + location.pathname + '.patch'
6-
const response = await fetch(patchUrl, { method: 'HEAD' })
6+
const response = await fetch(patchUrl, { method: 'HEAD' }).catch(() => ({}))
77
if (response.ok) return patchUrl
88
return ''
99
}
@@ -16,13 +16,118 @@ const getPatchData = async (patchUrl) => {
1616
return patchData
1717
}
1818

19+
const isPull = () => {
20+
return location.href.match(/\/pull\/\d+$/)
21+
}
22+
23+
const isIssue = () => {
24+
return location.href.match(/\/issues\/\d+$/)
25+
}
26+
27+
function parseGitHubIssueData() {
28+
// Function to parse a single comment
29+
function parseComment(commentElement) {
30+
// Parse the date
31+
const dateElement = commentElement.querySelector('relative-time')
32+
const date = dateElement.getAttribute('datetime')
33+
34+
// Parse the author
35+
const authorElement =
36+
commentElement.querySelector('.author') || commentElement.querySelector('.author-name')
37+
const author = authorElement.textContent.trim()
38+
39+
// Parse the body
40+
const bodyElement = commentElement.querySelector('.comment-body')
41+
const body = bodyElement.textContent.trim()
42+
43+
return { date, author, body }
44+
}
45+
46+
// Function to parse all messages on the page
47+
function parseAllMessages() {
48+
// Find all comment containers
49+
const commentElements = document.querySelectorAll('.timeline-comment-group')
50+
const messages = Array.from(commentElements).map(parseComment)
51+
52+
// The initial post is not a ".timeline-comment-group", so we need to handle it separately
53+
const initialPostElement = document.querySelector('.js-comment-container')
54+
const initialPost = parseComment(initialPostElement)
55+
56+
// Combine the initial post with the rest of the comments
57+
return [initialPost, ...messages]
58+
}
59+
60+
// Function to get the content of the comment input box
61+
function getCommentInputContent() {
62+
const commentInput = document.querySelector('.js-new-comment-form textarea')
63+
return commentInput ? commentInput.value : ''
64+
}
65+
66+
// Get the issue title
67+
const title = document.querySelector('.js-issue-title').textContent.trim()
68+
69+
// Get all messages
70+
const messages = parseAllMessages()
71+
72+
// Get the content of the new comment box
73+
const commentBoxContent = getCommentInputContent()
74+
75+
// Return an object with both results
76+
return {
77+
title: title,
78+
messages: messages,
79+
commentBoxContent: commentBoxContent,
80+
}
81+
}
82+
83+
function createChatGPtSummaryPrompt(issueData, isIssue = true) {
84+
// Destructure the issueData object into messages and commentBoxContent
85+
const { title, messages, commentBoxContent } = issueData
86+
87+
// Start crafting the prompt
88+
let prompt = ''
89+
90+
if (isIssue) {
91+
prompt =
92+
'Please summarize the following GitHub issue thread.\nWhat is the main issue and key points discussed in this thread?\n\n'
93+
} else {
94+
prompt =
95+
'Please summarize the following GitHub pull request thread.\nWhat is the main issue this pull request is trying to solve?\n\n'
96+
}
97+
98+
prompt += '---\n\n'
99+
100+
prompt += `Title:\n${title}\n\n`
101+
102+
// Add each message to the prompt
103+
messages.forEach((message, index) => {
104+
prompt += `Message ${index + 1} by ${message.author} on ${message.date}:\n${message.body}\n\n`
105+
})
106+
107+
// If there's content in the comment box, add it as a draft message
108+
if (commentBoxContent) {
109+
prompt += '---\n\n'
110+
prompt += `Draft message in comment box:\n${commentBoxContent}\n\n`
111+
}
112+
113+
// Add a request for summary at the end of the prompt
114+
// prompt += 'What is the main issue and key points discussed in this thread?'
115+
116+
return prompt
117+
}
118+
19119
export default {
20120
init: async (hostname, userConfig, getInput, mountComponent) => {
21121
try {
22122
let oldUrl = location.href
23123
const checkUrlChange = async () => {
24124
if (location.href !== oldUrl) {
25125
oldUrl = location.href
126+
if (isPull() || isIssue()) {
127+
mountComponent(config.github, userConfig)
128+
return
129+
}
130+
26131
const patchUrl = await getPatchUrl()
27132
if (patchUrl) {
28133
mountComponent(config.github, userConfig)
@@ -33,9 +138,16 @@ export default {
33138
} catch (e) {
34139
/* empty */
35140
}
141+
return (await getPatchUrl()) || isPull() || isIssue()
36142
},
37143
inputQuery: async () => {
38144
try {
145+
if (isPull() || isIssue()) {
146+
const issueData = parseGitHubIssueData()
147+
const summaryPrompt = createChatGPtSummaryPrompt(issueData, isIssue())
148+
149+
return await cropText(summaryPrompt)
150+
}
39151
const patchUrl = await getPatchUrl()
40152
const patchData = await getPatchData(patchUrl)
41153
if (!patchData) return

src/content-script/site-adapters/index.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ export const config = {
137137
},
138138
github: {
139139
inputQuery: github.inputQuery,
140-
sidebarContainerQuery: ['#diff', '.commit'],
140+
sidebarContainerQuery: ['#diff', '.commit', '.Layout-main'],
141141
appendContainerQuery: [],
142-
resultsContainerQuery: ['#diff', '.commit'],
142+
resultsContainerQuery: ['#diff', '.commit', '.Layout-main'],
143143
action: {
144144
init: github.init,
145145
},

src/content-script/site-adapters/youtube/index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default {
2121
} catch (e) {
2222
/* empty */
2323
}
24+
return true
2425
},
2526
inputQuery: async () => {
2627
try {

0 commit comments

Comments
 (0)