-
Notifications
You must be signed in to change notification settings - Fork 3
Add GitBlame connection for GitHub repositories #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ArjunS7864
wants to merge
11
commits into
KellisLab:aryana_github
Choose a base branch
from
ArjunS7864:aryana_github
base: aryana_github
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f7e5f74
improved UI
Arun-V18 4029afc
fix crashing after space create
Arun-V18 edf6f19
small fixes
Arun-V18 278399c
changing mantis AI to just mantis for simplicity
Arun-V18 71ce262
gemini
Arun-V18 b23b5f2
Merge pull request #17 from KellisLab/dialog-connection-improvement
Arun-V18 f4f57b5
Add GitBlame connection for GitHub repositories - Clean implementatio…
ArjunS7864 418d4e5
Fix all Gemini code review issues: implement proper GitHub token mana…
ArjunS7864 6379aa1
Fix all Gemini code review issues: PAT optional for public repos, eff…
ArjunS7864 9f029d2
Fix HTML response issue: add proper error handling for GitHub API res…
ArjunS7864 640e9ec
Add PR support to GitBlame connection
ArjunS7864 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,257 @@ | ||
| import type { MantisConnection, injectUIType, onMessageType, registerListenersType, setProgressType, establishLogSocketType } from "../types"; | ||
| import { GenerationProgress } from "../types"; | ||
| import { Octokit } from "@octokit/rest"; | ||
|
|
||
| import githubIcon from "data-base64:../../../assets/github.svg"; | ||
| import { getSpacePortal, registerAuthCookies, reqSpaceCreation } from "../../driver"; | ||
|
|
||
| const trigger = (url: string) => { | ||
| return url.includes("github.com") && url.includes("/blob/"); | ||
| } | ||
|
|
||
| const createSpace = async (injectUI: injectUIType, setProgress: setProgressType, onMessage: onMessageType, registerListeners: registerListenersType, establishLogSocket: establishLogSocketType) => { | ||
| setProgress(GenerationProgress.GATHERING_DATA); | ||
|
|
||
| // Extract repository information from the URL | ||
| const url = new URL(window.location.href); | ||
| const pathParts = url.pathname.split('/'); | ||
| const owner = pathParts[1]; | ||
| const repo = pathParts[2]; | ||
| const branch = pathParts[4] || "main"; | ||
| const filePath = pathParts.slice(5).join('/'); | ||
ArjunS7864 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| console.log(`Processing repository: ${owner}/${repo}, branch: ${branch}, file: ${filePath}`); | ||
|
|
||
| // Initialize Octokit with GitHub token from environment | ||
| const octokit = new Octokit({ | ||
| auth: process.env.PLASMO_PUBLIC_GITHUB_TOKEN | ||
| }); | ||
ArjunS7864 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| try { | ||
| // Get file blame information | ||
| const blameData = await getFileBlame(octokit, owner, repo, filePath, branch); | ||
|
|
||
| // Get additional repository information | ||
| const repoInfo = await getRepositoryInfo(octokit, owner, repo); | ||
|
|
||
| // Combine data for space creation | ||
| const extractedData = blameData.map(entry => ({ | ||
| filename: entry.filename, | ||
| lineNumber: entry.lineNumber, | ||
| commit: entry.commit, | ||
| author: entry.author, | ||
| date: entry.date, | ||
| lineContent: entry.lineContent, | ||
| repository: `${owner}/${repo}`, | ||
| branch: branch | ||
| })); | ||
|
|
||
| // Add repository metadata | ||
| if (repoInfo) { | ||
| extractedData.push({ | ||
| filename: "repository_info", | ||
| lineNumber: 0, | ||
| commit: "metadata", | ||
| author: repoInfo.owner.login, | ||
| date: repoInfo.created_at, | ||
| lineContent: `Repository: ${repoInfo.full_name}, Description: ${repoInfo.description || 'No description'}, Language: ${repoInfo.language || 'Unknown'}`, | ||
| repository: `${owner}/${repo}`, | ||
| branch: branch | ||
| }); | ||
| } | ||
|
|
||
| console.log(`Extracted ${extractedData.length} blame entries`); | ||
|
|
||
| setProgress(GenerationProgress.CREATING_SPACE); | ||
|
|
||
| const spaceData = await reqSpaceCreation(extractedData, { | ||
| "filename": "text", | ||
| "lineNumber": "number", | ||
| "commit": "text", | ||
| "author": "text", | ||
| "date": "date", | ||
| "lineContent": "semantic", | ||
| "repository": "text", | ||
| "branch": "text" | ||
| }, establishLogSocket, `GitBlame: ${owner}/${repo}/${filePath}`); | ||
|
|
||
| setProgress(GenerationProgress.INJECTING_UI); | ||
|
|
||
| const spaceId = spaceData.space_id; | ||
| const createdWidget = await injectUI(spaceId, onMessage, registerListeners); | ||
|
|
||
| setProgress(GenerationProgress.COMPLETED); | ||
|
|
||
| return { spaceId, createdWidget }; | ||
|
|
||
| } catch (error) { | ||
| console.error('Error creating GitBlame space:', error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| async function getFileBlame(octokit: Octokit, owner: string, repo: string, path: string, branch: string) { | ||
| try { | ||
| const commits = await octokit.paginate( | ||
| octokit.rest.repos.listCommits, | ||
| { | ||
| owner, | ||
| repo, | ||
| path, | ||
| sha: branch, | ||
| per_page: 100 | ||
| } | ||
| ); | ||
|
|
||
| const blameMap: Record<number, any> = {}; | ||
ArjunS7864 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let lineCount = 0; | ||
|
|
||
| for (const commit of commits.reverse()) { | ||
| const commitSha = commit.sha; | ||
|
|
||
| const commitData = await octokit.rest.repos.getCommit({ | ||
| owner, | ||
| repo, | ||
| ref: commitSha | ||
| }); | ||
|
|
||
| for (const file of commitData.data.files || []) { | ||
| if (file.filename === path && file.patch) { | ||
| const patchLines = file.patch.split("\n"); | ||
|
|
||
| let currentOldLine = 0; | ||
| let currentNewLine = 0; | ||
|
|
||
| for (const line of patchLines) { | ||
| if (line.startsWith("@@")) { | ||
| const match = /@@ -(\d+),?\d* \+(\d+),?\d* @@/.exec(line); | ||
| if (match) { | ||
| currentOldLine = parseInt(match[1], 10); | ||
| currentNewLine = parseInt(match[2], 10); | ||
| } | ||
| } else if (line.startsWith("+")) { | ||
| blameMap[currentNewLine] = { | ||
| filename: path, | ||
| lineNumber: currentNewLine, | ||
| commit: commitSha, | ||
| author: commit.commit.author.name, | ||
| date: commit.commit.author.date, | ||
ArjunS7864 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| lineContent: line.slice(1) | ||
| }; | ||
| currentNewLine++; | ||
| } else if (line.startsWith("-")) { | ||
| currentOldLine++; | ||
| } else { | ||
| currentOldLine++; | ||
| currentNewLine++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!lineCount && Object.keys(blameMap).length > 0) { | ||
| lineCount = Math.max(...Object.keys(blameMap).map(Number)); | ||
| } | ||
| } | ||
|
|
||
| const blameData = []; | ||
| for (let i = 1; i <= lineCount; i++) { | ||
| if (blameMap[i]) { | ||
| blameData.push(blameMap[i]); | ||
| } | ||
| } | ||
|
|
||
| return blameData; | ||
| } catch (error) { | ||
| console.warn(`Could not get blame for ${path}:`, error); | ||
| return []; | ||
| } | ||
| } | ||
ArjunS7864 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
ArjunS7864 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| async function getRepositoryInfo(octokit: Octokit, owner: string, repo: string) { | ||
| try { | ||
| const { data } = await octokit.rest.repos.get({ | ||
| owner, | ||
| repo | ||
| }); | ||
| return data; | ||
| } catch (error) { | ||
| console.warn(`Could not get repository info for ${owner}/${repo}:`, error); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| const injectUI = async (space_id: string, onMessage: onMessageType, registerListeners: registerListenersType) => { | ||
| // Find the GitHub file header to inject our UI | ||
| const fileHeader = document.querySelector('.file-header') || | ||
| document.querySelector('.Box-header') || | ||
| document.querySelector('.d-flex.flex-column.flex-md-row'); | ||
|
|
||
| if (!fileHeader) { | ||
| throw new Error('Could not find GitHub file header'); | ||
| } | ||
|
|
||
| // Container for everything | ||
| const div = document.createElement("div"); | ||
|
|
||
| // Toggle switch wrapper | ||
| const label = document.createElement("label"); | ||
| label.style.display = "inline-flex"; | ||
| label.style.alignItems = "center"; | ||
| label.style.cursor = "pointer"; | ||
| label.style.marginLeft = "16px"; | ||
| label.style.marginRight = "16px"; | ||
|
|
||
| // Checkbox as toggle | ||
| const checkbox = document.createElement("input"); | ||
| checkbox.type = "checkbox"; | ||
| checkbox.style.display = "none"; | ||
|
|
||
| // Text container with GitHub-style styling | ||
| const textContainer = document.createElement("span"); | ||
| textContainer.innerText = "Mantis GitBlame"; | ||
| textContainer.style.background = "linear-gradient(90deg, #0366d6, #28a745)"; | ||
| textContainer.style.backgroundClip = "text"; | ||
| textContainer.style.webkitTextFillColor = "transparent"; | ||
| textContainer.style.fontWeight = "600"; | ||
| textContainer.style.fontSize = "14px"; | ||
ArjunS7864 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| await registerAuthCookies(); | ||
|
|
||
| const iframeScalerParent = await getSpacePortal(space_id, onMessage, registerListeners); | ||
| iframeScalerParent.style.display = "none"; | ||
|
|
||
| // Toggle behavior | ||
| checkbox.addEventListener("change", () => { | ||
| if (checkbox.checked) { | ||
| iframeScalerParent.style.display = "block"; | ||
| textContainer.style.background = "linear-gradient(90deg, #28a745, #0366d6)"; | ||
| } else { | ||
| iframeScalerParent.style.display = "none"; | ||
| textContainer.style.background = "linear-gradient(90deg, #0366d6, #28a745)"; | ||
| } | ||
| textContainer.style.backgroundClip = "text"; | ||
| }); | ||
|
|
||
| // Assemble elements | ||
| label.appendChild(textContainer); | ||
| label.appendChild(checkbox); | ||
| div.appendChild(label); | ||
|
|
||
| // Insert the iframe after the file header | ||
| fileHeader.parentNode?.insertBefore(iframeScalerParent, fileHeader.nextSibling); | ||
|
|
||
| // Insert into the file header | ||
| fileHeader.appendChild(div); | ||
|
|
||
| return div; | ||
| } | ||
|
|
||
| export const GitBlameConnection: MantisConnection = { | ||
| name: "GitBlame", | ||
| description: "Builds spaces based on Git blame information from GitHub repositories", | ||
| icon: githubIcon, | ||
| trigger: trigger, | ||
| createSpace: createSpace, | ||
| injectUI: injectUI, | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When running this on my end, this extension requires me to give a GitHub PAT even for public repos. If I enter a blank input, I see:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see @gconsigli, I am currently working to fix this. Thank you for letting me know