From 6ad4f0931f914b54376f9f675d56652014a7c250 Mon Sep 17 00:00:00 2001 From: Agnes Garoux Date: Thu, 12 Sep 2024 14:44:22 +0100 Subject: [PATCH 1/5] workflow_dispatch event to trigger workflow in private repo --- .../generate_miro_image_suppressions_table.py | 45 ------------------- 1 file changed, 45 deletions(-) delete mode 100755 scripts/generate_miro_image_suppressions_table.py diff --git a/scripts/generate_miro_image_suppressions_table.py b/scripts/generate_miro_image_suppressions_table.py deleted file mode 100755 index 26058c17d2..0000000000 --- a/scripts/generate_miro_image_suppressions_table.py +++ /dev/null @@ -1,45 +0,0 @@ -import contextlib -import os -import subprocess -import tempfile - -from _common import git, get_secret_string - - -@contextlib.contextmanager -def working_directory(path): - """ - Changes the working directory to the given path, then returns to the - original directory when done. - """ - prev_cwd = os.getcwd() - os.chdir(path) - try: - yield - finally: - os.chdir(prev_cwd) - - -@contextlib.contextmanager -def cloned_repo(git_url): - """ - Clones the repository and changes the working directory to the cloned - repo. Cleans up the clone when it's done. - """ - with tempfile.TemporaryDirectory() as repo_dir: - git( - "clone", - git_url, - repo_dir, - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, - ) - - with working_directory(repo_dir): - yield - - -def get_github_api_key(sess): - return get_secret_string( - sess, secret_id="builds/github_wecobot/scala_libs_pr_bumps" - ) From 73e1fa5485f7b6be658f7fa878099c4c0e11a860 Mon Sep 17 00:00:00 2001 From: Agnes Garoux Date: Thu, 12 Sep 2024 14:50:23 +0100 Subject: [PATCH 2/5] we're going to need this --- scripts/miro_updates.py | 82 +++++------------------------------------ 1 file changed, 9 insertions(+), 73 deletions(-) diff --git a/scripts/miro_updates.py b/scripts/miro_updates.py index e6a4e8a180..c44064f552 100644 --- a/scripts/miro_updates.py +++ b/scripts/miro_updates.py @@ -9,6 +9,7 @@ import json import sys import re +import os import boto3 import httpx @@ -20,9 +21,7 @@ get_secret_string, get_session, get_date_from_index_name, - get_dynamodb_items, ) -from generate_miro_image_suppressions_table import cloned_repo, get_github_api_key SESSION = get_session(role_arn="arn:aws:iam::760097843905:role/platform-developer") DYNAMO_CLIENT = SESSION.resource("dynamodb").meta.client @@ -408,79 +407,16 @@ def get_all_miro_suppression_events(): def update_miro_image_suppressions_doc(): - print("*** Updating the list of Miro image suppressions...") - api_key = get_github_api_key(SESSION) - - client = httpx.Client( - auth=("weco-bot", api_key), - event_hooks={"response": [lambda r: r.raise_for_status()]}, + print("*** Creating a workflow_dispatch event for update_miro_suppressions_doc.yml in repo wellcomecollection/private ***") + + # create a workflow_dispatch event to trigger the update_miro_suppressions_doc.yml workflow + # the Github CLI is required + os.system( + f"gh workflow run update_miro_suppressions_doc.yml " + f"--repo wellcomecollection/private " + f"--field committer='{git('config', 'user.name')} <{git('config', 'user.email')}>'" ) - with cloned_repo("git@github.com:wellcomecollection/private.git"): - with open("miro-suppressions.md", "w") as outfile: - today = datetime.date.today() - - outfile.write("# Miro image suppressions\n\n") - outfile.write(f"Last updated {today.strftime('%-d %B %Y')}\n") - outfile.write( - "Note: this file is autogenerated by a script in the catalogue-pipeline repo; any manual edits will be overwritten.\n\n" - ) - - outfile.write("\n") - outfile.write("\n") - outfile.write(" \n") - outfile.write(" \n") - outfile.write(" \n") - outfile.write("\n") - - for (date, message), events in itertools.groupby( - sorted( - get_all_miro_suppression_events(), - key=lambda e: e["date"], - reverse=True, - ), - key=lambda e: (e["date"].date(), e["message"]), - ): - outfile.write("\n") - outfile.write(f' \n') - outfile.write(f' \n') - outfile.write(f" \n") - - outfile.write("
Miro IDSuppression dateReason
{"
".join(ev["id"] for ev in events)}
{date.strftime("%-d %B %Y")}{message}\n") - outfile.write("
\n") - - branch_name = f"miro-suppressions-{today.strftime('%Y-%m-%d')}" - - git("checkout", "-b", branch_name) - git("add", "miro-suppressions.md") - git("commit", "-m", "update miro-suppressions.md") - git("push", "origin", branch_name, "--force", "--quiet") - - r = client.post( - "https://api.github.com/repos/wellcomecollection/private/pulls", - headers={"Accept": "application/vnd.github+json"}, - json={ - "head": branch_name, - "base": "main", - "title": f"Update miro-suppressions.md for {today.strftime('%-d %B %Y')}", - "maintainer_can_modify": True, - "body": f"This is an auto-generated update of the Miro suppressions table.\n\nView rendered version: https://github.com/wellcomecollection/private/blob/{branch_name}/miro-suppressions.md", - }, - ) - - new_pr_number = r.json()["number"] - - r = client.post( - f"https://api.github.com/repos/wellcomecollection/private/pulls/{new_pr_number}/requested_reviewers", - headers={"Accept": "application/vnd.github.v3+json"}, - json={"team_reviewers": ["developers"]}, - ) - - print( - f"*** To approve these changes, visit https://github.com/wellcomecollection/private/pull/{new_pr_number}" - ) - - def register_on_dlcs(origin_url, miro_id): dlcs_response = dlcs_api_client().post( f"https://api.dlcs.io/customers/2/queue/priority", From 7cc40dc787ad3d5bb4ccd87ba2f4a1caadf66ad3 Mon Sep 17 00:00:00 2001 From: Agnes Garoux Date: Thu, 12 Sep 2024 14:57:11 +0100 Subject: [PATCH 3/5] fix linting issues --- scripts/miro_updates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/miro_updates.py b/scripts/miro_updates.py index c44064f552..0ac056d2a7 100644 --- a/scripts/miro_updates.py +++ b/scripts/miro_updates.py @@ -5,7 +5,6 @@ import datetime import functools -import itertools import json import sys import re @@ -21,6 +20,7 @@ get_secret_string, get_session, get_date_from_index_name, + get_dynamodb_items ) SESSION = get_session(role_arn="arn:aws:iam::760097843905:role/platform-developer") From e526cc994554cdb3e9960587462b4d779ce2008d Mon Sep 17 00:00:00 2001 From: Agnes Garoux Date: Thu, 12 Sep 2024 14:58:59 +0100 Subject: [PATCH 4/5] fix linting issues take 2 --- scripts/miro_updates.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/miro_updates.py b/scripts/miro_updates.py index 0ac056d2a7..edb8f4f56c 100644 --- a/scripts/miro_updates.py +++ b/scripts/miro_updates.py @@ -413,10 +413,11 @@ def update_miro_image_suppressions_doc(): # the Github CLI is required os.system( f"gh workflow run update_miro_suppressions_doc.yml " - f"--repo wellcomecollection/private " - f"--field committer='{git('config', 'user.name')} <{git('config', 'user.email')}>'" + f"--repo wellcomecollection/private " + f"--field committer='{git('config', 'user.name')} <{git('config', 'user.email')}>'" ) + def register_on_dlcs(origin_url, miro_id): dlcs_response = dlcs_api_client().post( f"https://api.dlcs.io/customers/2/queue/priority", From 628f2e84f4b8ea64317e28e5815065d3bf31cc0e Mon Sep 17 00:00:00 2001 From: Github on behalf of Wellcome Collection Date: Thu, 12 Sep 2024 14:01:38 +0000 Subject: [PATCH 5/5] Apply auto-formatting rules --- scripts/miro_updates.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/miro_updates.py b/scripts/miro_updates.py index edb8f4f56c..20c73763b0 100644 --- a/scripts/miro_updates.py +++ b/scripts/miro_updates.py @@ -20,7 +20,7 @@ get_secret_string, get_session, get_date_from_index_name, - get_dynamodb_items + get_dynamodb_items, ) SESSION = get_session(role_arn="arn:aws:iam::760097843905:role/platform-developer") @@ -407,7 +407,9 @@ def get_all_miro_suppression_events(): def update_miro_image_suppressions_doc(): - print("*** Creating a workflow_dispatch event for update_miro_suppressions_doc.yml in repo wellcomecollection/private ***") + print( + "*** Creating a workflow_dispatch event for update_miro_suppressions_doc.yml in repo wellcomecollection/private ***" + ) # create a workflow_dispatch event to trigger the update_miro_suppressions_doc.yml workflow # the Github CLI is required