Skip to content

Instantly share code, notes, and snippets.

@doomspork
Created April 26, 2018 16:07
Show Gist options
  • Save doomspork/2edf3e64ae229beacccb694cf9191969 to your computer and use it in GitHub Desktop.
Save doomspork/2edf3e64ae229beacccb694cf9191969 to your computer and use it in GitHub Desktop.
defmodule Contributors do
@client %Tentacat.Client{auth: ""}
def commit(user, repo), do: Tentacat.Commits.list(user, repo, @client)
def analysis(commits) do
commits
|> Enum.filter(&is_map/1)
|> Enum.reduce(%{}, &users_first_commit/2)
|> Enum.sort(fn {_, date1}, {_, date2} -> date1 < date2 end)
end
def print(commits) do
commits
|> analysis()
|> Enum.each(fn
{"missing_author", _} -> nil
{username, first_commit} ->
pretty_date =
first_commit
|> DateTime.from_iso8601()
|> elem(1)
|> DateTime.to_date()
|> Date.to_string()
IO.puts("#{username} on #{pretty_date}")
end)
end
defp users_first_commit(%{"author" => %{"login" => author}, "commit" => %{"author" => %{"date" => date}}}, acc) do
{_, new_acc} = Map.get_and_update(acc, author, fn current_value ->
new_value =
cond do
not is_nil(current_value) and current_value < date -> current_value
true -> date
end
{current_value, new_value}
end)
new_acc
end
defp users_first_commit(%{"commit" => %{"author" => %{"date" => date}, "message" => commit_msg}}, acc) do
case Regex.run(~r/from ([\w-]+)\//, commit_msg) do
[_match, username] -> users_first_commit(%{"author" => %{"login" => username}, "commit" => %{"author" => %{"date" => date}}}, acc)
nil ->
missing_author = Map.get(acc, "missing_author", [])
Map.put(acc, "missing_author", [{commit_msg, date}|missing_author])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment