|
| 1 | +name: 'Create Pull Request' |
| 2 | +description: 'Creates or updates a pull request for changes to the repository' |
| 3 | +inputs: |
| 4 | + token: |
| 5 | + description: 'GitHub token for authentication' |
| 6 | + required: true |
| 7 | + branch: |
| 8 | + description: 'The pull request branch name' |
| 9 | + required: true |
| 10 | + base: |
| 11 | + description: 'The base branch for the pull request' |
| 12 | + required: false |
| 13 | + default: 'main' |
| 14 | + title: |
| 15 | + description: 'The title of the pull request' |
| 16 | + required: true |
| 17 | + body: |
| 18 | + description: 'The body of the pull request' |
| 19 | + required: true |
| 20 | + labels: |
| 21 | + description: 'A newline-separated list of labels' |
| 22 | + required: false |
| 23 | + default: '' |
| 24 | + commit-message: |
| 25 | + description: 'The commit message to use when committing changes' |
| 26 | + required: false |
| 27 | + default: '[create-pull-request] automated change' |
| 28 | + branch-already-exists: |
| 29 | + description: 'Set to true if the branch is already pushed remotely (skips commit/push)' |
| 30 | + required: false |
| 31 | + default: 'false' |
| 32 | +outputs: |
| 33 | + pull-request-number: |
| 34 | + description: 'The pull request number' |
| 35 | + value: ${{ steps.create-pr.outputs.pull-request-number }} |
| 36 | + pull-request-url: |
| 37 | + description: 'The URL of the pull request' |
| 38 | + value: ${{ steps.create-pr.outputs.pull-request-url }} |
| 39 | + pull-request-operation: |
| 40 | + description: 'The pull request operation performed (created, updated, none)' |
| 41 | + value: ${{ steps.create-pr.outputs.pull-request-operation }} |
| 42 | +runs: |
| 43 | + using: 'composite' |
| 44 | + steps: |
| 45 | + - name: Configure git authentication |
| 46 | + shell: bash |
| 47 | + env: |
| 48 | + GH_TOKEN: ${{ inputs.token }} |
| 49 | + run: | |
| 50 | + # Use the provided token for both git and gh operations |
| 51 | + gh auth setup-git |
| 52 | +
|
| 53 | + - name: Commit and push changes |
| 54 | + if: inputs.branch-already-exists != 'true' |
| 55 | + id: commit-and-push |
| 56 | + shell: bash |
| 57 | + env: |
| 58 | + BRANCH: ${{ inputs.branch }} |
| 59 | + COMMIT_MESSAGE: ${{ inputs.commit-message }} |
| 60 | + run: | |
| 61 | + # Check for any changes (staged, unstaged, or untracked) |
| 62 | + if git diff --quiet && git diff --cached --quiet && [ -z "$(git ls-files --others --exclude-standard)" ]; then |
| 63 | + echo "No changes to commit" |
| 64 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 65 | + exit 0 |
| 66 | + fi |
| 67 | +
|
| 68 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 69 | +
|
| 70 | + git config user.name "github-actions[bot]" |
| 71 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 72 | +
|
| 73 | + # Create or reset branch |
| 74 | + git checkout -B "$BRANCH" |
| 75 | + git add -A |
| 76 | + git commit -m "$COMMIT_MESSAGE" |
| 77 | + git push -f origin "$BRANCH" |
| 78 | +
|
| 79 | + - name: Create or update pull request |
| 80 | + id: create-pr |
| 81 | + if: inputs.branch-already-exists == 'true' || steps.commit-and-push.outputs.has_changes == 'true' |
| 82 | + shell: bash |
| 83 | + env: |
| 84 | + GH_TOKEN: ${{ inputs.token }} |
| 85 | + BRANCH: ${{ inputs.branch }} |
| 86 | + BASE: ${{ inputs.base }} |
| 87 | + PR_TITLE: ${{ inputs.title }} |
| 88 | + PR_BODY: ${{ inputs.body }} |
| 89 | + LABELS: ${{ inputs.labels }} |
| 90 | + run: | |
| 91 | + # Check if a PR already exists for this branch |
| 92 | + EXISTING_PR=$(gh pr list --head "$BRANCH" --base "$BASE" --json number,url --jq '.[0] // empty') |
| 93 | +
|
| 94 | + if [ -n "$EXISTING_PR" ]; then |
| 95 | + PR_NUMBER=$(echo "$EXISTING_PR" | jq -r '.number') |
| 96 | + PR_URL=$(echo "$EXISTING_PR" | jq -r '.url') |
| 97 | + echo "Pull request #$PR_NUMBER already exists: $PR_URL" |
| 98 | + echo "pull-request-number=$PR_NUMBER" >> $GITHUB_OUTPUT |
| 99 | + echo "pull-request-url=$PR_URL" >> $GITHUB_OUTPUT |
| 100 | + echo "pull-request-operation=none" >> $GITHUB_OUTPUT |
| 101 | + else |
| 102 | + # Build label arguments as a bash array (avoids eval/injection) |
| 103 | + LABEL_ARGS=() |
| 104 | + if [ -n "$LABELS" ]; then |
| 105 | + while IFS= read -r label; do |
| 106 | + label=$(echo "$label" | xargs) # trim whitespace |
| 107 | + if [ -n "$label" ]; then |
| 108 | + LABEL_ARGS+=(--label "$label") |
| 109 | + fi |
| 110 | + done <<< "$LABELS" |
| 111 | + fi |
| 112 | +
|
| 113 | + # Write body to a temp file to avoid shell quoting issues with special characters |
| 114 | + BODY_FILE=$(mktemp) |
| 115 | + trap 'rm -f "$BODY_FILE"' EXIT |
| 116 | + printf '%s\n' "$PR_BODY" > "$BODY_FILE" |
| 117 | +
|
| 118 | + # Create the pull request without eval — all args are properly quoted |
| 119 | + PR_URL=$(gh pr create \ |
| 120 | + --title "$PR_TITLE" \ |
| 121 | + --body-file "$BODY_FILE" \ |
| 122 | + --base "$BASE" \ |
| 123 | + --head "$BRANCH" \ |
| 124 | + "${LABEL_ARGS[@]}") |
| 125 | +
|
| 126 | + rm -f "$BODY_FILE" |
| 127 | + trap - EXIT |
| 128 | +
|
| 129 | + PR_NUMBER=$(gh pr view "$BRANCH" --json number --jq '.number') |
| 130 | + echo "Created pull request #$PR_NUMBER: $PR_URL" |
| 131 | + echo "pull-request-number=$PR_NUMBER" >> $GITHUB_OUTPUT |
| 132 | + echo "pull-request-url=$PR_URL" >> $GITHUB_OUTPUT |
| 133 | + echo "pull-request-operation=created" >> $GITHUB_OUTPUT |
| 134 | + fi |
0 commit comments