Skip to content

Commit

Permalink
feat(resrc-template): add GHA workflow for starting PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
allejo committed Mar 3, 2025
1 parent 7f6d813 commit 93852fc
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 10 deletions.
42 changes: 32 additions & 10 deletions .github/scripts/resource-template.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def parse_issue_body(body):

return result

#
# Utility functions for CI/CD operations
#

def is_ci():
return os.getenv('CI', 'false') == 'true'

Expand All @@ -81,6 +85,12 @@ def set_gha_output(name, value):
print(value, file=fh)
print(delimiter, file=fh)

def write_debug(message):
if os.getenv('RUNNER_DEBUG', '0') == '1':
print(f'::debug::{message}')
elif os.getenv('DEBUG', 'false') == 'true':
print(f'[DEBUG] {message}')

#
# Utility functions for I/O operations
#
Expand Down Expand Up @@ -120,18 +130,29 @@ def main():
else:
input_str = sys.stdin.read()

json_data = json.loads(input_str)
try:
json_data = json.loads(input_str)
except json.decoder.JSONDecodeError as e:
write_debug(f'Failed to parse input as JSON: {e}')

if is_ci():
set_gha_output('success', 'false')
set_gha_output('failure_reason', 'invalid_json')
else:
print('This script expects valid JSON as its input.')

return

issue = parse_issue_body(json_data['body'])
write_debug(f'Parsed issue body: {json.dumps(issue, indent=2)}')

# If the resource hasn't been approved yet, skip creation
if issue['approved'].strip().lower() != 'yes':
message = 'Resource has not been approved, skipping creation'
write_debug('Resource has not been approved, skipping creation')

if is_ci():
set_gha_output('success', 'false')
set_gha_output('message', message)
else:
print(message)
set_gha_output('failure_reason', 'not_approved')

return

Expand All @@ -140,13 +161,13 @@ def main():
required_issue_keys = ['approved', 'date_year', 'date_month', 'title', 'asset_url', 'category', 'tag']
if not all(key in issue for key in required_issue_keys):
missing_keys = [key for key in required_issue_keys if key not in issue]
message = f'Issue body is missing one or more of the following keys: {missing_keys}'

write_debug(f'Expected keys: {required_issue_keys}')
write_debug(f'Issue body is missing one or more of the following keys: {missing_keys}')

if is_ci():
set_gha_output('success', 'false')
set_gha_output('message', message)
else:
print(message)
set_gha_output('failure_reason', 'missing_keys')

return

Expand All @@ -168,7 +189,8 @@ def main():
if is_ci():
set_gha_output('success', 'true')
set_gha_output('resource_filename', filename)
set_gha_output('message', f'Created resource: {filename}')

write_debug(f'Creating resource: {filename}')
else:
print(f'Created resource: {filename}')
print('')
Expand Down
64 changes: 64 additions & 0 deletions .github/workflows/resource-template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Auto-Create PRs for Resource Requests
on:
issues:
types: [opened, transferred]
workflow_dispatch:
inputs:
issue_number:
description: 'Issue number'
required: true
default: '1'
type: number

permissions:
contents: write
pull-requests: write

jobs:
generate-resource:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
name: Set up Python
with:
python-version: '3.13'

- name: Configure State
id: workflow_state
run: |
echo "issue_numer=${{ github.event.issues.issue.number || github.event.inputs.issue_number }}" >> "$GITHUB_OUTPUT"
- name: Run the script
id: resource_gen
shell: bash
run: |
gh issue view ${{ steps.workflow_state.outputs.issue_number }} --json body | python .github/scripts/resource-template.py
- name: Check if Supported Template
if: steps.resource_gen.outputs.failure_reason == 'missing_keys'
run: |
echo "This issue does not have the expected keys, it's likely not a resource request."
- name: Warn about unapproved resource
if: steps.resource_gen.outputs.failure_reason == 'not_approved'
uses: peter-evans/create-or-update-comment@v4
with:
issue-number: ${{ steps.workflow_state.outputs.issue_number }}
body: |
Your resource request was marked as not approved, therefore I have
not created a PR for it. Once it's approved, we can rerun this
process.
- name: Create the Pull Request
uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
title: "Auto-PR for Resource Request #${{ steps.workflow_state.outputs.issue_number }}"
draft: true
body: |
This PR is created automatically from a resource request. Please review
the changes and merge when ready.
add-paths: "src/_resources/"
commit-message: "feat: start resource for req #${{ steps.workflow_state.outputs.issue_number }}"
branch: "chore/resource-${{ steps.workflow_state.outputs.issue_number }}"

0 comments on commit 93852fc

Please sign in to comment.