@@ -6,31 +6,45 @@ defmodule Algora.Workspace.Repository do
6
6
7
7
@ derive { Inspect , except: [ :provider_meta ] }
8
8
typed_schema "repositories" do
9
- field :provider , :string
10
- field :provider_id , :string
11
- field :provider_meta , :map
9
+ field :provider , :string , null: false
10
+ field :provider_id , :string , null: false
11
+ field :provider_meta , :map , null: false
12
12
13
- field :name , :string
14
- field :url , :string
13
+ field :name , :string , null: false
14
+ field :url , :string , null: false
15
+ field :description , :string
16
+ field :og_image_url , :string , null: false
17
+ field :og_image_updated_at , :utc_datetime_usec
15
18
16
19
has_many :tickets , Algora.Workspace.Ticket
17
- belongs_to :user , Algora.Accounts.User
20
+ belongs_to :user , Algora.Accounts.User , null: false
18
21
19
22
timestamps ( )
20
23
end
21
24
25
+ defp og_image_base_url , do: "https://opengraph.githubassets.com"
26
+
27
+ def has_default_og_image? ( % Repository { } = repository ) ,
28
+ do: String . starts_with? ( repository . og_image_url , og_image_base_url ( ) )
29
+
30
+ def default_og_image_url ( repo_owner , repo_name ) , do: "#{ og_image_base_url ( ) } /0/#{ repo_owner } /#{ repo_name } "
31
+
22
32
def github_changeset ( meta , user ) do
23
33
params = % {
24
34
provider_id: to_string ( meta [ "id" ] ) ,
25
35
name: meta [ "name" ] ,
36
+ description: meta [ "description" ] ,
37
+ og_image_url: default_og_image_url ( meta [ "owner" ] [ "login" ] , meta [ "name" ] ) ,
38
+ og_image_updated_at: DateTime . utc_now ( ) ,
26
39
url: meta [ "html_url" ] ,
27
40
user_id: user . id
28
41
}
29
42
30
43
% Repository { provider: "github" , provider_meta: meta }
31
- |> cast ( params , [ :provider_id , :name , :url , :user_id ] )
44
+ |> cast ( params , [ :provider_id , :name , :url , :description , :og_image_url , :og_image_updated_at , : user_id] )
32
45
|> generate_id ( )
33
46
|> validate_required ( [ :provider_id , :name , :url , :user_id ] )
47
+ |> foreign_key_constraint ( :user_id )
34
48
|> unique_constraint ( [ :provider , :provider_id ] )
35
49
end
36
50
end
0 commit comments