Skip to content

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.yml
index caae52bc0..cb564555f 100644
--- a/.github/workflows/ci_cd.yml
+++ b/.github/workflows/ci_cd.yml
@@ -80,6 +80,7 @@ jobs:
id: set-matrix
env:
CHANGES: ${{ needs.get-changes.outputs.changes }}
+ PYTHONPATH: ${{ github.workspace }}/automations/python
working-directory: automations/python/workflows
run: 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/python
working-directory: automations/python/workflows
run: python generate_report.py
diff --git a/.github/workflows/pr_label_check.yml b/.github/workflows/pr_label_check.yml
index 56abc77e0..7c72bfb0c 100644
--- a/.github/workflows/pr_label_check.yml
+++ b/.github/workflows/pr_label_check.yml
@@ -14,46 +14,74 @@ on:
- unlabeled
jobs:
+ # 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=aspect
check_aspect_label:
name: Check aspect label
runs-on: ubuntu-latest
+ needs:
+ - get_label_groups
steps:
- name: Check aspect label
uses: docker://agilepathway/pull-request-label-checker:v1.4.26
with:
- 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=goal
check_goal_label:
name: Check goal label
runs-on: ubuntu-latest
+ needs:
+ - get_label_groups
steps:
- name: Check goal label
uses: docker://agilepathway/pull-request-label-checker:v1.4.26
with:
- 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=priority
check_priority_label:
name: Check priority label
runs-on: ubuntu-latest
+ needs:
+ - get_label_groups
steps:
- name: Check priority label
uses: docker://agilepathway/pull-request-label-checker:v1.4.26
with:
- 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=stack
check_stack_label:
name: Check stack label
runs-on: ubuntu-latest
+ needs:
+ - get_label_groups
steps:
- name: Check stack label
uses: docker://agilepathway/pull-request-label-checker:v1.4.26
with:
- 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.py
new file mode 100644
index 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.py
new file mode 100644
index 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_labels
diff --git a/automations/python/sync_labels.py b/automations/python/sync_labels.py
index af8789be1..93c20ef30 100644
--- a/automations/python/sync_labels.py
+++ b/automations/python/sync_labels.py
@@ -1,38 +1,16 @@
import logging
-from copy import deepcopy
from github import Repository
from models.label import Label
-from models.label_group import LabelGroup
from shared.data import get_data
from shared.github import get_client
+from shared.labels import get_labels
from shared.log import configure_logger
log = 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.py
index 752c22492..b5cafef49 100644
--- a/automations/python/workflows/generate_report.py
+++ b/automations/python/workflows/generate_report.py
@@ -2,9 +2,10 @@
import json
import os
-import sys
from 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.py
new file mode 100644
index 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.py
index 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-ima
import json
import os
-import sys
+
+from shared.actions import write_to_github_output
changes = 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)