-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#48] Renaming Repos.Warmer to CacheWarmer and adding basic Backends.…
…Behaviour implemented at Backends.Github for get_org/2, get_members/1, get_repos/1, get_topics/2 and headers/0 (WIP).
- Loading branch information
1 parent
1cb57f5
commit d59ab8a
Showing
6 changed files
with
197 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule Coophub.Backends.Behaviour do | ||
alias Coophub.Schemas.{Organization, Repository} | ||
|
||
require Logger | ||
|
||
@type headers :: [{String.t(), String.t()}] | ||
|
||
@callback get_org(String.t(), Map.t()) :: Organization.t() | :error | ||
@callback get_members(Organization.t()) :: [map] | ||
|
||
@callback get_repos(Organization.t()) :: [Repository.t()] | ||
@callback get_topics(Organization.t(), Repository.t()) :: [String.t()] | ||
|
||
@callback headers() :: headers | ||
|
||
@spec call_api_get(String.t(), headers()) :: {:ok, map | [map]} | {:error, any} | ||
def call_api_get(url, headers) do | ||
case HTTPoison.get(url, headers) do | ||
{:ok, %HTTPoison.Response{status_code: 200, body: body}} -> | ||
{:ok, Jason.decode!(body)} | ||
|
||
{:ok, %HTTPoison.Response{status_code: 404}} -> | ||
{:error, "Not found: #{url}"} | ||
|
||
{:error, %HTTPoison.Error{reason: reason}} -> | ||
{:error, reason} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
defmodule Coophub.Backends.Github do | ||
alias Coophub.Repos | ||
alias Coophub.Backends | ||
alias Coophub.Schemas.{Organization, Repository} | ||
|
||
require Logger | ||
|
||
@behaviour Backends.Behaviour | ||
|
||
@repos_max_fetch Application.get_env(:coophub, :fetch_max_repos) | ||
|
||
######## | ||
## BEHAVIOUR IMPLEMENTATION | ||
######## | ||
|
||
@impl Backends.Behaviour | ||
@spec get_org(String.t(), Map.t()) :: Organization.t() | :error | ||
def get_org(key, _yml_data) do | ||
Logger.info("Fetching '#{key}' organization from github..", ansi_color: :yellow) | ||
|
||
case call_api_get("orgs/#{key}") do | ||
{:ok, org} -> | ||
Logger.info("Fetched '#{key}' organization!", ansi_color: :yellow) | ||
Repos.to_struct(Organization, org) | ||
|
||
{:error, reason} -> | ||
Logger.error("Error getting '#{key}' organization from github: #{inspect(reason)}") | ||
:error | ||
end | ||
end | ||
|
||
@impl Backends.Behaviour | ||
@spec get_members(Organization.t()) :: [map] | ||
# @TODO Isn't fetching all the org members (ie: just 5 for fiqus) | ||
def get_members(%Organization{key: key}) do | ||
Logger.info("Fetching '#{key}' members from github..", ansi_color: :yellow) | ||
|
||
case call_api_get("orgs/#{key}/members") do | ||
{:ok, members} -> | ||
Logger.info("Fetched #{length(members)} '#{key}' members!", ansi_color: :yellow) | ||
members | ||
|
||
{:error, reason} -> | ||
Logger.error("Error getting '#{key}' members from github: #{inspect(reason)}") | ||
[] | ||
end | ||
end | ||
|
||
@impl Backends.Behaviour | ||
@spec get_repos(Organization.t()) :: [Repository.t()] | ||
def get_repos(%Organization{key: key}) do | ||
Logger.info("Fetching '#{key}' repos from github..", ansi_color: :yellow) | ||
path = "orgs/#{key}/repos?per_page=#{@repos_max_fetch}&type=public&sort=pushed&direction=desc" | ||
|
||
case call_api_get(path) do | ||
{:ok, repos} -> | ||
Logger.info("Fetched #{length(repos)} '#{key}' repos!", ansi_color: :yellow) | ||
Repos.to_struct(Repository, repos) | ||
|
||
{:error, reason} -> | ||
Logger.error("Error getting '#{key}' repos from github: #{inspect(reason)}") | ||
[] | ||
end | ||
end | ||
|
||
@impl Backends.Behaviour | ||
@spec get_topics(Organization.t(), Repository.t()) :: [String.t()] | ||
def get_topics(%Organization{key: key}, %Repository{name: name}) do | ||
Logger.info("Fetching '#{key}/#{name}' topics from github..", ansi_color: :yellow) | ||
|
||
case call_api_get("repos/#{key}/#{name}/topics") do | ||
{:ok, data} -> | ||
topics = Map.get(data, "names", []) | ||
Logger.info("Fetched #{length(topics)} '#{key}/#{name}' topics!", ansi_color: :yellow) | ||
topics | ||
|
||
{:error, reason} -> | ||
Logger.error("Error getting '#{key}/#{name}' topics from github: #{inspect(reason)}") | ||
[] | ||
end | ||
end | ||
|
||
@impl Backends.Behaviour | ||
@spec headers() :: Backends.Behaviour.headers() | ||
def headers() do | ||
headers = [ | ||
{"Accept", "application/vnd.github.mercy-preview+json"} | ||
] | ||
|
||
token = System.get_env("GITHUB_OAUTH_TOKEN") | ||
|
||
if is_binary(token) do | ||
[{"Authorization", "token #{token}"} | headers] | ||
else | ||
headers | ||
end | ||
end | ||
|
||
######## | ||
## INTERNALS | ||
######## | ||
|
||
defp call_api_get(path) do | ||
url = "https://api.github.com/#{path}" | ||
Backends.Behaviour.call_api_get(url, headers()) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.