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

Retrieve mandatory email address from Github #128

Closed
wants to merge 6 commits into from

Conversation

Zoly
Copy link
Contributor

@Zoly Zoly commented Jul 22, 2024

The /user API returns only the data the user explicitly set as public and null for those he didn't.

If the user did not set the email address public (me for example), the email address being mandatory for the process, the authentication fails returning an error message.

By using the /user/emails API in conjunction, it is possible to retrieve all the email addresses the user set in Github, regardless if they are set public or not, and select one of those (ex: primary one), for the login process to complete successfully.

Summary by CodeRabbit

  • New Features

    • Enhanced user email retrieval from GitHub, ensuring email information is accurately populated.
    • Introduced a method to fetch user email addresses directly and determine the primary email address.
  • Bug Fixes

    • Improved error handling for email retrieval, increasing robustness in user info management.

Zoly added 6 commits July 20, 2024 09:03
Retrieve the user's email addresses regardless if their status is set public or not
The /user API returns only the data the user explicitly set as public and null for those he didn't. 

If the user did not set the email address public (me for example), the email address being mandatory for the process, the authentication fails returning an error message.

By using the /user/emails API in conjunction, it is possible to retrieve all the email addresses the user set in Github, regardless if they are set public or not, and select one of those (ex: primary one), for the login process to complete successfully.
…-Github

Retrieve mandatory email address from Github
The /user API returns only the data the user explicitly set as public and null for those he didn't.

If the user did not set the email address public (me for example), the email address being mandatory for the process, the authentication fails returning an error message.

By using the /user/emails API in conjunction, it is possible to retrieve all the email addresses the user set in Github, regardless if they are set public or not, and select one of those (ex: primary one), for the login process to complete successfully.
Copy link

coderabbitai bot commented Jul 22, 2024

Walkthrough

The recent updates to the GithubOAuth class significantly improve how user email information is managed. A new API endpoint for fetching user emails is introduced, enhancing the accuracy and reliability of email retrieval. Modifications to existing methods ensure that if user emails are absent from the initial response, they can be fetched seamlessly. Overall, these changes bolster the class's capability to handle user data efficiently.

Changes

Files Change Summary
src/Libraries/GithubOAuth.php Introduced API_USER_EMAILS_URL, modified fetchUserInfoWithToken() to fetch emails when absent, added fetchUserEmailsWithToken() for API calls, and implemented getUserPrimaryEmail() to determine the primary email.

Poem

🐰 In the land of code where the bunnies play,
New email pathways brighten the way!
With tokens and fetches, we hop with glee,
Ensuring each user is as neat as can be.
So here’s to the changes, a joyful cheer,
For emails are gathered, let’s all give a cheer! 🎉


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
src/Libraries/GithubOAuth.php (1)

94-101: Missing Email Retrieval Logic in GoogleOAuth.php

The fetchUserInfoWithToken method in GoogleOAuth.php does not handle cases where the user's email is missing from the initial response. Unlike GithubOAuth.php, which includes logic to fetch the primary email if it is not present, GoogleOAuth.php should implement similar logic to ensure the email is always retrieved.

  • File: src/Libraries/GoogleOAuth.php
    • Add logic to handle missing email similar to GithubOAuth.php.
$userInfo = json_decode($response->getBody(), false);

// the email address is mandatory
if (empty($userInfo->email)) {
    $userInfo->email = $this->getUserPrimaryEmail($this->fetchUserEmailsWithToken());
}

return $userInfo;

Please ensure this logic is implemented to avoid issues with missing email addresses.

Analysis chain

LGTM! Enhanced email retrieval logic.

The changes ensure that if the user's email is not present in the initial response, it fetches the email using the new method fetchUserEmailsWithToken.

However, ensure that all function calls to fetchUserInfoWithToken handle the possibility of the email being retrieved from a secondary source.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `fetchUserInfoWithToken`.

# Test: Search for the function usage. Expect: Proper handling of email retrieval.
rg --type php -A 5 $'fetchUserInfoWithToken'

Length of output: 2158


Script:

#!/bin/bash
# Description: Verify all subclass implementations and usages of `fetchUserInfoWithToken`.

# Find all PHP files that extend the AbstractOAuth class
fd --type f --name '*.php' | xargs grep -l 'extends AbstractOAuth' | xargs -I {} sh -c '
  echo "File: {}"
  # Check the implementation of fetchUserInfoWithToken in the subclasses
  rg --type php -A 5 "fetchUserInfoWithToken" {}
'

Length of output: 485


Script:

#!/bin/bash
# Description: Verify all subclass implementations and usages of `fetchUserInfoWithToken`.

# Find all PHP files and filter those that extend the AbstractOAuth class
fd -e php | xargs grep -l 'extends AbstractOAuth' | while read -r file; do
  echo "File: $file"
  # Check the implementation of fetchUserInfoWithToken in the subclasses
  rg --type php -A 5 "fetchUserInfoWithToken" "$file"
done

Length of output: 1200


Script:

#!/bin/bash
# Description: Verify email retrieval logic in the fetchUserInfoWithToken method.

# Check the email retrieval logic in GithubOAuth.php
echo "File: src/Libraries/GithubOAuth.php"
rg --type php -A 10 "fetchUserInfoWithToken" src/Libraries/GithubOAuth.php

# Check the email retrieval logic in GoogleOAuth.php
echo "File: src/Libraries/GoogleOAuth.php"
rg --type php -A 10 "fetchUserInfoWithToken" src/Libraries/GoogleOAuth.php

Length of output: 1293


Script:

#!/bin/bash
# Description: Retrieve the complete implementation of the fetchUserInfoWithToken method.

# Get the complete implementation in GithubOAuth.php
echo "File: src/Libraries/GithubOAuth.php"
rg --type php -A 20 "fetchUserInfoWithToken" src/Libraries/GithubOAuth.php

# Get the complete implementation in GoogleOAuth.php
echo "File: src/Libraries/GoogleOAuth.php"
rg --type php -A 20 "fetchUserInfoWithToken" src/Libraries/GoogleOAuth.php

Length of output: 1865

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6d42536 and 7f3d664.

Files selected for processing (1)
  • src/Libraries/GithubOAuth.php (2 hunks)
Additional comments not posted (3)
src/Libraries/GithubOAuth.php (3)

27-27: LGTM! New constant for user emails API URL.

The new constant API_USER_EMAILS_URL is correctly defined and aligns with the intended functionality.


107-124: LGTM! New method for fetching user emails.

The new method fetchUserEmailsWithToken is correctly implemented to handle the API call to fetch user emails, including error handling.


129-136: LGTM! New method for determining primary email.

The new method getUserPrimaryEmail correctly identifies the primary email from an array of email addresses, defaulting to the first email if no primary is marked.

@Zoly Zoly deleted the branch datamweb:develop July 22, 2024 03:49
@Zoly Zoly closed this Jul 22, 2024
@Zoly Zoly deleted the develop branch July 22, 2024 03:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant