-
Notifications
You must be signed in to change notification settings - Fork 855
Expand file tree
/
Copy pathindex.mjs
More file actions
173 lines (143 loc) · 5.88 KB
/
index.mjs
File metadata and controls
173 lines (143 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { cropText, limitedFetch } from '../../../utils'
import { config } from '../index.mjs'
const getPatchUrl = async () => {
const patchUrl = location.origin + location.pathname + '.patch'
const response = await fetch(patchUrl, { method: 'HEAD' }).catch(() => ({}))
if (response.ok) return patchUrl
return ''
}
const getPatchData = async (patchUrl) => {
if (!patchUrl) return
let patchData = await limitedFetch(patchUrl, 1024 * 40)
patchData = patchData.substring(patchData.indexOf('---'))
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 the issue title
const title = document.querySelector('.js-issue-title').textContent.trim()
// Get all messages
const messages = parseAllMessages()
// Get the content of the new comment box
const commentBoxContent = getCommentInputContent()
// Return an object with both results
return {
title: title,
messages: messages,
commentBoxContent: commentBoxContent,
}
}
function createChatGPtSummaryPrompt(issueData, isIssue = true) {
// Destructure the issueData object into messages and commentBoxContent
const { title, messages, commentBoxContent } = issueData
// Start crafting the prompt
let prompt = ''
if (isIssue) {
prompt =
`You are an expert in analyzing GitHub discussions. ` +
`Please provide a concise summary of the following GitHub issue thread. ` +
`Identify the main problem reported, key points discussed by participants, proposed solutions (if any), and the current status or next steps. ` +
`Present the summary in a structured markdown format.\n\n`
} else {
prompt =
`You are an expert in analyzing GitHub discussions and code reviews. ` +
`Please provide a concise summary of the following GitHub pull request thread. ` +
`Identify the main problem this pull request aims to solve, the proposed changes, key discussion points from the review, and the overall status of the PR (e.g., approved, needs changes, merged). ` +
`Present the summary in a structured markdown format.\n\n`
}
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`
})
// 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 {
let oldUrl = location.href
const checkUrlChange = async () => {
if (location.href !== oldUrl) {
oldUrl = location.href
if (isPull() || isIssue()) {
mountComponent(config.github)
return
}
const patchUrl = await getPatchUrl()
if (patchUrl) {
mountComponent(config.github)
}
}
}
window.setInterval(checkUrlChange, 500)
} catch (e) {
/* empty */
}
return (await getPatchUrl()) || isPull() || isIssue()
},
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
return await cropText(
`You are an expert in analyzing git commits and crafting clear, concise commit messages. ` +
`Based on the following git patch, please perform two tasks:\n` +
`1. Generate a suitable commit message. It should follow standard conventions: a short imperative subject line (max 50 chars), ` +
`followed by a blank line and a more detailed body if necessary, explaining the "what" and "why" of the changes.\n` +
`2. Provide a brief summary of the changes introduced in this commit, highlighting the main purpose and impact.\n\n` +
`The patch contents are as follows:\n${patchData}`,
)
} catch (e) {
console.log(e)
}
},
}