-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor fixes to router and config for events
- Loading branch information
Showing
34 changed files
with
864 additions
and
69 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
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,20 @@ | ||
defmodule CodeCorps.GitHub.Adapters.GithubRepo do | ||
def from_api(%{"owner" => owner} = payload) do | ||
%{} | ||
|> Map.merge(payload |> adapt_base()) | ||
|> Map.merge(owner |> adapt_owner()) | ||
end | ||
|
||
defp adapt_base(%{"id" => id, "name" => name}) do | ||
%{github_id: id, name: name} | ||
end | ||
|
||
defp adapt_owner(%{"id" => id, "avatar_url" => avatar_url, "login" => login, "type" => type }) do | ||
%{ | ||
github_account_id: id, | ||
github_account_avatar_url: avatar_url, | ||
github_account_login: login, | ||
github_account_type: type | ||
} | ||
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
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,84 @@ | ||
defmodule CodeCorps.GitHub.Installation do | ||
@moduledoc """ | ||
Used to perform installation actions on the GitHub API. | ||
""" | ||
|
||
alias CodeCorps.{ | ||
GitHub, | ||
GitHub.APIError, | ||
GitHub.Request, | ||
GithubAppInstallation, | ||
Repo | ||
} | ||
|
||
@doc """ | ||
List repositories that are accessible to the authenticated installation. | ||
https://developer.github.com/v3/apps/installations/#list-repositories | ||
""" | ||
@spec repositories(GithubAppInstallation.t) :: {:ok, list} | {:error, APIError.t} | ||
def repositories(%GithubAppInstallation{} = installation) do | ||
endpoint = "installation/repositories" | ||
{:ok, access_token} = installation |> get_access_token() | ||
case Request.retrieve(endpoint, [access_token: access_token]) do | ||
{:error, %APIError{} = error} -> {:error, error} | ||
{:ok, %{"total_count" => _, "repositories" => repositories}} -> {:ok, repositories} | ||
end | ||
end | ||
|
||
@doc """ | ||
Get the access token for the installation. | ||
Returns either the current access token stored in the database because | ||
it has not yet expired, or makes a request to the GitHub API for a new | ||
access token using the GitHub App's JWT. | ||
https://developer.github.com/apps/building-integrations/setting-up-and-registering-github-apps/about-authentication-options-for-github-apps/#authenticating-as-an-installation | ||
""" | ||
@spec get_access_token(GithubAppInstallation.t) :: {:ok, String.t} | {:error, APIError.t} | {:error, Ecto.Changeset.t} | ||
def get_access_token(%GithubAppInstallation{access_token: token, access_token_expires_at: expires_at} = installation) do | ||
case token_expired?(expires_at) do | ||
true -> installation |> refresh_token() | ||
false -> {:ok, token} # return the existing token | ||
end | ||
end | ||
|
||
@doc """ | ||
Refreshes the access token for the installation. | ||
Makes a request to the GitHub API for a new access token using the GitHub | ||
App's JWT. | ||
https://developer.github.com/apps/building-integrations/setting-up-and-registering-github-apps/about-authentication-options-for-github-apps/#authenticating-as-an-installation | ||
""" | ||
@spec refresh_token(GithubAppInstallation.t) :: {:ok, String.t} | {:error, APIError.t} | {:error, Ecto.Changeset.t} | ||
def refresh_token(%GithubAppInstallation{github_id: installation_id} = installation) do | ||
endpoint = "installations/#{installation_id}/access_tokens" | ||
with {:ok, %{"token" => token, "expires_at" => expires_at}} <- | ||
GitHub.authenticated_integration_request(%{}, :post, endpoint, %{}, []), | ||
{:ok, %GithubAppInstallation{}} <- | ||
update_token(installation, token, expires_at) | ||
do | ||
{:ok, token} | ||
else | ||
{:error, error} -> {:error, error} | ||
end | ||
end | ||
|
||
@spec update_token(GithubAppInstallation.t, String.t, String.t) :: {:ok, GithubAppInstallation.t} | {:error, Ecto.Changeset.t} | ||
defp update_token(%GithubAppInstallation{} = installation, token, expires_at) do | ||
installation | ||
|> GithubAppInstallation.access_token_changeset(%{access_token: token, access_token_expires_at: expires_at}) | ||
|> Repo.update | ||
end | ||
|
||
@doc false | ||
@spec token_expired?(String.t | DateTime.t | nil) :: true | false | ||
def token_expired?(expires_at) when is_binary(expires_at) do | ||
expires_at | ||
|> Timex.parse!("{ISO:Extended:Z}") | ||
|> token_expired?() | ||
end | ||
def token_expired?(%DateTime{} = expires_at), do: Timex.now > expires_at | ||
def token_expired?(nil), do: true | ||
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
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,20 @@ | ||
defmodule CodeCorps.Repo.Migrations.AddGithubRepos do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:github_repos) do | ||
add :github_id, :integer | ||
add :name, :string | ||
add :github_account_id, :integer | ||
add :github_account_login, :string | ||
add :github_account_avatar_url, :string | ||
add :github_account_type, :string | ||
|
||
add :github_app_installation_id, references(:github_app_installations, on_delete: :nothing) | ||
|
||
timestamps() | ||
end | ||
|
||
create index(:github_repos, [:github_app_installation_id]) | ||
end | ||
end |
10 changes: 10 additions & 0 deletions
10
priv/repo/migrations/20170628213609_add_access_tokens_to_github_app_installations.exs
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,10 @@ | ||
defmodule CodeCorps.Repo.Migrations.AddAccessTokensToGithubAppInstallations do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:github_app_installations) do | ||
add :access_token, :string | ||
add :access_token_expires_at, :utc_datetime | ||
end | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
...o/migrations/20170629183404_add_unique_index_on_github_id_to_github_app_installations.exs
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,7 @@ | ||
defmodule CodeCorps.Repo.Migrations.AddUniqueIndexOnGithubIdToGithubAppInstallations do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create unique_index(:github_app_installations, :github_id) | ||
end | ||
end |
Oops, something went wrong.