mirror of
https://github.com/openSUSE/osc.git
synced 2025-10-25 17:52:16 +02:00
Move code from 'git-obs meta pull' command to GitStore.pull()
This commit is contained in:
@@ -13,64 +13,17 @@ class MetaPullCommand(osc.commandline_git.GitObsCommand):
|
||||
pass
|
||||
|
||||
def run(self, args):
|
||||
from osc import gitea_api
|
||||
from osc.git_scm.configuration import Configuration
|
||||
from osc.git_scm.manifest import Manifest
|
||||
from osc.git_scm.store import LocalGitStore
|
||||
from osc.git_scm.store import GitStore
|
||||
from osc.output import KeyValueTable
|
||||
|
||||
self.print_gitea_settings()
|
||||
|
||||
store = LocalGitStore(".")
|
||||
|
||||
apiurl = None
|
||||
project = None
|
||||
|
||||
# read apiurl and project from _manifest that lives in <owner>/_ObsPrj, matching <branch>
|
||||
# XXX: when the target branch doesn't exist, file from the default branch is returned
|
||||
if store.is_package:
|
||||
try:
|
||||
owner, _ = store._git.get_owner_repo()
|
||||
repo = "_ObsPrj"
|
||||
store = GitStore(".", check=False)
|
||||
branch = store._git.current_branch
|
||||
|
||||
url = self.gitea_conn.makeurl("repos", owner, repo, "raw", "_manifest", query={"ref": branch})
|
||||
response = self.gitea_conn.request("GET", url)
|
||||
if response.data:
|
||||
manifest = Manifest.from_string(response.data.decode("utf-8"))
|
||||
if manifest.obs_apiurl:
|
||||
apiurl = manifest.obs_apiurl
|
||||
if manifest.obs_project:
|
||||
project = manifest.obs_project
|
||||
except gitea_api.GiteaException as e:
|
||||
if e.status != 404:
|
||||
raise
|
||||
|
||||
# read apiurl from the global configuration in obs/configuration. branch
|
||||
if not apiurl:
|
||||
try:
|
||||
url = self.gitea_conn.makeurl("repos", "obs", "configuration", "raw", "configuration.yaml", query={"ref": "main"})
|
||||
response = self.gitea_conn.request("GET", url)
|
||||
if response.data:
|
||||
configuration = Configuration.from_string(response.data.decode("utf-8"))
|
||||
if configuration.obs_apiurl:
|
||||
apiurl = configuration.obs_apiurl
|
||||
except gitea_api.GiteaException as e:
|
||||
if e.status != 404:
|
||||
raise
|
||||
|
||||
if apiurl:
|
||||
store.apiurl = apiurl
|
||||
|
||||
if project:
|
||||
store.project = project
|
||||
|
||||
branch = store._git.current_branch
|
||||
meta = store._read_meta(branch=branch).dict()
|
||||
meta.pop("header", None)
|
||||
changed = store.pull(self.gitea_conn)
|
||||
|
||||
table = KeyValueTable(min_key_length=10)
|
||||
table.add("Branch", branch, color="bold")
|
||||
for key, value in meta.items():
|
||||
for key, value in changed.items():
|
||||
table.add(key, value)
|
||||
print(str(table))
|
||||
|
||||
@@ -5,6 +5,7 @@ import os
|
||||
from pathlib import Path
|
||||
import typing
|
||||
import urllib.parse
|
||||
from typing import Dict
|
||||
from typing import List
|
||||
from typing import Optional
|
||||
from typing import Tuple
|
||||
@@ -16,7 +17,8 @@ from ..util.models import Field
|
||||
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
from .core import Repo
|
||||
from ..core import Repo
|
||||
from ..obs_api import GiteaConnection
|
||||
|
||||
|
||||
class Header(BaseModel):
|
||||
@@ -570,3 +572,57 @@ class GitStore(LocalGitStore):
|
||||
@property
|
||||
def scmurl(self) -> Optional[str]:
|
||||
return self._git.get_remote_url()
|
||||
|
||||
def pull(self, gitea_conn) -> Dict[str, Optional[str]]:
|
||||
from osc.git_scm.configuration import Configuration
|
||||
from osc.git_scm.manifest import Manifest
|
||||
from .. import gitea_api
|
||||
|
||||
apiurl = None
|
||||
project = None
|
||||
|
||||
# read apiurl and project from _manifest that lives in <owner>/_ObsPrj, matching <branch>
|
||||
# XXX: when the target branch doesn't exist, file from the default branch is returned
|
||||
if self.is_package:
|
||||
try:
|
||||
owner, _ = self._git.get_owner_repo()
|
||||
repo = "_ObsPrj"
|
||||
branch = self._git.current_branch
|
||||
|
||||
url = gitea_conn.makeurl("repos", owner, repo, "raw", "_manifest", query={"ref": branch})
|
||||
response = gitea_conn.request("GET", url)
|
||||
if response.data:
|
||||
manifest = Manifest.from_string(response.data.decode("utf-8"))
|
||||
if manifest.obs_apiurl:
|
||||
apiurl = manifest.obs_apiurl
|
||||
if manifest.obs_project:
|
||||
project = manifest.obs_project
|
||||
except gitea_api.GiteaException as e:
|
||||
if e.status != 404:
|
||||
raise
|
||||
|
||||
# read apiurl from the global configuration in obs/configuration. branch
|
||||
if not apiurl:
|
||||
try:
|
||||
url = gitea_conn.makeurl("repos", "obs", "configuration", "raw", "configuration.yaml", query={"ref": "main"})
|
||||
response = gitea_conn.request("GET", url)
|
||||
if response.data:
|
||||
configuration = Configuration.from_string(response.data.decode("utf-8"))
|
||||
if configuration.obs_apiurl:
|
||||
apiurl = configuration.obs_apiurl
|
||||
except gitea_api.GiteaException as e:
|
||||
if e.status != 404:
|
||||
raise
|
||||
|
||||
if apiurl:
|
||||
self.set_apiurl(apiurl)
|
||||
|
||||
if project:
|
||||
self.set_project(project)
|
||||
|
||||
# return the values we've set
|
||||
result = {
|
||||
"apiurl": apiurl,
|
||||
"project": project,
|
||||
}
|
||||
return result
|
||||
|
||||
@@ -8,6 +8,8 @@ from typing import List
|
||||
from typing import Optional
|
||||
from typing import Tuple
|
||||
|
||||
from . import exceptions
|
||||
|
||||
|
||||
class SshParseResult(urllib.parse.ParseResult):
|
||||
"""
|
||||
@@ -262,6 +264,8 @@ class Git:
|
||||
|
||||
def get_owner_repo(self, remote: Optional[str] = None) -> Tuple[str, str]:
|
||||
remote_url = self.get_remote_url(name=remote)
|
||||
if not remote_url:
|
||||
raise exceptions.GitObsRuntimeError("Couldn't determine owner and repo due to a missing remote")
|
||||
return self.get_owner_repo_from_url(remote_url)
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in New Issue
Block a user