git-importer/lib/request.py
2022-10-26 14:15:42 +02:00

90 lines
2.7 KiB
Python

class Request:
def parse(self, xml):
self.number = int(xml.get("id"))
self.creator = xml.get("creator")
self.state = xml.find("state").get("name")
# not used in the database
self.target = xml.find("action/target").get("project")
self.type_ = xml.find("action").get("type")
if self.type_ == "delete":
self.source_package = None
self.source_project = None
self.source_rev = None
# not much to do
return self
self.source_project = xml.find("action/source").get("project")
self.source_package = xml.find("action/source").get("package")
# expanded MD5 or commit revision
self.source_rev = xml.find("action/source").get("rev")
# TODO: support muti-action requests
# TODO: parse review history
# TODO: add description
return self
def type(self):
return self.type_
def __str__(self):
return f"Req {self.number} {self.creator} {self.type_} {self.source_project} {self.state}"
def __repr__(self):
return f"[{self.__str__()}]"
def import_into_db(self, db):
with db.cursor() as cur:
cur.execute(
"""INSERT INTO requests (number, creator, type, state,
source_project, source_package, source_rev)
VALUES (%s,%s,%s,%s,%s,%s,%s) RETURNING id""",
(
self.number,
self.creator,
self.type_,
self.state,
self.source_project,
self.source_package,
self.source_rev,
),
)
rowid = cur.fetchone()[0]
cur.execute(
"UPDATE revisions SET request_id=%s WHERE request_number=%s",
(rowid, self.number),
)
@staticmethod
def find(db, request_id):
with db.cursor() as cur:
cur.execute("""SELECT * from requests WHERE id=%s""", (request_id,))
row = cur.fetchone()
ret = Request()
ret._from_db(row)
return ret
def _from_db(self, row):
(
self.dbid,
self.number,
self.creator,
self.type_,
self.state,
self.source_package,
self.source_project,
self.source_rev,
) = row
def as_dict(self):
return {
"number": self.number,
"creator": self.creator,
"type": self.type_,
"state": self.state,
"source_project": self.source_project,
"source_package": self.source_package,
"source_rev": self.source_rev,
}