Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Fix project automation logic around closed PRs (#396)
Madison Swain-Bowdenbda3e18cec72
diff --git a/.github/workflows/project_automation.yml b/.github/workflows/project_automation.ymlindex 8cca44135..6b88d628a 100644--- a/.github/workflows/project_automation.yml+++ b/.github/workflows/project_automation.yml@@ -30,13 +30,23 @@ jobs:python -m pip install --user pipenvpipenv install --deploy+ - name: Move stale issues from "In progress"+ working-directory: ./automations/python+ run: |+ pipenv run python issues_with_prs.py \+ --project-number 3 \+ --source-column "In progress" \+ --target-column "Backlog" \+ --linked-pr-state "closed"+- name: Move issues from "To do"working-directory: ./automations/pythonrun: |pipenv run python issues_with_prs.py \--project-number 3 \--source-column "To do" \- --target-column "In progress"+ --target-column "In progress" \+ --linked-pr-state "open"- name: Move issues from "Backlog"working-directory: ./automations/python@@ -44,4 +54,5 @@ jobs:pipenv run python issues_with_prs.py \--project-number 3 \--source-column "Backlog" \- --target-column "In progress"+ --target-column "In progress" \+ --linked-pr-state "open"diff --git a/automations/python/issues_with_prs.py b/automations/python/issues_with_prs.pyindex 0e5fbceae..49df53ad9 100755--- a/automations/python/issues_with_prs.py+++ b/automations/python/issues_with_prs.py@@ -3,9 +3,10 @@ import argparseimport loggingimport sys+import requestsfrom github import Github, GithubException, Issue, ProjectCard, ProjectColumnfrom shared.data import get_data-from shared.github import get_client+from shared.github import get_access_token, get_clientfrom shared.log import configure_loggerfrom shared.project import get_org_project, get_project_column@@ -40,46 +41,106 @@ parser.add_argument(default="In progress",help="column in which to move cards containing issues with PRs",)-+parser.add_argument(+ "--linked-pr-state",+ dest="linked_pr_state",+ metavar="linked-pr-state",+ type=str,+ default="open",+ help="filter issues by this state of their linked PRs",+)# endregion+def run_query(+ query,+) -> dict:+ """+ Run a GitHub GraphQL query.+ Taken from https://gist.github.com/gbaman/b3137e18c739e0cf98539bf4ec4366ad+ """+ log.debug(f"{query=}")+ request = requests.post(+ "https://api.github.com/graphql",+ json={"query": query},+ headers={"Authorization": f"Bearer {get_access_token()}"},+ )+ if request.status_code == 200:+ results = request.json()+ log.debug(f"{results=}")+ return request.json().get("data")+ else:+ raise Exception(+ "Query failed to run by returning code of {}. {}".format(+ request.status_code, query+ )+ )++def get_open_issues_with_prs(gh: Github,org_handle: str,repo_names: list[str],-) -> list[Issue]:+ linked_pr_state: str,+) -> set[tuple[str, str]]:"""Retrieve open issues with linked PRs.:param gh: the GitHub client:param org_handle: the name of the org in which to look for issues:param repo_names: the name of the repos in which to look for issues- :return: the list of open issues with linked PRs+ :param linked_pr_state: the state of the linked PRs to filter by+ :return: a set of tuples containing the repo name and issue number for each issue"""- all_issues = []+ all_issues = set()for repo_name in repo_names:- log.info(f"Looking for issues with PRs in {org_handle}/{repo_name}")- issues = gh.search_issues(- query="",- sort="updated",- order="desc",- **{- "repo": f"{org_handle}/{repo_name}",- "is": "issue",- "state": "open",- "linked": "pr",- },+ log.info(+ f"Looking for {linked_pr_state} PRs in {org_handle}/{repo_name} "+ f"with linked issues"+ )+ results = run_query(+ """+{+ repository(owner: "%s", name: "%s") {+ pullRequests(first: 100, states:%s,+ orderBy:{field:UPDATED_AT, direction:DESC}) {+ nodes {+ number+ title+ closingIssuesReferences (first: 50) {+ edges {+ node {+ number+ title+ state+ }+ }+ }+ }+ }+ }+}+ """+ % (org_handle, repo_name, linked_pr_state.upper()))- issues = list(issues)+ pulls = results["repository"]["pullRequests"]["nodes"]+ log.info(f"Found {len(pulls)} PRs in {org_handle}/{repo_name}")+ issues = {+ issue["node"]["number"]: issue["node"]["title"]+ for pull in pulls+ for issue in pull["closingIssuesReferences"]["edges"]+ }log.info(f"Found {len(issues)} issues")- for issue in issues:- log.info(f"• #{issue.number} | {issue.title}")- all_issues += issues+ for number, title in issues.items():+ log.info(f"• #{number: >5} | {title}")+ all_issues.add((repo_name, number))- log.info(f"Found a total of {len(all_issues)} open issues with linked PRs")+ log.info(+ f"Found a total of {len(all_issues)} open issues "+ f"with linked {linked_pr_state} PRs"+ )return all_issues@@ -114,6 +175,7 @@ def main():log.debug(f"Project number: {args.proj_number}")log.debug(f"Source column name: {args.source_col_name}")log.debug(f"Target column name: {args.target_col_name}")+ log.debug(f"Linked issue PR state: {args.linked_pr_state}")github_info = get_data("github.yml")org_handle = github_info["org"]@@ -128,6 +190,7 @@ def main():gh=gh,org_handle=org_handle,repo_names=repo_names,+ linked_pr_state=args.linked_pr_state,)if len(issues_with_prs) == 0:log.warning("Found no issues with PRs, stopping")@@ -143,8 +206,8 @@ def main():issue_cards = get_issue_cards(source_column)cards_to_move = []- for (issue_card, issue) in issue_cards:- if issue in issues_with_prs:+ for issue_card, issue in issue_cards:+ if (issue.repository.name, issue.number) in issues_with_prs:cards_to_move.append((issue_card, issue))log.info(f"Found {len(cards_to_move)} cards to move")diff --git a/automations/python/shared/github.py b/automations/python/shared/github.pyindex d6e37f614..6a243d5b7 100644--- a/automations/python/shared/github.py+++ b/automations/python/shared/github.py@@ -7,6 +7,19 @@ from github import Githublog = logging.getLogger(__name__)+def get_access_token() -> str:+ """+ Get the GitHub access token from the environment variables.++ :return: the GitHub access token+ """+ access_token: str | None = os.getenv("ACCESS_TOKEN")+ if access_token is None:+ log.error("Access token was not found in env.ACCESS_TOKEN.")+ raise ValueError("Access token not found")+ return access_token++def get_client(is_authenticated: bool = True) -> Github:"""Get a PyGithub client to access the GitHub API.@@ -20,10 +33,7 @@ def get_client(is_authenticated: bool = True) -> Github:log.info("Setting up GitHub client")if is_authenticated:- access_token: str | None = os.getenv("ACCESS_TOKEN")- if access_token is None:- log.error("Access token was not found in env.ACCESS_TOKEN.")- raise ValueError("Access token not found")+ access_token = get_access_token()else:log.debug("Generating a non-authenticated client")access_token = None