-
Notifications
You must be signed in to change notification settings - Fork 1
Auto pr #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dhidalgX
wants to merge
3
commits into
main
Choose a base branch
from
auto-pr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Auto pr #23
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: | | ||||||
| BRANCH_NAME="${GITHUB_REF#refs/heads/}" | ||||||
| echo "Detected branch: $BRANCH_NAME" | ||||||
|
|
||||||
| IFS=' ' read -r -a allowed <<< "${{ inputs.allowed-branches }}" | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||
|
|
||||||
| 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 | ||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
${{ }}syntax withinrunis 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.