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 #127

Closed
wants to merge 5 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 GitHub OAuth integration now allows retrieval of user email addresses.
    • Added methods for fetching user emails and determining the primary email address.
  • Improvements

    • Updated user info fetching logic to ensure email information is included.

Zoly added 5 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
Copy link

coderabbitai bot commented Jul 22, 2024

Walkthrough

The updates to the GithubOAuth class enhance its ability to manage user email data by introducing a new endpoint for retrieving user email addresses. Key additions include a public static property for the email API URL, a method to fetch user emails when they are not present, and logic to determine the user's primary email address. These improvements streamline integration with GitHub's OAuth services and enrich user data handling.

Changes

Files Change Summary
src/Libraries/GithubOAuth.php Added $API_USER_EMAILS_URL property, new methods fetchUserEmailsWithToken() and getUserPrimaryEmail(), and modified fetchUserInfoWithToken() to enhance email retrieval functionality.

Poem

In the meadow where rabbits play,
New changes hop in, brightening the day!
With emails fetched from GitHub's embrace,
Our class now shines with a smiling face.
So here’s to the code, let’s dance and cheer,
For every new feature brings us near! 🐇✨


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: 1

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

93-100: Update required in GoogleOAuth.php:

The function fetchUserInfoWithToken in GoogleOAuth.php does not include the logic for fetching the user's primary email if it is missing. Please update the function to include this logic, similar to the implementation in GithubOAuth.php.

  • src/Libraries/GoogleOAuth.php:
    • Update fetchUserInfoWithToken to fetch the user's primary email if it is missing.
Analysis chain

LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to fetchUserInfoWithToken correctly handle the new logic for fetching the user's primary email.


Let's verify the implementation of fetchUserInfoWithToken in GoogleOAuth.php and ensure that fetchUserEmailsWithToken and getUserPrimaryEmail methods are correctly implemented and used.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `fetchUserInfoWithToken` correctly handle the new logic for fetching the user's primary email.

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

Length of output: 2158


Script:

#!/bin/bash
# Description: Verify the implementation of `fetchUserInfoWithToken` in `GoogleOAuth.php` and ensure `fetchUserEmailsWithToken` and `getUserPrimaryEmail` methods are correctly implemented and used.

# Search for the implementation of `fetchUserInfoWithToken` in `GoogleOAuth.php`
rg --type php -A 10 $'protected function fetchUserInfoWithToken()' src/Libraries/GoogleOAuth.php

# Search for the `fetchUserEmailsWithToken` method
rg --type php -A 5 $'function fetchUserEmailsWithToken' src/Libraries

# Search for the `getUserPrimaryEmail` method
rg --type php -A 5 $'function getUserPrimaryEmail' src/Libraries

Length of output: 1721

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

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

Files selected for processing (1)
  • src/Libraries/GithubOAuth.php (2 hunks)
Additional context used
GitHub Check: PHP 8.1 Static Analysis
src/Libraries/GithubOAuth.php

[failure] 103-103:
Method Datamweb\ShieldOAuth\Libraries\GithubOAuth::fetchUserEmailsWithToken() has no return type specified.


[failure] 122-122:
Method Datamweb\ShieldOAuth\Libraries\GithubOAuth::getUserPrimaryEmail() has parameter $emailAddresses with no value type specified in iterable type array.

GitHub Check: PHP 8.0 Static Analysis
src/Libraries/GithubOAuth.php

[failure] 103-103:
Method Datamweb\ShieldOAuth\Libraries\GithubOAuth::fetchUserEmailsWithToken() has no return type specified.


[failure] 122-122:
Method Datamweb\ShieldOAuth\Libraries\GithubOAuth::getUserPrimaryEmail() has parameter $emailAddresses with no value type specified in iterable type array.

GitHub Check: PHP 7.4 Static Analysis
src/Libraries/GithubOAuth.php

[failure] 103-103:
Method Datamweb\ShieldOAuth\Libraries\GithubOAuth::fetchUserEmailsWithToken() has no return type specified.


[failure] 122-122:
Method Datamweb\ShieldOAuth\Libraries\GithubOAuth::getUserPrimaryEmail() has parameter $emailAddresses with no value type specified in iterable type array.

Additional comments not posted (1)
src/Libraries/GithubOAuth.php (1)

122-129: Specify the parameter type for getUserPrimaryEmail.

The function parameter lacks a type specification. Adding a type will improve code clarity and type safety.

-    protected function getUserPrimaryEmail(array $emailAddresses): string
+    protected function getUserPrimaryEmail(array $emailAddresses): string

Likely invalid or redundant comment.

Tools
GitHub Check: PHP 8.1 Static Analysis

[failure] 122-122:
Method Datamweb\ShieldOAuth\Libraries\GithubOAuth::getUserPrimaryEmail() has parameter $emailAddresses with no value type specified in iterable type array.

GitHub Check: PHP 8.0 Static Analysis

[failure] 122-122:
Method Datamweb\ShieldOAuth\Libraries\GithubOAuth::getUserPrimaryEmail() has parameter $emailAddresses with no value type specified in iterable type array.

GitHub Check: PHP 7.4 Static Analysis

[failure] 122-122:
Method Datamweb\ShieldOAuth\Libraries\GithubOAuth::getUserPrimaryEmail() has parameter $emailAddresses with no value type specified in iterable type array.

Comment on lines +103 to +120
protected function fetchUserEmailsWithToken()
{
// send request to API URL
try {
$response = $this->client->request('GET', self::$API_USER_EMAILS_URL, [
'headers' => [
'User-Agent' => self::$APPLICATION_NAME . '/1.0',
'Accept' => 'application/vnd.github+json',
'Authorization' => 'Bearer ' . $this->getToken(),
],
'http_errors' => false,
]);
} catch (Exception $e) {
exit($e->getMessage());
}

return json_decode($response->getBody(), false);
}
Copy link

Choose a reason for hiding this comment

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

Specify the return type for fetchUserEmailsWithToken.

The function lacks a return type specification. Adding a return type will improve code clarity and type safety.

-    protected function fetchUserEmailsWithToken()
+    protected function fetchUserEmailsWithToken(): array
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
protected function fetchUserEmailsWithToken()
{
// send request to API URL
try {
$response = $this->client->request('GET', self::$API_USER_EMAILS_URL, [
'headers' => [
'User-Agent' => self::$APPLICATION_NAME . '/1.0',
'Accept' => 'application/vnd.github+json',
'Authorization' => 'Bearer ' . $this->getToken(),
],
'http_errors' => false,
]);
} catch (Exception $e) {
exit($e->getMessage());
}
return json_decode($response->getBody(), false);
}
protected function fetchUserEmailsWithToken(): array
{
// send request to API URL
try {
$response = $this->client->request('GET', self::$API_USER_EMAILS_URL, [
'headers' => [
'User-Agent' => self::$APPLICATION_NAME . '/1.0',
'Accept' => 'application/vnd.github+json',
'Authorization' => 'Bearer ' . $this->getToken(),
],
'http_errors' => false,
]);
} catch (Exception $e) {
exit($e->getMessage());
}
return json_decode($response->getBody(), false);
}
Tools
GitHub Check: PHP 8.1 Static Analysis

[failure] 103-103:
Method Datamweb\ShieldOAuth\Libraries\GithubOAuth::fetchUserEmailsWithToken() has no return type specified.

GitHub Check: PHP 8.0 Static Analysis

[failure] 103-103:
Method Datamweb\ShieldOAuth\Libraries\GithubOAuth::fetchUserEmailsWithToken() has no return type specified.

GitHub Check: PHP 7.4 Static Analysis

[failure] 103-103:
Method Datamweb\ShieldOAuth\Libraries\GithubOAuth::fetchUserEmailsWithToken() has no return type specified.

@Zoly Zoly closed this Jul 22, 2024
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