Skip to content

Commit

Permalink
test action
Browse files Browse the repository at this point in the history
  • Loading branch information
Noahnc committed Oct 27, 2023
1 parent 9f204c3 commit 99502a8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
15 changes: 15 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ inputs:
description: "Working directory to run the command in. Defaults to the root of the repository"
required: false
default: ${{ github.workspace }}
outputs:
target_branch:
description: "Name of the branch where changes will be pushed to"
value: ${{ inputs.target_branch_name }}
pr_number:
description: "Number of the pull request that was created"
value: ${{ steps.statistics.outputs.pr_number }}

runs:
using: composite
steps:
Expand Down Expand Up @@ -119,3 +127,10 @@ runs:
arguments+=("--working-directory" "${{ inputs.working_directory }}")
arguments+=("--default-registry-domain" "${{ inputs.default_registry_domain }}")
python -m "$module" "${arguments[@]}"
- name: Get statistics
id: statistics
shell: pwsh
run: |
$report = Get-Content "return_values.json" -Raw | ConvertFrom-Json
Write-Output "pr_number=$($report.pull_number)" >> $GITHUB_OUTPUT
22 changes: 17 additions & 5 deletions infrapatch/action/__main__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
import subprocess
from pathlib import Path
import logging as log
import click
from github import Auth, Github
from github.PullRequest import PullRequest

from infrapatch.core.composition import build_main_handler
from infrapatch.core.log_helper import catch_exception, setup_logging
Expand Down Expand Up @@ -52,6 +54,18 @@ def main(debug: bool, default_registry_domain: str, registry_secrets_string: str
main_handler.print_resource_table(upgradable_resources)
main_handler.dump_statistics(upgradable_resources, save_as_json_file=True)

push_changes(target_branch, working_directory)

pull = create_pr(github_token, head_branch, repository_name, target_branch)
return_values = {"pr_number": pull.number}
return_value_file = Path("return_values.json")
if return_value_file.exists():
return_value_file.unlink()
with open(return_value_file, "w") as f:
f.write(json.dumps(return_values))


def push_changes(target_branch, working_directory):
command = ["git", "push", "-f", "-u", "origin", target_branch]
log.debug(f"Executing command: {' '.join(command)}")
try:
Expand All @@ -62,19 +76,17 @@ def main(debug: bool, default_registry_domain: str, registry_secrets_string: str
log.error(f"Stdout: {result.stdout}")
raise Exception(f"Error pushing to remote: {result.stderr}")

create_pr(github_token, head_branch, repository_name, target_branch)


def create_pr(github_token, head_branch, repository_name, target_branch):
def create_pr(github_token, head_branch, repository_name, target_branch) -> PullRequest:
token = Auth.Token(github_token)
github = Github(auth=token)
repo = github.get_repo(repository_name)
pull = repo.get_pulls(state='open', sort='created', base=head_branch, head=target_branch)
if pull.totalCount != 0:
log.info(f"Pull request found from '{target_branch}' to '{head_branch}'")
return
return pull[0]
log.info(f"No pull request found from '{target_branch}' to '{head_branch}'. Creating a new one.")
pull = repo.create_pull(title=f"InfraPatch Module and Provider Update", body=f"InfraPatch Module and Provider Update", base=head_branch, head=target_branch)
return repo.create_pull(title=f"InfraPatch Module and Provider Update", body=f"InfraPatch Module and Provider Update", base=head_branch, head=target_branch)


def get_credentials_from_string(credentials_string: str) -> dict:
Expand Down

0 comments on commit 99502a8

Please sign in to comment.