summaryrefslogtreecommitdiff
path: root/lib/plugins/link/github.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/plugins/link/github.ex')
-rw-r--r--lib/plugins/link/github.ex87
1 files changed, 57 insertions, 30 deletions
diff --git a/lib/plugins/link/github.ex b/lib/plugins/link/github.ex
index 0069a40..77fa81f 100644
--- a/lib/plugins/link/github.ex
+++ b/lib/plugins/link/github.ex
@@ -3,11 +3,10 @@ defmodule Nola.Plugins.Link.Github do
@impl true
def match(uri = %URI{host: "github.com", path: path}, _) do
- case String.split(path, "/") do
- ["", user, repo] ->
- {true, %{user: user, repo: repo, path: "#{user}/#{repo}"}}
- _ ->
- false
+ with ["", user, repo] <- String.split(path, "/") do
+ {true, %{user: user, repo: repo, path: "#{user}/#{repo}"}}
+ else
+ _ -> false
end
end
@@ -18,32 +17,60 @@ defmodule Nola.Plugins.Link.Github do
@impl true
def expand(_uri, %{user: user, repo: repo}, _opts) do
- case HTTPoison.get("https://api.github.com/repos/#{user}/#{repo}") do
- {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
- {:ok, json} = Jason.decode(body)
- src = json["source"]["full_name"]
- disabled = if(json["disabled"], do: " (disabled)", else: "")
- archived = if(json["archived"], do: " (archived)", else: "")
- fork = if src && src != json["full_name"] do
- " (⑂ #{json["source"]["full_name"]})"
- else
- ""
- end
- start = "#{json["full_name"]}#{disabled}#{archived}#{fork} - #{json["description"]}"
- tags = for(t <- json["topics"]||[], do: "##{t}") |> Enum.intersperse(", ") |> Enum.join("")
- lang = if(json["language"], do: "#{json["language"]} - ", else: "")
- issues = if(json["open_issues_count"], do: "#{json["open_issues_count"]} issues - ", else: "")
- last_push = if at = json["pushed_at"] do
- {:ok, date, _} = DateTime.from_iso8601(at)
- " - last pushed #{DateTime.to_string(date)}"
- else
- ""
- end
- network = "#{lang}#{issues}#{json["stargazers_count"]} stars - #{json["subscribers_count"]} watchers - #{json["forks_count"]} forks#{last_push}"
- {:ok, [start, tags, network]}
- other ->
- :error
+ with {:ok, response} <- HTTPoison.get("https://api.github.com/repos/#{user}/#{repo}"),
+ {:ok, json} <- Jason.decode(response.body) do
+ info = %{
+ full_name: json["full_name"],
+ disabled: json["disabled"],
+ archived: json["archived"],
+ source: json["source"],
+ description: json["description"],
+ topics: json["topics"],
+ language: json["language"],
+ open_issues_count: json["open_issues_count"],
+ pushed_at: json["pushed_at"],
+ stargazers_count: json["stargazers_count"],
+ subscribers_count: json["subscribers_count"],
+ forks_count: json["forks_count"]
+ }
+
+ start = build_start(info)
+ tags = build_tags(info)
+ network = build_network(info)
+
+ {:ok, [start, tags, network]}
+ else
+ _ -> :error
end
end
+ defp build_start(info) do
+ parts = []
+ |> maybe_add(info.disabled, " (disabled)")
+ |> maybe_add(info.archived, " (archived)")
+ |> maybe_add(info.source && info.source["full_name"] != info.full_name, " (⑂ #{info.source["full_name"]})")
+
+ "#{info.full_name}#{parts} - #{info.description}"
+ end
+
+ defp build_tags(info) do
+ for(t <- info.topics || [], do: "##{t}") |> Enum.intersperse(", ") |> Enum.join("")
+ end
+
+ defp build_network(info) do
+ lang = info.language && "#{info.language} - " || ""
+ issues = info.open_issues_count && "#{info.open_issues_count} issues - " || ""
+ last_push =
+ if at = info.pushed_at do
+ {:ok, date, _} = DateTime.from_iso8601(at)
+ " - last pushed #{DateTime.to_string(date)}"
+ else
+ ""
+ end
+ "#{lang}#{issues}#{info.stargazers_count} stars - #{info.subscribers_count} watchers - #{info.forks_count} forks#{last_push}"
+ end
+
+ defp maybe_add(acc, condition, value) do
+ if condition, do: acc ++ [value], else: acc
+ end
end