1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-10-26 17:12:15 +01:00

Move code from 'git-obs meta pull' command to GitStore.pull()

This commit is contained in:
2025-10-09 13:26:26 +02:00
parent a5afc88561
commit 87de410542
3 changed files with 65 additions and 52 deletions

View File

@@ -13,64 +13,17 @@ class MetaPullCommand(osc.commandline_git.GitObsCommand):
pass pass
def run(self, args): def run(self, args):
from osc import gitea_api from osc.git_scm.store import GitStore
from osc.git_scm.configuration import Configuration
from osc.git_scm.manifest import Manifest
from osc.git_scm.store import LocalGitStore
from osc.output import KeyValueTable from osc.output import KeyValueTable
self.print_gitea_settings() self.print_gitea_settings()
store = LocalGitStore(".") store = GitStore(".", check=False)
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"
branch = store._git.current_branch branch = store._git.current_branch
changed = store.pull(self.gitea_conn)
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)
table = KeyValueTable(min_key_length=10) table = KeyValueTable(min_key_length=10)
table.add("Branch", branch, color="bold") table.add("Branch", branch, color="bold")
for key, value in meta.items(): for key, value in changed.items():
table.add(key, value) table.add(key, value)
print(str(table)) print(str(table))

View File

@@ -5,6 +5,7 @@ import os
from pathlib import Path from pathlib import Path
import typing import typing
import urllib.parse import urllib.parse
from typing import Dict
from typing import List from typing import List
from typing import Optional from typing import Optional
from typing import Tuple from typing import Tuple
@@ -16,7 +17,8 @@ from ..util.models import Field
if typing.TYPE_CHECKING: if typing.TYPE_CHECKING:
from .core import Repo from ..core import Repo
from ..obs_api import GiteaConnection
class Header(BaseModel): class Header(BaseModel):
@@ -570,3 +572,57 @@ class GitStore(LocalGitStore):
@property @property
def scmurl(self) -> Optional[str]: def scmurl(self) -> Optional[str]:
return self._git.get_remote_url() 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

View File

@@ -8,6 +8,8 @@ from typing import List
from typing import Optional from typing import Optional
from typing import Tuple from typing import Tuple
from . import exceptions
class SshParseResult(urllib.parse.ParseResult): class SshParseResult(urllib.parse.ParseResult):
""" """
@@ -262,6 +264,8 @@ class Git:
def get_owner_repo(self, remote: Optional[str] = None) -> Tuple[str, str]: def get_owner_repo(self, remote: Optional[str] = None) -> Tuple[str, str]:
remote_url = self.get_remote_url(name=remote) 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) return self.get_owner_repo_from_url(remote_url)
@staticmethod @staticmethod