{"record":{"id":"0258bf598ea61551","repo":"gitbutlerapp/gitbutler","slug":"failed-to-add-to-github-stack","errorCode":null,"errorMessage":"Failed to add to GitHub stack: {}","messagePattern":"Failed to add to GitHub stack: (.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-github/src/stacks.rs","lineNumber":335,"sourceCode":"\n    async fn add_to_stack(\n        &self,\n        owner: &str,\n        repo: &str,\n        stack_number: i64,\n        pull_requests: &[i64],\n    ) -> Result<()> {\n        let response = self\n            .client\n            .post(format!(\n                \"{}/repos/{owner}/{repo}/stacks/{stack_number}/add\",\n                self.base_url\n            ))\n            .json(&StackMembersBody { pull_requests })\n            .send()\n            .await?;\n        if !response.status().is_success() {\n            bail!(\n                \"Failed to add to GitHub stack: {}\",\n                response_error(response).await\n            );\n        }\n        Ok(())\n    }\n\n    async fn unstack(&self, owner: &str, repo: &str, stack_number: i64) -> Result<()> {\n        let response = self\n            .client\n            .post(format!(\n                \"{}/repos/{owner}/{repo}/stacks/{stack_number}/unstack\",\n                self.base_url\n            ))\n            .send()\n            .await?;\n        if !response.status().is_success() {\n            bail!(","sourceCodeStart":317,"sourceCodeEnd":353,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-github/src/stacks.rs#L317-L353","documentation":"The REST request to add pull requests to an existing GitHub stack returned a non-success status; GitHub's response body is included via `response_error`. This is the append-members operation on an existing stack.","triggerScenarios":"Calling add_to_stack when POSTing members to `{base_url}/repos/{owner}/{repo}/stacks/{stack_number}` fails — nonexistent stack_number (404), insufficient permissions (403/401), invalid PR ids in the body (422), or 5xx.","commonSituations":"Using a stale stack number after the stack was unstacked/deleted elsewhere; PRs from a different repo; expired token; concurrent edits causing 409.","solutions":["Read the embedded status/body for the specific cause.","If 404, re-list stacks (existing_stacks) to get a fresh stack number — the old one is gone.","If 401/403, repair token scopes and repo access.","If 422, verify all pull_request numbers belong to the same repo and are open.","On 5xx or 409, retry with exponential backoff."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// refresh stack numbers before appending so the target still exists\nlet stacks = stacks_client.existing_stacks(owner, repo, pr).await?;\nanyhow::ensure!(stacks.iter().any(|s| s.number == stack_number), \"stack {stack_number} no longer exists\");","typeGuard":null,"tryCatchPattern":"match stacks.add_to_stack(owner, repo, stack_number, prs).await {\n    Ok(()) => (),\n    Err(e) if e.to_string().contains(\"404\") => re_list_and_reconcile(), // stale stack number\n    Err(e) if e.to_string().contains(\"409\") || e.to_string().contains(\"5\") => retry_with_backoff(),\n    Err(e) => return Err(e),\n}","preventionTips":["Re-list stacks before appending instead of caching stack numbers.","Keep all PR numbers within the same repository.","Handle concurrent unstack operations from other sessions.","Back off on 5xx and conflicts."],"tags":["github","api","http","stacks"],"backgroundTag":"api-error-response","analyzedSha":"58e5313667b857ef39a730e380af31816a7b1768","analyzedAt":"2026-09-18T06:50:32.052Z","contentChangedAt":"2026-09-18T06:50:32.052Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}