summaryrefslogtreecommitdiff
path: root/lib/lsg_irc/link_plugin/github.ex
blob: c7444c2cd1135c9d1032855531f8bbc57e43a7a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
defmodule LSG.IRC.LinkPlugin.Github do
  @behaviour LSG.IRC.LinkPlugin

  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
    end
  end

  def match(_, _), do: false

  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
    end
  end

end