import gradio as gr from github import Github, GithubException import os import requests # Załaduj token z pliku .env lub ustaw bezpośrednio GITHUB_TOKEN = os.getenv('GITHUB_TOKEN') g = Github(GITHUB_TOKEN) def import_repository(owner: str, repo_name: str, vcs_url: str): """Importuje repozytorium z podanego URL.""" try: user = g.get_user() repo = user.get_repo(repo_name) # Sprawdź, czy repozytorium już istnieje return "Repozytorium o tej nazwie już istnieje." except GithubException: pass # Repozytorium nie istnieje, można importować headers = { 'Authorization': f'token {GITHUB_TOKEN}', 'Accept': 'application/vnd.github.v3+json', } import_url = f'https://api.github.com/repos/{owner}/{repo_name}/import' payload = { 'vcs_url': vcs_url, 'vcs': 'git', } try: response = requests.put(import_url, json=payload, headers=headers) if response.status_code == 201: return "Import repozytorium został rozpoczęty." else: return f"Nie udało się rozpocząć importu repozytorium. Status: {response.status_code}, Szczegóły: {response.json()}" except Exception as e: return f"Wystąpił błąd podczas próby importu repozytorium: {str(e)}" def create_repository(name: str): """Tworzy nowe repozytorium.""" try: user = g.get_user() repo = user.create_repo(name=name) return f"Repozytorium {name} zostało utworzone: {repo.html_url}" except GithubException as e: return f"Nie udało się utworzyć repozytorium: {str(e)}" def create_file(repo_name: str, branch: str, message: str, content: str, path: str): """Tworzy plik w repozytorium.""" try: user = g.get_user() repo = user.get_repo(repo_name) repo.create_file(path, message, content, branch=branch) return f"Plik {path} został utworzony w {repo_name} na gałęzi {branch}" except GithubException as e: return f"Nie udało się utworzyć pliku: {str(e)}" def get_file(repo_name: str, path: str): """Pobiera zawartość pliku z repozytorium.""" try: user = g.get_user() repo = user.get_repo(repo_name) file_content = repo.get_contents(path) return file_content.decoded_content.decode() except GithubException as e: return f"Nie znaleziono pliku: {str(e)}" def delete_file(repo_name: str, branch: str, path: str): """Usuwa plik z repozytorium.""" try: user = g.get_user() repo = user.get_repo(repo_name) file_contents = repo.get_contents(path, ref=branch) repo.delete_file(path, "Usuń plik", file_contents.sha, branch=branch) return f"Plik {path} został usunięty z {repo_name} na gałęzi {branch}" except GithubException as e: return f"Nie udało się usunąć pliku: {str(e)}" def update_file(repo_name: str, branch: str, path: str, message: str, content: str): """Aktualizuje plik w repozytorium.""" try: user = g.get_user() repo = user.get_repo(repo_name) file_contents = repo.get_contents(path, ref=branch) repo.update_file(path, message, content, file_contents.sha, branch=branch) return f"Plik {path} został zaktualizowany w {repo_name} na gałęzi {branch}" except GithubException as e: return f"Nie udało się zaktualizować pliku: {str(e)}" def list_branches(repo_name: str): """Listuje gałęzie w repozytorium.""" try: user = g.get_user() repo = user.get_repo(repo_name) branches = repo.get_branches() return [branch.name for branch in branches] except GithubException as e: return f"Nie udało się pobrać listy gałęzi: {str(e)}" def create_branch(repo_name: str, source_branch: str, new_branch: str): """Tworzy nową gałąź w repozytorium.""" try: user = g.get_user() repo = user.get_repo(repo_name) source_branch = repo.get_branch(source_branch) repo.create_git_ref(ref=f"refs/heads/{new_branch}", sha=source_branch.commit.sha) return f"Gałąź {new_branch} została utworzona od {source_branch}" except GithubException as e: return f"Nie udało się utworzyć gałęzi: {str(e)}" def delete_branch(repo_name: str, branch_name: str): """Usuwa gałąź z repozytorium.""" try: user = g.get_user() repo = user.get_repo(repo_name) repo.get_git_ref(f"heads/{branch_name}").delete() return f"Gałąź {branch_name} została usunięta" except GithubException as e: return f"Nie znaleziono gałęzi lub błąd podczas usuwania: {str(e)}" def create_pull_request(repo_name: str, title: str, body: str, base: str, head: str): """Tworzy pull request.""" try: user = g.get_user() repo = user.get_repo(repo_name) pr = repo.create_pull(title=title, body=body, base=base, head=head) return f"Pull request utworzony: {pr.html_url}" except GithubException as e: return f"Nie udało się utworzyć pull requesta: {str(e)}" def list_open_pull_requests(repo_name: str): """Listuje otwarte pull requesty.""" try: user = g.get_user() repo = user.get_repo(repo_name) open_prs = repo.get_pulls(state='open') return [{'title': pr.title, 'url': pr.html_url} for pr in open_prs] except GithubException as e: return f"Nie udało się pobrać listy otwartych pull requestów: {str(e)}" def create_issue(repo_name: str, title: str, body: str): """Tworzy issue.""" try: user = g.get_user() repo = user.get_repo(repo_name) issue = repo.create_issue(title=title, body=body) return f"Issue utworzone: {issue.html_url}" except GithubException as e: return f"Nie udało się utworzyć issue: {str(e)}" def list_issues(repo_name: str): """Listuje issue.""" try: user = g.get_user() repo = user.get_repo(repo_name) issues = repo.get_issues(state='open') return [{'title': issue.title, 'url': issue.html_url} for issue in issues] except GithubException as e: return f"Nie udało się pobrać listy issue: {str(e)}" def add_label_to_issue(repo_name: str, issue_number: int, labels: list[str]): """Dodaje etykiety do issue.""" try: user = g.get_user() repo = user.get_repo(repo_name) issue = repo.get_issue(number=int(issue_number)) for label in labels: issue.add_to_labels(label) return f"Dodano etykiety do issue #{issue_number}" except GithubException as e: return f"Nie udało się dodać etykiet do issue: {str(e)}" def close_issue(repo_name: str, issue_number: int): """Zamyka issue.""" try: user = g.get_user() repo = user.get_repo(repo_name) issue = repo.get_issue(number=int(issue_number)) issue.edit(state='closed') return f"Issue #{issue_number} zostało zamknięte" except GithubException as e: return f"Nie udało się zamknąć issue: {str(e)}" def add_comment_to_issue(repo_name: str, issue_number: int, comment: str): """Dodaje komentarz do issue.""" try: user = g.get_user() repo = user.get_repo(repo_name) issue = repo.get_issue(number=int(issue_number)) issue.create_comment(body=comment) return f"Dodano komentarz do issue #{issue_number}" except GithubException as e: return f"Nie udało się dodać komentarza do issue: {str(e)}" def create_release(repo_name: str, tag: str, name: str, message: str): """Tworzy release.""" try: user = g.get_user() repo = user.get_repo(repo_name) release = repo.create_git_release(tag=tag, name=name, message=message) return f"Release utworzone: {release.html_url}" except GithubException as e: return f"Nie udało się utworzyć release: {str(e)}" def list_releases(repo_name: str): """Listuje release.""" try: user = g.get_user() repo = user.get_repo(repo_name) releases = repo.get_releases() return [{'tag_name': release.tag_name, 'url': release.html_url} for release in releases] except GithubException as e: return f"Nie udało się pobrać listy release: {str(e)}" def fork_repository(repo_name: str): """Forkuje repozytorium.""" try: user = g.get_user() repo = g.get_repo(repo_name) # Pobierz repozytorium do forkowania fork = user.create_fork(repo) return f"Fork repozytorium utworzony: {fork.html_url}" except GithubException as e: return f"Nie udało się utworzyć forka repozytorium: {str(e)}" def list_forks(repo_name: str): """Listuje forki repozytorium.""" try: repo = g.get_repo(repo_name) # Pobierz repozytorium, którego forki chcesz wyświetlić forks = repo.get_forks() return [{'full_name': fork.full_name, 'url': fork.html_url} for fork in forks] except GithubException as e: return f"Nie udało się pobrać listy forków: {str(e)}" def list_files(owner: str, repo_name: str, path: str = ""): """Listuje pliki w repozytorium.""" try: repo = g.get_repo(f"{owner}/{repo_name}") contents = repo.get_contents(path) files = [{'name': content.name, 'path': content.path, 'download_url': content.download_url} for content in contents] return files except GithubException as e: return f"Nie udało się pobrać listy plików: {str(e)}" with gr.Blocks() as demo: with gr.Tab("Repozytoria"): with gr.Row(): import_repo_owner = gr.Textbox(label="Właściciel") import_repo_name = gr.Textbox(label="Nazwa repozytorium") import_repo_vcs_url = gr.Textbox(label="URL VCS") import_repo_button = gr.Button("Importuj repozytorium") import_repo_output = gr.Textbox(label="Wynik") import_repo_button.click( import_repository, inputs=[import_repo_owner, import_repo_name, import_repo_vcs_url], outputs=import_repo_output, api_name="import_repository" ) with gr.Row(): create_repo_name = gr.Textbox(label="Nazwa repozytorium") create_repo_button = gr.Button("Utwórz repozytorium") create_repo_output = gr.Textbox(label="Wynik") create_repo_button.click( create_repository, inputs=create_repo_name, outputs=create_repo_output, api_name="create_repository" ) with gr.Tab("Pliki"): with gr.Row(): create_file_repo_name = gr.Textbox(label="Nazwa repozytorium") create_file_branch = gr.Textbox(label="Gałąź") create_file_message = gr.Textbox(label="Wiadomość commita") create_file_content = gr.Textbox(label="Zawartość pliku") create_file_path = gr.Textbox(label="Ścieżka do pliku") create_file_button = gr.Button("Utwórz plik") create_file_output = gr.Textbox(label="Wynik") create_file_button.click( create_file, inputs=[create_file_repo_name, create_file_branch, create_file_message, create_file_content, create_file_path], outputs=create_file_output, api_name="create_file" ) with gr.Row(): get_file_repo_name = gr.Textbox(label="Nazwa repozytorium") get_file_path = gr.Textbox(label="Ścieżka do pliku") get_file_button = gr.Button("Pobierz plik") get_file_output = gr.Textbox(label="Zawartość pliku") get_file_button.click( get_file, inputs=[get_file_repo_name, get_file_path], outputs=get_file_output, api_name="get_file" ) with gr.Row(): delete_file_repo_name = gr.Textbox(label="Nazwa repozytorium") delete_file_branch = gr.Textbox(label="Gałąź") delete_file_path = gr.Textbox(label="Ścieżka do pliku") delete_file_button = gr.Button("Usuń plik") delete_file_output = gr.Textbox(label="Wynik") delete_file_button.click( delete_file, inputs=[delete_file_repo_name, delete_file_branch, delete_file_path], outputs=delete_file_output, api_name="delete_file" ) with gr.Row(): update_file_repo_name = gr.Textbox(label="Nazwa repozytorium") update_file_branch = gr.Textbox(label="Gałąź") update_file_path = gr.Textbox(label="Ścieżka do pliku") update_file_message = gr.Textbox(label="Wiadomość commita") update_file_content = gr.Textbox(label="Nowa zawartość pliku") update_file_button = gr.Button("Zaktualizuj plik") update_file_output = gr.Textbox(label="Wynik") update_file_button.click( update_file, inputs=[update_file_repo_name, update_file_branch, update_file_path, update_file_message, update_file_content], outputs=update_file_output, api_name="update_file" ) with gr.Tab("Gałęzie"): with gr.Row(): list_branches_repo_name = gr.Textbox(label="Nazwa repozytorium") list_branches_button = gr.Button("Listuj gałęzie") list_branches_output = gr.Textbox(label="Lista gałęzi") list_branches_button.click( list_branches, inputs=list_branches_repo_name, outputs=list_branches_output, api_name="list_branches" ) with gr.Row(): create_branch_repo_name = gr.Textbox(label="Nazwa repozytorium") create_branch_source_branch = gr.Textbox( label="Nazwa gałęzi źródłowej") create_branch_new_branch = gr.Textbox(label="Nazwa nowej gałęzi") create_branch_button = gr.Button("Utwórz gałąź") create_branch_output = gr.Textbox(label="Wynik") create_branch_button.click( create_branch, inputs=[create_branch_repo_name, create_branch_source_branch, create_branch_new_branch], outputs=create_branch_output, api_name="create_branch" ) with gr.Row(): delete_branch_repo_name = gr.Textbox(label="Nazwa repozytorium") delete_branch_branch_name = gr.Textbox(label="Nazwa gałęzi") delete_branch_button = gr.Button("Usuń gałąź") delete_branch_output = gr.Textbox(label="Wynik") delete_branch_button.click( delete_branch, inputs=[delete_branch_repo_name, delete_branch_branch_name], outputs=delete_branch_output, api_name="delete_branch" ) with gr.Tab("Pull Requesty"): with gr.Row(): create_pr_repo_name = gr.Textbox(label="Nazwa repozytorium") create_pr_title = gr.Textbox(label="Tytuł") create_pr_body = gr.Textbox(label="Treść") create_pr_base = gr.Textbox(label="Gałąź bazowa") create_pr_head = gr.Textbox(label="Gałąź docelowa") create_pr_button = gr.Button("Utwórz Pull Request") create_pr_output = gr.Textbox(label="Wynik") create_pr_button.click( create_pull_request, inputs=[create_pr_repo_name, create_pr_title, create_pr_body, create_pr_base, create_pr_head], outputs=create_pr_output, api_name="create_pull_request" ) with gr.Row(): list_open_prs_repo_name = gr.Textbox(label="Nazwa repozytorium") list_open_prs_button = gr.Button("Listuj otwarte Pull Requesty") list_open_prs_output = gr.JSON(label="Lista otwartych Pull Requestów") list_open_prs_button.click( list_open_pull_requests, inputs=list_open_prs_repo_name, outputs=list_open_prs_output, api_name="list_open_pull_requests" ) with gr.Tab("Issue"): with gr.Row(): create_issue_repo_name = gr.Textbox(label="Nazwa repozytorium") create_issue_title = gr.Textbox(label="Tytuł") create_issue_body = gr.Textbox(label="Treść") create_issue_button = gr.Button("Utwórz Issue") create_issue_output = gr.Textbox(label="Wynik") create_issue_button.click( create_issue, inputs=[create_issue_repo_name, create_issue_title, create_issue_body], outputs=create_issue_output, api_name="create_issue" ) with gr.Row(): list_issues_repo_name = gr.Textbox(label="Nazwa repozytorium") list_issues_button = gr.Button("Listuj Issue") list_issues_output = gr.JSON(label="Lista Issue") list_issues_button.click( list_issues, inputs=list_issues_repo_name, outputs=list_issues_output, api_name="list_issues" ) with gr.Row(): add_label_repo_name = gr.Textbox(label="Nazwa repozytorium") add_label_issue_number = gr.Number( label="Numer Issue", precision=0) add_label_labels = gr.Textbox( label="Etykiety (oddzielone przecinkami)") add_label_button = gr.Button("Dodaj etykiety") add_label_output = gr.Textbox(label="Wynik") add_label_button.click( lambda repo_name, issue_number, labels: add_label_to_issue( repo_name, issue_number, labels.split(",")), inputs=[add_label_repo_name, add_label_issue_number, add_label_labels], outputs=add_label_output, api_name="add_label_to_issue" ) with gr.Row(): close_issue_repo_name = gr.Textbox(label="Nazwa repozytorium") close_issue_issue_number = gr.Number( label="Numer Issue", precision=0) close_issue_button = gr.Button("Zamknij Issue") close_issue_output = gr.Textbox(label="Wynik") close_issue_button.click( close_issue, inputs=[close_issue_repo_name, close_issue_issue_number], outputs=close_issue_output, api_name="close_issue" ) with gr.Row(): add_comment_repo_name = gr.Textbox(label="Nazwa repozytorium") add_comment_issue_number = gr.Number( label="Numer Issue", precision=0) add_comment_comment = gr.Textbox(label="Komentarz") add_comment_button = gr.Button("Dodaj komentarz") add_comment_output = gr.Textbox(label="Wynik") add_comment_button.click( add_comment_to_issue, inputs=[add_comment_repo_name, add_comment_issue_number, add_comment_comment], outputs=add_comment_output, api_name="add_comment_to_issue" ) with gr.Tab("Release"): with gr.Row(): create_release_repo_name = gr.Textbox(label="Nazwa repozytorium") create_release_tag = gr.Textbox(label="Tag") create_release_name = gr.Textbox(label="Nazwa") create_release_message = gr.Textbox(label="Wiadomość") create_release_button = gr.Button("Utwórz Release") create_release_output = gr.Textbox(label="Wynik") create_release_button.click( create_release, inputs=[create_release_repo_name, create_release_tag, create_release_name, create_release_message], outputs=create_release_output, api_name="create_release" ) with gr.Row(): list_releases_repo_name = gr.Textbox(label="Nazwa repozytorium") list_releases_button = gr.Button("Listuj Release") list_releases_output = gr.JSON(label="Lista Release") list_releases_button.click( list_releases, inputs=list_releases_repo_name, outputs=list_releases_output, api_name="list_releases" ) with gr.Tab("Forki"): with gr.Row(): fork_repo_name = gr.Textbox(label="Nazwa repozytorium") fork_repo_button = gr.Button("Forkuj repozytorium") fork_repo_output = gr.Textbox(label="Wynik") fork_repo_button.click( fork_repository, inputs=fork_repo_name, outputs=fork_repo_output, api_name="fork_repository" ) with gr.Row(): list_forks_repo_name = gr.Textbox(label="Nazwa repozytorium") list_forks_button = gr.Button("Listuj forki") list_forks_output = gr.JSON(label="Lista forków") list_forks_button.click( list_forks, inputs=list_forks_repo_name, outputs=list_forks_output, api_name="list_forks" ) with gr.Tab("Listowanie plików"): with gr.Row(): list_files_owner = gr.Textbox(label="Właściciel") list_files_repo_name = gr.Textbox(label="Nazwa repozytorium") list_files_path = gr.Textbox( label="Ścieżka (opcjonalnie)", value="") list_files_button = gr.Button("Listuj pliki") list_files_output = gr.JSON(label="Lista plików") list_files_button.click( list_files, inputs=[list_files_owner, list_files_repo_name, list_files_path], outputs=list_files_output, api_name="list_files" ) demo.launch()