From b9a26b5ad1b9f7d528dd67959844f05b2491b18a Mon Sep 17 00:00:00 2001 From: Dave Date: Wed, 28 Feb 2024 16:22:57 +0000 Subject: [PATCH] Update awxdraft.py --- awxdraft.py | 124 +++++++++------------------------------------------- 1 file changed, 20 insertions(+), 104 deletions(-) diff --git a/awxdraft.py b/awxdraft.py index 0f3a967..ef0b654 100644 --- a/awxdraft.py +++ b/awxdraft.py @@ -1,99 +1,5 @@ import json import subprocess -# import os -# import sys -# import requests -# from pprint import pprint -# from graphql import oid - -# def update_pull_request_branch(owner, repo, pr_number, base_branch): -# # GitHub API endpoint for GraphQL -# graphql_endpoint = f"https://api.github.com/graphql" - -# # GraphQL query to get the current pull request information -# query = f""" -# query {{ -# repository(owner: "{owner}", name: "{repo}") {{ -# pullRequest(number: {pr_number}) {{ -# headRefName -# headRepository {{ -# id -# nameWithOwner -# }} -# }} -# }} -# }} -# """ - -# access_token = os.getenv("GH_TOKEN") - -# # Make the GraphQL request to get current branch information -# response = requests.post(graphql_endpoint, json={"query": query}, headers={"Authorization": f"Bearer {access_token}"}) -# response_data = response.json() - -# pprint(response_data) - -# # Extract relevant information from the response -# current_branch = response_data['data']['repository']['pullRequest']['headRefName'] -# head_repository_id = response_data['data']['repository']['pullRequest']['headRepository']['id'] -# head_repository_name = response_data['data']['repository']['pullRequest']['headRepository']['nameWithOwner'] - -# # GraphQL mutation to update the pull request branch - -# mutation = f""" -# mutation {{ -# updatePullRequestBranch( -# input: {{ -# pullRequestId: "{pr_number}", -# expectedHeadOid: {oid({head_repository_id})}, -# expectedBaseOid: "{base_branch}" -# }} -# ) {{ -# pullRequest {{ -# id -# headRefName -# }} -# }} -# }} -# """ - -# # Make the GraphQL request to update the pull request branch -# response = requests.post(graphql_endpoint, json={"query": mutation}, headers={"Authorization": f"Bearer {access_token}"}) -# response_data = response.json() - -# pprint(response_data) - -# # Extract information from the response -# updated_branch = response_data['data']['updatePullRequestBranch']['pullRequest']['headRefName'] - -# sys.exit(1) - -# print(f"Pull Request branch updated from '{current_branch}' to '{updated_branch}'.") - -# def needs_rebase(owner, repo, pr_number): -# # Run 'gh pr view' to get information about the pull request -# result = subprocess.run(["gh", "pr", "view", str(pr_number), "--json", "mergeStateStatus", "--repo", f"{owner}/{repo}"], capture_output=True, text=True) - -# if result.returncode != 0: -# print(f"Error fetching pull request information: {result.stderr}") -# return False - -# # Parse the JSON output -# pr_info = json.loads(result.stdout) - -# valid_states = ["BEHIND" , "MERGEABLE", "BLOCKED"] - -# # Check if the pull request needs rebasing -# return pr_info.get("mergeStateStatus", "DIRTY") in valid_states - -# def rebase_pull_request(owner, repo, pr_number): -# # Check if rebase is needed -# if needs_rebase(owner, repo, pr_number): -# # Run the 'gh pr rebase' command -# subprocess.run(["gh", "pr", "rebase", str(pr_number), "--repo", f"{owner}/{repo}"]) -# print("Rebase completed successfully.") -# else: -# print("No rebase needed.") def get_open_prs(owner, repo, label='blocked'): command = f'gh pr list -R https://github.com/{owner}/{repo}/ --search "draft:false" --json number,title,labels' @@ -132,12 +38,30 @@ def check_pr_status(owner, repo, pull_request_number): pr_url = f'https://github.com/{owner}/{repo}/pull/{pull_request_number}' command = f'gh pr checks {pr_url}' - result = subprocess.run(command, shell=True, capture_output=True) + result = subprocess.run(command, shell=True, capture_output=True, text=True) # Check the return code of the command if result.returncode != 0: print(f"Checks failed {result.returncode}.") - return False + + # Split the output into lines + lines = result.stdout.strip().split('\n') + + # Check if all lines begin with '✓' except for the specific line + begin = False + for line in lines: + if "NAME" in line: + begin=True + continue + if begin == False: + continue + if line.startswith('X') and "CI/api-schema" in line: + continue # Ignore the specific line + elif not line.startswith('✓'): + print(f"Not all lines start with '✓'.") + return False + + return True print("All checks have passed.") return True @@ -168,11 +92,3 @@ def update_pull_request_to_draft(owner, repo, pull_request_number): if check_status is not None and not check_status: update_pull_request_to_draft(owner, repo, pr['number']) - - # all_prs = get_all_prs(owner, repo) - - # if all_prs is not None: - # print(f"Checking all PR's for rebasing") - # for pr in all_prs: - # print(f"#{pr['number']} - {pr['title']}") - # update_pull_request_branch(owner, repo, pr['number'], "devel")