Skip to content

Commit

Permalink
feat: git patch summary for github commit and compare
Browse files Browse the repository at this point in the history
  • Loading branch information
josStorer committed Mar 12, 2023
1 parent a33e37a commit b751f7e
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/content-script/site-adapters/github/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { cropText } from '../../../utils/crop-text.mjs'
import { limitedFetch } from '../../../utils/limited-fetch.mjs'
import { config } from '../index.mjs'

const getPatchUrl = async () => {
const patchUrl = location.origin + location.pathname + '.patch'
const response = await fetch(patchUrl, { method: 'HEAD' })
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
}

export default {
init: async (hostname, userConfig, getInput, mountComponent) => {
try {
const targetNode = document.querySelector('body')
const observer = new MutationObserver(async (records) => {
if (
records.some(
(record) =>
record.type === 'childList' &&
[...record.addedNodes].some((node) => node.classList.contains('page-responsive')),
)
) {
const patchUrl = await getPatchUrl()
if (patchUrl) {
mountComponent(config.github, userConfig)
}
}
})
observer.observe(targetNode, { childList: true })
} catch (e) {
/* empty */
}
},
inputQuery: async () => {
try {
const patchUrl = await getPatchUrl()
const patchData = await getPatchData(patchUrl)
if (!patchData) return

return cropText(
`Analyze the contents of a git commit,provide a suitable commit message,and summarize the contents of the commit.` +
`The patch contents of this commit are as follows:\n${patchData}`,
)
} catch (e) {
console.log(e)
}
},
}

0 comments on commit b751f7e

Please sign in to comment.