Skip to content

Commit

Permalink
MNT Add auto-tag workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jun 9, 2022
1 parent d9bf58b commit 31d890f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
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
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# 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
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.

This will be created on the account/repo that called the actions, it will not create a pull-request in a forked repo.
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

**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
```
28 changes: 20 additions & 8 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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
Expand All @@ -11,45 +12,50 @@ inputs:
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
git check-ref-format "heads/$BRANCH" > /dev/null
if [[ $? != "0" ]]; then
echo "Invalid branch name"
exit 1
fi
if ! [[ -z $(git ls-remote --heads origin $BRANCH) ]]; then
if [[ $(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 }}
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"
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.email "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 \
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 }}" \
-d @- << EOF
Expand All @@ -60,4 +66,10 @@ runs:
"base": "$BASE_BRANCH"
}
EOF
echo "New pull-request created"
)
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

0 comments on commit 31d890f

Please sign in to comment.