Skip to content
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

NEW Create action #1

Merged
merged 2 commits into from
Jun 12, 2022
Merged
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
12 changes: 12 additions & 0 deletions .github/workflows/auto-tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Auto-tag
on:
push:
tags:
- '*.*.*'
jobs:
auto-tag:
name: Auto-tag
runs-on: ubuntu-latest
steps:
- name: Auto-tag
uses: silverstripe/gha-auto-tag@main
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
# 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. These will be created on the account/repo that called the actions.
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved

This action is intended as a step in a larger workflow and at a minimum the repository must have already been checked out and be on the branch that we want to target and there must be some changes to commit.

## Usage

GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
**workflow.yml**
```yml
steps:
- name: Create pull-request
uses: silverstripe/gha-pull-request@main
with:
branch: pulls/4/my-branch
title: NEW My pull-request title
description: (Optional) This text will appear in the body of the GitHub pull-request
```
78 changes: 78 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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: |
git check-ref-format "heads/$BRANCH" > /dev/null
if [[ $? != "0" ]]; then
echo "Invalid branch name"
exit 1
fi
if [[ $(git ls-remote --heads origin $BRANCH) != "" ]]; then
echo "Branch $BRANCH already exists"
exit 1
fi

- name: Branch commit and pull-request
shell: bash
env:
BRANCH: ${{ inputs.branch }}
TITLE: ${{ inputs.title }}
DESCRIPTION: ${{ inputs.description }}
GITHUB_REPOSITORY: ${{ github.repository }}
run: |
# Escape double quotes '"' => '\"'
TITLE=${TITLE//\"/\\\"}
DESCRIPTION=${DESCRIPTION//\"/\\\"}
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"
# The 41898282+ email prefixed is the required, matches the ID here
# https://api.github.com/users/github-actions%5Bbot%5D
# https://github.community/t/github-actions-bot-email-address/17204/6
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
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
RESP_CODE=$(curl -w %{http_code} -s -o /dev/null \
-X POST https://api.github.com/repos/$GITHUB_REPOSITORY/pulls \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${{ github.token }}" \
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
-d @- << EOF
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
{
"title": "$TITLE",
"body": "$DESCRIPTION",
"head": "$BRANCH",
"base": "$BASE_BRANCH"
}
EOF
)
if [[ $RESP_CODE == "201" ]]; then
echo "New pull-request created"
else
echo "Fail to create pull-request - HTTP response code was $RESP_CODE"
exit 1
fi