|
| 1 | +import osc.commandline_git |
| 2 | + |
| 3 | + |
| 4 | +class MetaPullCommand(osc.commandline_git.GitObsCommand): |
| 5 | + """ |
| 6 | + Pull metadata about the project or package from Gitea. |
| 7 | + """ |
| 8 | + |
| 9 | + name = "pull" |
| 10 | + parent = "MetaCommand" |
| 11 | + |
| 12 | + def init_arguments(self): |
| 13 | + pass |
| 14 | + |
| 15 | + def run(self, args): |
| 16 | + from osc import gitea_api |
| 17 | + from osc.git_scm.configuration import Configuration |
| 18 | + from osc.git_scm.manifest import Manifest |
| 19 | + from osc.git_scm.store import LocalGitStore |
| 20 | + from osc.output import KeyValueTable |
| 21 | + |
| 22 | + self.print_gitea_settings() |
| 23 | + |
| 24 | + store = LocalGitStore(".") |
| 25 | + |
| 26 | + apiurl = None |
| 27 | + project = None |
| 28 | + |
| 29 | + # read apiurl and project from _manifest that lives in <owner>/_ObsPrj, matching <branch> |
| 30 | + # XXX: when the target branch doesn't exist, file from the default branch is returned |
| 31 | + if store.is_package: |
| 32 | + try: |
| 33 | + owner, _ = store._git.get_owner_repo() |
| 34 | + repo = "_ObsPrj" |
| 35 | + branch = store._git.current_branch |
| 36 | + |
| 37 | + url = self.gitea_conn.makeurl("repos", owner, repo, "raw", "_manifest", query={"ref": branch}) |
| 38 | + response = self.gitea_conn.request("GET", url) |
| 39 | + if response.data: |
| 40 | + manifest = Manifest.from_string(response.data.decode("utf-8")) |
| 41 | + if manifest.obs_apiurl: |
| 42 | + apiurl = manifest.obs_apiurl |
| 43 | + if manifest.obs_project: |
| 44 | + project = manifest.obs_project |
| 45 | + except gitea_api.GiteaException as e: |
| 46 | + if e.status != 404: |
| 47 | + raise |
| 48 | + |
| 49 | + # read apiurl from the global configuration in obs/configuration. branch |
| 50 | + if not apiurl: |
| 51 | + try: |
| 52 | + url = self.gitea_conn.makeurl("repos", "obs", "configuration", "raw", "configuration.yaml", query={"ref": "main"}) |
| 53 | + response = self.gitea_conn.request("GET", url) |
| 54 | + if response.data: |
| 55 | + configuration = Configuration.from_string(response.data.decode("utf-8")) |
| 56 | + if configuration.obs_apiurl: |
| 57 | + apiurl = configuration.obs_apiurl |
| 58 | + except gitea_api.GiteaException as e: |
| 59 | + if e.status != 404: |
| 60 | + raise |
| 61 | + |
| 62 | + if apiurl: |
| 63 | + store.apiurl = apiurl |
| 64 | + |
| 65 | + if project: |
| 66 | + store.project = project |
| 67 | + |
| 68 | + branch = store._git.current_branch |
| 69 | + meta = store._read_meta(branch=branch).dict() |
| 70 | + meta.pop("header", None) |
| 71 | + |
| 72 | + table = KeyValueTable(min_key_length=10) |
| 73 | + table.add("Branch", branch, color="bold") |
| 74 | + for key, value in meta.items(): |
| 75 | + table.add(key, value) |
| 76 | + print(str(table)) |
0 commit comments