gitbutlerapp/gitbutler · error

Failed to create merge request

Error message

Failed to create merge request: {status} - {error_text}

What it means

GitLab rejected the merge-request creation POST with a non-success HTTP status. The client embeds both the status code and the raw response body so the caller can see GitLab's exact validation or authorization message.

Solutions

  1. Read the `{status} - {error_text}` payload — GitLab states the exact validation failure.
  2. Ensure the source branch is pushed and differs from the target (has a diff).
  3. Check for an existing open MR between the same source and target branches and reuse it instead.
  4. Verify the access token is valid and has api scope with write access to the project.
  5. Confirm the project ID (numeric) and target branch names are correct.

Example fix

// before
let mr = gitlab.create_merge_request(project_id, &source, &target, &title).await?;

// after
if let Err(e) = gitlab.create_merge_request(project_id, &source, &target, &title).await {
    if e.to_string().contains("422") && e.to_string().contains("nothing to merge") {
        return Ok(None); // no diff — skip MR creation
    }
    return Err(e);
}
Defensive patterns

Strategy: try-catch

Validate before calling

// validate inputs before creating the MR
anyhow::ensure!(source_branch != target_branch, "source and target branches must differ");
anyhow::ensure!(!title.trim().is_empty(), "MR title required");
// optionally: confirm source branch is pushed and has a diff vs target

Try / catch

match gitlab.create_merge_request(project_id, &source, &target, &title).await {
    Ok(mr) => mr,
    Err(e) if e.to_string().contains("422") && e.to_string().contains("open merge request") => {
        find_existing_mr(project_id, &source, &target).await? // reuse instead
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Calling create_merge_request when POST /projects/{id}/merge_requests returns 4xx/5xx — 401 bad token, 403 insufficient permissions, 400/422 validation failures (missing source_branch/target_branch/title, branches with no diff, duplicate open MR), 404 wrong project id.

Common situations: Source and target branch identical or source has no commits beyond target (422 'nothing to merge'); an open MR already exists for the same branches; project ID vs path confusion; token scope too narrow; pushing to a fork but creating the MR against the wrong project.

Related errors


AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18). Data as JSON: /api/errors/9127b754c4f59464. Report an issue: GitHub.

Appendix: source

Thrown at crates/but-gitlab/src/client.rs:348

            None
        };

        let title = update_draft_state_in_title(params.title, params.draft);

        let body = CreateMergeRequestBody {
            title: &title,
            description: params.body,
            source_branch: params.source_branch,
            target_branch: params.target_branch,
            target_project_id,
        };

        let response = self.client.post(&url).json(&body).send().await?;

        if !response.status().is_success() {
            let status = response.status();
            let error_text = response.text().await.unwrap_or_default();
            bail!("Failed to create merge request: {status} - {error_text}");
        }

        let mr: GitLabMergeRequest = response.json().await?;
        Ok(mr.into())
    }

    pub async fn get_merge_request(
        &self,
        project_id: GitLabProjectId,
        mr_iid: i64,
    ) -> Result<MergeRequest> {
        let url = format!(
            "{}/projects/{}/merge_requests/{}",
            self.base_url, project_id, mr_iid
        );

        let response = self.client.get(&url).send().await?;

View on GitHub (pinned to 58e5313667)