From 305cfa5631f01f66feb91ccc13f4dc60190f1a47 Mon Sep 17 00:00:00 2001 From: xEsiah Date: Mon, 16 Nov 2026 00:58:41 +0100 Subject: [PATCH] ci: simplify the automatic pull request workflow 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. --- .github/workflows/auto-create-pr.yml | 67 +++++++++++++++------------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/.github/workflows/auto-create-pr.yml b/.github/workflows/auto-create-pr.yml index 7c878e3..b23125c 100644 --- a/.github/workflows/auto-create-pr.yml +++ b/.github/workflows/auto-create-pr.yml @@ -1,47 +1,52 @@ -name: Auto Create PR +name: PR — Ouverture automatique on: push: - branches-ignore: - - main + branches-ignore: [main] + workflow_dispatch: jobs: create-pr: - # Same act_runner label as deploy.yml (config: labels: ["cohaise:host"]). - runs-on: cohaise + runs-on: [self-hosted, cohaise-cloud] permissions: - contents: read + contents: write pull-requests: write - steps: - - name: Create Pull Request + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Ouvrir (ou vérifier) la pull request env: - # Built-in job token: no secret or personal token to configure. - TOKEN: ${{ github.token }} - API_URL: ${{ github.api_url }} + GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }} + API: ${{ github.server_url }}/api/v1 REPO: ${{ github.repository }} BRANCH: ${{ github.ref_name }} - MESSAGE: ${{ github.event.head_commit.message }} run: | 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) - TITLE=${TITLE:-"Update $BRANCH"} - TITLE=$(printf '%s' "$TITLE" | sed 's/\\/\\\\/g; s/"/\\"/g') + open=$(curl -fsS -H "Authorization: token $GITEA_TOKEN" \ + "$API/repos/$REPO/pulls?state=open&limit=50" \ + | 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}' \ - -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 + python3 - "$BRANCH" "$title" "$body" <<'PY' | curl -fsS -X POST \ + -H "Authorization: token $GITEA_TOKEN" \ + -H "Content-Type: application/json" \ + --data-binary @- \ + "$API/repos/$REPO/pulls" + import json, sys + branch, title, body = sys.argv[1], sys.argv[2], sys.argv[3] + print(json.dumps({"base": "main", "head": branch, "title": title, "body": body})) + PY + echo "PR créée."