Skip to content

Commit 4c5478c

Browse files
authored
feat(front): add job status badge (#120)
1 parent c15f323 commit 4c5478c

File tree

8 files changed

+67
-4
lines changed

8 files changed

+67
-4
lines changed

front/lib/front_web/controllers/artifacts_controller.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ defmodule FrontWeb.ArtifactsController do
111111
source_kind = "jobs"
112112
source_id = job.id
113113

114+
badge_pollman = %{
115+
state: job.state,
116+
href: "/jobs/#{job.id}/status_badge"
117+
}
118+
114119
block =
115120
Enum.find(pipeline.blocks, fn block ->
116121
Enum.any?(block.jobs, fn job -> job.id == source_id end)
@@ -126,6 +131,7 @@ defmodule FrontWeb.ArtifactsController do
126131
|> Map.put(:workflow_name, hook.commit_message |> String.split("\n") |> hd)
127132
|> Map.put(:pipeline, pipeline)
128133
|> Map.put(:block, block)
134+
|> Map.put(:badge_pollman, badge_pollman)
129135
|> Map.put(:title, "Job Artifacts・#{project.name}#{organization.name}")
130136
|> Front.Breadcrumbs.Artifacts.construct(conn, source_kind)
131137

front/lib/front_web/controllers/job_controller.ex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ defmodule FrontWeb.JobController do
103103
href: "/jobs/#{job_id}/status"
104104
}
105105

106+
badge_pollman = %{
107+
state: pollman_state,
108+
href: "/jobs/#{conn.assigns.job.id}/status_badge"
109+
}
110+
106111
log_state = %{
107112
dark: memory["logDark"],
108113
wrap: memory["logWrap"],
@@ -117,6 +122,7 @@ defmodule FrontWeb.JobController do
117122
assigns =
118123
%{
119124
pollman: pollman,
125+
badge_pollman: badge_pollman,
120126
log_state: log_state,
121127
finished_job: finished_job,
122128
token: token,
@@ -187,6 +193,22 @@ defmodule FrontWeb.JobController do
187193
end)
188194
end
189195

196+
def status_badge(conn, _params) do
197+
pollman_state = extract_state(conn.assigns.job.state)
198+
199+
badge_pollman = %{
200+
state: pollman_state,
201+
href: "/jobs/#{conn.assigns.job.id}/status_badge"
202+
}
203+
204+
data = [badge_pollman: badge_pollman]
205+
206+
conn
207+
|> put_view(FrontWeb.JobView)
208+
|> put_layout(false)
209+
|> render("_status_badge.html", data)
210+
end
211+
190212
def stop(conn, _params) do
191213
Watchman.benchmark("stop.duration", fn ->
192214
job_id = conn.assigns.job.id

front/lib/front_web/controllers/test_results_controller.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,16 @@ defmodule FrontWeb.TestResultsController do
5151
{:ok, org} = Async.await(fetch_organization)
5252
{:ok, {:ok, junit_json_url}} = Async.await(fetch_junit_json_url)
5353

54+
badge_pollman = %{
55+
state: job.state,
56+
href: "/jobs/#{job.id}/status_badge"
57+
}
58+
5459
assigns = %{
5560
js: "testResults",
5661
job_id: job.id,
57-
json_artifacts_url: junit_json_url
62+
json_artifacts_url: junit_json_url,
63+
badge_pollman: badge_pollman
5864
}
5965

6066
case conn.assigns.authorization do

front/lib/front_web/router.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ defmodule FrontWeb.Router do
630630
# Job Page
631631
get("/jobs/:id", JobController, :show)
632632
get("/jobs/:id/status", JobController, :status)
633+
get("/jobs/:id/status_badge", JobController, :status_badge)
633634
get("/jobs/:id/summary", TestResultsController, :job_summary)
634635
get("/jobs/:id/logs", JobController, :logs)
635636

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div
2+
class="flex mt1"
3+
data-poll-background
4+
data-poll-state="<%= @badge_pollman.state %>"
5+
data-poll-href="<%= @badge_pollman.href %>"
6+
>
7+
<span class="<%= job_state_color(@job.state) %> white br1 ph2">
8+
<%= String.capitalize(@job.state) %>
9+
</span>
10+
</div>

front/lib/front_web/templates/layout/job.html.eex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@
4646
</div>
4747
</div>
4848
<div class="flex items-center justify-between mt1 mt2-m mb2 mb3-m">
49-
<h1 class="f2 f1-m lh-title mt1 mb0 pr3 truncate">
50-
<%= @job.name %>
51-
</h1>
49+
<div class="flex justify-center items-center">
50+
<h1 class="f2 f1-m lh-title mt1 mb0 pr3 truncate">
51+
<%= @job.name %>
52+
</h1>
53+
<%= render FrontWeb.JobView, "_status_badge.html", job: @job, badge_pollman: @badge_pollman %>
54+
</div>
5255
<div class="flex">
5356
<%= if !assigns[:permissions] or @permissions["project.job.rerun"] do %>
5457
<%= link "Rerun", to: workflow_path(@conn, :rebuild, @workflow.id), method: :post, class: "btn btn-secondary ml1", "data-tippy-content": "Rerun the parent workflow, including this job" %>

front/lib/front_web/views/job_view.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,10 @@ defmodule FrontWeb.JobView do
6262
"The job is waiting on #{job.machine_type} quota."
6363
end
6464
end
65+
66+
defp job_state_color("pending"), do: "bg-orange"
67+
defp job_state_color("running"), do: "bg-indigo"
68+
defp job_state_color("passed"), do: "bg-green"
69+
defp job_state_color("failed"), do: "bg-red"
70+
defp job_state_color("stopped"), do: "bg-gray"
6571
end

front/test/front_web/controllers/job_controller_test.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,13 @@ defmodule FrontWeb.JobControllerTest do
7474
assert html_response(conn, 404) =~ "404"
7575
end
7676
end
77+
78+
describe "status_badge" do
79+
test "renders badge", %{conn: conn, job: job} do
80+
conn = get(conn, job_path(conn, :status_badge, job.id))
81+
82+
assert html_response(conn, 200) ==
83+
"<div\n class=\"flex mt1\"\n data-poll-background\n data-poll-state=\"poll\"\n data-poll-href=\"/jobs/job-id/status_badge\"\n>\n <span class=\"bg-indigo white br1 ph2\">\nRunning\n </span>\n</div>\n"
84+
end
85+
end
7786
end

0 commit comments

Comments
 (0)