Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions auto-pr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Auto PR for Allowed Branches

This composite GitHub Action checks if the current branch is part of an allowed list and creates a Pull Request (PR) automatically if it matches.

## Use cases

- Automating PR creation from branches triggered by FluxCD image automation.
- Enforcing allowed branch controls for automated merges.
- Simplifying the flow of image updates into main or staging branches.

## Inputs

| Input Name | Description | Required | Example |
| ------------------ | ------------------------------------------------------------ | -------- | ----------------------------- |
| `allowed-branches` | Space-separated list of allowed branch names | ✅ | `api-staging cronjob-staging` |
| `pr-base-branch` | The base branch to merge into | ✅ | `main` |
| `pr-title` | Title of the Pull Request | ✅ | `Automated PR: Update main` |
| `pr-body` | Body content of the Pull Request | ✅ | `This is an automated PR.` |
| `reviewers` | Comma-separated list of GitHub usernames for review requests | ✅ | `user1,user2` |

## Usage Example

```yaml
jobs:
auto-pr:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Auto PR for allowed branches
uses: hemilabs/actions/auto-pr@main
with:
allowed-branches: "token-prices-staging token-prices-cron-staging"
pr-base-branch: "main"
pr-title: "Merge $BRANCH_NAME into main"
pr-body: "*An automated PR* — contact DevOps if needed"
reviewers: "dhidalgX,gabmontes,gndelia"
```
58 changes: 58 additions & 0 deletions auto-pr/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Auto PR for Allowed Branches
description: Checks if current branch is allowed and creates PR if match.
inputs:
allowed-branches:
description: "Space separated list of allowed branch names"
required: true
pr-base-branch:
description: "The base branch to merge into"
required: true
pr-title:
description: "Title of the PR"
required: true
pr-body:
description: "Body of the PR"
required: true
reviewers:
description: "Comma-separated list of GitHub usernames to request review"
required: true
runs:
using: "composite"
steps:
- name: Check if branch is allowed and create PR
run: |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using ${{ }} syntax within run is dangerous -- as it is replaced before the script is executed, and allows script injection.

Instead, it is best to use environment variables to pass in the variables, e.g.

      env:
        ALLOWED_BRANCHES: "${{ inputs.allowed-branches }}"
      run: |
        echo "$ALLOWED_BRANCHES"

BRANCH_NAME="${GITHUB_REF#refs/heads/}"
echo "Detected branch: $BRANCH_NAME"

IFS=' ' read -r -a allowed <<< "${{ inputs.allowed-branches }}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is space-separated ideal here? I think comma-separated could be more consistent (e.g. with reviewers):

Suggested change
IFS=' ' read -r -a allowed <<< "${{ inputs.allowed-branches }}"
IFS=',' read -r -a allowed <<< "${{ inputs.allowed-branches }}"


is_allowed=false
for allowed_branch in "${allowed[@]}"; do
if [[ "$BRANCH_NAME" == "$allowed_branch" ]]; then
is_allowed=true
break
fi
done

if [[ "$is_allowed" == "true" ]]; then
echo "Branch $BRANCH_NAME is allowed. Creating PR..."

REVIEWERS_OPTION=""
if [[ -n "${{ inputs.reviewers }}" ]]; then
IFS=',' read -r -a reviewers <<< "${{ inputs.reviewers }}"
for reviewer in "${reviewers[@]}"; do
REVIEWERS_OPTION+=" --reviewer=$reviewer"
done
fi

gh pr create \
--head="$BRANCH_NAME" \
--base="${{ inputs.pr-base-branch }}" \
--title="${{ inputs.pr-title }}" \
--body="${{ inputs.pr-body }}" \
$REVIEWERS_OPTION

else
echo "Branch $BRANCH_NAME is not in the allowed list. Skipping PR creation."
fi
shell: bash