Skip to content

Automerge stuck with multiple repositories creating PRs around the same time #34988

Closed
@posativ

Description

@posativ

Description

I have notices a weird but reproducible misbehavior with the automerge functionality. I have a couple of repositories that I need to orchestrate for a combined release process (aka poly repo) with a script:

  1. have main/dev branch + a simple workflow action,e.g. exit 0
  2. create a release branch with changes
  3. create PRs from that said release branch to main/dev
  4. immediately trigger auto merge
  5. repeat for up to 5 repositories

Repository settings: default, merge commit, no status checks or any branch protection (tested with many configurations).

With that workflow, the automerge feature gets stuck after the first repository almost always. To recover from this, I can simply add a success to that particular commit (via API). Alternatively, sleep 30 && exit 0 as workflow action resolves this issue too.

After diving into the code I suspect that there might an issue with the notification of commit statuses: the pull request might still check if it is mergeable and thus the automerge is not triggered.

This is also reproducible with a single repository and creating many branches + PRs + auto merge requests with a script. Roughly 50% of automergeable PRs get stuck with this:

Create a sample repository, e.g. I've cloned gitea/helm-gitea and used this repository with the script below.

env GITEA_HOST=git.example.com OWNER=My_User REPOSITORY=test-repo USERNAME=My_User PASSWORD=an_application_token ./reproduce-automerge-stuck.sh
#!/bin/bash
# filename: reproduce-automerge-stuck.sh

set -eu -o pipefail

GITEA_HOST="${GITEA_HOST:-demo.gitea.com}"

USERNAME="${USERNAME:-}"
PASSWORD="${PASSWORD:-}"

OWNER="${OWNER:-posativ}"
REPOSITORY="${REPOSITORY:-automerge-issue}"

if [[ -z "$USERNAME" || -z "$PASSWORD" ]]; then
  echo "Please set USERNAME and PASSWORD environment variables."
  exit 1
fi

automerge() {
  TEST_BRANCH="test-branch-$(date +%s)"
  TEMP_DIR=$(mktemp -d)
  trap 'rm -rf "$TEMP_DIR"' EXIT

  # clone + config
  git clone --depth 1 "https://${USERNAME}:${PASSWORD}@${GITEA_HOST}/${OWNER}/${REPOSITORY}" "$TEMP_DIR/repo"
  cd "$TEMP_DIR/repo"

  git config user.name "foo"
  git config user.email "bar@example.com"

  # Create a new branch and commit changes
  git checkout -b "$TEST_BRANCH"
  echo "$(date +%s)" > "$TEST_BRANCH.txt"
  git add "$TEST_BRANCH.txt"
  git commit -m "Add test file"

  sha=$(git rev-parse HEAD)

  git push origin "$TEST_BRANCH"

  # set commit status to pending
  curl -vX POST -H "Content-Type: application/json" \
    -u "$USERNAME:$PASSWORD" \
    -d '{"context": "foo", "description": "bar", "state": "pending", "target_url": "https://example.com"}' \
    "https://${GITEA_HOST}/api/v1/repos/${OWNER}/${REPOSITORY}/statuses/${sha}"

  # CI or something
  sleep 1

  # set commit status to success
  curl -vX POST -H "Content-Type: application/json" \
    -u "$USERNAME:$PASSWORD" \
    -d '{"context": "foo", "description": "bar", "state": "success", "target_url": "https://example.com"}' \
    "https://${GITEA_HOST}/api/v1/repos/${OWNER}/${REPOSITORY}/statuses/${sha}"

  # create pr
  index=$(curl -vX POST -H "Content-Type: application/json" \
    -u "$USERNAME:$PASSWORD" \
    -d "{\"head\": \"$TEST_BRANCH\", \"base\": \"main\", \"title\": \"Test PR\", \"body\": \"This is a test PR.\"}" \
    "https://${GITEA_HOST}/api/v1/repos/${OWNER}/${REPOSITORY}/pulls" | jq -r '.number')

  # create auto merge
  curl -vX POST -H "Content-Type: application/json" \
    -u "$USERNAME:$PASSWORD" \
    -d '{"do": "merge", "merge_when_checks_succeed": true}' \
    "https://${GITEA_HOST}/api/v1/repos/${OWNER}/${REPOSITORY}/pulls/${index}/merge"
}


automerge

Gitea Version

1.24

Can you reproduce the bug on the Gitea demo site?

No

Log Gist

No response

Screenshots

Image

Git Version

1.24.2

Operating System

No response

How are you running Gitea?

Official Gitea Helm Chart with Kubernetes + Valkey.

Database

PostgreSQL

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions