Skip to content

Commit

Permalink
Add support for extracting github code permalinks
Browse files Browse the repository at this point in the history
  • Loading branch information
Marti2203 committed Jul 18, 2024
1 parent 2f055f3 commit d27df6f
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/raw_tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import re
import shutil
from abc import ABC, abstractmethod
from os.path import join as pjoin
Expand Down Expand Up @@ -147,10 +148,38 @@ def fetch_issue(self):

title, body, created_at = retrieved_issue

body = self.process_links(body)

problem_statement = f"{title}\n{body}"

return problem_statement, created_at

@classmethod
def process_links(cls, body: str):
code_pattern = re.compile(
r"https://github.com/(.*?)/blob/(.*)/(.*)#L(\d+)-L(\d+)"
)
replacements = []

for code_links in code_pattern.finditer(body):
print("Found link!")
repo_name = code_links.group(1)
commit = code_links.group(2)
file_path = code_links.group(3)
start_line = int(code_links.group(4))
end_line = int(code_links.group(5))

file_contents = httpx.get(
f"https://raw.githubusercontent.com/{repo_name}/{commit}/{file_path}"
).text.splitlines()
fragment = "\n".join(file_contents[start_line - 1 : end_line])

replacements.append((code_links.group(0), f"\n```{fragment }```\n"))

for code_link, replacement in replacements:
body = body.replace(code_link, code_link + replacement)
return body

@classmethod
def fetch_github_issue(cls, issue_url: str) -> tuple[str, str, str]:
"""Extract owner, repo, and issue number from the URL"""
Expand Down

0 comments on commit d27df6f

Please sign in to comment.