Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions services/common/contract/worker_messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"await_merge": {"type":"await_merge","team_id":"TEAM#team-a","installation_id":1,"repo_owner":"acme","repo_name":"widgets","pr_number":2,"head_sha":"abc","base_branch":"main","self_authored":true,"attempts":1,"chain_id":"c","error_retries":0},
"graph_index": {"type":"graph_index","team_id":"TEAM#team-a","installation_id":1,"repo_owner":"acme","repo_name":"widgets","branch":"main","changed_files":["a.rs"]},
"release_notes": {"type":"release_notes","team_id":"TEAM#team-a","installation_id":1,"repo_owner":"acme","repo_name":"widgets","tag":"v1.0.0","sha":"abc","resend":"","check_branch":true},
"restack": {"type":"restack","team_id":"TEAM#team-a","installation_id":1,"repo_owner":"acme","repo_name":"widgets","branch":"coderhelm/feature-a","before":"abc","after":"def"},
"tag_sweep": {"type":"tag_sweep","team_id":"TEAM#team-a","installation_id":1,"repo_owner":"acme","repo_name":"widgets","base_branch":"main","pr_number":2,"delay_remaining_secs":0,"sweep_id":"s","manual":true,"claimed":false,"attempt":0},
"test_ingest": {"type":"test_ingest","team_id":"TEAM#team-a","installation_id":1,"repo_owner":"acme","repo_name":"widgets","repo_id":9,"default_branch":"main","workflow":"e2e","workflow_path":".github/workflows/e2e.yml","run_id":10,"run_attempt":1,"head_sha":"abc","head_branch":"main","event":"push","pr_numbers":[2],"conclusion":"success","html_url":"https://example.test/run/10","started_at":"2026-01-01T00:00:00Z","updated_at":"2026-01-01T00:10:00Z","upload_key":null},
"test_action": {"type":"test_action","team_id":"TEAM#team-a","repo":"acme/widgets","action":"fix","target_id":"t1","actor":"someone"},
Expand Down
2 changes: 2 additions & 0 deletions services/common/src/dlq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub const WORKER_MESSAGE_TYPES: &[&str] = &[
"await_merge",
"release_steps",
"tag_sweep",
// A force push: the pull requests stacked on that branch are rebuilt.
"restack",
"release_notes",
"graph_index",
"test_ingest",
Expand Down
26 changes: 23 additions & 3 deletions services/common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ pub enum WorkerMessage {
/// branch's current HEAD — skipping if that commit is already tagged.
#[serde(rename = "tag_sweep")]
TagSweep(TagSweepMessage),
/// A branch was force-pushed (rebased): CoderHelm's open pull requests
/// stacked on it get their own change rebuilt on its new head, one at
/// a time (`worker::passes::restack`).
#[serde(rename = "restack")]
Restack(RestackMessage),
/// Release notes for a release tag CoderHelm just cut (or a re-send from the
/// dashboard): GitHub Release + Confluence changelog entry + IT email.
#[serde(rename = "release_notes")]
Expand Down Expand Up @@ -345,9 +350,10 @@ impl WorkerMessage {
use crate::usage::feature as f;
match self {
WorkerMessage::Ticket(t) if matches!(t.source, TicketSource::Alert) => f::ALERT,
WorkerMessage::Ticket(_) | WorkerMessage::Resume(_) | WorkerMessage::MarkReady(_) => {
f::RUN
}
WorkerMessage::Ticket(_)
| WorkerMessage::Resume(_)
| WorkerMessage::MarkReady(_)
| WorkerMessage::Restack(_) => f::RUN,
WorkerMessage::Feedback(_) | WorkerMessage::JiraComment(_) => f::FEEDBACK,
WorkerMessage::Lesson(_) | WorkerMessage::RewardOutcome(_) => f::MEMORY,
WorkerMessage::Onboard(_) => f::ONBOARD,
Expand Down Expand Up @@ -727,6 +733,20 @@ pub struct MarkReadyMessage {
pub pr_number: u64,
}

/// A force push to `branch` (from `before` to `after`).
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct RestackMessage {
pub team_id: String,
pub installation_id: u64,
pub repo_owner: String,
pub repo_name: String,
pub branch: String,
/// The branch's head before the push: where the stacked branches'
/// own work starts.
pub before: String,
pub after: String,
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct PlanExecuteMessage {
pub team_id: String,
Expand Down
86 changes: 86 additions & 0 deletions services/gateway/src/routes/github_webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3623,6 +3623,38 @@ fn tool_definition_paths(payload: &Value) -> Vec<String> {
paths
}

/// The rebuild a push asks for: a force push (not a deletion or a new
/// branch) to a branch other than the default one.
fn restack_message(
payload: &Value,
installation_id: u64,
team_id: &str,
owner: &str,
name: &str,
) -> Option<crate::models::RestackMessage> {
let flag = |k: &str| payload[k].as_bool().unwrap_or(false);
if !flag("forced") || flag("deleted") || flag("created") {
return None;
}
let branch = payload["ref"].as_str()?.strip_prefix("refs/heads/")?;
let default_branch = payload["repository"]["default_branch"]
.as_str()
.unwrap_or("main");
let before = payload["before"].as_str().unwrap_or("");
if branch == default_branch || before.is_empty() || before.chars().all(|c| c == '0') {
return None;
}
Some(crate::models::RestackMessage {
team_id: team_id.to_string(),
installation_id,
repo_owner: owner.to_string(),
repo_name: name.to_string(),
branch: branch.to_string(),
before: before.to_string(),
after: payload["after"].as_str().unwrap_or("").to_string(),
})
}

async fn handle_push(
state: &AppState,
payload: &Value,
Expand All @@ -3648,6 +3680,16 @@ async fn handle_push(
if let Some(tag) = git_ref.strip_prefix("refs/tags/") {
return handle_tag_push(state, payload, installation_id, team_id, owner, name, tag).await;
}
// A force push (a rebase) to a branch other pull requests may stack on:
// CoderHelm's stacked pull requests get rebuilt on its new head.
if let Some(m) = restack_message(payload, installation_id, team_id, owner, name) {
let _ = send_to_queue(
state,
&state.config.ticket_queue_url,
&WorkerMessage::Restack(m),
)
.await;
}
// Only the branch the graph tracks.
if git_ref != format!("refs/heads/{default_branch}") {
return Ok(StatusCode::OK);
Expand Down Expand Up @@ -5012,3 +5054,47 @@ mod close_reason_tests {
assert!(!is_own_actor(&serde_json::json!({})));
}
}

#[cfg(test)]
mod restack_tests {
use super::*;

#[test]
fn only_a_force_push_to_another_branch_rebuilds_its_stack() {
let push = |forced: bool, r: &str, before: &str| {
serde_json::json!({
"ref": r,
"forced": forced,
"before": before,
"after": "b2",
"repository": {"default_branch": "main"},
})
};
let m = restack_message(
&push(true, "refs/heads/coderhelm/feature-a", "a1"),
7,
"t1",
"acme",
"web",
)
.unwrap();
assert_eq!(
(m.branch.as_str(), m.before.as_str(), m.after.as_str()),
("coderhelm/feature-a", "a1", "b2")
);
assert_eq!((m.installation_id, m.repo_owner.as_str()), (7, "acme"));
// A plain push, the default branch, a tag, a new branch: nothing.
for p in [
push(false, "refs/heads/coderhelm/feature-a", "a1"),
push(true, "refs/heads/main", "a1"),
push(true, "refs/tags/v1", "a1"),
push(
true,
"refs/heads/x",
"0000000000000000000000000000000000000000",
),
] {
assert!(restack_message(&p, 7, "t1", "acme", "web").is_none());
}
}
}
Loading
Loading