Skip to content

Commit a5e37e8

Browse files
committed
MNT Add auto-tag workflow
1 parent d9bf58b commit a5e37e8

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

.github/workflows/auto-tag.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Auto-tag
2+
on:
3+
push:
4+
tags:
5+
- '*.*.*'
6+
jobs:
7+
auto-tag:
8+
name: Auto-tag
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Auto-tag
12+
uses: silverstripe/gha-auto-tag@main

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# GitHub Actions - Pull request
22

3-
Create a new branch, makes a commit and a creates a pull-request using with a github-actions user as the author
3+
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.
44

5-
This will be created on the account/repo that called the actions, it will not create a pull-request in a forked repo.
5+
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.
6+
7+
## Usage
8+
9+
**workflow.yml**
10+
```yml
11+
steps:
12+
- name: Create pull-request
13+
uses: silverstripe/gha-pull-request@main
14+
with:
15+
branch: pulls/4/my-branch
16+
title: NEW My pull-request title
17+
description: (Optional) This text will appear in the body of the GitHub pull-request
18+
```

action.yml

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
name: Branch commit and pull-request
22
description: GitHub Action to create a branch, commit and a pull-request as the github-actions user
3+
34
inputs:
45
branch:
56
type: string
@@ -11,35 +12,43 @@ inputs:
1112
type: string
1213
required: false
1314
default: ''
15+
1416
runs:
1517
using: composite
1618
steps:
19+
1720
- name: Validate branch
1821
shell: bash
1922
env:
2023
BRANCH: ${{ inputs.branch }}
2124
run: |
22-
if [[ "$BRANCH" =~ [^a-zA-Z0-9\./_\-] ]] || [ -z "$BRANCH" ]; then
25+
git check-ref-format "heads/$BRANCH" > /dev/null
26+
if [[ $? != "0" ]]; then
2327
echo "Invalid branch name"
2428
exit 1
2529
fi
26-
if ! [[ -z $(git ls-remote --heads origin $BRANCH) ]]; then
30+
if [[ $(git ls-remote --heads origin $BRANCH) != "" ]]; then
2731
echo "Branch $BRANCH already exists"
2832
exit 1
2933
fi
3034
3135
- name: Branch commit and pull-request
3236
shell: bash
33-
# Add string inputs to memory instead of using string substitution in shell script
34-
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
3537
env:
3638
BRANCH: ${{ inputs.branch }}
3739
TITLE: ${{ inputs.title }}
3840
DESCRIPTION: ${{ inputs.description }}
41+
GITHUB_REPOSITORY: ${{ github.repository }}
3942
run: |
43+
# Escape double quotes '"' => '\"'
44+
TITLE=${TITLE//\"/\\\"}
45+
DESCRIPTION=${DESCRIPTION//\"/\\\"}
4046
BASE_BRANCH=$(git rev-parse --abbrev-ref HEAD)
4147
# Run git commit, push and create pull-request as 'github-actions' user
4248
git config --local user.name "github-actions"
49+
# The 41898282+ email prefixed is the required, matches the ID here
50+
# https://api.github.com/users/github-actions%5Bbot%5D
51+
# https://github.community/t/github-actions-bot-email-address/17204/6
4352
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
4453
git checkout -b "$BRANCH"
4554
git add .
@@ -48,8 +57,8 @@ runs:
4857
git push --set-upstream origin "$BRANCH"
4958
# Create new pull-request via GitHub API
5059
# https://docs.github.com/en/rest/reference/pulls#create-a-pull-request
51-
curl -s \
52-
-X POST https://api.github.com/repos/${{ github.repository }}/pulls \
60+
RESP_CODE=$(curl -w %{http_code} -s -o /dev/null \
61+
-X POST https://api.github.com/repos/$GITHUB_REPOSITORY/pulls \
5362
-H "Accept: application/vnd.github.v3+json" \
5463
-H "Authorization: token ${{ github.token }}" \
5564
-d @- << EOF
@@ -60,4 +69,10 @@ runs:
6069
"base": "$BASE_BRANCH"
6170
}
6271
EOF
63-
echo "New pull-request created"
72+
)
73+
if [[ $RESP_CODE == "201" ]]; then
74+
echo "New pull-request created"
75+
else
76+
echo "Fail to create pull-request - HTTP response code was $RESP_CODE"
77+
exit 1
78+
fi

0 commit comments

Comments
 (0)