feat: Implement User and Organization repo creation

Until now, the repository creation was only possible using the
gitea.Gitea.create_repo function. Get, this only allows the admin to
create a repo for a user or an organization.

By implementing these two new functions:
1. a non-root user can now create an own repo,
2. an allowed user can now create a repo into an organization.
pull/13/head
Etienne Monier 2022-03-11 11:36:39 +01:00
rodzic cfd1b7f42d
commit f9e4c68fa7
3 zmienionych plików z 122 dodań i 4 usunięć

Wyświetl plik

@ -48,6 +48,45 @@ class Organization(ApiObject):
)
self.dirty_fields = {}
def create_repo(
self,
repoName: str,
description: str = "",
private: bool = False,
autoInit=True,
gitignores: str = None,
license: str = None,
readme: str = "Default",
issue_labels: str = None,
default_branch="master",
):
"""Create an organization Repository
Throws:
AlreadyExistsException: If the Repository exists already.
Exception: If something else went wrong.
"""
result = self.gitea.requests_post(
f"/orgs/{self.name}/repos",
data={
"name": repoName,
"description": description,
"private": private,
"auto_init": autoInit,
"gitignores": gitignores,
"license": license,
"issue_labels": issue_labels,
"readme": readme,
"default_branch": default_branch,
},
)
if "id" in result:
self.gitea.logger.info("Successfully created Repository %s " % result["name"])
else:
self.gitea.logger.error(result["message"])
raise Exception("Repository not created... (gitea: %s)" % result["message"])
return Repository.parse_response(self, result)
def get_repositories(self) -> List["Repository"]:
results = self.gitea.requests_get_paginated(
Organization.ORG_REPOS_REQUEST % self.username
@ -175,6 +214,45 @@ class User(ApiObject):
self.gitea.requests_patch(User.ADMIN_EDIT_USER.format(**args), data=values)
self.dirty_fields = {}
def create_repo(
self,
repoName: str,
description: str = "",
private: bool = False,
autoInit=True,
gitignores: str = None,
license: str = None,
readme: str = "Default",
issue_labels: str = None,
default_branch="master",
):
"""Create a user Repository
Throws:
AlreadyExistsException: If the Repository exists already.
Exception: If something else went wrong.
"""
result = self.gitea.requests_post(
"/user/repos",
data={
"name": repoName,
"description": description,
"private": private,
"auto_init": autoInit,
"gitignores": gitignores,
"license": license,
"issue_labels": issue_labels,
"readme": readme,
"default_branch": default_branch,
},
)
if "id" in result:
self.gitea.logger.info("Successfully created Repository %s " % result["name"])
else:
self.gitea.logger.error(result["message"])
raise Exception("Repository not created... (gitea: %s)" % result["message"])
return Repository.parse_response(self, result)
def get_repositories(self) -> List["Repository"]:
""" Get all Repositories owned by this User."""
url = f"/users/{self.username}/repos"

Wyświetl plik

@ -234,11 +234,15 @@ class Gitea:
issue_labels: str = None,
default_branch="master",
):
""" Create a Repository.
Throws:
AlreadyExistsException, if Repository exists already.
Exception, if something else went wrong.
""" Create a Repository as the administrator
Throws:
AlreadyExistsException: If the Repository exists already.
Exception: If something else went wrong.
Note:
Non-admin users can not use this method. Please use instead
`gitea.User.create_repo` or `gitea.Organization.create_repo`.
"""
# although this only says user in the api, this also works for
# organizations

36
tests/conftest.py 100644
Wyświetl plik

@ -0,0 +1,36 @@
#!/usr/bin/env python
"""Fixtures for testing py-gitea
Instructions
------------
put a ".token" file into your directory containg only the token for gitea
"""
import os
import pytest
from gitea import Gitea
@pytest.fixture
def instance(scope="module"):
try:
url = os.getenv("PY_GITEA_URL")
token = os.getenv("PY_GITEA_TOKEN")
auth = os.getenv("PY_GITEA_AUTH")
if not url:
raise ValueError("No Gitea URL was provided")
if token and auth:
raise ValueError("Please provide auth or token_text, but not both")
g = Gitea(url, token_text=token, auth=auth, verify=False)
print("Gitea Version: " + g.get_version())
print("API-Token belongs to user: " + g.get_user().username)
return g
except:
assert (
False
), "Gitea could not load. \
- Instance running at http://localhost:3000 \
- Token at .token \
?"