Compare commits

...

19 Commits

Author SHA1 Message Date
Dirk Müller
44b4d690db
Only stop importing when it isn't a jengelh repository 2024-12-02 09:34:49 +01:00
Dirk Müller
a69e861614
Switch the operating organization on the "pool" 2024-12-02 09:33:52 +01:00
Dirk Müller
1da740bd8b
Strip multibuild flavors from monitoring 2024-09-09 09:34:12 +02:00
Dirk Mueller
b3107ba3bf Merge pull request 'Stop importing/exporting scmsync packages/projects' (#32) from adamm/git-importer:option_for_non_factory into main
Reviewed-on: importers/git-importer#32
Reviewed-by: Dirk Mueller <dirkmueller@noreply@src.opensuse.org>
2024-09-03 12:40:00 +02:00
86f82325d8 Stop importing/exporting scmsync packages/projects
Also, allow other-than Factory projects
2024-08-08 10:35:53 +02:00
Dirk Mueller
39ba616226 Merge pull request 'Add ability to specify non-Factory' (#31) from adamm/git-importer:option_for_non_factory into main
Reviewed-on: importers/git-importer#31
Reviewed-by: Dirk Mueller <dirkmueller@noreply@src.opensuse.org>
2024-08-07 18:27:11 +02:00
531dbc7c1b Add ability to specify non-Factory
This is important for devel-project only imports
non-factory is still blocked by assert
2024-08-07 16:55:05 +02:00
Dirk Müller
1318f9e0c4
Remove devel branch import
this for yet undefined reason screws up systemd history import
2024-08-07 09:47:54 +02:00
Dirk Müller
d563076d9e
add explicit conversion to string to fix the concatenation 2024-08-07 09:47:18 +02:00
b11b3f1adb
Add and remove literal files
pathspec in git has special characters that we should not trigger.
Assume every filespec as literal
2024-08-01 16:53:46 +02:00
Dirk Müller
479738d4b2
ruff format run 2024-07-10 10:34:20 +02:00
Adam Majer
2d04136ca5
Make sure we create devel branch, when no diff to Factory 2024-06-13 15:36:59 +02:00
Adam Majer
40ad64ddff
Ignore .osc directory 2024-06-10 18:13:51 +02:00
Adam Majer
6bd5d72100
New branch is empty
New branches must be born empty
2024-06-10 17:06:15 +02:00
Dirk Müller
022ae5ab58
remember failed tasks in a separate directory 2024-06-10 17:04:43 +02:00
Dirk Müller
2ff8ed76d0
Reconnect to the AMQP bus when the connection breaks down 2024-06-10 17:04:25 +02:00
Dirk Müller
5f228dc046
enable robust push 2024-05-17 21:47:35 +02:00
Dirk Müller
4e07d8272e
don't loop over failed packages 2024-05-17 21:47:15 +02:00
Dirk Müller
2a3475ab6e
Create with sha256 enabled 2024-05-17 20:39:55 +02:00
7 changed files with 129 additions and 41 deletions

View File

@ -42,8 +42,8 @@ PROJECTS = [
]
def export_package(package, repodir, cachedir, gc):
exporter = GitExporter(URL_OBS, "openSUSE:Factory", package, repodir, cachedir)
def export_package(project, package, repodir, cachedir, gc):
exporter = GitExporter(URL_OBS, project, package, repodir, cachedir)
exporter.set_gc_interval(gc)
exporter.export_as_git()
@ -51,6 +51,12 @@ def export_package(package, repodir, cachedir, gc):
def main():
parser = argparse.ArgumentParser(description="OBS history importer into git")
parser.add_argument("packages", help="OBS package names", nargs="*")
parser.add_argument(
"-p",
"--project",
default="openSUSE:Factory",
help="Project to import/export, default is openSUSE:Factory",
)
parser.add_argument(
"-r",
"--repodir",
@ -110,10 +116,13 @@ def main():
if not args.cachedir:
args.cachedir = pathlib.Path("~/.cache/git-import/").expanduser()
importer = Importer(URL_OBS, "openSUSE:Factory", args.packages)
importer = Importer(URL_OBS, args.project, args.packages)
importer.import_into_db()
for package in args.packages:
export_package(package, args.repodir, args.cachedir, args.gc)
if not importer.package_with_scmsync(package):
export_package(args.project, package, args.repodir, args.cachedir, args.gc)
else:
logging.debug(f"{args.project}/{package} has scmsync links - skipping export")
if __name__ == "__main__":

View File

@ -48,6 +48,7 @@ class Git:
def open(self):
if not self.exists():
self.git_run(["init", "--object-format=sha256", "-b", "factory"])
self.git_run(["config", "lfs.allowincompletepush", "true"])
def is_dirty(self):
"""Check if there is something to commit"""
@ -85,7 +86,7 @@ class Git:
"""Checkout into the branch HEAD"""
new_branch = False
if branch not in self.branches():
self.git_run(["branch", "-q", branch, "HEAD"])
self.git_run(["switch", "-q", "--orphan", branch])
new_branch = True
else:
ref = f"refs/heads/{branch}"
@ -180,7 +181,13 @@ class Git:
# logging.warning(f"Error removing file {path}: {e}")
def add(self, filename):
self.git_run(["add", filename])
self.git_run(["add", ":(literal)" + str(filename)])
def add_default_gitignore(self):
if not (self.path / ".gitignore").exists():
with (self.path / ".gitignore").open("w") as f:
f.write(".osc\n")
self.add(".gitignore")
def add_default_lfs_gitattributes(self, force=False):
if not (self.path / ".gitattributes").exists() or force:
@ -235,7 +242,7 @@ class Git:
def remove(self, file: pathlib.Path):
self.git_run(
["rm", "-q", "-f", "--ignore-unmatch", file.name],
["rm", "-q", "-f", "--ignore-unmatch", ":(literal)" + file.name],
)
patterns = self.get_specific_lfs_gitattributes()
if file.name in patterns:
@ -244,7 +251,7 @@ class Git:
def add_gitea_remote(self, package):
repo_name = package.replace("+", "_")
org_name = "rpm"
org_name = "pool"
if not os.getenv("GITEA_TOKEN"):
logging.warning("Not adding a remote due to missing $GITEA_TOKEN")
@ -253,7 +260,7 @@ class Git:
url = f"https://src.opensuse.org/api/v1/org/{org_name}/repos"
response = requests.post(
url,
data={"name": repo_name},
data={"name": repo_name, "object_format_name": "sha256"},
headers={"Authorization": f"token {os.getenv('GITEA_TOKEN')}"},
timeout=10,
)
@ -271,13 +278,11 @@ class Git:
["remote"],
stdout=subprocess.PIPE,
).stdout.decode("utf-8"):
logger.warning("Not pushing to remote because no 'origin' configured")
logging.warning("Not pushing to remote because no 'origin' configured")
return
cmd = ["push"]
if force:
cmd.append("-f")
cmd.append("origin")
cmd.append("refs/heads/factory")
cmd.append("refs/heads/devel")
cmd += ["origin", "--all"]
self.git_run(cmd)

View File

@ -47,7 +47,7 @@ class GitExporter:
left_to_commit = []
for flat in reversed(flats):
found_state = False
for branch in ["factory", "devel"]:
for branch in ["factory"]:
if flat.commit.dbid == state_data.get(branch):
branch_state[branch] = flat.commit
flat.commit.git_commit = self.git.branch_head(branch)
@ -86,6 +86,11 @@ class GitExporter:
logging.debug(f"Committing {flat}")
self.commit_flat(flat, branch_state)
# make sure that we create devel branch
if not branch_state["devel"]:
logging.debug("force creating devel")
self.git.set_branch_head("devel", self.git.branch_head("factory"))
self.git.push(force=True)
def run_gc(self):
@ -150,6 +155,7 @@ class GitExporter:
# create file if not existant
self.git.add_default_lfs_gitattributes(force=False)
self.git.add_default_gitignore()
to_download, to_delete = flat.commit.calc_delta(branch_state[flat.branch])
for file in to_delete:

View File

@ -26,13 +26,16 @@ class Importer:
# Import multiple Factory packages into the database
self.packages = packages
self.project = project
self.scmsync_cache = dict()
self.packages_with_scmsync = set()
self.db = DB()
self.obs = OBS(api_url)
assert project == "openSUSE:Factory"
assert not self.has_scmsync(project)
self.refreshed_packages = set()
self.gone_packages_set = None
def import_request(self, number):
self.obs.request(number).import_into_db(self.db)
@ -213,6 +216,10 @@ class Importer:
return
logging.debug(f"Refresh {project}/{package}")
self.refreshed_packages.add(key)
if self.has_scmsync(project) or self.has_scmsync(key):
self.packages_with_scmsync.add(package)
logging.debug(f"{project}/{package} already in Git - skipping")
return
self.update_db_package(project, package)
self.fetch_all_linked_packages(project, package)
@ -255,3 +262,21 @@ class Importer:
for line in f.readlines():
self.gone_packages_set.add(line.strip())
return key in self.gone_packages_set
def has_scmsync(self, key):
if key in self.scmsync_cache:
return self.scmsync_cache[key]
root = self.obs._meta(key)
scmsync = None
scmsync_exists = False
if root and root.find('scmsync') is not None:
scmsync = root.find('scmsync').text
if scmsync:
scmsync_exists = scmsync.startswith('https://src.opensuse.org/pool/')
self.scmsync_cache[key] = scmsync_exists
return scmsync_exists
def package_with_scmsync(self, package):
return package in self.packages_with_scmsync

View File

@ -73,11 +73,11 @@ class OBS:
logging.debug(f"GET {url}")
return ET.parse(osc.core.http_GET(url)).getroot()
def _meta(self, project, package, **params):
def _meta(self, key, **params):
try:
root = self._xml(f"source/{project}/{package}/_meta", **params)
root = self._xml(f"source/{key}/_meta", **params)
except HTTPError:
logging.error(f"Package [{project}/{package} {params}] has no meta")
logging.error(f"Project/Package [{key} {params}] has no meta")
return None
return root
@ -118,13 +118,13 @@ class OBS:
return root
def exists(self, project, package):
root = self._meta(project, package)
root = self._meta(f"{project}/{package}")
if root is None:
return False
return root.get("project") == project
def devel_project(self, project, package):
root = self._meta(project, package)
root = self._meta(f"{project}/{package}")
devel = root.find("devel")
if devel is None:
return None

View File

@ -2,36 +2,60 @@
import json
from pathlib import Path
import pika
import sys
import random
import time
MY_TASKS_DIR = Path(__file__).parent / "tasks"
connection = pika.BlockingConnection(pika.URLParameters("amqps://opensuse:opensuse@rabbit.opensuse.org"))
channel = connection.channel()
channel.exchange_declare(exchange='pubsub', exchange_type='topic', passive=True, durable=True)
def listen_events():
connection = pika.BlockingConnection(
pika.URLParameters("amqps://opensuse:opensuse@rabbit.opensuse.org")
)
channel = connection.channel()
result = channel.queue_declare("", exclusive=True)
queue_name = result.method.queue
channel.exchange_declare(
exchange="pubsub", exchange_type="topic", passive=True, durable=False
)
channel.queue_bind(exchange='pubsub',
queue=queue_name,routing_key='#')
result = channel.queue_declare("", exclusive=True)
queue_name = result.method.queue
print(' [*] Waiting for logs. To exit press CTRL+C')
channel.queue_bind(
exchange="pubsub", queue=queue_name, routing_key="opensuse.obs.package.commit"
)
def callback(ch, method, properties, body):
if method.routing_key not in ("opensuse.obs.package.commit",):
return
body = json.loads(body)
if 'project' in body and 'package' in body and body['project'] == 'openSUSE:Factory':
if '/' in body['package']:
print(" [*] Waiting for logs. To exit press CTRL+C")
def callback(ch, method, properties, body):
if method.routing_key not in ("opensuse.obs.package.commit",):
return
body = json.loads(body)
if (
"project" in body
and "package" in body
and body["project"] == "openSUSE:Factory"
):
# Strip multibuild flavors
package = body["package"].partition(':')[0]
if "/" in package:
return
(MY_TASKS_DIR / body['package']).touch()
print(" [x] %r:%r" % (method.routing_key, body['package']))
(MY_TASKS_DIR / package).touch()
print(" [x] %r:%r" % (method.routing_key, body["package"]))
channel.basic_consume(queue_name,
callback,
auto_ack=True)
channel.basic_consume(queue_name, callback, auto_ack=True)
channel.start_consuming()
channel.start_consuming()
def main():
while True:
try:
listen_events()
except (pika.exceptions.ConnectionClosed, pika.exceptions.AMQPHeartbeatTimeout):
time.sleep(random.randint(10, 100))
if __name__ == "__main__":
main()

19
update-tasks.sh Executable file
View File

@ -0,0 +1,19 @@
#!/bin/bash
#
cd /space/dmueller/git-importer
source credentials.sh
while true; do
for i in $PWD/tasks/*; do
if test -f "$i"; then
echo "$(date): Importing $(basename $i)"
if ! python3 ./git-importer.py -c repos/.cache $(basename $i); then
mkdir -p $PWD/failed-tasks
mv -f $i $PWD/failed-tasks
fi
rm -f $i
fi
done
inotifywait -q -e create $PWD/tasks
done