Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Determine labels for checks using labels.yml, factor out common workflow utils (#2046)
Madison Swain-Bowden95f268a561b8
diff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.ymlindex caae52bc0..cb564555f 100644--- a/.github/workflows/ci_cd.yml+++ b/.github/workflows/ci_cd.yml@@ -80,6 +80,7 @@ jobs:id: set-matrixenv:CHANGES: ${{ needs.get-changes.outputs.changes }}+ PYTHONPATH: ${{ github.workspace }}/automations/pythonworking-directory: automations/python/workflowsrun: python set_matrix_images.py@@ -940,6 +941,7 @@ jobs:SERVER_URL: ${{ github.server_url }}REPOSITORY: ${{ github.repository }}RUN_ID: ${{ github.run_id }}+ PYTHONPATH: ${{ github.workspace }}/automations/pythonworking-directory: automations/python/workflowsrun: python generate_report.pydiff --git a/.github/workflows/pr_label_check.yml b/.github/workflows/pr_label_check.ymlindex 56abc77e0..7c72bfb0c 100644--- a/.github/workflows/pr_label_check.yml+++ b/.github/workflows/pr_label_check.yml@@ -14,46 +14,74 @@ on:- unlabeledjobs:+ # Extract the current label list by group+ get_label_groups:+ name: Get label groups+ runs-on: ubuntu-latest+ outputs:+ aspect: ${{ steps.get_groups.outputs.aspect }}+ goal: ${{ steps.get_groups.outputs.goal }}+ priority: ${{ steps.get_groups.outputs.priority }}+ stack: ${{ steps.get_groups.outputs.stack }}+ steps:+ - name: Checkout repository+ uses: actions/checkout@v3++ - name: Get label groups+ id: get_groups+ env:+ PYTHONPATH: ${{ github.workspace }}/automations/python+ working-directory: automations/python/workflows+ run: python get_label_groups.py+# List of aspect labels: https://github.com/WordPress/openverse/labels?q=aspectcheck_aspect_label:name: Check aspect labelruns-on: ubuntu-latest+ needs:+ - get_label_groupssteps:- name: Check aspect labeluses: docker://agilepathway/pull-request-label-checker:v1.4.26with:- one_of: "π» aspect: code,π aspect: text,π€ aspect: dx,πΉ aspect: interface,βΏοΈ aspect: a11y"+ one_of: ${{ needs.get_label_groups.outputs.aspect }}repo_token: ${{ secrets.GITHUB_TOKEN }}# List of goal labels: https://github.com/WordPress/openverse/labels?q=goalcheck_goal_label:name: Check goal labelruns-on: ubuntu-latest+ needs:+ - get_label_groupssteps:- name: Check goal labeluses: docker://agilepathway/pull-request-label-checker:v1.4.26with:- one_of: "π goal: addition,π goal: fix,β¨ goal: improvement,π§° goal: internal improvement"+ one_of: ${{ needs.get_label_groups.outputs.goal }}repo_token: ${{ secrets.GITHUB_TOKEN }}# List of priority labels: https://github.com/WordPress/openverse/labels?q=prioritycheck_priority_label:name: Check priority labelruns-on: ubuntu-latest+ needs:+ - get_label_groupssteps:- name: Check priority labeluses: docker://agilepathway/pull-request-label-checker:v1.4.26with:- one_of: "π© priority: low,π¨ priority: medium,π§ priority: high,π₯ priority: critical"+ one_of: ${{ needs.get_label_groups.outputs.priority }}repo_token: ${{ secrets.GITHUB_TOKEN }}# List of stack labels: https://github.com/WordPress/openverse/labels?q=stackcheck_stack_label:name: Check stack labelruns-on: ubuntu-latest+ needs:+ - get_label_groupssteps:- name: Check stack labeluses: docker://agilepathway/pull-request-label-checker:v1.4.26with:- any_of: "π§± stack: backend,π§± stack: analytics,π§± stack: documentation,π§± stack: frontend,π§± stack: mgmt,π§± stack: api,π§± stack: catalog,π§± stack: ingestion server,π§± stack: infra"+ any_of: ${{ needs.get_label_groups.outputs.stack }}repo_token: ${{ secrets.GITHUB_TOKEN }}diff --git a/automations/python/shared/actions.py b/automations/python/shared/actions.pynew file mode 100644index 000000000..1deea276f--- /dev/null+++ b/automations/python/shared/actions.py@@ -0,0 +1,15 @@+import os+import sys+++def write_to_github_output(lines: list[str]) -> None:+ """+ Write a list of strings to both stdout and the GITHUB_OUTPUT environment variable.+ Useful for GitHub Actions, although lines must be formatted as `key=value`.+ """+ with open(+ os.environ.get("GITHUB_OUTPUT", "/dev/null"), "a", encoding="utf-8"+ ) as gh_out:+ for dest in [sys.stdout, gh_out]:+ for line in lines:+ print(line, file=dest)diff --git a/automations/python/shared/labels.py b/automations/python/shared/labels.pynew file mode 100644index 000000000..3f89b94a4--- /dev/null+++ b/automations/python/shared/labels.py@@ -0,0 +1,37 @@+from copy import deepcopy+from itertools import chain++from models.label import Label+from models.label_group import LabelGroup+from shared.data import get_data+++def get_labels_by_group(labels_file: dict = None) -> dict[str, LabelGroup]:+ """Get all the grouped labels as a mapping of group name to label group."""++ labels_file = labels_file or deepcopy(get_data("labels.yml"))+ grouped_labels = {}+ for group_info in labels_file["groups"]:+ labels = group_info.pop("labels", [])+ group = LabelGroup(**group_info)+ for label_info in labels:+ # Set up the labels+ Label(**label_info, group=group)+ grouped_labels[group.name] = group+ return grouped_labels+++def get_labels() -> list[Label]:+ """+ Get all the standard labels as a list.++ :return: a list of Label objects+ """+ labels_file = deepcopy(get_data("labels.yml"))+ grouped_labels = get_labels_by_group(labels_file)+ standard_labels = list(chain(group.labels for group in grouped_labels.values()))+ # Also grab standard labels+ for label_info in labels_file["standalone"]:+ label = Label(**label_info)+ standard_labels.append(label)+ return standard_labelsdiff --git a/automations/python/sync_labels.py b/automations/python/sync_labels.pyindex af8789be1..93c20ef30 100644--- a/automations/python/sync_labels.py+++ b/automations/python/sync_labels.py@@ -1,38 +1,16 @@import logging-from copy import deepcopyfrom github import Repositoryfrom models.label import Label-from models.label_group import LabelGroupfrom shared.data import get_datafrom shared.github import get_client+from shared.labels import get_labelsfrom shared.log import configure_loggerlog = logging.getLogger(__name__)-def get_labels() -> list[Label]:- """- Get all the standard labels as a list.-- :return: a list of Label objects- """-- labels_file = deepcopy(get_data("labels.yml"))- standard_labels = []- for group_info in labels_file["groups"]:- labels = group_info.pop("labels", [])- group = LabelGroup(**group_info)- for label_info in labels:- label = Label(**label_info, group=group)- standard_labels.append(label)- for label_info in labels_file["standalone"]:- label = Label(**label_info)- standard_labels.append(label)- return standard_labels--def set_labels(repo: Repository, labels: list[Label]):"""Set the given list of labels on the given repository.@@ -45,7 +23,7 @@ def set_labels(repo: Repository, labels: list[Label]):"""log.info(f"Fetching existing labels from {repo.full_name}")- existing_labels = {label.name.casefold(): label for label in repo.get_labels()}+ existing_labels = {label.name.casefold(): label for label in get_labels()}log.info(f"Found {len(existing_labels)} existing labels")for label in labels:diff --git a/automations/python/workflows/generate_report.py b/automations/python/workflows/generate_report.pyindex 752c22492..b5cafef49 100644--- a/automations/python/workflows/generate_report.py+++ b/automations/python/workflows/generate_report.py@@ -2,9 +2,10 @@import jsonimport os-import sysfrom collections import defaultdict+from shared.actions import write_to_github_output+server_url = os.environ.get("SERVER_URL")repository = os.environ.get("REPOSITORY")@@ -46,6 +47,5 @@ payload = {],}-with open(os.environ.get("GITHUB_OUTPUT"), "a", encoding="utf-8") as gh_out:- for dest in [sys.stdout, gh_out]:- print(f"payload={json.dumps(payload)}", file=dest)+lines = [f"payload={json.dumps(payload)}"]+write_to_github_output(lines)diff --git a/automations/python/workflows/get_label_groups.py b/automations/python/workflows/get_label_groups.pynew file mode 100644index 000000000..4d66b631e--- /dev/null+++ b/automations/python/workflows/get_label_groups.py@@ -0,0 +1,20 @@+from shared.actions import write_to_github_output+from shared.labels import get_labels_by_group+++def main() -> None:+ """+ Get all the grouped labels as a mapping of group name to label group.++ :return: a dict of LabelGroup objects+ """+ label_groups = get_labels_by_group()+ lines = []+ for name, group in label_groups.items():+ labels = ",".join(map(str, group.labels))+ lines.append(f"{name}={labels}")+ write_to_github_output(lines)+++if __name__ == "__main__":+ main()diff --git a/automations/python/workflows/set_matrix_images.py b/automations/python/workflows/set_matrix_images.pyindex f68ff0604..0bc32a98d 100644--- a/automations/python/workflows/set_matrix_images.py+++ b/automations/python/workflows/set_matrix_images.py@@ -6,7 +6,8 @@ https://docs.openverse.org/meta/ci_cd/jobs/docker_preparation.html#determine-imaimport jsonimport os-import sys++from shared.actions import write_to_github_outputchanges = json.loads(os.environ.get("CHANGES"))@@ -59,9 +60,10 @@ do_publish = "true" if len(publish_matrix["image"]) else "false"build_matrix = json.dumps(build_matrix, default=ser_set)publish_matrix = json.dumps(publish_matrix, default=ser_set)-with open(os.environ.get("GITHUB_OUTPUT", "/dev/null"), "a") as gh_out:- for dest in [sys.stdout, gh_out]:- print(f"do_build={do_build}", file=dest)- print(f"build_matrix={build_matrix}", file=dest)- print(f"do_publish={do_publish}", file=dest)- print(f"publish_matrix={publish_matrix}", file=dest)+lines = [+ f"do_build={do_build}",+ f"build_matrix={build_matrix}",+ f"do_publish={do_publish}",+ f"publish_matrix={publish_matrix}",+]+write_to_github_output(lines)