summaryrefslogtreecommitdiff
path: root/lib/nola_plugins/link/github.ex
blob: 0069a401c5c8b9fd4c7908980b71c39c7378adc5 (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
45
46
47
48
49
defmodule Nola.Plugins.Link.Github do
  @behaviour Nola.Plugins.Link

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

  def match(_, _), do: false

  @impl true
  def post_match(_, _, _, _), do: false

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

end