From 33418f4e1898c38df4a7426ae13b40ed9efcc273 Mon Sep 17 00:00:00 2001 From: Adnan Khan Date: Mon, 25 Nov 2024 22:21:00 -0500 Subject: [PATCH] Fix inconsistency with environment enumeration (#63) * Query for environments if any of the repos in a batch is writeable, not just the first. * Formatting --- gatox/github/gql_queries.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/gatox/github/gql_queries.py b/gatox/github/gql_queries.py index ee78924..226e543 100644 --- a/gatox/github/gql_queries.py +++ b/gatox/github/gql_queries.py @@ -1,3 +1,6 @@ +from functools import reduce + + class GqlQueries: """Constructs graphql queries for use with the GitHub GraphQL api.""" @@ -220,21 +223,22 @@ def get_workflow_ymls(repos: list): for i in range(0, (len(repos) // 100) + 1): - top_len = len(repos) if len(repos) < (100 + i * 100) else (100 + i * 100) + top_len = len(repos) if len(repos) < 100 * (i + 1) else 100 * (i + 1) + # Use reduce to accumulate node_ids and can_push in a single iteration + node_ids, can_push = reduce( + lambda acc, repo: ( + acc[0] + [repo.repo_data["node_id"]], + acc[1] or repo.can_push(), + ), + repos[100 * i : top_len], + ([], False), + ) + query = { # We list envs if we have write access to one in the set (for secrets # reasons, otherwise we don't list them) - "query": ( - GqlQueries.GET_YMLS_ENV - if repos[i].can_push() - else GqlQueries.GET_YMLS - ), - "variables": { - "node_ids": [ - repo.repo_data["node_id"] - for repo in repos[0 + 100 * i : top_len] - ] - }, + "query": (GqlQueries.GET_YMLS_ENV if can_push else GqlQueries.GET_YMLS), + "variables": {"node_ids": node_ids}, } queries.append(query)