-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53502eb
commit d9bf58b
Showing
2 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1 +1,5 @@ | ||
# gha-pull-request | ||
# GitHub Actions - Pull request | ||
|
||
Create a new branch, makes a commit and a creates a pull-request using with a github-actions user as the author | ||
|
||
This will be created on the account/repo that called the actions, it will not create a pull-request in a forked repo. |
This file contains 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,63 @@ | ||
name: Branch commit and pull-request | ||
description: GitHub Action to create a branch, commit and a pull-request as the github-actions user | ||
inputs: | ||
branch: | ||
type: string | ||
required: true | ||
title: | ||
type: boolean | ||
required: true | ||
description: | ||
type: string | ||
required: false | ||
default: '' | ||
runs: | ||
using: composite | ||
steps: | ||
- name: Validate branch | ||
shell: bash | ||
env: | ||
BRANCH: ${{ inputs.branch }} | ||
run: | | ||
if [[ "$BRANCH" =~ [^a-zA-Z0-9\./_\-] ]] || [ -z "$BRANCH" ]; then | ||
echo "Invalid branch name" | ||
exit 1 | ||
fi | ||
if ! [[ -z $(git ls-remote --heads origin $BRANCH) ]]; then | ||
echo "Branch $BRANCH already exists" | ||
exit 1 | ||
fi | ||
- name: Branch commit and pull-request | ||
shell: bash | ||
# Add string inputs to memory instead of using string substitution in shell script | ||
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable | ||
env: | ||
BRANCH: ${{ inputs.branch }} | ||
TITLE: ${{ inputs.title }} | ||
DESCRIPTION: ${{ inputs.description }} | ||
run: | | ||
BASE_BRANCH=$(git rev-parse --abbrev-ref HEAD) | ||
# Run git commit, push and create pull-request as 'github-actions' user | ||
git config --local user.name "github-actions" | ||
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com" | ||
git checkout -b "$BRANCH" | ||
git add . | ||
git commit -m "$TITLE" | ||
git status | ||
git push --set-upstream origin "$BRANCH" | ||
# Create new pull-request via GitHub API | ||
# https://docs.github.com/en/rest/reference/pulls#create-a-pull-request | ||
curl -s \ | ||
-X POST https://api.github.com/repos/${{ github.repository }}/pulls \ | ||
-H "Accept: application/vnd.github.v3+json" \ | ||
-H "Authorization: token ${{ github.token }}" \ | ||
-d @- << EOF | ||
{ | ||
"title": "$TITLE", | ||
"body": "$DESCRIPTION", | ||
"head": "$BRANCH", | ||
"base": "$BASE_BRANCH" | ||
} | ||
EOF | ||
echo "New pull-request created" |