Skip to content
Open
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 app/controllers/api/v1/branches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def show
branch = load(PatchBranch.where(lineage_only: false, id: path_param(:id))).first
raise ActiveRecord::RecordNotFound unless branch

BranchSerializer.preload_messages([ branch ])
render json: BranchSerializer.new(branch, context, repo_state: repo_state).full
end

Expand Down
30 changes: 28 additions & 2 deletions app/serializers/api/v1/branch_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,34 @@ class BranchSerializer < BaseSerializer
TEXT_LIMIT = 2000
REF_COLUMNS = %i[id branch_name status ci_status pushed_at].freeze

# needs REF_COLUMNS only
# the REF_COLUMNS and the Message-ID header of the patch message, as `ref` reads them
def self.load_refs(relation)
relation.joins(:message).select(
*REF_COLUMNS.map { |name| PatchBranch.arel_table[name] },
"messages.message_id AS rfc_message_id"
)
end

# loads only the Message-ID header of each patch message, never the body
def self.preload_messages(records)
ActiveRecord::Associations::Preloader.new(
records: records.to_a, associations: :message, scope: Message.select(:id, :message_id)
).call
records
end

def self.brief_list(records, context, **extras)
preload_messages(records)
super
end

# needs a row loaded through load_refs
def ref
{ **REF_COLUMNS.index_with { |name| record.public_send(name) }, url: api_url("/branches/#{record.id}") }
{
**REF_COLUMNS.index_with { |name| record.public_send(name) },
rfc_message_id: record.rfc_message_id,
url: api_url("/branches/#{record.id}")
}
end

def brief
Expand All @@ -20,6 +45,7 @@ def brief
branch_name: record.branch_name,
topic: topic_ref(record.topic),
message_id: record.message_id,
rfc_message_id: record.message&.message_id,
status: record.status,
failure_stage: record.failure_stage,
failure_reason: record.failure_reason&.first(TEXT_LIMIT),
Expand Down
6 changes: 2 additions & 4 deletions app/serializers/api/v1/event_page_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module Api
module V1
# What the events of one page show, loaded once per event type.
class EventPageData
BRANCH_COLUMNS = BranchSerializer::REF_COLUMNS

def initialize(events, context)
@events = events
@context = context
Expand Down Expand Up @@ -90,8 +88,8 @@ def ci_runs

def load_ci_runs(ids)
runs = PatchCiRun.where(id: ids).select(*PatchCiRun.list_columns).to_a
branches = PatchBranch.where(id: runs.map(&:patch_branch_id).uniq)
.select(*BRANCH_COLUMNS).index_by(&:id)
branches = BranchSerializer.load_refs(PatchBranch.where(id: runs.map(&:patch_branch_id).uniq))
.index_by(&:id)
runs.each_with_object({}) do |run, found|
branch = branches[run.patch_branch_id]
next unless branch
Expand Down
6 changes: 3 additions & 3 deletions app/serializers/api/v1/patchset_page_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def current_runs
end

def branches
@branches ||= @ids.empty? ? {} : PatchBranch
.where(message_id: @ids, lineage_only: false)
.select(:message_id, *BranchSerializer::REF_COLUMNS).index_by(&:message_id)
@branches ||= @ids.empty? ? {} : BranchSerializer
.load_refs(PatchBranch.where(message_id: @ids, lineage_only: false))
.select(PatchBranch.arel_table[:message_id]).index_by(&:message_id)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion app/serializers/api/v1/patchset_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Api
module V1
# A message that carries patches.
class PatchsetSerializer < BaseSerializer
COLUMNS = %w[id topic_id sender_person_id subject created_at].freeze
COLUMNS = %w[id message_id topic_id sender_person_id subject created_at].freeze
PRELOAD = [ :topic, { sender_person: PersonSerializer::PRELOAD } ].freeze

def self.load(relation)
Expand All @@ -16,6 +16,7 @@ def self.brief_list(records, context, **extras)
def brief
{
message_id: record.id,
rfc_message_id: record.message_id,
number: page_data.number(record.id),
url: api_url("/messages/#{record.id}"),
html_url: message_html_url(record),
Expand Down
2 changes: 1 addition & 1 deletion docs/api/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Lists answer `{"data": [...], "next_cursor": ...}`. `limit` sets the page size:
5. `GET /topics/{id}/patchsets` lists the messages that carry patches, newest first: `limit=1` is the current version. `files` are the patch files, `changed_paths` the source files they touch. Download a file from its `content_url` (`GET /attachments/{id}/content`): the bytes come as `text/plain` or `application/octet-stream`, not as JSON.
6. `GET /topics/{id}/commits` is what got committed for the thread, a change and its backports as one entry.

`rfc_message_id` is the Message-ID header of a mail. `GET /messages/lookup?rfc_message_id=...` finds the message by it. Percent-encode the value, a `+` is sent as `%2B`. A field named `message_id` is always the numeric id.
`rfc_message_id` is the Message-ID header of a mail. `GET /messages/lookup?rfc_message_id=...` finds the message by it. Percent-encode the value, a `+` is sent as `%2B`. A field named `message_id` is always the numeric id. Branches and patchsets carry both, so a consumer that holds the mail can match them without the lookup.

## Patches, branches and CI

Expand Down
15 changes: 12 additions & 3 deletions docs/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3173,7 +3173,7 @@ components:
The record of hackorum trying to apply this patchset. `status` says
whether it applied. A git branch to fetch exists only when
`pushed_at` is set.
required: [id, branch_name, status, ci_status, pushed_at, url]
required: [id, branch_name, status, ci_status, pushed_at, rfc_message_id, url]
properties:
id:
type: integer
Expand All @@ -3193,6 +3193,9 @@ components:
- $ref: "#/components/schemas/CiStatus"
- type: "null"
description: CI verdict for the commit that is pushed now, null when CI never ran for this branch
rfc_message_id:
type: [string, "null"]
description: Message-ID header of the patch message, usually without angle brackets
url:
type: string
TopicRef:
Expand Down Expand Up @@ -3560,7 +3563,7 @@ components:
The record of hackorum trying to apply a patchset, and where it
stands on CI. A git branch exists only when `pushed_at` is set.
required:
[id, url, html_url, branch_name, topic, message_id, status, failure_stage,
[id, url, html_url, branch_name, topic, message_id, rfc_message_id, status, failure_stage,
failure_reason, conflict_files, state, state_reason, superseded, base_sha, base_source,
base_committed_at,
on_master, pg_major, behind, attempted_at, pushed_at, pushed_head_sha, github_url, git_url,
Expand All @@ -3586,6 +3589,9 @@ components:
message_id:
type: integer
description: Numeric id of the patch message; use `/messages/{message_id}`
rfc_message_id:
type: [string, "null"]
description: Message-ID header of the patch message, usually without angle brackets
status:
type: string
enum: [applied, failed]
Expand Down Expand Up @@ -3725,12 +3731,15 @@ components:
additionalProperties: false
description: A message that carries patches
required:
[message_id, number, url, html_url, created_at, subject, sender, topic, files,
[message_id, rfc_message_id, number, url, html_url, created_at, subject, sender, topic, files,
changed_paths, branch, download_url]
properties:
message_id:
type: integer
description: Numeric id of the message; use `/messages/{message_id}`
rfc_message_id:
type: [string, "null"]
description: Message-ID header of the patch message, usually without angle brackets
number:
type: integer
description: |
Expand Down
7 changes: 5 additions & 2 deletions spec/requests/api/v1/branches_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:start) { Time.zone.parse("2026-03-01 10:00:00") }

let(:branch_keys) do
%w[id url html_url branch_name topic message_id status failure_stage failure_reason conflict_files
%w[id url html_url branch_name topic message_id rfc_message_id status failure_stage failure_reason conflict_files
state state_reason superseded base_sha base_source base_committed_at on_master pg_major behind attempted_at pushed_at
pushed_head_sha github_url git_url last_master_apply_at master_apply_sha master_apply_error master_conflict_files
ci_status ci_skip_reason latest_run created_at updated_at]
Expand Down Expand Up @@ -73,6 +73,7 @@ def run_for(row, status: "success", **attrs)
expect(json.keys).to match_array(branch_keys)
expect(json).to include(
"id" => row.id, "branch_name" => "t#{topic.id}_4", "message_id" => row.message_id,
"rfc_message_id" => row.message.message_id,
"status" => "applied", "failure_stage" => nil, "failure_reason" => nil, "conflict_files" => [],
"state" => "needs_rebase", "state_reason" => nil, "superseded" => false, "base_sha" => "a" * 40, "base_source" => "master",
"base_committed_at" => "2026-03-01T11:00:00.000Z", "on_master" => true, "pg_major" => 19,
Expand Down Expand Up @@ -407,13 +408,15 @@ def run_for(row, status: "success", **attrs)

expect(response).to have_http_status(:ok)
expect(body.keys).to match_array(branch_keys)
expect(body).to include("id" => row.id, "state" => "applies", "ci_status" => "success",
expect(body).to include("id" => row.id, "rfc_message_id" => row.message.message_id, "state" => "applies",
"ci_status" => "success",
"behind" => { "days" => 1, "commits" => nil })
expect(body["latest_run"].keys).to match_array(run_keys)
expect(body["latest_run"]).to include("id" => run.id,
"github_url" => "#{github}/actions/runs/#{run.github_run_id}")
expect(queries.grep(/\A\s*(INSERT|UPDATE|DELETE)/i)).to be_empty
expect(queries.grep(/FROM "patch_ci_runs"/).grep(/payload|"patch_ci_runs"\.\*/)).to be_empty
expect(queries.grep(/FROM "messages"/).grep(/"messages"\.\*|"messages"\."body"/)).to be_empty
expect(response.body).not_to include("secret")
end

Expand Down
12 changes: 8 additions & 4 deletions spec/requests/api/v1/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
is_patch_submission body attachments me]
end
let(:patchset_keys) do
%w[message_id number url html_url created_at subject sender topic files changed_paths branch download_url lineage]
%w[message_id rfc_message_id number url html_url created_at subject sender topic files changed_paths branch download_url
lineage]
end
let(:branch_keys) do
%w[id url html_url branch_name topic message_id status failure_stage failure_reason conflict_files
%w[id url html_url branch_name topic message_id rfc_message_id status failure_stage failure_reason conflict_files
state state_reason superseded base_sha base_source base_committed_at on_master pg_major behind attempted_at pushed_at
pushed_head_sha github_url git_url last_master_apply_at master_apply_sha master_apply_error master_conflict_files
ci_status ci_skip_reason latest_run created_at updated_at]
Expand Down Expand Up @@ -94,13 +95,15 @@ def patch_events(in_topic: topic, sender: alice, at: start)
expect(second["occurred_at"]).to eq("2026-03-01T10:00:00.000Z")
expect(second["author"]).to include("email" => "alice.a@example.org")
expect(second["patchset"].keys).to match_array(patchset_keys)
expect(second["patchset"]).to include("message_id" => posted.message_id, "number" => 2)
expect(second["patchset"]).to include("message_id" => posted.message_id, "number" => 2,
"rfc_message_id" => posted.message.message_id)
expect(second["patchset"]["files"].map { |file| file["file_name"] }).to eq([ "v1-0001-feature.patch" ])
expect(second["patchset"]["branch"]).to include("id" => created.patch_branch_id, "ci_status" => "success")

expect(third["occurred_at"]).to eq("2026-03-01T10:01:00.000Z")
expect(third["branch"].keys).to match_array(branch_keys)
expect(third["branch"]).to include("id" => created.patch_branch_id, "message_id" => posted.message_id,
"rfc_message_id" => posted.message.message_id,
"state" => "applies", "behind" => { "days" => 3, "commits" => nil })
expect(third["branch"]["latest_run"]).to include("id" => completed.patch_ci_run_id)

Expand All @@ -109,7 +112,8 @@ def patch_events(in_topic: topic, sender: alice, at: start)
expect(fourth["ci_run"]).to include("id" => completed.patch_ci_run_id, "status" => "success",
"first_run" => true, "previous_status" => nil)
expect(fourth["ci_run"]["branch"]).to eq(second["patchset"]["branch"])
expect(fourth["ci_run"]["branch"].keys).to match_array(%w[id branch_name status ci_status pushed_at url])
expect(fourth["ci_run"]["branch"].keys).to match_array(%w[id branch_name status ci_status pushed_at rfc_message_id url])
expect(fourth["ci_run"]["branch"]).to include("rfc_message_id" => posted.message.message_id)
expect(response.body).not_to include("secret")
end

Expand Down
9 changes: 6 additions & 3 deletions spec/requests/api/v1/patchsets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
let(:start) { Time.zone.parse("2026-03-01 10:00:00") }

let(:patchset_keys) do
%w[message_id number url html_url created_at subject sender topic files changed_paths branch download_url lineage]
%w[message_id rfc_message_id number url html_url created_at subject sender topic files changed_paths branch download_url
lineage]
end
let(:attachment_keys) { %w[id message_id file_name content_type byte_size is_patch url content_url] }

Expand Down Expand Up @@ -72,7 +73,8 @@ def patchset(files: [ "v1-0001-feature.patch" ], paths: [], **attrs)

row = data.last
expect(row.keys).to match_array(patchset_keys)
expect(row).to include("message_id" => first.id, "number" => 1, "subject" => "Add a feature",
expect(row).to include("message_id" => first.id, "rfc_message_id" => first.message_id, "number" => 1,
"subject" => "Add a feature",
"created_at" => "2026-03-01T10:00:00.000Z", "branch" => nil)
expect(row["url"]).to end_with("/api/v1/messages/#{first.id}")
expect(row["html_url"]).to end_with("/topics/#{topic.id}#message-#{first.id}")
Expand Down Expand Up @@ -137,8 +139,9 @@ def patchset(files: [ "v1-0001-feature.patch" ], paths: [], **attrs)

expect(ids).to eq([ internal.id, built.id ])
expect(data.first["branch"]).to be_nil
expect(data.last["branch"].keys).to match_array(%w[id branch_name status ci_status pushed_at url])
expect(data.last["branch"].keys).to match_array(%w[id branch_name status ci_status pushed_at rfc_message_id url])
expect(data.last["branch"]).to include("id" => branch.id, "branch_name" => "t#{topic.id}_1",
"rfc_message_id" => built.message_id,
"status" => "applied", "ci_status" => "success",
"pushed_at" => "2026-03-01T11:00:00.000Z")
expect(data.last["branch"]["url"]).to end_with("/api/v1/branches/#{branch.id}")
Expand Down