-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathweb_platform_features.py
More file actions
128 lines (104 loc) · 4.2 KB
/
web_platform_features.py
File metadata and controls
128 lines (104 loc) · 4.2 KB
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import urllib
from dataclasses import dataclass
from typing import Any, Iterable, Mapping, Optional
from bugbot import gcp
from bugbot.bzcleaner import BzCleaner
@dataclass
class FeatureUrls:
feature: str
spec_url: Optional[list[str]]
feature_url: str
sp_url: Optional[str]
def url_keys(urls: Iterable[str]) -> Mapping[tuple[str, str], str]:
rv = {}
for url in urls:
try:
parsed = urllib.parse.urlparse(url)
if parsed.hostname is None:
continue
rv[(parsed.hostname, parsed.path)] = url
except ValueError:
pass
return rv
class WebPlatformFeatures(BzCleaner):
def __init__(self) -> None:
super().__init__()
self.feature_bugs: Mapping[int, FeatureUrls] = {}
def description(self) -> str:
return "Update See Also for web-features bugs"
def filter_no_nag_keyword(self) -> bool:
return False
def has_default_products(self) -> bool:
return False
def columns(self) -> list[str]:
return ["id", "summary", "added"]
def handle_bug(
self, bug: dict[str, Any], data: dict[str, Any]
) -> Optional[dict[str, Any]]:
features_key = bug["id"]
bug_id = str(bug["id"])
changes = {}
if bug_id not in data:
data[bug_id] = {}
data[bug_id]["added"] = []
if features_key in self.feature_bugs:
existing_keys = url_keys(bug["see_also"] + [bug["url"]])
feature_urls = self.feature_bugs[features_key]
expected_urls = [feature_urls.feature_url]
if feature_urls.sp_url is not None:
expected_urls.append(feature_urls.sp_url)
if feature_urls.spec_url is not None:
expected_urls.extend(feature_urls.spec_url)
expected_keys = url_keys(expected_urls)
add_urls = [
url for key, url in expected_keys.items() if key not in existing_keys
]
if add_urls:
changes["see_also"] = {"add": add_urls}
data[bug_id]["added"] = add_urls
if changes:
self.autofix_changes[bug_id] = changes
return bug
return None
def get_bz_params(self, date) -> dict[str, str | int | list[str] | list[int]]:
fields = ["id", "url", "see_also"]
self.feature_bugs = self.get_feature_bugs()
return {"include_fields": fields, "id": list(self.feature_bugs.keys())}
def get_feature_bugs(self) -> Mapping[int, FeatureUrls]:
project = "moz-fx-dev-dschubert-wckb"
client = gcp.get_bigquery_client(project, ["cloud-platform", "drive"])
query = f"""
WITH feature_bugs as (
SELECT
bugs.number,
feature,
bugs.see_also,
web_features.spec as spec_url,
concat("https://web-platform-dx.github.io/web-features-explorer/features/", feature, "/") as feature_url,
concat("https://github.com/mozilla/standards-positions/issues/", sp_mozilla.issue) as sp_url
FROM `{project}.webcompat_knowledge_base.bugzilla_bugs` AS bugs
JOIN `{project}.web_features.features_latest` AS web_features
ON web_features.feature IN UNNEST(`{project}.webcompat_knowledge_base.EXTRACT_ARRAY`(bugs.user_story, "$.web-feature"))
LEFT JOIN `{project}.standards_positions.mozilla_standards_positions` AS sp_mozilla
ON `{project}.webcompat_knowledge_base.BUG_ID_FROM_BUGZILLA_URL`(sp_mozilla.bug) = bugs.number
)
SELECT number, feature, spec_url, feature_url, sp_url FROM feature_bugs
WHERE
NOT EXISTS(SELECT 1 FROM feature_bugs.spec_url WHERE spec_url NOT IN UNNEST(see_also))
OR feature_url NOT IN UNNEST(see_also)
OR sp_url NOT IN UNNEST(see_also)
"""
return {
row["number"]: FeatureUrls(
feature=row["feature"],
spec_url=row["spec_url"],
feature_url=row["feature_url"],
sp_url=row["sp_url"],
)
for row in client.query(query).result()
}
if __name__ == "__main__":
WebPlatformFeatures().run()