Elffuss BraidHub

kikocis/auto-code-rover public

Add support for extracting github code permalinks
Changesetd942afa3e83c412c258ff1def3742c8f3e03b2c8a2d6869cb01dd54036fc3cf8
AuthorMartin Mirchev <mirchevmartin2203@gmail.com> · 2024-07-18 14:02 +08:00
CommitterMartin Mirchev <mirchevmartin2203@gmail.com> · 2024-07-18 14:02 +08:00
Parents831d2a8f2ea0

Browse the files at this changeset

1 file changed

modified app/raw_tasks.py +29 −0
@@ -1,5 +1,6 @@
11 import json
22 import os
3+import re
34 import shutil
45 from abc import ABC, abstractmethod
56 from os.path import join as pjoin
@@ -147,10 +148,38 @@
147148
148149 title, body, created_at = retrieved_issue
149150
151+ body = self.process_links(body)
152+
150153 problem_statement = f"{title}\n{body}"
151154
152155 return problem_statement, created_at
153156
157+ @classmethod
158+ def process_links(cls, body: str):
159+ code_pattern = re.compile(
160+ r"https://github.com/(.*?)/blob/(.*)/(.*)#L(\d+)-L(\d+)"
161+ )
162+ replacements = []
163+
164+ for code_links in code_pattern.finditer(body):
165+ print("Found link!")
166+ repo_name = code_links.group(1)
167+ commit = code_links.group(2)
168+ file_path = code_links.group(3)
169+ start_line = int(code_links.group(4))
170+ end_line = int(code_links.group(5))
171+
172+ file_contents = httpx.get(
173+ f"https://raw.githubusercontent.com/{repo_name}/{commit}/{file_path}"
174+ ).text.splitlines()
175+ fragment = "\n".join(file_contents[start_line - 1 : end_line])
176+
177+ replacements.append((code_links.group(0), f"\n```{fragment }```\n"))
178+
179+ for code_link, replacement in replacements:
180+ body = body.replace(code_link, code_link + replacement)
181+ return body
182+
154183 @classmethod
155184 def fetch_github_issue(cls, issue_url: str) -> tuple[str, str, str]:
156185 """Extract owner, repo, and issue number from the URL"""