Files
pullrequest/action.yaml
T

98 lines
3.0 KiB
YAML

name: "Create Gitea Pull Request"
description: "Creates a pull request on a Gitea repository via the REST API"
inputs:
gitea_url:
description: "Base URL of the Gitea instance. Empty uses the current instance."
required: false
default: ""
repository:
description: "Target repository as owner/repo"
required: false
default: ""
token:
description: "Gitea token with pull-request write access"
required: true
title:
description: "Pull request title"
required: true
body:
description: "Pull request description"
required: false
default: ""
head:
description: "Source branch. Same-repo: branch name. Fork: owner:branch"
required: true
base:
description: "Target branch"
required: false
default: "main"
outputs:
number:
description: "Pull request number"
value: ${{ steps.create_pr.outputs.number }}
url:
description: "API URL of the pull request"
value: ${{ steps.create_pr.outputs.url }}
html_url:
description: "Web URL of the pull request"
value: ${{ steps.create_pr.outputs.html_url }}
runs:
using: "composite"
steps:
- id: create_pr
name: Create Pull Request
shell: bash
run: |
set -euo pipefail
GITEA_URL="${{ inputs.gitea_url }}"
GITEA_URL="${GITEA_URL:-${{ github.server_url }}}"
GITEA_URL="${GITEA_URL%/}"
REPOSITORY="${{ inputs.repository }}"
REPOSITORY="${REPOSITORY:-${{ github.repository }}}"
TOKEN="${{ inputs.token }}"
TITLE="${{ inputs.title }}"
BODY="${{ inputs.body }}"
HEAD="${{ inputs.head }}"
BASE="${{ inputs.base }}"
BASE="${BASE:-main}"
PAYLOAD="$(jq -n --arg title "$TITLE" --arg body "$BODY" --arg head "$HEAD" --arg base "$BASE" \
'{title: $title, body: $body, head: $head, base: $base}')"
RESPONSE=/tmp/gitea-pr.json
echo "::notice::Creating pull request ${REPOSITORY}: ${HEAD} -> ${BASE}"
STATUS="$(
curl -sS -o "$RESPONSE" -w "%{http_code}" \
-X POST \
-H "Authorization: token ${TOKEN}" \
-H "Content-Type: application/json" \
--data "$PAYLOAD" \
"${GITEA_URL}/api/v1/repos/${REPOSITORY}/pulls"
)"
if [ "$STATUS" != 201 ]; then
cat "$RESPONSE"
echo "----"
# Gitea returns 409 when a PR for this head/base is already open.
if jq -e '.message | test("already exists")' "$RESPONSE" >/dev/null; then
echo "::notice::Pull request already exists. Nothing to do."
exit 0
fi
echo "::error::create failed with HTTP ${STATUS}"
exit 1
fi
NUMBER="$(jq -r '.number' "$RESPONSE")"
echo "number=${NUMBER}" >> "$GITHUB_OUTPUT"
echo "url=$(jq -r '.url' "$RESPONSE")" >> "$GITHUB_OUTPUT"
echo "html_url=$(jq -r '.html_url' "$RESPONSE")" >> "$GITHUB_OUTPUT"
echo "::notice::Pull request #${NUMBER}: $(jq -r '.html_url' "$RESPONSE")"