From 9d594fe492dfa4e0200dc78a53bab58334a992c1 Mon Sep 17 00:00:00 2001 From: Marco Enrico Piras Date: Fri, 8 Mar 2024 10:22:17 +0100 Subject: [PATCH 1/3] refactor(gh-app): :recycle: determine the submitter through a dedicated function --- lifemonitor/integrations/github/services.py | 61 +++++++++++---------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/lifemonitor/integrations/github/services.py b/lifemonitor/integrations/github/services.py index 032cdb19..e53c2146 100644 --- a/lifemonitor/integrations/github/services.py +++ b/lifemonitor/integrations/github/services.py @@ -104,16 +104,46 @@ def __get_registries_map__(w: Workflow, registries: List[str]): def find_workflow_version(repository_reference: GithubRepositoryReference) -> Tuple[Workflow, WorkflowVersion]: - # found the existing workflow associated with repo + # get a reference to the github workflow registry for this installation github_registry: GithubWorkflowRegistry = repository_reference.event.installation.github_registry + # find the workflow workflow_version = None workflow = github_registry.find_workflow(repository_reference.repository.full_name) if workflow: + # get the workflow version (the one associated with the branch or tag of the repository) workflow_version = workflow.versions.get(repository_reference.branch or repository_reference.tag, None) logger.debug("Found workflow version: %r", workflow_version) return workflow, workflow_version +def identify_workflow_version_submitter(repository_reference: GithubRepositoryReference) -> User: + """ Identify the submitter of the workflow version. + The submitter is the user who trigger a github event on the repository. + If the "sender" of the event is not a LifeMonitor user, the submitter is the user who triggered the event + for the registration of the first workflow version (e.g., the user who registered the LifeMonitor Github app on the github repository). + """ + # search user identity + submitter = None + try: + identity: OAuthIdentity = repository_reference.event.sender + submitter = identity.user + logger.debug("Found a Github identity for the sender %r --> %r, %r", repository_reference.event.sender, identity, submitter) + except OAuthIdentityNotFoundException as e: + logger.warning("Github identity of the sender '%r' doesn't match with any LifeMonitor user identity", repository_reference.owner_id) + if logger.isEnabledFor(logging.DEBUG): + logger.exception(e) + + # fallback the submitter to the original submitter of the workflow + # (i.e., the user who registered the LifeMonitor Github app on the github repository) + if not submitter: + # get the workflow + workflow, _ = find_workflow_version(repository_reference) + if workflow: + submitter = workflow.earliest_version.submitter + + return submitter + + def register_repository_workflow(repository_reference: GithubRepositoryReference, registries: List[str] = None) -> WorkflowVersion: logger.debug("Repository ref: %r", repository_reference) # set a reference to LifeMonitorService @@ -139,15 +169,7 @@ def register_repository_workflow(repository_reference: GithubRepositoryReference workflow_version = repository_reference.branch or repository_reference.tag # search user identity - submitter = None - try: - identity: OAuthIdentity = repository_reference.event.sender - submitter = identity.user - logger.debug("Found a Github identity for the sender %r --> %r, %r", repository_reference.event.sender, identity, submitter) - except OAuthIdentityNotFoundException as e: - logger.warning("Github identity of the sender '%r' doesn't match with any LifeMonitor user identity", repository_reference.owner_id) - if logger.isEnabledFor(logging.DEBUG): - logger.exception(e) + submitter = identify_workflow_version_submitter(repository_reference) # set the repo link repo_link = f"{hosting_service.uri}/{repo.full_name}.git" @@ -158,11 +180,6 @@ def register_repository_workflow(repository_reference: GithubRepositoryReference if workflow: logger.debug("Found workflow associated with the repo: %r", workflow) - # fallback the submitter to the original submitter of the workflow - # (i.e., the user who registered the LifeMonitor Github app on the github repository) - if not submitter: - submitter = workflow.earliest_version.submitter - # look up the existing workflow version current_wv = wv = workflow.versions.get(workflow_version, None) @@ -239,14 +256,7 @@ def delete_repository_workflow_version(repository_reference: GithubRepositoryRef workflow_version = repository_reference.branch or repository_reference.tag # search user identity - submitter = None - try: - identity: OAuthIdentity = repository_reference.event.sender - submitter = identity.user - except OAuthIdentityNotFoundException as e: - logger.warning("Github identity of the sender '%r' doesn't match with any LifeMonitor user identity", repository_reference.owner_id) - if logger.isEnabledFor(logging.DEBUG): - logger.exception(e) + submitter = identify_workflow_version_submitter(repository_reference) # set the repo link repo_link = f"{hosting_service.uri}/{repo.full_name}.git" @@ -260,11 +270,6 @@ def delete_repository_workflow_version(repository_reference: GithubRepositoryRef logger.warning(f"No workflow associated with '{repo.full_name}' found") else: - # fallback the submitter to the original submitter of the workflow - # (i.e., the user who registered the LifeMonitor Github app on the github repository) - if not submitter: - submitter = w.earliest_version.submitter - # try to find the workflow version wv = lm.get_user_workflow_version(submitter, w.uuid, workflow_version) if not wv: From 8dbda57ec96f82b03fb9d6f892ad960218dc77b6 Mon Sep 17 00:00:00 2001 From: Marco Enrico Piras Date: Fri, 8 Mar 2024 10:24:19 +0100 Subject: [PATCH 2/3] fix(gh-app): :bug: proper identification of submitter on gh events --- .../integrations/github/controllers.py | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/lifemonitor/integrations/github/controllers.py b/lifemonitor/integrations/github/controllers.py index b80b00ff..62ed68de 100644 --- a/lifemonitor/integrations/github/controllers.py +++ b/lifemonitor/integrations/github/controllers.py @@ -423,11 +423,17 @@ def create(event: GithubEvent): logger.warning("Is Tag: %r", repo_info.tag) logger.warning("Is Branch: %r", repo_info.branch) + # workflow submitter + submitter = services.identify_workflow_version_submitter(event.repository_reference) + if not submitter: + logger.warning("Unable to identify the submitter of the workflow version") + return + if repo_info.branch: try: __check_for_issues_and_register__(repo_info, - event.sender.user.github_settings, - event.sender.user.registry_settings, + submitter.github_settings, + submitter.registry_settings, True) except Exception as e: logger.exception(e) @@ -457,10 +463,16 @@ def delete(event: GithubEvent): logger.warning("Is Tag: %s", repo_info.tag) logger.warning("Is Branch: %s", repo_info.branch) + # workflow submitter + submitter = services.identify_workflow_version_submitter(event.repository_reference) + if not submitter: + logger.warning("Unable to identify the submitter of the workflow version") + return + if repo_info.tag or repo_info.branch: try: workflow_version = services.delete_repository_workflow_version(repo_info, - registries=event.sender.user.registry_settings.registries) + registries=submitter.registry_settings.registries) if workflow_version: __notify_workflow_version_event__(repo_info, workflow_version, action='deleted') except Exception as e: @@ -490,12 +502,17 @@ def push(event: GithubEvent): logger.debug("Tree: %r", repo.trees_url) logger.debug("Commit: %r", repo.rev) + # workflow submitter + submitter = services.identify_workflow_version_submitter(event.repository_reference) + if not submitter: + logger.warning("Unable to identify the submitter of the workflow version") + return + if not repo_info.ref or repo_info.deleted: logger.debug("Repo ref not defined or branch/tag deleted: %r", repo) else: - settings: GithubUserSettings = event.sender.user.github_settings - __check_for_issues_and_register__(repo_info, settings, - event.sender.user.registry_settings, True) + __check_for_issues_and_register__(repo_info, submitter.github_settings, + submitter.registry_settings, True) return "No content", 204 except Exception as e: From d3af0544563cb2b8991cb169795502564b07657b Mon Sep 17 00:00:00 2001 From: Marco Enrico Piras Date: Wed, 13 Mar 2024 16:27:44 +0100 Subject: [PATCH 3/3] build(core): :bookmark: Bump version to 0.13.2 --- k8s/Chart.yaml | 2 +- lifemonitor/static/src/package.json | 2 +- specs/api.yaml | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/k8s/Chart.yaml b/k8s/Chart.yaml index 581566d9..b5ab493a 100644 --- a/k8s/Chart.yaml +++ b/k8s/Chart.yaml @@ -12,7 +12,7 @@ version: 0.12.0 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. Versions are not expected to # follow Semantic Versioning. They should reflect the version the application is using. -appVersion: 0.13.1 +appVersion: 0.13.2 # Chart dependencies dependencies: diff --git a/lifemonitor/static/src/package.json b/lifemonitor/static/src/package.json index 98affc22..19adbd67 100644 --- a/lifemonitor/static/src/package.json +++ b/lifemonitor/static/src/package.json @@ -1,7 +1,7 @@ { "name": "lifemonitor", "description": "Workflow Testing Service", - "version": "0.13.1", + "version": "0.13.2", "license": "MIT", "author": "CRS4", "main": "../dist/js/lifemonitor.min.js", diff --git a/specs/api.yaml b/specs/api.yaml index 52749572..b773b6b0 100644 --- a/specs/api.yaml +++ b/specs/api.yaml @@ -3,7 +3,7 @@ openapi: "3.0.0" info: - version: "0.13.1" + version: "0.13.2" title: "Life Monitor API" description: | *Workflow sustainability service* @@ -18,7 +18,7 @@ info: servers: - url: / description: > - Version 0.13.1 of API. + Version 0.13.2 of API. tags: - name: GitHub Integration