Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Add Batch details page that links from Repository history tab #834

Merged
merged 6 commits into from Dec 28, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions lib/web/controllers/batch_controller.ex
@@ -0,0 +1,42 @@
defmodule BorsNG.BatchController do
@moduledoc """
The controller for the batches

This will either show a batch detail page
"""

use BorsNG.Web, :controller

alias BorsNG.Database.Batch
alias BorsNG.Database.Context.Permission
alias BorsNG.Database.Repo
alias BorsNG.Database.Patch
alias BorsNG.Database.Project
alias BorsNG.Database.Status

def show(conn, %{"id" => id}) do
batch = Repo.get(Batch, id)
project = Repo.get(Project, batch.project_id)

allow_private_repos = Confex.fetch_env!(:bors, BorsNG)[:allow_private_repos]
admin? = conn.assigns.user.is_admin
mode = conn.assigns.user
|> Permission.get_permission(project)
|> case do
_ when admin? -> :rw
:reviewer -> :rw
:member -> :ro
_ when not allow_private_repos -> :ro
_ -> raise BorsNG.PermissionDeniedError
end

case mode do
:ro -> raise BorsNG.PermissionDeniedError
_ ->
end

patches = Repo.all(Patch.all_for_batch(batch.id))
statuses = Repo.all(Status.all_for_batch(batch.id))
render conn, "show.html", batch: batch, patches: patches, project: project, statuses: statuses
end
end
8 changes: 8 additions & 0 deletions lib/web/router.ex
Expand Up @@ -45,6 +45,14 @@ defmodule BorsNG.Router do
get "/", PageController, :index
end

scope "/batches", BorsNG do
pipe_through :browser_page
pipe_through :browser_session
pipe_through :browser_login

get "/:id", BatchController, :show
end

scope "/repositories", BorsNG do
pipe_through :browser_page
pipe_through :browser_session
Expand Down
29 changes: 29 additions & 0 deletions lib/web/templates/batch/show.html.eex
@@ -0,0 +1,29 @@
<main role=main><div class=wrapper>

<h2 class=header>Batch Details</h2>

<%= if @batch do %>
<p>Priority: <%= @batch.priority %></p>
<p>State: <%= stringify_state(@batch.state) %></p>
<p>
Status:
<%= for status <- @statuses do %>
<%= if status.url do %>
<a href="<%= status.url %>"><%= status.identifier %> (<%= stringify_state(status.state) %>)</a>
<% else %>
<span><%= status.identifier %> (<%= stringify_state(status.state) %>)</span>
notriddle marked this conversation as resolved.
Show resolved Hide resolved
<% end %>
notriddle marked this conversation as resolved.
Show resolved Hide resolved
<% end %>
</p>
<p>
Pull Requests:
<%= for patch <- @patches do %>
<a href="<%= Confex.fetch_env!(:bors, :html_github_root) %>/<%= @project.name %>/pull/<%= patch.pr_xref %>">#<%= patch.pr_xref %></a>
<% end %>
</p>

<% else %>
There is no such batch
<% end %>

</div></main>
2 changes: 1 addition & 1 deletion lib/web/templates/project/log_page.html.eex
Expand Up @@ -10,7 +10,7 @@
<% %BorsNG.Database.Batch{id: id, state: state, patches: patches, updated_at: updated_at} -> %>
<tr role=row id="batch-<%= id %>" data-id="<%= id %>" data-datetime="<%= updated_at %>">
<td><%= htmlify_naive_datetime(updated_at) %></td>
<td>Batch</td>
<td><a href="/batches/<%= id %>">Batch <%= id %></a></td>
<td><%= stringify_state(state) %></td>
<td>
<%= for patch <- patches do %>
Expand Down
18 changes: 18 additions & 0 deletions lib/web/views/batch_view.ex
@@ -0,0 +1,18 @@
defmodule BorsNG.BatchView do
@moduledoc """
Batch details page
"""

use BorsNG.Web, :view

def stringify_state(state) do
case state do
:waiting -> "Waiting to run"
:running -> "Running"
:ok -> "Succeeded"
:error -> "Failed"
:canceled -> "Canceled"
_ -> "Invalid"
end
end
end
117 changes: 117 additions & 0 deletions test/controllers/batch_controller_test.exs
@@ -0,0 +1,117 @@
defmodule BorsNG.BatchControllerTest do
use BorsNG.ConnCase

alias BorsNG.Database.Batch
alias BorsNG.Database.Installation
alias BorsNG.Database.LinkPatchBatch
alias BorsNG.Database.LinkMemberProject
alias BorsNG.Database.LinkUserProject
alias BorsNG.Database.Patch
alias BorsNG.Database.Project
alias BorsNG.Database.Repo
alias BorsNG.Database.Status
alias BorsNG.Database.User

setup do
installation = Repo.insert!(%Installation{
installation_xref: 31,
})
project = Repo.insert!(%Project{
installation_id: installation.id,
repo_xref: 13,
name: "example/project",
})
user = Repo.insert!(%User{
user_xref: 23,
login: "ghost",
})
patch = Repo.insert!(%Patch{
project_id: project.id,
pr_xref: 43
})
batch = Repo.insert!(%Batch{
project_id: project.id,
priority: 33
})
Repo.insert!(%LinkPatchBatch{
patch_id: patch.id,
batch_id: batch.id,
})
Repo.insert!(%Status{
batch_id: batch.id,
identifier: "some-identifier",
state: :running
})
Repo.insert!(%Status{
batch_id: batch.id,
identifier: "with-url-identifier",
url: "http://example.com",
state: :waiting
})
{:ok, installation: installation, project: project, user: user, batch: batch, user: user}
end

test "need to log in to see this", %{conn: conn, batch: batch} do
conn = get conn, "/batches/#{batch.id}"
assert html_response(conn, 302) =~ "auth"
end

def login(conn) do
conn = get conn, auth_path(conn, :index, "github")
assert html_response(conn, 302) =~ "MOCK_GITHUB_AUTHORIZE_URL"
conn = get conn, auth_path(
conn,
:callback,
"github",
%{"code" => "MOCK_GITHUB_AUTHORIZE_CODE"})
html_response(conn, 302)
conn
end

test "hides batch details from unlinked user", %{conn: conn, batch: batch} do
conn = login conn

assert_raise BorsNG.PermissionDeniedError, fn ->
get conn, "/batches/#{batch.id}"
end
end

test "shows the batch details as a reviewer", %{conn: conn, batch: batch, user: user, project: project} do
Repo.insert!(%LinkUserProject{
user_id: user.id,
project_id: project.id
})

conn = login conn
conn = get conn, "/batches/#{batch.id}"

assert html_response(conn, 200) =~ "Batch Details"
assert html_response(conn, 200) =~ "Priority: 33"
assert html_response(conn, 200) =~ "State: Invalid"
assert html_response(conn, 200) =~ "#43"
assert html_response(conn, 200) =~ "<span>some-identifier (Running)</span>"
assert html_response(conn, 200) =~ ~s(<a href="http://example.com">with-url-identifier (Waiting to run\)</a>)
end

test "hides batch details from a member", %{conn: conn, batch: batch, user: user, project: project} do
Repo.insert!(%LinkMemberProject{
user_id: user.id,
project_id: project.id
})

conn = login conn

assert_raise BorsNG.PermissionDeniedError, fn ->
get conn, "/batches/#{batch.id}"
end
end

test "shows the batch details for an admin", %{conn: conn, batch: batch, user: user} do
Repo.update!(User.changeset(user, %{is_admin: true}))

conn = login conn
conn = get conn, "/batches/#{batch.id}"

assert html_response(conn, 200) =~ "Batch Details"
end
end