From d9bf58b6788303760a9c1c7a1e55307013da11a9 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Tue, 31 May 2022 13:44:32 +1200 Subject: [PATCH 1/2] NEW Create action --- README.md | 6 +++++- action.yml | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 action.yml diff --git a/README.md b/README.md index 3eb8799..21b7f91 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -# gha-pull-request \ No newline at end of file +# 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. diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..3a5b275 --- /dev/null +++ b/action.yml @@ -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" From a5e37e8d99f21c9c937dc246bc2b8402c8f9b539 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Mon, 13 Jun 2022 09:41:34 +1200 Subject: [PATCH 2/2] MNT Add auto-tag workflow --- .github/workflows/auto-tag.yml | 12 ++++++++++++ README.md | 17 +++++++++++++++-- action.yml | 29 ++++++++++++++++++++++------- 3 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/auto-tag.yml diff --git a/.github/workflows/auto-tag.yml b/.github/workflows/auto-tag.yml new file mode 100644 index 0000000..33446ca --- /dev/null +++ b/.github/workflows/auto-tag.yml @@ -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 diff --git a/README.md b/README.md index 21b7f91..a659e6e 100644 --- a/README.md +++ b/README.md @@ -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 +``` diff --git a/action.yml b/action.yml index 3a5b275..d450206 100644 --- a/action.yml +++ b/action.yml @@ -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 @@ -11,35 +12,43 @@ 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" + # 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" git checkout -b "$BRANCH" git add . @@ -48,8 +57,8 @@ runs: 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 @@ -60,4 +69,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