ci: simplify the automatic pull request workflow
PR — Ouverture automatique / create-pr (push) Canceled after 0s
PR — Ouverture automatique / create-pr (push) Canceled after 0s
Use a minimal Gitea API flow: checkout, reuse the latest commit message, skip when a PR already exists, and create the PR with python3 + curl.
This commit is contained in:
@@ -1,47 +1,52 @@
|
|||||||
name: Auto Create PR
|
name: PR — Ouverture automatique
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches-ignore:
|
branches-ignore: [main]
|
||||||
- main
|
workflow_dispatch:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
create-pr:
|
create-pr:
|
||||||
# Same act_runner label as deploy.yml (config: labels: ["cohaise:host"]).
|
runs-on: [self-hosted, cohaise-cloud]
|
||||||
runs-on: cohaise
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: write
|
||||||
pull-requests: write
|
pull-requests: write
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Create Pull Request
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Ouvrir (ou vérifier) la pull request
|
||||||
env:
|
env:
|
||||||
# Built-in job token: no secret or personal token to configure.
|
GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
TOKEN: ${{ github.token }}
|
API: ${{ github.server_url }}/api/v1
|
||||||
API_URL: ${{ github.api_url }}
|
|
||||||
REPO: ${{ github.repository }}
|
REPO: ${{ github.repository }}
|
||||||
BRANCH: ${{ github.ref_name }}
|
BRANCH: ${{ github.ref_name }}
|
||||||
MESSAGE: ${{ github.event.head_commit.message }}
|
|
||||||
run: |
|
run: |
|
||||||
set -eu
|
set -eu
|
||||||
|
if [ "$BRANCH" = "main" ]; then
|
||||||
|
echo "main est la base : aucune PR à ouvrir."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
title=$(git log -1 --pretty=%s)
|
||||||
|
body=$(git log -1 --pretty=%b)
|
||||||
|
[ -n "$body" ] || body="PR automatique de $BRANCH vers main."
|
||||||
|
|
||||||
TITLE=$(printf '%s' "$MESSAGE" | head -n 1)
|
open=$(curl -fsS -H "Authorization: token $GITEA_TOKEN" \
|
||||||
TITLE=${TITLE:-"Update $BRANCH"}
|
"$API/repos/$REPO/pulls?state=open&limit=50" \
|
||||||
TITLE=$(printf '%s' "$TITLE" | sed 's/\\/\\\\/g; s/"/\\"/g')
|
| python3 -c 'import json,os,sys; b=os.environ["BRANCH"]; print(sum(1 for p in json.load(sys.stdin) if p.get("head", {}).get("ref") == b))')
|
||||||
|
if [ "$open" -gt 0 ]; then
|
||||||
|
echo "Une PR est déjà ouverte pour $BRANCH."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
STATUS=$(curl -sS -o /tmp/pr.json -w '%{http_code}' \
|
python3 - "$BRANCH" "$title" "$body" <<'PY' | curl -fsS -X POST \
|
||||||
-X POST \
|
-H "Authorization: token $GITEA_TOKEN" \
|
||||||
-H "Authorization: token ${TOKEN}" \
|
-H "Content-Type: application/json" \
|
||||||
-H 'Content-Type: application/json' \
|
--data-binary @- \
|
||||||
--data "{\"head\":\"${BRANCH}\",\"base\":\"main\",\"title\":\"${TITLE}\",\"body\":\"Automated PR from ${BRANCH} to main.\"}" \
|
"$API/repos/$REPO/pulls"
|
||||||
"${API_URL}/repos/${REPO}/pulls")
|
import json, sys
|
||||||
|
branch, title, body = sys.argv[1], sys.argv[2], sys.argv[3]
|
||||||
case "$STATUS" in
|
print(json.dumps({"base": "main", "head": branch, "title": title, "body": body}))
|
||||||
201) echo "Pull request created." ;;
|
PY
|
||||||
409) echo "Pull request already exists." ;;
|
echo "PR créée."
|
||||||
422) echo "Nothing to open a pull request for." ;;
|
|
||||||
*)
|
|
||||||
cat /tmp/pr.json
|
|
||||||
exit 1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|||||||
Reference in New Issue
Block a user