Merge pull request #98 from plers-org/sm/#95
✏️ Fix: [#95] 커밋 파일 목록 페이지네이션, 커밋 파일 확장자와 코드 언어 매핑
This commit is contained in:
69
common/utils/mapManager.py
Normal file
69
common/utils/mapManager.py
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def guess_language_from_path(path: str) -> str:
|
||||||
|
_, ext = os.path.splitext(path or "")
|
||||||
|
ext = ext.lower()
|
||||||
|
|
||||||
|
ext_to_lang = {
|
||||||
|
# Web
|
||||||
|
".js": "javascript",
|
||||||
|
".mjs": "javascript",
|
||||||
|
".cjs": "javascript",
|
||||||
|
".ts": "typescript",
|
||||||
|
".tsx": "typescript",
|
||||||
|
".jsx": "javascript",
|
||||||
|
".html": "html",
|
||||||
|
".htm": "html",
|
||||||
|
".css": "css",
|
||||||
|
".scss": "scss",
|
||||||
|
".sass": "sass",
|
||||||
|
".less": "less",
|
||||||
|
|
||||||
|
# Backend / General
|
||||||
|
".py": "python",
|
||||||
|
".java": "java",
|
||||||
|
".kt": "kotlin",
|
||||||
|
".kts": "kotlin",
|
||||||
|
".go": "go",
|
||||||
|
".rb": "ruby",
|
||||||
|
".php": "php",
|
||||||
|
".cs": "csharp",
|
||||||
|
".rs": "rust",
|
||||||
|
".c": "c",
|
||||||
|
".h": "c", # 프로젝트에 따라 c/cpp 헤더지만 일단 c로
|
||||||
|
".cpp": "cpp",
|
||||||
|
".cc": "cpp",
|
||||||
|
".cxx": "cpp",
|
||||||
|
".hpp": "cpp",
|
||||||
|
".hh": "cpp",
|
||||||
|
".swift": "swift",
|
||||||
|
|
||||||
|
# Shell / Infra
|
||||||
|
".sh": "bash",
|
||||||
|
".bash": "bash",
|
||||||
|
".zsh": "zsh",
|
||||||
|
".ps1": "powershell",
|
||||||
|
".dockerfile": "dockerfile", # 보통 안 걸림. 아래 별도 처리도 참고.
|
||||||
|
".yml": "yaml",
|
||||||
|
".yaml": "yaml",
|
||||||
|
".json": "json",
|
||||||
|
".toml": "toml",
|
||||||
|
".ini": "ini",
|
||||||
|
|
||||||
|
# Data / Docs
|
||||||
|
".md": "markdown",
|
||||||
|
".txt": "text",
|
||||||
|
".sql": "sql",
|
||||||
|
".xml": "xml",
|
||||||
|
".csv": "csv",
|
||||||
|
}
|
||||||
|
|
||||||
|
# 확장자 없는 케이스들 (대표적으로 Dockerfile 등)
|
||||||
|
base = os.path.basename(path or "").lower()
|
||||||
|
if base == "dockerfile":
|
||||||
|
return "dockerfile"
|
||||||
|
if base in {"makefile"}:
|
||||||
|
return "makefile"
|
||||||
|
|
||||||
|
return ext_to_lang.get(ext, "text")
|
||||||
@@ -26,6 +26,7 @@ from .services import *
|
|||||||
from .permissions import *
|
from .permissions import *
|
||||||
|
|
||||||
from common.utils.fileManager import file_delete
|
from common.utils.fileManager import file_delete
|
||||||
|
from common.utils.mapManager import guess_language_from_path
|
||||||
|
|
||||||
from projects.serializers import *
|
from projects.serializers import *
|
||||||
from portfolios.serializers import *
|
from portfolios.serializers import *
|
||||||
@@ -213,8 +214,50 @@ class GithubAPIViewSet(viewsets.ViewSet):
|
|||||||
if not (repo_full and sha):
|
if not (repo_full and sha):
|
||||||
return Response({"is_query_param": False}, status=status.HTTP_400_BAD_REQUEST)
|
return Response({"is_query_param": False}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
commit = self.github.get_repo(repo_full).get_commit(sha)
|
try:
|
||||||
return Response([{"filename": f.filename, "status": f.status} for f in commit.files])
|
page = int(request.query_params.get("page", 1))
|
||||||
|
except ValueError:
|
||||||
|
return Response({"is_page_int": False,"detail": "page and page_size must be integers"}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
if page < 1:
|
||||||
|
return Response({"is_page_gte_1": False, "detail": "page and page_size must be >= 1"}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
try:
|
||||||
|
repo = self.github.get_repo(repo_full)
|
||||||
|
commit = repo.get_commit(sha)
|
||||||
|
files = list(commit.files)
|
||||||
|
total_count = len(files)
|
||||||
|
|
||||||
|
total_page = GithubTokenService.cal_github_total_page(total_count, PAGE_SIZE)
|
||||||
|
|
||||||
|
if total_page != 0 and page > total_page:
|
||||||
|
return Response(
|
||||||
|
{
|
||||||
|
"is_page_le_total_page": False,
|
||||||
|
"detail": "page is out of range",
|
||||||
|
"page": page,
|
||||||
|
"total_page": total_page,
|
||||||
|
},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
|
)
|
||||||
|
|
||||||
|
start = (page - 1) * PAGE_SIZE
|
||||||
|
end = start + PAGE_SIZE
|
||||||
|
paged_files = files[start:end]
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
results = [{"filename": f.filename, "status": f.status} for f in paged_files]
|
||||||
|
|
||||||
|
return Response(
|
||||||
|
{
|
||||||
|
"page": page,
|
||||||
|
"page_size": PAGE_SIZE,
|
||||||
|
"total_page": total_page,
|
||||||
|
"results": results
|
||||||
|
}, status=status.HTTP_200_OK
|
||||||
|
)
|
||||||
|
|
||||||
@action(detail=False, methods=["get"], url_path="file-content")
|
@action(detail=False, methods=["get"], url_path="file-content")
|
||||||
def file_content(self, request):
|
def file_content(self, request):
|
||||||
@@ -229,7 +272,15 @@ class GithubAPIViewSet(viewsets.ViewSet):
|
|||||||
content = file.decoded_content.decode("utf-8", errors="replace")
|
content = file.decoded_content.decode("utf-8", errors="replace")
|
||||||
else:
|
else:
|
||||||
content = file.content
|
content = file.content
|
||||||
return Response({"path": file.path, "ref": sha, "content": content})
|
|
||||||
|
language = guess_language_from_path(file.path)
|
||||||
|
|
||||||
|
return Response({
|
||||||
|
"path": file.path,
|
||||||
|
"ref": sha,
|
||||||
|
"language": language,
|
||||||
|
"content": content,
|
||||||
|
})
|
||||||
except GithubException as e:
|
except GithubException as e:
|
||||||
return Response({"detail": str(e)}, status=e.status)
|
return Response({"detail": str(e)}, status=e.status)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user