Runner context: set github.event_name

0239060 · Gabriel Morell · 2026-10-17 04:25

8 files +63 -0
Message
{commit_body(@commit)}

Files changed

modified lib/git_gud_web/controllers/runner_controller.ex
+4 −0
@@ -286,6 +286,10 @@ defmodule GitGudWeb.RunnerController do
286 286
287 287 %{
288 288 "event" => run.trigger || "push",
289 + # `github.event_name`, which is what workflows compare against —
290 + # `if: github.event_name == 'push'` read it as empty and skipped the
291 + # job on every push.
292 + "event_name" => run.trigger || "push",
289 293 "ref" => run.head_ref || "",
290 294 # `github.ref_name` / `github.ref_type`, and the GITHUB_REF_NAME and
291 295 # GITHUB_REF_TYPE the runner exports from them. Every workflow written
modified test/git_gud_web/controllers/runner_controller_test.exs
+59 −0
@@ -149,6 +149,17 @@ defmodule GitGudWeb.RunnerControllerTest do
149 149 assert resp.status == 200
150 150 end
151 151
152 + test "FetchTask hands the job GitHub's context names", %{conn: conn, runner_token: token} do
153 + context =
154 + fetch_context(conn, token, %{trigger: "push", head_ref: "refs/heads/main"})
155 +
156 + assert context["event_name"] == "push"
157 + assert context["event"] == "push"
158 + assert context["ref"] == "refs/heads/main"
159 + assert context["ref_name"] == "main"
160 + assert context["ref_type"] == "branch"
161 + end
162 +
152 163 test "FetchTask without token → 401", %{conn: conn} do
153 164 body = FetchTaskRequest.encode(%FetchTaskRequest{}) |> IO.iodata_to_binary()
154 165
@@ -306,4 +317,52 @@ defmodule GitGudWeb.RunnerControllerTest do
306 317 assert bodies.(second_step) == ["compiling the cli"]
307 318 end
308 319 end
320 +
321 + # Queue one job on a run built from `run_attrs`, let the runner fetch it,
322 + # and return the task's context as a plain string map.
323 + defp fetch_context(conn, token, run_attrs) do
324 + user = GitGud.AccountsFixtures.user_fixture()
325 +
326 + {:ok, repo} =
327 + GitGud.Repositories.create_repository(user, %{
328 + "name" => "ctx-#{System.unique_integer([:positive])}"
329 + })
330 +
331 + run =
332 + %GitGud.Workflows.WorkflowRun{repository_id: repo.id}
333 + |> GitGud.Workflows.WorkflowRun.create_changeset(
334 + Map.merge(
335 + %{
336 + number: 1,
337 + workflow_path: ".forgejo/workflows/ci.yml",
338 + head_sha: :crypto.strong_rand_bytes(20)
339 + },
340 + run_attrs
341 + )
342 + )
343 + |> GitGud.Repo.insert!()
344 +
345 + %GitGud.Workflows.WorkflowJob{workflow_run_id: run.id}
346 + |> GitGud.Workflows.WorkflowJob.create_changeset(%{
347 + job_id: "build",
348 + runs_on: %{"labels" => ["ubuntu-latest"]},
349 + definition: %{"runs-on" => "ubuntu-latest", "steps" => [%{"run" => "true"}]}
350 + })
351 + |> GitGud.Repo.insert!()
352 +
353 + body = FetchTaskRequest.encode(%FetchTaskRequest{tasks_version: 0}) |> IO.iodata_to_binary()
354 +
355 + resp =
356 + conn
357 + |> put_req_header("content-type", "application/protobuf")
358 + |> put_req_header("authorization", "Bearer " <> token)
359 + |> post("/api/actions/_apis/runner.v1.RunnerService/FetchTask", body)
360 +
361 + assert resp.status == 200
362 + %FetchTaskResponse{task: task} = FetchTaskResponse.decode(resp.resp_body)
363 +
364 + Map.new(task.context.fields, fn {k, %Google.Protobuf.Value{kind: {:string_value, v}}} ->
365 + {k, v}
366 + end)
367 + end
309 368 end

Parents: 34b2c06