Skip to content

Commit 3e12a93

Browse files
authored
Merge pull request #1194 from code-corps/1191-remove-project-github-repo
Remove ProjectGithubRepo
2 parents 4764879 + 5db3605 commit 3e12a93

File tree

55 files changed

+460
-866
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+460
-866
lines changed

Diff for: lib/code_corps/analytics/segment_traits_builder.ex

+15-9
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,22 @@ defmodule CodeCorps.Analytics.SegmentTraitsBuilder do
3737
user_id: installation.user_id
3838
}
3939
end
40-
defp traits(%CodeCorps.ProjectGithubRepo{} = record) do
41-
record = record |> Repo.preload([:project, :github_repo])
40+
defp traits(%CodeCorps.GithubRepo{} = record) do
41+
project_title =
42+
record
43+
|> Repo.preload([:project])
44+
|> Map.get(:project)
45+
|> (&(&1 || %{})).()
46+
|> Map.get(:title, "")
47+
4248
%{
43-
project: record.project.title,
44-
project_id: record.project_id,
45-
github_repo_id: record.github_repo_id,
46-
github_repo_github_account_login: record.github_repo.github_account_login,
47-
github_repo_github_account_type: record.github_repo.github_account_type,
48-
github_repo_github_id: record.github_repo.github_id,
49-
github_repo_name: record.github_repo.name
49+
id: record.id,
50+
github_account_login: record.github_account_login,
51+
github_account_type: record.github_account_type,
52+
github_id: record.github_id,
53+
github_repo_name: record.name,
54+
project: project_title,
55+
project_id: record.project_id
5056
}
5157
end
5258
defp traits(%CodeCorps.ProjectSkill{} = record) do

Diff for: lib/code_corps/github/event/installation_repositories/installation_repositories.ex

-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositories do
3838
- delete affected `CodeCorps.GithubRepo` records, respecting the rules
3939
- if the GitHub payload for a repo is not matched with a record in our
4040
database, just skip deleting it
41-
- if the deleted `CodeCorps.GithubRepo` record is associated with
42-
`CodeCorps.ProjectGithubRepo` records, they are deleted automatically,
43-
due to `on_delete: :delete_all` set at the database level.
4441
"""
4542
@spec handle(map) :: outcome
4643
def handle(payload) do

Diff for: lib/code_corps/github/sync/comment/comment/comment.ex

+9-12
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ defmodule CodeCorps.GitHub.Sync.Comment.Comment do
1818
GithubIssue,
1919
GithubRepo,
2020
GithubUser,
21-
Project,
22-
ProjectGithubRepo,
2321
Repo,
2422
Task,
2523
User
@@ -48,37 +46,36 @@ defmodule CodeCorps.GitHub.Sync.Comment.Comment do
4846

4947
@doc ~S"""
5048
Creates or updates `CodeCorps.Comment` records for the specified
51-
`CodeCorps.ProjectGithubRepo`.
49+
`CodeCorps.GithubRepo`.
5250
5351
For each `CodeCorps.GithubComment` record that relates to the
54-
`CodeCorps.GithubRepo` for a given`CodeCorps.ProjectGithubRepo`:
52+
`CodeCorps.GithubRepo` for a given`CodeCorps.GithubRepo`:
5553
5654
- Find the related `CodeCorps.Task` record
5755
- Create or update the related `CodeCorps.Comment` record
5856
- Associate the `CodeCorps.Comment` record with the `CodeCorps.User` that
5957
relates to the `CodeCorps.GithubUser` for the `CodeCorps.GithubComment`
6058
"""
61-
@spec sync_project_github_repo(ProjectGithubRepo.t) :: outcome
62-
def sync_project_github_repo(%ProjectGithubRepo{github_repo: %GithubRepo{} = _} = project_github_repo) do
59+
@spec sync_github_repo(GithubRepo.t) :: outcome
60+
def sync_github_repo(%GithubRepo{} = github_repo) do
6361
preloads = [
64-
:project,
65-
github_repo: [github_comments: [:github_issue, github_user: [:user]]]
62+
github_comments: [:github_issue, github_user: [:user]]
6663
]
67-
%ProjectGithubRepo{github_repo: %{github_comments: github_comments}} =
68-
project_github_repo |> Repo.preload(preloads)
64+
%GithubRepo{github_comments: github_comments} =
65+
github_repo |> Repo.preload(preloads)
6966

7067
github_comments
7168
|> Enum.map(fn %GithubComment{github_user: %GithubUser{user: %User{} = user}} = github_comment ->
7269
github_comment
73-
|> find_task(project_github_repo)
70+
|> find_task(github_repo)
7471
|> sync(github_comment, user)
7572
end)
7673
|> ResultAggregator.aggregate
7774
end
7875

7976
defp find_task(
8077
%GithubComment{github_issue: %GithubIssue{id: github_issue_id}},
81-
%ProjectGithubRepo{project: %Project{id: project_id}}) do
78+
%GithubRepo{project_id: project_id}) do
8279
query = from t in Task,
8380
where: t.project_id == ^project_id,
8481
join: gi in GithubIssue, on: t.github_issue_id == gi.id, where: gi.id == ^github_issue_id

Diff for: lib/code_corps/github/sync/issue/issue.ex

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ defmodule CodeCorps.GitHub.Sync.Issue do
1010
1111
The process is as follows:
1212
13-
- match with `User` using `GitHub.Sync.User.RecordLinker`
14-
- for the `ProjectGithubRepo` belonging to the matched repo:
15-
- create or update the `Task` for the `Project`
13+
- match with `CodeCorps.User` using `CodeCorps.GitHub.Sync.User.RecordLinker`
14+
- create or update the `CodeCorps.Task` for the `CodeCorps.Project` in the
15+
matched `CodeCorps.GithubRepo`
1616
1717
If the sync succeeds, it will return an `:ok` tuple with the created or
1818
updated task.

Diff for: lib/code_corps/github/sync/issue/task/changeset.ex

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule CodeCorps.GitHub.Sync.Issue.Task.Changeset do
66

77
alias CodeCorps.{
88
GithubIssue,
9-
ProjectGithubRepo,
9+
GithubRepo,
1010
Repo,
1111
Services.MarkdownRendererService,
1212
Task,
@@ -23,25 +23,25 @@ defmodule CodeCorps.GitHub.Sync.Issue.Task.Changeset do
2323
2424
The changeset can be used to create or update a `Task`
2525
"""
26-
@spec build_changeset(Task.t, GithubIssue.t, ProjectGithubRepo.t, User.t) :: Changeset.t
26+
@spec build_changeset(Task.t, GithubIssue.t, GithubRepo.t, User.t) :: Changeset.t
2727
def build_changeset(
2828
%Task{id: task_id} = task,
2929
%GithubIssue{} = github_issue,
30-
%ProjectGithubRepo{} = project_github_repo,
30+
%GithubRepo{} = github_repo,
3131
%User{} = user) do
3232

3333
case is_nil(task_id) do
34-
true -> create_changeset(task, github_issue, project_github_repo, user)
35-
false -> update_changeset(task, github_issue, project_github_repo)
34+
true -> create_changeset(task, github_issue, github_repo, user)
35+
false -> update_changeset(task, github_issue, github_repo)
3636
end
3737
end
3838

3939
@create_attrs ~w(created_at markdown modified_at status title)a
40-
@spec create_changeset(Task.t, GithubIssue.t, ProjectGithubRepo.t, User.t) :: Changeset.t
40+
@spec create_changeset(Task.t, GithubIssue.t, GithubRepo.t, User.t) :: Changeset.t
4141
defp create_changeset(
4242
%Task{} = task,
4343
%GithubIssue{id: github_issue_id} = github_issue,
44-
%ProjectGithubRepo{project_id: project_id, github_repo_id: github_repo_id},
44+
%GithubRepo{id: github_repo_id, project_id: project_id},
4545
%User{id: user_id}) do
4646

4747
task
@@ -64,11 +64,11 @@ defmodule CodeCorps.GitHub.Sync.Issue.Task.Changeset do
6464
end
6565

6666
@update_attrs ~w(markdown modified_at status title)a
67-
@spec update_changeset(Task.t, GithubIssue.t, ProjectGithubRepo.t) :: Changeset.t
67+
@spec update_changeset(Task.t, GithubIssue.t, GithubRepo.t) :: Changeset.t
6868
defp update_changeset(
6969
%Task{} = task,
7070
%GithubIssue{} = github_issue,
71-
%ProjectGithubRepo{project_id: project_id}) do
71+
%GithubRepo{project_id: project_id}) do
7272
task
7373
|> Changeset.cast(github_issue |> IssueAdapter.to_task, @update_attrs)
7474
|> MarkdownRendererService.render_markdown_to_html(:markdown, :body)

Diff for: lib/code_corps/github/sync/issue/task/task.ex

+22-26
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,71 @@
11
defmodule CodeCorps.GitHub.Sync.Issue.Task do
22
alias CodeCorps.{
3+
GitHub.Sync,
34
GitHub.Utils.ResultAggregator,
45
GithubIssue,
56
GithubRepo,
67
GithubUser,
7-
ProjectGithubRepo,
88
Task,
99
User,
1010
Repo
1111
}
12-
alias CodeCorps.GitHub.Sync.Issue.Task.Changeset, as: TaskChangeset
1312
alias Ecto.Changeset
1413

1514
@type outcome :: {:ok, list(Task.t)} |
1615
{:error, {list(Task.t), list(Changeset.t)}}
1716

1817
@doc """
1918
When provided a `CodeCorps.GithubIssue` and a `CodeCorps.User`, for the
20-
`CodeCorps.Project` associated to that `CodeCorps.GithubRepo` via a
21-
`CodeCorps.ProjectGithubRepo`, it creates or updates a `CodeCorps.Task`.
19+
`CodeCorps.Project` associated to that `CodeCorps.GithubRepo`, it creates or
20+
updates a `CodeCorps.Task`.
2221
"""
2322
@spec sync_github_issue(GithubIssue.t, User.t) :: {:ok, Task.t}
2423
def sync_github_issue(%GithubIssue{} = github_issue, %User{} = user) do
2524
%GithubIssue{
26-
github_repo: %GithubRepo{project_github_repo: project_github_repo}
27-
} = github_issue |> Repo.preload(github_repo: :project_github_repo)
25+
github_repo: %GithubRepo{} = github_repo
26+
} = github_issue |> Repo.preload(:github_repo)
2827

2928
github_issue
30-
|> sync(project_github_repo, user)
29+
|> sync(github_repo, user)
3130
end
3231

3332
@doc ~S"""
34-
Creates or updates `CodeCorps.Task` records for the specified
35-
`CodeCorps.ProjectGithubRepo`.
36-
37-
For each `CodeCorps.GithubIssue` record that relates to the
38-
`CodeCorps.GithubRepo` for a given `CodeCorps.ProjectGithubRepo`:
33+
Creates or updates `CodeCorps.Task` records for each `CodeCorps.GithubIssue`
34+
record that relates to the `CodeCorps.GithubRepo`:
3935
4036
- Create or update the `CodeCorps.Task`
4137
- Associate the `CodeCorps.Task` record with the `CodeCorps.User` that
4238
relates to the `CodeCorps.GithubUser` for the `CodeCorps.GithubIssue`
4339
"""
44-
@spec sync_project_github_repo(ProjectGithubRepo.t) :: outcome
45-
def sync_project_github_repo(%ProjectGithubRepo{github_repo: %GithubRepo{} = _} = project_github_repo) do
46-
%ProjectGithubRepo{
47-
github_repo: %GithubRepo{github_issues: github_issues}
48-
} = project_github_repo |> Repo.preload([:project, github_repo: [github_issues: [github_user: [:user]]]])
40+
@spec sync_github_repo(GithubRepo.t) :: outcome
41+
def sync_github_repo(%GithubRepo{} = github_repo) do
42+
%GithubRepo{
43+
github_issues: github_issues
44+
} = github_repo |> Repo.preload([:project, github_issues: [github_user: [:user]]])
4945

5046
github_issues
51-
|> Enum.map(&find_or_create_task(&1, project_github_repo))
47+
|> Enum.map(&find_or_create_task(&1, github_repo))
5248
|> ResultAggregator.aggregate
5349
end
5450

5551
defp find_or_create_task(
5652
%GithubIssue{github_user: %GithubUser{user: %User{} = user}} = github_issue,
57-
%ProjectGithubRepo{} = project_github_repo) do
53+
%GithubRepo{} = github_repo) do
5854

59-
sync(github_issue, project_github_repo, user)
55+
sync(github_issue, github_repo, user)
6056
end
6157

62-
@spec sync(GithubIssue.t, ProjectGithubRepo.t, User.t) :: {:ok, ProjectGithubRepo.t} | {:error, Changeset.t}
63-
defp sync(%GithubIssue{} = github_issue, %ProjectGithubRepo{} = project_github_repo, %User{} = user) do
64-
project_github_repo
58+
@spec sync(GithubIssue.t, GithubRepo.t, User.t) :: {:ok, GithubRepo.t} | {:error, Changeset.t}
59+
defp sync(%GithubIssue{} = github_issue, %GithubRepo{} = github_repo, %User{} = user) do
60+
github_repo
6561
|> find_or_init_task(github_issue)
66-
|> TaskChangeset.build_changeset(github_issue, project_github_repo, user)
62+
|> Sync.Issue.Task.Changeset.build_changeset(github_issue, github_repo, user)
6763
|> Repo.insert_or_update()
6864
end
6965

70-
@spec find_or_init_task(ProjectGithubRepo.t, GithubIssue.t) :: Task.t
66+
@spec find_or_init_task(GithubRepo.t, GithubIssue.t) :: Task.t
7167
defp find_or_init_task(
72-
%ProjectGithubRepo{project_id: project_id},
68+
%GithubRepo{project_id: project_id},
7369
%GithubIssue{id: github_issue_id}
7470
) do
7571
case Task |> Repo.get_by(github_issue_id: github_issue_id, project_id: project_id) do

Diff for: lib/code_corps/github/sync/pull_request/pull_request.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ defmodule CodeCorps.GitHub.Sync.PullRequest do
1313
`CodeCorps.GitHub.Sync.Utils.RepoFinder`
1414
- match with `CodeCorps.User` using
1515
`CodeCorps.GitHub.Event.PullRequest.UserLinker`
16-
- for each `CodeCorps.ProjectGithubRepo` belonging to matched repo:
17-
- create or update `CodeCorps.Task` for the `CodeCorps.Project`
16+
- create or update each `CodeCorps.Task` for the `CodeCorps.Project` matching
17+
the `CodeCorps.GithubRepo`
1818
1919
If the sync succeeds, it will return an `:ok` tuple with a list of created or
2020
updated tasks.

Diff for: lib/code_corps/github/sync/sync.ex

+25-52
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ defmodule CodeCorps.GitHub.Sync do
1212
GithubRepo,
1313
GitHub.Sync,
1414
GitHub.Sync.Utils.RepoFinder,
15-
ProjectGithubRepo,
1615
Repo
1716
}
1817

@@ -134,14 +133,6 @@ defmodule CodeCorps.GitHub.Sync do
134133
|> Repo.update
135134
end
136135

137-
@spec mark_project_repo(ProjectGithubRepo.t, String.t, Keyword.t) :: {:ok, GithubRepo.t} | {:error, Changeset.t}
138-
defp mark_project_repo(%ProjectGithubRepo{} = project_github_repo, sync_state, opts \\ []) do
139-
params = build_sync_params(sync_state, opts)
140-
project_github_repo
141-
|> ProjectGithubRepo.update_sync_changeset(params)
142-
|> Repo.update
143-
end
144-
145136
@count_fields [
146137
:syncing_comments_count,
147138
:syncing_issues_count,
@@ -181,9 +172,15 @@ defmodule CodeCorps.GitHub.Sync do
181172
- Fetches the comments from the API
182173
- Creates or updates `GithubComment` records (and their related `GithubUser`
183174
records)
175+
- Creates or updates `User` records for the `GithubUser` records
176+
- Creates or updates `Task` records, and relates them to any related
177+
`GithubIssue` and `User` records created previously
178+
- Creates or updates `Comment` records, and relates them to any related
179+
`GithubComment` and `User` records created previously
184180
"""
185181
@spec sync_repo(GithubRepo.t) :: {:ok, GithubRepo.t}
186182
def sync_repo(%GithubRepo{} = repo) do
183+
repo = preload_github_repo(repo)
187184
with {:ok, repo} <- repo |> mark_repo("fetching_pull_requests"),
188185
{:ok, pr_payloads} <- repo |> GitHub.API.Repository.pulls |> sync_step(:fetch_pull_requests),
189186
{:ok, repo} <- repo |> mark_repo("syncing_github_pull_requests", [syncing_pull_requests_count: pr_payloads |> Enum.count]),
@@ -196,7 +193,14 @@ defmodule CodeCorps.GitHub.Sync do
196193
{:ok, comment_payloads} <- repo |> GitHub.API.Repository.issue_comments |> sync_step(:fetch_comments),
197194
{:ok, repo} <- repo |> mark_repo("syncing_github_comments", [syncing_comments_count: comment_payloads |> Enum.count]),
198195
{:ok, _comments} <- comment_payloads |> Enum.map(&Sync.Comment.GithubComment.create_or_update_comment(repo, &1)) |> ResultAggregator.aggregate |> sync_step(:sync_comments),
199-
{:ok, repo} <- repo |> mark_repo("receiving_webhooks")
196+
repo <- Repo.get(GithubRepo, repo.id) |> preload_github_repo(),
197+
{:ok, repo} <- repo |> mark_repo("syncing_users"),
198+
{:ok, _users} <- repo |> Sync.User.User.sync_github_repo() |> sync_step(:sync_users),
199+
{:ok, repo} <- repo |> mark_repo("syncing_tasks"),
200+
{:ok, _tasks} <- repo |> Sync.Issue.Task.sync_github_repo() |> sync_step(:sync_tasks),
201+
{:ok, repo} <- repo |> mark_repo("syncing_comments"),
202+
{:ok, _comments} <- repo |> Sync.Comment.Comment.sync_github_repo() |> sync_step(:sync_comments),
203+
{:ok, repo} <- repo |> mark_repo("synced")
200204
do
201205
{:ok, repo}
202206
else
@@ -206,51 +210,20 @@ defmodule CodeCorps.GitHub.Sync do
206210
{:error, :sync_issues} -> repo |> mark_repo("errored_syncing_issues")
207211
{:error, :fetch_comments} -> repo |> mark_repo("errored_fetching_comments")
208212
{:error, :sync_comments} -> repo |> mark_repo("errored_syncing_comments")
213+
{:error, :sync_users} -> repo |> mark_repo("errored_syncing_users")
214+
{:error, :sync_tasks} -> repo |> mark_repo("errored_syncing_tasks")
215+
{:error, :sync_comments} -> repo |> mark_repo("errored_syncing_comments")
209216
end
210217
end
211218

212-
@doc ~S"""
213-
Syncs a `ProjectGithubRepo` with Code Corps.
214-
215-
Fetches and syncs records from the GitHub API for a given project's GitHub
216-
repository, marking progress of the sync state along the way.
217-
218-
- Finds the `GithubRepo` and syncs it with Github using `sync_repo/1`
219-
- Creates or updates `User` records for the `GithubUser` records
220-
- Creates or updates `Task` records, and relates them to any related
221-
`GithubIssue` and `User` records created previously
222-
- Creates or updates `Comment` records, and relates them to any related
223-
`GithubComment` and `User` records created previously
224-
"""
225-
@spec sync_project_github_repo(ProjectGithubRepo.t) :: {:ok, ProjectGithubRepo.t}
226-
def sync_project_github_repo(%ProjectGithubRepo{} = project_github_repo) do
227-
%ProjectGithubRepo{github_repo: %GithubRepo{} = repo} = project_github_repo =
228-
project_github_repo
229-
|> preload_project_github_repo
230-
231-
with {:ok, project_github_repo} <- project_github_repo |> mark_project_repo("syncing_github_repo"),
232-
{:ok, %GithubRepo{sync_state: "receiving_webhooks"}} <- repo |> sync_repo(),
233-
project_github_repo <- Repo.get(ProjectGithubRepo, project_github_repo.id) |> preload_project_github_repo(),
234-
{:ok, project_github_repo} <- project_github_repo |> mark_project_repo("syncing_users"),
235-
{:ok, _users} <- project_github_repo |> Sync.User.User.sync_project_github_repo() |> sync_step(:sync_users),
236-
{:ok, project_github_repo} <- project_github_repo |> mark_project_repo("syncing_tasks"),
237-
{:ok, _tasks} <- project_github_repo |> Sync.Issue.Task.sync_project_github_repo() |> sync_step(:sync_tasks),
238-
{:ok, project_github_repo} <- project_github_repo |> mark_project_repo("syncing_comments"),
239-
{:ok, _comments} <- project_github_repo |> Sync.Comment.Comment.sync_project_github_repo() |> sync_step(:sync_comments),
240-
{:ok, project_github_repo} <- project_github_repo |> mark_project_repo("synced")
241-
do
242-
{:ok, project_github_repo}
243-
else
244-
{:ok, %GithubRepo{}} -> project_github_repo |> mark_project_repo("errored_syncing_github_repo")
245-
{:error, :sync_users} -> repo |> mark_project_repo("errored_syncing_users")
246-
{:error, :sync_tasks} -> repo |> mark_project_repo("errored_syncing_tasks")
247-
{:error, :sync_comments} -> repo |> mark_project_repo("errored_syncing_comments")
248-
end
249-
end
250-
251-
defp preload_project_github_repo(%ProjectGithubRepo{} = project_github_repo) do
252-
project_github_repo
253-
|> Repo.preload([:project, github_repo: [:github_app_installation, [github_comments: [:github_issue, :github_user], github_issues: [:github_comments, :github_user]]]])
219+
defp preload_github_repo(%GithubRepo{} = github_repo) do
220+
github_repo
221+
|> Repo.preload([
222+
:github_app_installation,
223+
:project,
224+
github_comments: [:github_issue, :github_user],
225+
github_issues: [:github_comments, :github_user]
226+
])
254227
end
255228

256229
@spec marshall_result(tuple) :: tuple

0 commit comments

Comments
 (0)