Skip to content

Commit

Permalink
Update awxdraft.py
Browse files Browse the repository at this point in the history
  • Loading branch information
dmzoneill authored Feb 28, 2024
1 parent 188eac8 commit b9a26b5
Showing 1 changed file with 20 additions and 104 deletions.
124 changes: 20 additions & 104 deletions awxdraft.py
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")

0 comments on commit b9a26b5

Please sign in to comment.