summaryrefslogtreecommitdiffstats
path: root/mga-advisor.py
blob: 0c3d07f368d918bc24984458db5ea2a30bbc9ae7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# This Python file uses the following encoding: utf-8
import os
from pathlib import Path
import sys
import yaml
import re
from subprocess import run
import requests
from textwrap import wrap
import time

from PySide6.QtWidgets import (
    QApplication,
    QWidget,
    QDialog,
    QFileDialog,
    QLineEdit,
    QPushButton,
    QVBoxLayout,
    QMessageBox,
    QHBoxLayout,
    QComboBox,
    QTextEdit,
    )
from PySide6.QtGui import QCursor
from PySide6.QtCore import QFile, QDate, QDir, Qt, QTimer, QSettings
from PySide6.QtUiTools import QUiLoader
BASE_URL = "https://bugs.mageia.org/rest/bug/"

class folded_str(str): pass

class literal_str(str): pass

class LineDialog(QDialog):
    def __init__(self, title, init="", parent=None):
        super().__init__(parent)
        self.setWindowTitle(title)
        self.name_ql = QLineEdit()
        self.name_ql.setText(init)
        apply_bt = QPushButton("Apply")
        layout = QHBoxLayout()
        layout.addWidget(self.name_ql)
        layout.addWidget(apply_bt)
        self.setLayout(layout)
        apply_bt.clicked.connect(self.apply)

    def apply(self):
        self.accept()

class SrcDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("Add source")
        self.name_ql = QLineEdit()
        self.release = QComboBox()
        self.release.addItems(["9", "8"])
        self.repo = QComboBox()
        self.repo.addItems(["core", "nonfree", "tainted"])
        apply_bt = QPushButton("Apply")
        layout = QHBoxLayout()
        layout.addWidget(self.release)
        layout.addWidget(self.repo)
        layout.addWidget(self.name_ql)
        layout.addWidget(apply_bt)
        self.setLayout(layout)
        apply_bt.clicked.connect(self.apply)

    def apply(self):
        self.accept()

class Widget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.load_ui()
        self.setWindowTitle("Mageia advisor")
        self.settings = QSettings("Mageia","advisor")

    def load_ui(self):
        loader = QUiLoader()
        path = Path(__file__).resolve().parent / "form.ui"
        ui_file = QFile(path)
        ui_file.open(QFile.ReadOnly)
        self.ui = loader.load(ui_file, self)
        ui_file.close()
        self.ui.retrieve_pb.clicked.connect(self.retrieve)
        self.ui.add_cve_pb.clicked.connect(self.add_cve)
        self.ui.remove_cve_pb.clicked.connect(self.remove_cve)
        self.ui.add_src_pb.clicked.connect(self.add_src)
        self.ui.remove_src_pb.clicked.connect(self.remove_src)
        self.ui.add_ref_pb.clicked.connect(self.add_reference)
        self.ui.remove_ref_pb.clicked.connect(self.remove_reference)
        self.ui.export_pb.clicked.connect(self.export)
        self.ui.configuration_pb.clicked.connect(self.select_path)
        self.ui.cancel_pb.clicked.connect(self.cancel)
        self.ui.preview_pb.clicked.connect(self.preview)
        self.ui.bug_le.editingFinished.connect(self.valid_number)
        self.ui.bug_le.returnPressed.connect(self.retrieve)

    def retrieve(self):
        """
        Retrieve CVEs, URLs and Source package name from the bug report given by its number
        """
        if not self.valid_number():
            return
        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
        self.clean()
        self.ui.status.setText("Loading...")
        self.ui.status.repaint()
        url = os.path.join(BASE_URL, self.ui.bug_le.text()) + "?include_fields=cf_rpmpkg,cf_cve,url,component"
        headers = {'Accept': 'application/json'}
        r = requests.get(url, headers=headers)
        if r.status_code == 200 and r.json()["faults"] == []:
            desc =""
            for pkg in  re.split(';|,| ', r.json()['bugs'][0]['cf_rpmpkg']):
                pkg = pkg.strip()
                if pkg == "":
                    continue
                analyze = re.search(r"([\w\-\+_]+)-\d", pkg)
                if analyze is not None:
                    pkg = analyze.group(1)
                sources = self.src_populate(pkg)
                for source in sources:
                    suffix = ".mga" + source["mga_release"]
                    if source["repo"] in ("tainted", "nonfree"):
                        suffix +=  "." + source["repo"]
                    self.ui.list_src.addItem(
                        " ".join((source["mga_release"],
                                  source["repo"],
                                  source["package"] +
                                  "-" + source["version"] +
                                  "-" + source["release"] +
                                  suffix,
                        )))
                desc += f" {pkg}"
            for cve in  re.split(';|,| ', r.json()['bugs'][0]['cf_cve']):
                cve = cve.strip()
                if cve != "":
                    self.ui.list_cve.addItem(cve)
            self.ui.list_ref.addItem(os.path.join(BASE_URL, self.ui.bug_le.text()) )
            for url in  re.split(';|,| ', r.json()['bugs'][0]['url']):
                url = url.strip()
                if url != "":
                    self.ui.list_ref.addItem(url)
            if "component" in r.json()['bugs'][0].keys():
                if r.json()['bugs'][0]["component"] == "Security":
                    self.ui.security_rb.setChecked(True)
                    self.ui.subject_le.setText(f"Updated{desc} packages fix security vulnerabilities")
                else:
                    self.ui.bugfix_rb.setChecked(True)
                    self.ui.subject_le.setText(f"Updated{desc} packages fix ")
            self.ui.status.setText("")
        else:
            self.ui.status.setText("No info retreived")
            QTimer.singleShot(5000, self.clean_status)
        QApplication.restoreOverrideCursor()

    def clean_status(self):
        self.ui.status.setText("")

    def clean(self):
        # Delete all fields except bug number
        self.ui.list_ref.clear()
        self.ui.list_src.clear()
        self.ui.subject_le.setText("")
        self.ui.list_cve.clear()
        self.ui.description_te.clear()
        self.ui.list_ref.repaint()
        self.ui.list_src.repaint()
        self.ui.subject_le.repaint()
        self.ui.list_cve.repaint()
        self.ui.description_te.repaint()

    def add_src(self):
        dl = SrcDialog()
        name = dl.exec()
        self.ui.list_src.addItem(
            " ".join((dl.release.currentText(),
                      dl.repo.currentText(),
                      self.sanitize_line(dl.name_ql.text())
                    ))
            )

    def remove_src(self):
        self.ui.list_src.takeItem(self.ui.list_src.currentRow())

    def add_reference(self):
        dl = LineDialog("Add reference")
        dl.exec()
        self.ui.list_ref.addItem(self.sanitize_line(dl.name_ql.text()))


    def remove_reference(self):
        self.ui.list_ref.takeItem(self.ui.list_ref.currentRow())

    def add_cve(self):
        init_value = f"CVE-{QDate.currentDate().year()}-"
        dl = LineDialog("Add CVE", init=init_value)
        dl.exec()
        self.ui.list_cve.addItem(self.sanitize_line(dl.name_ql.text()))

    def remove_cve(self):
        self.ui.list_cve.takeItem(self.ui.list_cve.currentRow())

    def adv_text(self):
        # https://stackoverflow.com/questions/74955147/how-to-add-literal-string-pattern-in-pyyaml-with-chomping-indicator
        def change_style(style, representer):
            def new_representer(dumper, data):
                scalar = representer(dumper, data)
                scalar.style = style
                return scalar
            return new_representer
        from yaml.representer import SafeRepresenter
        represent_folded_str = change_style('>', SafeRepresenter.represent_str)
        represent_literal_str = change_style('|', SafeRepresenter.represent_str)
        yaml.add_representer(folded_str, represent_folded_str)
        yaml.add_representer(literal_str, represent_literal_str)


        data = dict()
        if self.ui.bugfix_rb.isChecked():
            data['type'] = 'bugfix'
        if self.ui.security_rb.isChecked():
            data['type'] = 'security'
        if self.ui.subject_le.text() != "":
            data['subject'] = self.sanitize_line(self.ui.subject_le.text())
        cves = []
        if self.ui.list_cve.count() != 0:
            for n in range(0, self.ui.list_cve.count()):
                cves.append(self.ui.list_cve.item(n).text())
            data['CVE'] = cves
        srcs = {}
        if self.ui.list_src.count() != 0:
            for n in range(0, self.ui.list_src.count()):
                release, repo, name = self.ui.list_src.item(n).text().split(" ")
                rel = int(release)
                if not rel in srcs.keys():
                    srcs[rel] = {}
                    srcs[rel][repo] = []
                else:
                    if not repo in srcs[rel].keys():
                        srcs[rel][repo] = []
                srcs[rel][repo].append(self.sanitize_line(name))
            data['src'] = srcs
        if len(self.ui.description_te.toPlainText()) != 0:
            paragraphs = self.ui.description_te.toPlainText().split("\n")
            lines_desc = []
            for paragraph in paragraphs:
                 lines_desc += wrap(paragraph, width=72, break_on_hyphens=False)
            data['description'] = literal_str("\n".join(lines_desc)+"\n")
        refs = []
        if self.ui.list_ref.count():
            for n in range(0, self.ui.list_ref.count()):
                refs.append(self.ui.list_ref.item(n).text())
            data['references'] = refs
        return yaml.dump(data, default_flow_style=False, sort_keys=False, width=75, allow_unicode=True)

    def export(self):
        if not self.valid_number():
            return
        default_path = self.settings.value("Default/path", "/mageia-advisories/advisories")
        if QDir().mkpath(QDir().homePath() + default_path):
            if self.ui.bug_le.text() != "":
                filename = os.path.join(QDir().homePath()+ default_path, f"{self.ui.bug_le.text()}.adv")
                if os.path.exists(filename):
                    response = QMessageBox.question(self, 'File exists', f'The file {filename} already exists. Do you want to override it ?', QMessageBox.Yes | QMessageBox.No)
                    if response == QMessageBox.No:
                        return
                with open(filename, 'w') as f:
                    f.write(self.adv_text())
                QMessageBox.information(self, 'Success', f'The file {filename} has been written!')

    def select_path(self):
        filepath = QFileDialog.getExistingDirectory(self, "Select default directory for advisories repository", self.settings.value("Default/path"))
        if filepath:
            filepath = filepath[len(QDir().homePath()):]
            self.settings.setValue('Default/path', filepath)

    def cancel(self):
        self.close()

    def preview(self):
        dl = QDialog()
        dl.setWindowTitle("Advisory preview")
        te = QTextEdit()
        te.setPlainText(self.adv_text())
        te.setReadOnly(True)
        te.setMinimumSize(600, 0)
        ok_bt = QPushButton("OK")
        layout = QVBoxLayout()
        layout.addWidget(te)
        layout.addWidget(ok_bt)
        dl.setLayout(layout)
        ok_bt.clicked.connect(dl.close)
        dl.exec()

    def sanitize_line(self, line):
        if len(line) != 0:
            return line.splitlines()[0].strip()
        else:
            return ""

    def valid_number(self):
        if self.ui.bug_le.text() == "":
            mb = QMessageBox(QMessageBox.Warning, "Retrieve info", "Provide a bug number first", QMessageBox.Ok)
            mb.exec()
            return False
        self.ui.bug_le.setText(re.sub(r'\D', '', self.ui.bug_le.text()))
        return True

    def src_populate(self, package):
        # retrieve information with repo, release from package name
        cmd = ["mgarepo", "rpmlog"]
        sources = []
        for mga_release in range(9,10):
            source = {}
            repo = ""
            p = run(cmd + [f"{mga_release}/{package}"], capture_output=True, text=True)
            if p.returncode == 0:
                # example to parse : * Tue Aug 15 2023 squidf <squidf> 116.0.5845.96-1.mga9.tainted
                line1 = p.stdout.split('\n')[0]
                analyze = re.search(r"^\*.*\s(\w*[:\.\w]+)-([\.\d]+)", line1)
                if analyze is not None:
                    # get version, remove epoch
                    version = analyze.group(1).split(":")[-1]
                    release = analyze.group(2)[:-1]
                    source["mga_release"] = str(mga_release)
                    source['package'] = package
                    source["version"] = version
                    source["release"] = release
                first = True
                for line in p.stdout.split('\n'):
                    # Skip the first one which is badly formatted by mgarepo rpmlog
                    if first:
                        first = False
                    else:
                        analyze = re.search(r"^\*.*\s(\w*[:\.\d]+)-([\.\d]+).mga\d*(.*)", line)
                        if analyze is not None:
                            if  analyze.group(3):
                                source["repo"] = analyze.group(3)[1:]
                            else:
                                source["repo"] = "core"
                            sources.append(source)
                            break
        return sources


if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = Widget()
    widget.show()
    sys.exit(app.exec())