|
| 1 | +name: Create a PR if one doesn't exists |
| 2 | +description: > |
| 3 | + Creates a commit with the current changes to the repo, and opens a PR for that commit. If |
| 4 | + any PR with the same title exists, then this action is marked as succeeded. |
| 5 | +inputs: |
| 6 | + commit-message: |
| 7 | + description: > |
| 8 | + The message for the commit to be created. |
| 9 | + required: true |
| 10 | + |
| 11 | + title: |
| 12 | + description: > |
| 13 | + The title of the PR. If empty, the title and body will be determined from the commit message. |
| 14 | + default: '' |
| 15 | + required: false |
| 16 | + |
| 17 | + body: |
| 18 | + description: > |
| 19 | + The body (description) of the PR. The `title` input must be specified in order for this input to be used. |
| 20 | + default: '' |
| 21 | + required: false |
| 22 | + |
| 23 | + head-branch: |
| 24 | + description: > |
| 25 | + The name of the branch to hold the new commit. If an existing open PR with the same head |
| 26 | + branch exists, the new branch will be force-pushed to that PR instead of creating a new PR. |
| 27 | + required: true |
| 28 | + |
| 29 | + base-branch: |
| 30 | + description: > |
| 31 | + The base branch to target with the new PR. |
| 32 | + required: true |
| 33 | + |
| 34 | + token: |
| 35 | + description: | |
| 36 | + The GitHub token to use. It must have enough privileges to |
| 37 | + make API calls to create and close pull requests. |
| 38 | + required: true |
| 39 | + |
| 40 | +runs: |
| 41 | + using: composite |
| 42 | + steps: |
| 43 | + - name: Update git config |
| 44 | + shell: bash |
| 45 | + run: | |
| 46 | + git config --global user.email "github-actions@github.com" |
| 47 | + git config --global user.name "github-actions[bot]" |
| 48 | + - name: Commit, Push and Open PR |
| 49 | + shell: bash |
| 50 | + env: |
| 51 | + COMMIT_MESSAGE: ${{ inputs.commit-message }} |
| 52 | + HEAD_BRANCH: ${{ inputs.head-branch }} |
| 53 | + BASE_BRANCH: ${{ inputs.base-branch }} |
| 54 | + GH_TOKEN: ${{ inputs.token }} |
| 55 | + TITLE: ${{ inputs.title }} |
| 56 | + BODY: ${{ inputs.body }} |
| 57 | + run: | |
| 58 | + set -exu |
| 59 | + if ! [[ $(git diff --stat) != '' ]]; then |
| 60 | + exit 0 # exit early |
| 61 | + fi |
| 62 | + # stage changes in the working tree |
| 63 | + git add . |
| 64 | + git commit -m "$COMMIT_MESSAGE" |
| 65 | + git checkout -b "$HEAD_BRANCH" |
| 66 | + # CAUTION: gits history changes with the following |
| 67 | + git push --force origin "$HEAD_BRANCH" |
| 68 | + PR_JSON=$(gh pr list --state open --json number --head "$HEAD_BRANCH") |
| 69 | + if [[ $? -ne 0 ]]; then |
| 70 | + echo "Failed to fetch existing PRs." |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + PR_NUMBERS=$(echo $PR_JSON | jq '. | length') |
| 74 | + if [[ $PR_NUMBERS -ne 0 ]]; then |
| 75 | + echo "Found existing open PR: $PR_NUMBERS" |
| 76 | + exit 0 |
| 77 | + fi |
| 78 | + gh pr create --head "$HEAD_BRANCH" --base "$BASE_BRANCH" --title "$TITLE" --body "$BODY" --assignee ${{ github.actor }} |
| 79 | + if [[ $? -ne 0 ]]; then |
| 80 | + echo "Failed to create new PR." |
| 81 | + exit 1 |
| 82 | + fi |
0 commit comments