|
2 | 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this file, |
3 | 3 | # You can obtain one at http://mozilla.org/MPL/2.0/. |
4 | 4 |
|
| 5 | +from datetime import datetime |
| 6 | +from typing import Any, Optional |
| 7 | + |
5 | 8 | from bugbot import gcp |
6 | 9 | from bugbot.bzcleaner import BzCleaner |
7 | 10 |
|
8 | 11 |
|
9 | 12 | class WebcompatPlatformWithoutKeyword(BzCleaner): |
10 | 13 | normal_changes_max = 200 |
11 | 14 |
|
12 | | - def description(self): |
| 15 | + def __init__(self): |
| 16 | + super().__init__() |
| 17 | + self.last_bugzilla_import_time: Optional[datetime] = None |
| 18 | + |
| 19 | + def description(self) -> str: |
13 | 20 | return "Web Compat platform bugs without webcompat:platform-bug keyword" |
14 | 21 |
|
15 | | - def filter_no_nag_keyword(self): |
| 22 | + def filter_no_nag_keyword(self) -> bool: |
16 | 23 | return False |
17 | 24 |
|
18 | | - def has_default_products(self): |
| 25 | + def has_default_products(self) -> bool: |
19 | 26 | return False |
20 | 27 |
|
21 | 28 | def get_autofix_change(self): |
22 | 29 | return { |
23 | 30 | "keywords": {"add": ["webcompat:platform-bug"]}, |
24 | 31 | } |
25 | 32 |
|
26 | | - def get_bz_params(self, date): |
27 | | - fields = ["id", "summary", "keywords"] |
28 | | - return {"include_fields": fields, "id": self.get_core_bug_ids()} |
| 33 | + def handle_bug( |
| 34 | + self, bug: dict[str, Any], data: dict[str, Any] |
| 35 | + ) -> Optional[dict[str, Any]]: |
| 36 | + bug_id = str(bug["id"]) |
| 37 | + |
| 38 | + # If the bug was updated later than our latest bugzilla data there could be a race, |
| 39 | + # so prefer to do nothing. |
| 40 | + if ( |
| 41 | + self.last_bugzilla_import_time |
| 42 | + and datetime.fromisoformat(bug["last_change_time"]) |
| 43 | + > self.last_bugzilla_import_time |
| 44 | + ): |
| 45 | + return None |
29 | 46 |
|
30 | | - def get_core_bug_ids(self): |
| 47 | + self.autofix_changes[bug_id] = self.get_autofix_change() |
| 48 | + return bug |
| 49 | + |
| 50 | + def get_bz_params(self, date) -> dict[str, Any]: |
| 51 | + fields = ["id", "summary", "keywords", "last_change_time"] |
| 52 | + return {"include_fields": fields, "id": self.get_bug_ids()} |
| 53 | + |
| 54 | + def get_bug_ids(self) -> list[int]: |
31 | 55 | project = "moz-fx-dev-dschubert-wckb" |
32 | 56 | dataset = "webcompat_knowledge_base" |
33 | 57 |
|
34 | 58 | client = gcp.get_bigquery_client(project, ["cloud-platform", "drive"]) |
| 59 | + |
| 60 | + last_run_at_rows = list( |
| 61 | + client.query( |
| 62 | + f""" |
| 63 | +SELECT run_at FROM `{project}.{dataset}.import_runs` WHERE is_history_fetch_completed ORDER BY run_at DESC LIMIT 1 |
| 64 | +""" |
| 65 | + ).result() |
| 66 | + ) |
| 67 | + if last_run_at_rows: |
| 68 | + self.last_bugzilla_import_time = last_run_at_rows[0]["run_at"] |
| 69 | + |
35 | 70 | query = f""" |
36 | | - SELECT core_bug |
37 | | - FROM `{project}.{dataset}.prioritized_kb_entries` as kb_entries |
38 | | - JOIN `moz-fx-dev-dschubert-wckb.webcompat_knowledge_base.bugzilla_bugs` as bugzilla_bugs ON bugzilla_bugs.number = kb_entries.core_bug |
39 | | - WHERE "webcompat:platform-bug" not in UNNEST(bugzilla_bugs.keywords) |
40 | | - LIMIT {self.normal_changes_max} |
41 | | - """ |
42 | | - |
43 | | - return list(row["core_bug"] for row in client.query(query).result()) |
| 71 | +WITH webcompat_removed AS ( |
| 72 | + SELECT number FROM `{project}.{dataset}.bugs_history` as bugs_history |
| 73 | + JOIN bugs_history.changes AS changes |
| 74 | + WHERE changes.field_name = "keywords" AND changes.removed = "webcompat:platform-bug" |
| 75 | +), |
| 76 | +
|
| 77 | +site_reports AS ( |
| 78 | + SELECT DISTINCT bugs.number AS number FROM `{project}.{dataset}.bugzilla_bugs` AS bugs |
| 79 | + JOIN `{project}.{dataset}.breakage_reports` as breakage_reports ON bugs.number = breakage_reports.breakage_bug |
| 80 | + JOIN `{project}.{dataset}.breakage_reports_core_bugs` as breakage_reports_core_bugs ON bugs.number = breakage_reports_core_bugs.breakage_bug |
| 81 | + LEFT JOIN webcompat_removed ON webcompat_removed.number = bugs.number |
| 82 | + JOIN `{project}.{dataset}.bugzilla_bugs` AS core_bugs ON breakage_reports_core_bugs.core_bug = core_bugs.number |
| 83 | + WHERE "webcompat:platform-bug" NOT IN UNNEST(bugs.keywords) AND "webcompat:needs-diagnosis" NOT IN UNNEST(bugs.keywords) AND bugs.resolution = "" AND core_bugs.resolution = "" AND webcompat_removed.number IS NULL |
| 84 | +), |
| 85 | +
|
| 86 | +core_bugs AS ( |
| 87 | + SELECT core_bug AS number |
| 88 | + FROM `{project}.{dataset}.prioritized_kb_entries` as kb_entries |
| 89 | + JOIN `{project}.{dataset}.bugzilla_bugs` as bugzilla_bugs ON bugzilla_bugs.number = kb_entries.core_bug |
| 90 | + WHERE "webcompat:platform-bug" not in UNNEST(bugzilla_bugs.keywords) |
| 91 | +) |
| 92 | +
|
| 93 | +SELECT number FROM ( |
| 94 | + SELECT number FROM site_reports |
| 95 | + UNION ALL |
| 96 | + SELECT number FROM core_bugs |
| 97 | +) |
| 98 | +LIMIT {self.normal_changes_max} |
| 99 | +""" |
| 100 | + |
| 101 | + return list(row["number"] for row in client.query(query).result()) |
44 | 102 |
|
45 | 103 |
|
46 | 104 | if __name__ == "__main__": |
|
0 commit comments