Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GITHUB]: connect #807

Merged
merged 1 commit into from
May 31, 2017
Merged

[GITHUB]: connect #807

merged 1 commit into from
May 31, 2017

Conversation

snewcomer
Copy link
Contributor

This PR is meant to retrieving an access token for a user and updating the User record with their github_id.

Not sure where constants @token_url, @client_id, @client_secret come from.

Progress on: #789

end

defp update_user(%{github_id: _github_id} = github_info, user) do
associate(user, github_info)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just pass the github_id instead of the whole map?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean, pass into associate/2? That would work for now, if that's the only thing we need to pass in. Later on, we might not have just the client ID but instead store a larger set of values, but we can worry about that when we come to it.

That being said, if associate already exists, and tests for it exist, no point i changing it now.

@token_url ""
@client_id ""
@client_secret ""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need help with these.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

client_id and _secret will be environment variables which we will get when we register CodeCorps as a github application. It may even make sense not to have them as module variables, so we can inject them in tests.

Either way, assume the environment variables GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET exist, are loaded during config and stored into a config key.

For @token_url, this should simply be https://github.com/login/oauth/access_token

@snewcomer snewcomer changed the title [GITHUB]: connect initial with no tests [GITHUB]: connect May 11, 2017
@snewcomer
Copy link
Contributor Author

Just looking for some initial feedback and help! Haven't looked at testing, b/c wanted to see first if going down the right path.

@begedin
Copy link
Contributor

begedin commented May 15, 2017

@snewcomer I'm currently in the process of implementing the API key stuff as part of this PR, since, in order to do that, I need to work with Github API keys for CodeCorps. I'll try to implement it in a way so that tests will be reliable, but will not require other developers to provide API keys. Be sure to pull the latest before doing more work on this. I'll make sure to do the same.

@begedin
Copy link
Contributor

begedin commented May 15, 2017

@snewcomer My commit still needs thorough testing and wrapping up, but I managed to get through the basic flow.

  • Renamed user.github_id to user.github_auth_token, since that's a more accurate name.
  • Extracted the actual API calls into a separate module, so we can inject a test module in tests instead.
  • For testing, you can use the test module.
  • In dev, you can temporarily set the test module as the one being used, or you can register a github oauth app on your github profile, add those keys to your .env file (look at .env.example to see what to name them, then run source .env from bash before running the app.
  • At that point, you'd need to
    • visit https://github.com/login/oauth/authorize?scope=user:email&client_id=<your_client_id> to get a code
    • use that code when calling Github.connect(user, code)
      If you can work around this, feel free. Otherwise, I'll be able to wrap it up tomorrow.

Copy link
Contributor Author

@snewcomer snewcomer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So guessing we can remove the tentacat dep? These changes look great!

}

def connect(code) do
with {:ok, %HTTPoison.Response{body: response}} <- code |> build_connect_params() |> do_connect(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@begedin Just curious on the reason not to use Tentacat.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had issues getting it to work. The documented methods do not work as documented. They also raise exceptions when they should not be doing so and are named without exclamation marks.

Due to that, for this specific case, I went with a direct API call.

For the regular stuff, in the integration milestone, we should go with Tentacat.

|> Tentacat.Client.new
|> Tentacat.Users.me
|> update_user(user)
case code |> @api.connect do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I like this move to a separate module for testing

Copy link
Contributor

@joshsmith joshsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments that I think deserve some work before a merge.

do
{:ok, access_token}
else
{:ok, %{"error" => error}} -> {:error, error}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit weird to me that this returns an :ok tuple.

Copy link
Contributor

@npendery npendery May 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, surprised a HTTPoison request with an error response would return an :ok tuple, there should be a better way of differentiating a HTTPoison error and a response error

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is what it is, though. Seems that github itself returns a 200 ok, with an error hash. Not much we can do about it, other than interpret it here.

use Ecto.Migration

def change do
rename table(:users), :github_id, to: :github_auth_token
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you no longer expecting to track the GitHub user ID? I would think this would be important information for us to have on-hand and indexed.

Copy link
Contributor

@begedin begedin May 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not consider that. We'd need it as part of the integration feature. Technically, however, all the connect feature needs is the auth_token.

I'll revert this change and make it so both fields are there.

@@ -97,8 +98,8 @@ defmodule CodeCorps.User do

def github_associate_changeset(struct, params) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not github_association changeset, since it looks like this wants to be a noun and associate in this case is verbed?

@joshsmith
Copy link
Contributor

@snewcomer @begedin are we able to implement the above minor suggestions and then get this merged?

@begedin begedin force-pushed the github-connect branch 3 times, most recently from 0dc0c24 to 2c78d03 Compare May 26, 2017 10:20
Copy link
Contributor

@begedin begedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joshsmith Made the changes, rebased and squashed. This ought to be good to merge.

I've added support for an optional parameter for the connect method, so we can explicityl inject a boundary module when testing. This allows really easy testing for various response cases and is advised to do in http://blog.plataformatec.com.br/2015/10/mocks-and-explicit-contracts/

@joshsmith joshsmith merged commit e072844 into develop May 31, 2017
@joshsmith joshsmith deleted the github-connect branch May 31, 2017 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants