Skip to content

Commit 5bd21f8

Browse files
authored
Merge pull request #1163 from code-corps/1145-set-modified-from
Set modified_from to "code_corps" on tasks and comments when updating from codecorps
2 parents dcd5a93 + 5ab6e0b commit 5bd21f8

File tree

8 files changed

+101
-20
lines changed

8 files changed

+101
-20
lines changed

lib/code_corps/model/comment.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ defmodule CodeCorps.Comment do
3838
|> validate_required([:task_id, :user_id])
3939
|> assoc_constraint(:task)
4040
|> assoc_constraint(:user)
41+
|> put_change(:modified_from, "code_corps")
4142
end
4243

4344
def update_changeset(struct, params) do
4445
struct
4546
|> changeset(params)
4647
|> update_modified_at()
48+
|> put_change(:modified_from, "code_corps")
4749
end
4850

4951
defp set_created_and_modified_at(changeset) do

lib/code_corps/model/task.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ defmodule CodeCorps.Task do
7777
|> assoc_constraint(:project)
7878
|> assoc_constraint(:user)
7979
|> put_change(:status, "open")
80+
|> put_change(:modified_from, "code_corps")
8081
end
8182

8283
@spec update_changeset(struct, map) :: Ecto.Changeset.t
@@ -88,6 +89,7 @@ defmodule CodeCorps.Task do
8889
|> set_closed_at()
8990
|> update_modified_at()
9091
|> maybe_assoc_with_repo(params)
92+
|> put_change(:modified_from, "code_corps")
9193
end
9294

9395
def apply_position(changeset) do

priv/repo/structure.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- PostgreSQL database dump
33
--
44

5-
-- Dumped from database version 9.5.9
5+
-- Dumped from database version 10.0
66
-- Dumped by pg_dump version 10.0
77

88
SET statement_timeout = 0;

test/lib/code_corps/comment/service_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ defmodule CodeCorps.Comment.ServiceTest do
2929
refute_received({:post, "https://api.github.com/" <> _rest, _body, _headers, _options})
3030
end
3131

32+
test "sets modified_from to 'code_corps'" do
33+
{:ok, comment} = valid_attrs() |> Comment.Service.create
34+
assert comment.modified_from == "code_corps"
35+
end
36+
3237
test "returns errored changeset if attributes are invalid" do
3338
{:error, changeset} = Comment.Service.create(@base_attrs)
3439
refute changeset.valid?
@@ -92,6 +97,13 @@ defmodule CodeCorps.Comment.ServiceTest do
9297
refute_received({:patch, "https://api.github.com/" <> _rest, _body, _headers, _options})
9398
end
9499

100+
test "sets modified_from to 'code_corps'" do
101+
comment = insert(:comment, modified_from: "github")
102+
{:ok, updated_comment} = comment |> Comment.Service.update(@update_attrs)
103+
104+
assert updated_comment.modified_from == "code_corps"
105+
end
106+
95107
@preloads [task: [github_repo: :github_app_installation]]
96108

97109
test "propagates changes to github if comment is synced to github comment" do

test/lib/code_corps/github/sync/issue/task/task_test.exs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,59 @@ defmodule CodeCorps.GitHub.Sync.Issue.TaskTest do
33

44
use CodeCorps.DbAccessCase
55

6-
alias CodeCorps.{
7-
Project,
8-
Repo,
9-
Task
10-
}
6+
alias CodeCorps.{Repo, Task}
117
alias CodeCorps.GitHub.Sync.Issue.Task, as: IssueTaskSyncer
128

139
describe "sync all/3" do
14-
test "creates missing, updates existing tasks for each project associated with the github repo" do
10+
defp setup_test_data do
11+
# Creates a user, 3 projects and a github issue all linked to a
12+
# github repo. Returns that data as a map
1513
user = insert(:user)
14+
projects = insert_list(3, :project)
15+
github_repo = insert(:github_repo)
16+
github_issue = insert(
17+
:github_issue,
18+
github_repo: github_repo,
19+
github_updated_at: DateTime.utc_now |> Timex.shift(hours: 1)
20+
)
1621

17-
%{github_repo: github_repo} = github_issue =
18-
insert(:github_issue, github_updated_at: DateTime.utc_now |> Timex.shift(hours: 1))
22+
projects |> Enum.each(&insert(:task_list, project: &1, inbox: true))
1923

20-
[%{project: project_1}, _, _] = project_github_repos =
21-
insert_list(3, :project_github_repo, github_repo: github_repo)
24+
projects
25+
|> Enum.each(&insert(:project_github_repo, project: &1, github_repo: github_repo))
2226

23-
task_1 = insert(:task, project: project_1, github_issue: github_issue, github_repo: github_repo, user: user)
27+
%{github_issue: github_issue, github_repo: github_repo, projects: projects, user: user}
28+
end
2429

25-
project_ids = project_github_repos |> Enum.map(&Map.get(&1, :project_id))
30+
test "creates missing, updates existing tasks for each project associated with the github repo" do
31+
%{
32+
github_issue: github_issue,
33+
github_repo: github_repo,
34+
projects: [project_1 | _] = projects,
35+
user: user
36+
} = setup_test_data()
2637

27-
project_ids |> Enum.each(fn project_id ->
28-
project = Project |> Repo.get_by(id: project_id)
29-
insert(:task_list, project: project, inbox: true)
30-
end)
38+
existing_task =
39+
insert(:task, project: project_1, github_issue: github_issue, github_repo: github_repo, user: user)
3140

3241
{:ok, tasks} = github_issue |> IssueTaskSyncer.sync_all(user)
3342

34-
assert Repo.aggregate(Task, :count, :id) == 3
43+
assert Repo.aggregate(Task, :count, :id) == projects |> Enum.count
44+
assert tasks |> Enum.count == projects |> Enum.count
3545

3646
tasks |> Enum.each(fn task ->
3747
assert task.user_id == user.id
3848
assert task.markdown == github_issue.body
3949
assert task.github_issue_id == github_issue.id
4050
end)
4151

42-
task_ids = tasks |> Enum.map(&Map.get(&1, :id))
43-
assert task_1.id in task_ids
52+
assert existing_task.id in (tasks |> Enum.map(&Map.get(&1, :id)))
53+
end
54+
55+
test "sets task :modified_from to 'github'" do
56+
%{github_issue: github_issue, user: user} = setup_test_data()
57+
{:ok, tasks} = github_issue |> IssueTaskSyncer.sync_all(user)
58+
assert tasks |> Enum.all?(fn task -> task.modified_from == "github" end)
4459
end
4560

4661
test "fails on validation errors" do

test/lib/code_corps/model/comment_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
defmodule CodeCorps.CommentTest do
2+
@moduledoc false
3+
24
use CodeCorps.ModelCase
35

46
alias CodeCorps.Comment
7+
alias Ecto.Changeset
58

69
@valid_attrs %{markdown: "I love elixir!", state: "published"}
710
@invalid_attrs %{}
@@ -40,6 +43,14 @@ defmodule CodeCorps.CommentTest do
4043
{:ok, %Comment{created_at: created_at, modified_at: modified_at}} = Repo.insert(changeset)
4144
assert created_at == modified_at
4245
end
46+
47+
test "sets modified_from to 'code_corps'" do
48+
assert(
49+
%Comment{}
50+
|> Comment.create_changeset(%{})
51+
|> Changeset.get_field(:modified_from) == "code_corps"
52+
)
53+
end
4354
end
4455

4556
describe "update_changeset/2" do
@@ -48,5 +59,14 @@ defmodule CodeCorps.CommentTest do
4859
changeset = Comment.update_changeset(comment, %{})
4960
assert comment.modified_at < changeset.changes[:modified_at]
5061
end
62+
63+
test "sets modified_from to 'code_corps'" do
64+
assert(
65+
:comment
66+
|> insert(modified_from: "github")
67+
|> Comment.update_changeset(%{})
68+
|> Changeset.get_field(:modified_from) == "code_corps"
69+
)
70+
end
5171
end
5272
end

test/lib/code_corps/model/task_test.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule CodeCorps.TaskTest do
22
use CodeCorps.ModelCase
33

44
alias CodeCorps.Task
5+
alias Ecto.Changeset
56

67
@valid_attrs %{
78
title: "Test task",
@@ -65,6 +66,14 @@ defmodule CodeCorps.TaskTest do
6566
assert created_at == modified_at
6667
end
6768

69+
test "sets modified_from to 'code_corps'" do
70+
assert(
71+
%Task{}
72+
|> Task.create_changeset(%{})
73+
|> Changeset.get_field(:modified_from) == "code_corps"
74+
)
75+
end
76+
6877
test "sets the order when the task is not archived and position is set" do
6978
project = insert(:project)
7079
task_list = insert(:task_list)
@@ -134,5 +143,14 @@ defmodule CodeCorps.TaskTest do
134143
{:ok, %Task{order: order}} = Repo.update(changeset)
135144
refute order
136145
end
146+
147+
test "sets :modified_from to 'code_corps'" do
148+
assert(
149+
:task
150+
|> insert(modified_from: "github")
151+
|> Task.update_changeset(%{})
152+
|> Changeset.get_field(:modified_from) == "code_corps"
153+
)
154+
end
137155
end
138156
end

test/lib/code_corps/task/service_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ defmodule CodeCorps.Task.ServiceTest do
3838
refute_received({:post, "https://api.github.com/" <> _rest, _body, _headers, _options})
3939
end
4040

41+
test "sets modified_from to 'code_corps'" do
42+
{:ok, task} = valid_attrs() |> Task.Service.create
43+
assert task.modified_from == "code_corps"
44+
end
45+
4146
test "returns errored changeset if attributes are invalid" do
4247
{:error, changeset} = Task.Service.create(@base_attrs)
4348
refute changeset.valid?
@@ -108,6 +113,13 @@ defmodule CodeCorps.Task.ServiceTest do
108113
refute_received({:patch, "https://api.github.com/" <> _rest, _body, _headers, _options})
109114
end
110115

116+
test "sets modified_from to 'code_corps'" do
117+
task = insert(:task, modified_from: "github")
118+
{:ok, updated_task} = task |> Task.Service.update(@update_attrs)
119+
120+
assert updated_task.modified_from == "code_corps"
121+
end
122+
111123
test "returns {:error, changeset} if there are validation errors" do
112124
task = insert(:task)
113125
{:error, changeset} = task |> Task.Service.update(%{"title" => nil})

0 commit comments

Comments
 (0)