Skip to content

Commit 8493a3b

Browse files
committed
Add overall status to task
1 parent 44ffc48 commit 8493a3b

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

lib/code_corps_web/views/task_view.ex

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ defmodule CodeCorpsWeb.TaskView do
44
use JaSerializer.PhoenixView
55

66
attributes [
7-
:archived, :body, :created_at, :created_from, :inserted_at, :markdown,
8-
:modified_at, :modified_from, :number, :order, :status, :title, :updated_at
7+
:archived, :body, :created_at, :created_from, :has_github_pull_request,
8+
:inserted_at, :markdown, :modified_at, :modified_from, :number, :order,
9+
:overall_status, :status, :title, :updated_at
910
]
1011

1112
has_one :github_issue, type: "github-issue", field: :github_issue_id
@@ -18,4 +19,21 @@ defmodule CodeCorpsWeb.TaskView do
1819

1920
has_many :comments, serializer: CodeCorpsWeb.CommentView, identifiers: :always
2021
has_many :task_skills, serializer: CodeCorpsWeb.TaskSkillView, identifiers: :always
22+
23+
def has_github_pull_request(%{
24+
github_pull_request: %CodeCorps.GithubPullRequest{}
25+
}), do: true
26+
def has_github_pull_request(%{github_pull_request: nil}), do: false
27+
28+
def overall_status(%{
29+
github_pull_request: %CodeCorps.GithubPullRequest{merged: merged, state: state}
30+
}, _conn) do
31+
case merged do
32+
true -> "merged"
33+
false -> state
34+
end
35+
end
36+
def overall_status(%{github_pull_request: nil, status: status}, _conn) do
37+
status
38+
end
2139
end

test/lib/code_corps_web/views/task_view_test.exs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
defmodule CodeCorpsWeb.TaskViewTest do
2+
@moduledoc false
3+
24
use CodeCorpsWeb.ViewCase
35

46
test "renders all attributes and relationships properly" do
@@ -20,12 +22,14 @@ defmodule CodeCorpsWeb.TaskViewTest do
2022
"body" => task.body,
2123
"created-at" => task.created_at,
2224
"created-from" => task.created_from,
25+
"has-github-pull-request" => true,
2326
"inserted-at" => task.inserted_at,
2427
"markdown" => task.markdown,
2528
"modified-at" => task.modified_at,
2629
"modified-from" => task.modified_from,
2730
"number" => task.number,
2831
"order" => task.order,
32+
"overall-status" => "open",
2933
"status" => task.status,
3034
"title" => task.title,
3135
"updated-at" => task.updated_at
@@ -100,4 +104,65 @@ defmodule CodeCorpsWeb.TaskViewTest do
100104

101105
assert rendered_json == expected_json
102106
end
107+
108+
describe "has-github-pull-request" do
109+
test "when pull request exists" do
110+
github_pull_request = insert(:github_pull_request)
111+
github_issue = insert(:github_issue, github_pull_request: github_pull_request)
112+
task = insert(:task, github_issue: github_issue)
113+
task = CodeCorpsWeb.TaskController.preload(task)
114+
rendered_json = render(CodeCorpsWeb.TaskView, "show.json-api", data: task)
115+
assert rendered_json["data"]["attributes"]["has-github-pull-request"]
116+
end
117+
118+
test "when no pull request exists" do
119+
task = insert(:task)
120+
task = CodeCorpsWeb.TaskController.preload(task)
121+
rendered_json = render(CodeCorpsWeb.TaskView, "show.json-api", data: task)
122+
refute rendered_json["data"]["attributes"]["has-github-pull-request"]
123+
end
124+
end
125+
126+
describe "overall-status" do
127+
test "when pull request is open" do
128+
github_pull_request = insert(:github_pull_request, merged: false, state: "open")
129+
github_issue = insert(:github_issue, github_pull_request: github_pull_request)
130+
task = insert(:task, github_issue: github_issue)
131+
task = CodeCorpsWeb.TaskController.preload(task)
132+
rendered_json = render(CodeCorpsWeb.TaskView, "show.json-api", data: task)
133+
assert rendered_json["data"]["attributes"]["overall-status"] == "open"
134+
end
135+
136+
test "when pull request is closed" do
137+
github_pull_request = insert(:github_pull_request, merged: false, state: "closed")
138+
github_issue = insert(:github_issue, github_pull_request: github_pull_request)
139+
task = insert(:task, github_issue: github_issue)
140+
task = CodeCorpsWeb.TaskController.preload(task)
141+
rendered_json = render(CodeCorpsWeb.TaskView, "show.json-api", data: task)
142+
assert rendered_json["data"]["attributes"]["overall-status"] == "closed"
143+
end
144+
145+
test "when pull request is merged" do
146+
github_pull_request = insert(:github_pull_request, merged: false, state: "merged")
147+
github_issue = insert(:github_issue, github_pull_request: github_pull_request)
148+
task = insert(:task, github_issue: github_issue)
149+
task = CodeCorpsWeb.TaskController.preload(task)
150+
rendered_json = render(CodeCorpsWeb.TaskView, "show.json-api", data: task)
151+
assert rendered_json["data"]["attributes"]["overall-status"] == "merged"
152+
end
153+
154+
test "when task is open" do
155+
task = insert(:task, status: "open")
156+
task = CodeCorpsWeb.TaskController.preload(task)
157+
rendered_json = render(CodeCorpsWeb.TaskView, "show.json-api", data: task)
158+
assert rendered_json["data"]["attributes"]["overall-status"] == "open"
159+
end
160+
161+
test "when task is closed" do
162+
task = insert(:task, status: "closed")
163+
task = CodeCorpsWeb.TaskController.preload(task)
164+
rendered_json = render(CodeCorpsWeb.TaskView, "show.json-api", data: task)
165+
assert rendered_json["data"]["attributes"]["overall-status"] == "closed"
166+
end
167+
end
103168
end

0 commit comments

Comments
 (0)