|
12 | 12 |
|
13 | 13 | from gftools.push.items import Axis, Designer, Family, FamilyMeta |
14 | 14 | from gftools.push.utils import google_path_to_repo_path, repo_path_to_google_path |
| 15 | +import json |
15 | 16 |
|
16 | 17 | log = logging.getLogger("gftools.push") |
17 | 18 |
|
@@ -107,6 +108,7 @@ def from_string(string: str): # type: ignore[misc] |
107 | 108 | projectV2(number: 74) { |
108 | 109 | id |
109 | 110 | title |
| 111 | + updatedAt |
110 | 112 | items(first: 100, after: "%s") { |
111 | 113 | totalCount |
112 | 114 | edges { |
@@ -434,56 +436,75 @@ def from_server_file( |
434 | 436 | return results |
435 | 437 |
|
436 | 438 | @classmethod |
437 | | - def from_traffic_jam(cls): |
| 439 | + def from_traffic_jam(cls, fp=None): |
438 | 440 | log.info("Getting push items from traffic jam board") |
439 | 441 | from gftools.gfgithub import GitHubClient |
440 | 442 |
|
441 | 443 | g = GitHubClient("google", "fonts") |
442 | 444 | last_item = "" |
443 | 445 | 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 |
461 | 459 | last_item = data["data"]["organization"]["projectV2"]["items"]["edges"][-1][ |
462 | 460 | "cursor" |
463 | 461 | ] |
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." |
471 | 496 | ) |
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) |
487 | 508 |
|
488 | 509 | results = cls() |
489 | 510 | for item in board_items: |
|
0 commit comments