Skip to content

Commit a0dcb68

Browse files
authored
Merge pull request #1142 from code-corps/1135-add-syncing-statuses-to-github-repo
Add syncing statuses to GithubRepo
2 parents 7725cb9 + eafbfc7 commit a0dcb68

File tree

5 files changed

+115
-17
lines changed

5 files changed

+115
-17
lines changed

lib/code_corps/github/event/installation/repos.ex

+7-14
Original file line numberDiff line numberDiff line change
@@ -97,30 +97,23 @@ defmodule CodeCorps.GitHub.Event.Installation.Repos do
9797
end
9898

9999
@spec create(GithubAppInstallation.t, map) :: {:ok, GithubRepo.t}
100-
defp create(%GithubAppInstallation{} = installation, %{} = repo_attributes) do
100+
defp create(%GithubAppInstallation{id: installation_id}, %{} = repo_attributes) do
101+
params =
102+
repo_attributes
103+
|> Map.put(:github_app_installation_id, installation_id)
104+
101105
%GithubRepo{}
102-
|> changeset(repo_attributes)
103-
|> Changeset.put_assoc(:github_app_installation, installation)
106+
|> GithubRepo.changeset(params)
104107
|> Repo.insert()
105108
end
106109

107110
@spec update(GithubRepo.t, map) :: {:ok, GithubRepo.t}
108111
defp update(%GithubRepo{} = github_repo, %{} = repo_attributes) do
109112
github_repo
110-
|> changeset(repo_attributes)
113+
|> GithubRepo.changeset(repo_attributes)
111114
|> Repo.update()
112115
end
113116

114-
@spec changeset(GithubRepo.t, map) :: Changeset.t
115-
defp changeset(%GithubRepo{} = github_repo, %{} = repo_attributes) do
116-
github_repo
117-
|> Changeset.change(repo_attributes)
118-
|> Changeset.validate_required([
119-
:github_id, :name, :github_account_id,
120-
:github_account_avatar_url, :github_account_login, :github_account_type
121-
])
122-
end
123-
124117
# transaction step 5
125118
@spec mark_processed(%{processing_installation: GithubAppInstallation.t}) :: {:ok, GithubAppInstallation.t}
126119
defp mark_processed(%{processing_installation: %GithubAppInstallation{} = installation}) do

lib/code_corps/model/github_repo.ex

+41
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,51 @@ defmodule CodeCorps.GithubRepo do
1010
field :github_account_type, :string
1111
field :github_id, :integer
1212
field :name, :string
13+
field :sync_state, :string, default: "unsynced"
14+
field :syncing_comments_count, :integer, default: 0
15+
field :syncing_issues_count, :integer, default: 0
16+
field :syncing_pull_requests_count, :integer, default: 0
1317

1418
belongs_to :github_app_installation, CodeCorps.GithubAppInstallation
1519
has_many :project_github_repos, CodeCorps.ProjectGithubRepo
1620

1721
timestamps()
1822
end
23+
24+
@doc """
25+
Builds a changeset based on the `struct` and `params`.
26+
"""
27+
def changeset(struct, params \\ %{}) do
28+
struct
29+
|> cast(params, [
30+
:github_account_id, :github_account_avatar_url, :github_account_login,
31+
:github_account_type, :github_app_installation_id, :github_id, :name,
32+
:sync_state, :syncing_comments_count, :syncing_issues_count,
33+
:syncing_pull_requests_count
34+
])
35+
|> validate_required([
36+
:github_account_id, :github_account_avatar_url, :github_account_login,
37+
:github_account_type, :github_id, :name
38+
])
39+
|> assoc_constraint(:github_app_installation)
40+
end
41+
42+
def update_sync_changeset(struct, params) do
43+
struct
44+
|> changeset(params)
45+
|> validate_inclusion(:sync_state, sync_states())
46+
end
47+
48+
def sync_states do
49+
~w{
50+
unsynced
51+
fetching_pull_requests errored_fetching_pull_requests
52+
syncing_pull_requests errored_syncing_pull_requests
53+
fetching_issues errored_fetching_issues
54+
syncing_issues errored_syncing_issues
55+
fetching_comments errored_fetching_comments
56+
syncing_comments errored_syncing_comments
57+
receiving_webhooks
58+
}
59+
end
1960
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
defmodule CodeCorps.Repo.Migrations.AddSyncStatusesToGithubRepo do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:github_repos) do
6+
add :sync_state, :string, default: "unsynced"
7+
add :syncing_comments_count, :integer, default: 0
8+
add :syncing_issues_count, :integer, default: 0
9+
add :syncing_pull_requests_count, :integer, default: 0
10+
end
11+
12+
create index(:github_repos, [:sync_state])
13+
end
14+
end

priv/repo/structure.sql

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- PostgreSQL database dump
33
--
44

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

88
SET statement_timeout = 0;
@@ -425,7 +425,11 @@ CREATE TABLE github_repos (
425425
github_account_type character varying(255),
426426
github_app_installation_id bigint,
427427
inserted_at timestamp without time zone NOT NULL,
428-
updated_at timestamp without time zone NOT NULL
428+
updated_at timestamp without time zone NOT NULL,
429+
sync_state character varying(255) DEFAULT 'unsynced'::character varying,
430+
syncing_comments_count integer DEFAULT 0,
431+
syncing_issues_count integer DEFAULT 0,
432+
syncing_pull_requests_count integer DEFAULT 0
429433
);
430434

431435

@@ -2473,6 +2477,13 @@ CREATE INDEX github_repos_github_app_installation_id_index ON github_repos USING
24732477
CREATE UNIQUE INDEX github_repos_github_id_index ON github_repos USING btree (github_id);
24742478

24752479

2480+
--
2481+
-- Name: github_repos_sync_state_index; Type: INDEX; Schema: public; Owner: -
2482+
--
2483+
2484+
CREATE INDEX github_repos_sync_state_index ON github_repos USING btree (sync_state);
2485+
2486+
24762487
--
24772488
-- Name: index_categories_on_slug; Type: INDEX; Schema: public; Owner: -
24782489
--
@@ -3681,5 +3692,5 @@ ALTER TABLE ONLY user_tasks
36813692
-- PostgreSQL database dump complete
36823693
--
36833694

3684-
INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170224233516), (20170226050552), (20170228085250), (20170308214128), (20170308220713), (20170308222552), (20170313130611), (20170318032449), (20170318082740), (20170324194827), (20170424215355), (20170501225441), (20170505224222), (20170526095401), (20170602000208), (20170622205732), (20170626231059), (20170628092119), (20170628213609), (20170629183404), (20170630140136), (20170706132431), (20170707213648), (20170711122252), (20170717092127), (20170725060612), (20170727052644), (20170731130121), (20170814131722), (20170913114958), (20170921014405), (20170925214512), (20170925230419), (20170926134646), (20170927100300), (20170928234412), (20171003134956), (20171003225853), (20171006063358), (20171006161407), (20171012215106), (20171012221231), (20171016125229), (20171016125516), (20171016223356), (20171016235656), (20171017235433), (20171019191035), (20171025184225), (20171026010933), (20171027061833), (20171028011642), (20171028173508);
3695+
INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170224233516), (20170226050552), (20170228085250), (20170308214128), (20170308220713), (20170308222552), (20170313130611), (20170318032449), (20170318082740), (20170324194827), (20170424215355), (20170501225441), (20170505224222), (20170526095401), (20170602000208), (20170622205732), (20170626231059), (20170628092119), (20170628213609), (20170629183404), (20170630140136), (20170706132431), (20170707213648), (20170711122252), (20170717092127), (20170725060612), (20170727052644), (20170731130121), (20170814131722), (20170913114958), (20170921014405), (20170925214512), (20170925230419), (20170926134646), (20170927100300), (20170928234412), (20171003134956), (20171003225853), (20171006063358), (20171006161407), (20171012215106), (20171012221231), (20171016125229), (20171016125516), (20171016223356), (20171016235656), (20171017235433), (20171019191035), (20171025184225), (20171026010933), (20171027061833), (20171028011642), (20171028173508), (20171030182857);
36853696

test/lib/code_corps/model/github_repo_test.exs

+39
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,45 @@ defmodule CodeCorps.GithubRepoTest do
33

44
alias CodeCorps.GithubRepo
55

6+
@valid_attrs %{
7+
github_account_avatar_url: "https://avatars.githubusercontent.com/u/6752317?v=3",
8+
github_account_id: 6752317,
9+
github_account_login: "baxterthehacker",
10+
github_account_type: "User",
11+
github_id: 35129377,
12+
name: "public-repo",
13+
}
14+
@invalid_attrs %{}
15+
16+
describe "changeset/2" do
17+
test "with valid attributes" do
18+
changeset = GithubRepo.changeset(%GithubRepo{}, @valid_attrs)
19+
assert changeset.valid?
20+
end
21+
22+
test "with invalid attributes" do
23+
changeset = GithubRepo.changeset(%GithubRepo{}, @invalid_attrs)
24+
refute changeset.valid?
25+
end
26+
end
27+
28+
describe "update_sync_changeset/2" do
29+
test "with valid attributes" do
30+
GithubRepo.sync_states |> Enum.each(fn state ->
31+
attrs = @valid_attrs |> Map.put(:sync_state, state)
32+
changeset = GithubRepo.update_sync_changeset(%GithubRepo{}, attrs)
33+
assert changeset.valid?
34+
end)
35+
end
36+
37+
test "with invalid attributes" do
38+
attrs = @valid_attrs |> Map.put(:sync_state, "not_a_valid_sync_state")
39+
changeset = GithubRepo.update_sync_changeset(%GithubRepo{}, attrs)
40+
refute changeset.valid?
41+
assert changeset.errors[:sync_state] == {"is invalid", [validation: :inclusion]}
42+
end
43+
end
44+
645
test "deletes associated ProjectGithubRepo records when deleting GithubRepo" do
746
github_repo = insert(:github_repo)
847
insert_pair(:project_github_repo, github_repo: github_repo)

0 commit comments

Comments
 (0)