Skip to content

Commit d5fbcd9

Browse files
authored
Merge pull request #833 from googlefonts/pull-fix
manage-traffic-jam: improvements
2 parents edbb47f + 9d85f8c commit d5fbcd9

File tree

3 files changed

+81
-43
lines changed

3 files changed

+81
-43
lines changed

Lib/gftools/push/servers.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,15 @@ def update_all(self, last_checked: str):
145145
for family_data in families_data:
146146
family_name = family_data["family"]
147147
last_modified = family_data["lastModified"]
148-
if last_modified >= last_checked:
148+
149+
cached_family_version = self.family_versions.get(family_name)
150+
existing_family_version = self.families.get(family_name)
151+
# always ensure we repull family data if the family_version api
152+
# is different
153+
if cached_family_version and existing_family_version:
154+
if cached_family_version != existing_family_version.version:
155+
self.update(family_name)
156+
elif last_modified >= last_checked:
149157
self.update(family_name)
150158

151159
def update(self, family_name):
@@ -194,7 +202,11 @@ def compare_item(self, item: Items):
194202
def save(self, fp: "str | Path"):
195203
from copy import deepcopy
196204

197-
data = deepcopy(self).to_json()
205+
cp = deepcopy(self)
206+
# do not save family_versions data. We want to request this each time
207+
if hasattr(cp, "family_versions"):
208+
delattr(cp, "family_versions")
209+
data = cp.to_json()
198210
json.dump(data, open(fp, "w", encoding="utf8"), indent=4)
199211

200212
@classmethod
@@ -210,6 +222,10 @@ def from_dict(cls, data):
210222
server = getattr(inst, server_name)
211223

212224
for item_type, item_value in data[server_name].items():
225+
# if family_versions data is saved, skip it so we get requested
226+
# data instead
227+
if item_type in ["family_versions", "traffic_jam"]:
228+
continue
213229
if item_type == "families":
214230
server.families = {k: Family(**v) for k, v in item_value.items()}
215231
elif item_type == "designers":

Lib/gftools/push/trafficjam.py

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from gftools.push.items import Axis, Designer, Family, FamilyMeta
1414
from gftools.push.utils import google_path_to_repo_path, repo_path_to_google_path
15+
import json
1516

1617
log = logging.getLogger("gftools.push")
1718

@@ -107,6 +108,7 @@ def from_string(string: str): # type: ignore[misc]
107108
projectV2(number: 74) {
108109
id
109110
title
111+
updatedAt
110112
items(first: 100, after: "%s") {
111113
totalCount
112114
edges {
@@ -434,56 +436,75 @@ def from_server_file(
434436
return results
435437

436438
@classmethod
437-
def from_traffic_jam(cls):
439+
def from_traffic_jam(cls, fp=None):
438440
log.info("Getting push items from traffic jam board")
439441
from gftools.gfgithub import GitHubClient
440442

441443
g = GitHubClient("google", "fonts")
442444
last_item = ""
443445
data = g._run_graphql(GOOGLE_FONTS_TRAFFIC_JAM_QUERY % last_item, {})
444-
board_items = data["data"]["organization"]["projectV2"]["items"]["nodes"]
445-
446-
# paginate through items in board
447-
last_item = data["data"]["organization"]["projectV2"]["items"]["edges"][-1][
448-
"cursor"
449-
]
450-
item_count = data["data"]["organization"]["projectV2"]["items"]["totalCount"]
451-
while len(board_items) < item_count:
452-
data = None
453-
while not data:
454-
try:
455-
data = g._run_graphql(
456-
GOOGLE_FONTS_TRAFFIC_JAM_QUERY % last_item, {}
457-
)
458-
except:
459-
data = None
460-
board_items += data["data"]["organization"]["projectV2"]["items"]["nodes"]
446+
board_items = None
447+
# use cached items if board hasn't been updated
448+
if fp and fp.exists():
449+
existing = json.load(open(fp, encoding="utf8"))
450+
last_update = existing["updatedAt"]
451+
current_update = data["data"]["organization"]["projectV2"]["updatedAt"]
452+
if last_update == current_update:
453+
board_items = existing["board_items"]
454+
455+
if not board_items:
456+
board_items = data["data"]["organization"]["projectV2"]["items"]["nodes"]
457+
458+
# paginate through items in board
461459
last_item = data["data"]["organization"]["projectV2"]["items"]["edges"][-1][
462460
"cursor"
463461
]
464-
log.info(f"Getting items up to {last_item}")
465-
for item in board_items:
466-
if item["type"] != "PULL_REQUEST":
467-
raise ValueError(
468-
"Traffic Jam contains issues! All items must be pull requests. "
469-
"Please remove the issues and rerun the tool, "
470-
"https://github.com/orgs/google/projects/74/views/1"
462+
item_count = data["data"]["organization"]["projectV2"]["items"]["totalCount"]
463+
while len(board_items) < item_count:
464+
data = None
465+
while not data:
466+
try:
467+
data = g._run_graphql(
468+
GOOGLE_FONTS_TRAFFIC_JAM_QUERY % last_item, {}
469+
)
470+
except:
471+
data = None
472+
board_items += data["data"]["organization"]["projectV2"]["items"]["nodes"]
473+
last_item = data["data"]["organization"]["projectV2"]["items"]["edges"][-1][
474+
"cursor"
475+
]
476+
log.info(f"Getting items up to {last_item}")
477+
for item in board_items:
478+
if item["type"] != "PULL_REQUEST":
479+
raise ValueError(
480+
"Traffic Jam contains issues! All items must be pull requests. "
481+
"Please remove the issues and rerun the tool, "
482+
"https://github.com/orgs/google/projects/74/views/1"
483+
)
484+
# sort items by pr number
485+
board_items.sort(key=lambda k: k["content"]["url"])
486+
487+
# get files for prs which have more than 100 changed files
488+
for item in board_items:
489+
changed_files = item["content"]["files"]["totalCount"]
490+
if changed_files <= 100:
491+
continue
492+
pr_number = item["content"]["number"]
493+
pr_url = item["content"]["url"]
494+
log.warn(
495+
f"{pr_url} has {changed_files} changed files. Attempting to fetch them."
471496
)
472-
# sort items by pr number
473-
board_items.sort(key=lambda k: k["content"]["url"])
474-
475-
# get files for prs which have more than 100 changed files
476-
for item in board_items:
477-
changed_files = item["content"]["files"]["totalCount"]
478-
if changed_files <= 100:
479-
continue
480-
pr_number = item["content"]["number"]
481-
pr_url = item["content"]["url"]
482-
log.warn(
483-
f"{pr_url} has {changed_files} changed files. Attempting to fetch them."
484-
)
485-
files = g.pr_files(pr_number)
486-
item["content"]["files"]["nodes"] = [{"path": f["filename"]} for f in files]
497+
files = g.pr_files(pr_number)
498+
item["content"]["files"]["nodes"] = [{"path": f["filename"]} for f in files]
499+
500+
# save
501+
if fp:
502+
dat = {
503+
"updatedAt": data["data"]["organization"]["projectV2"]["updatedAt"],
504+
"board_items": board_items
505+
}
506+
with open(fp, "w", encoding="utf-8") as doc:
507+
json.dump(dat, doc, indent=4)
487508

488509
results = cls()
489510
for item in board_items:

Lib/gftools/scripts/manage_traffic_jam.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ def main(args=None):
274274

275275
os.chdir(args.fonts_repo)
276276

277-
push_items = PushItems.from_traffic_jam()
277+
traffic_jam_data = (Path("~") / ".gf_traffic_jam_data.json").expanduser()
278+
push_items = PushItems.from_traffic_jam(traffic_jam_data)
278279
if not args.show_open_prs:
279280
push_items = PushItems(i for i in push_items if i.merged == True)
280281
if "lists" in args.filter:

0 commit comments

Comments
 (0)