gh / app.py
adowu's picture
Create app.py
ca1d775 verified
raw
history blame
21.9 kB
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()