diff --git a/.github/workflows/auto-create-pr.yml b/.github/workflows/auto-create-pr.yml new file mode 100644 index 0000000..374e592 --- /dev/null +++ b/.github/workflows/auto-create-pr.yml @@ -0,0 +1,46 @@ +name: Auto Create PR + +on: + push: + branches-ignore: + - main + +jobs: + create-pr: + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write + + steps: + - name: Create Pull Request + env: + # Built-in job token: no secret or personal token to configure. + TOKEN: ${{ github.token }} + API_URL: ${{ github.api_url }} + REPO: ${{ github.repository }} + BRANCH: ${{ github.ref_name }} + MESSAGE: ${{ github.event.head_commit.message }} + run: | + set -eu + + TITLE=$(printf '%s' "$MESSAGE" | head -n 1) + TITLE=${TITLE:-"Update $BRANCH"} + TITLE=$(printf '%s' "$TITLE" | sed 's/\\/\\\\/g; s/"/\\"/g') + + STATUS=$(curl -sS -o /tmp/pr.json -w '%{http_code}' \ + -X POST \ + -H "Authorization: token ${TOKEN}" \ + -H 'Content-Type: application/json' \ + --data "{\"head\":\"${BRANCH}\",\"base\":\"main\",\"title\":\"${TITLE}\",\"body\":\"Automated PR from ${BRANCH} to main.\"}" \ + "${API_URL}/repos/${REPO}/pulls") + + case "$STATUS" in + 201) echo "Pull request created." ;; + 409) echo "Pull request already exists." ;; + 422) echo "Nothing to open a pull request for." ;; + *) + cat /tmp/pr.json + exit 1 + ;; + esac