-
Notifications
You must be signed in to change notification settings - Fork 17
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
Update GithubOAuth.php #123
Conversation
Retrieve the user's email addresses regardless if their status is set public or not
WalkthroughThe changes to the Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant GithubOAuth
participant GitHubAPI
Client->>GithubOAuth: Request user info with token
GithubOAuth->>GitHubAPI: GET /user
GitHubAPI-->>GithubOAuth: Return user info (email may be missing)
alt Email is missing
GithubOAuth->>GitHubAPI: GET /user/emails
GitHubAPI-->>GithubOAuth: Return list of emails
GithubOAuth->>GithubOAuth: Extract primary email or first email
end
GithubOAuth-->>Client: Return enriched user info with email
Poem
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 5
Outside diff range, codebase verification and nitpick comments (2)
src/Libraries/GithubOAuth.php (2)
95-95
: Improve the readability of the comment.The comment can be made more concise and clear.
- * Note: When making a call to /user, the API will only return the email address the user's explicitly set publicly visible - * (or null if the user has not specified a public email address in their profile). + * Note: The /user API returns only the publicly visible email address or null if none is set.
97-98
: Improve the readability of the comment.The comment can be made more concise and clear.
- * The email address is mandatory we have no other choice but to dig deeper. + * Since the email address is mandatory, we need to dig deeper if it's not available.
|
||
$userInfo = json_decode($response->getBody()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check for API request success before processing the response.
The response from the GitHub API should be checked for success before attempting to decode the body. This ensures that the code handles errors gracefully.
+ if ($response->getStatusCode() !== 200) {
+ throw new Exception('Failed to fetch user info from GitHub API');
+ }
$userInfo = json_decode($response->getBody());
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.
$userInfo = json_decode($response->getBody()); | |
if ($response->getStatusCode() !== 200) { | |
throw new Exception('Failed to fetch user info from GitHub API'); | |
} | |
$userInfo = json_decode($response->getBody()); |
* so, we'll choose the one marked as primary | ||
* or at least get the first one just to be covered | ||
*/ | ||
$userInfo->email = $emailAddresses[0]->email; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check for empty email addresses array.
Before accessing the first element of the $emailAddresses
array, ensure it is not empty to avoid potential errors.
+ if (empty($emailAddresses)) {
+ throw new Exception('No email addresses found for the user');
+ }
$userInfo->email = $emailAddresses[0]->email;
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.
$userInfo->email = $emailAddresses[0]->email; | |
if (empty($emailAddresses)) { | |
throw new Exception('No email addresses found for the user'); | |
} | |
$userInfo->email = $emailAddresses[0]->email; |
break; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle potential exceptions more gracefully.
Using exit
for exception handling is not recommended. Instead, consider logging the error and throwing an exception.
- exit($e->getMessage());
+ log_message('error', $e->getMessage());
+ throw new Exception('Error fetching user info with token');
Committable suggestion was skipped due to low confidence.
$response = $this->client->request('GET', self::$API_USER_INFO_URL.'/emails', [ | ||
'headers' => [ | ||
'User-Agent' => self::$APPLICATION_NAME . '/1.0', | ||
'Accept' => 'application/vnd.github+json', | ||
'Authorization' => 'Bearer ' . $this->getToken(), | ||
], | ||
'http_errors' => false, | ||
]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check for API request success before processing the response.
The response from the GitHub API should be checked for success before attempting to decode the body. This ensures that the code handles errors gracefully.
+ if ($response->getStatusCode() !== 200) {
+ throw new Exception('Failed to fetch user emails from GitHub API');
+ }
$emailAddresses = json_decode($response->getBody());
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.
$response = $this->client->request('GET', self::$API_USER_INFO_URL.'/emails', [ | |
'headers' => [ | |
'User-Agent' => self::$APPLICATION_NAME . '/1.0', | |
'Accept' => 'application/vnd.github+json', | |
'Authorization' => 'Bearer ' . $this->getToken(), | |
], | |
'http_errors' => false, | |
]); | |
$response = $this->client->request('GET', self::$API_USER_INFO_URL.'/emails', [ | |
'headers' => [ | |
'User-Agent' => self::$APPLICATION_NAME . '/1.0', | |
'Accept' => 'application/vnd.github+json', | |
'Authorization' => 'Bearer ' . $this->getToken(), | |
], | |
'http_errors' => false, | |
]); | |
if ($response->getStatusCode() !== 200) { | |
throw new Exception('Failed to fetch user emails from GitHub API'); | |
} | |
$emailAddresses = json_decode($response->getBody()); |
foreach ( $emailAddresses as $emailAddress ) { | ||
if ($emailAddress->primary) { | ||
$userInfo->email = $emailAddress->email; | ||
break; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optimize email address selection logic.
The logic for selecting the primary email address can be optimized by using array_filter
and array_shift
.
- foreach ( $emailAddresses as $emailAddress ) {
- if ($emailAddress->primary) {
- $userInfo->email = $emailAddress->email;
- break;
- }
- }
+ $primaryEmail = array_filter($emailAddresses, fn($email) => $email->primary);
+ $userInfo->email = !empty($primaryEmail) ? array_shift($primaryEmail)->email : $emailAddresses[0]->email;
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.
foreach ( $emailAddresses as $emailAddress ) { | |
if ($emailAddress->primary) { | |
$userInfo->email = $emailAddress->email; | |
break; | |
} | |
} | |
$primaryEmail = array_filter($emailAddresses, fn($email) => $email->primary); | |
$userInfo->email = !empty($primaryEmail) ? array_shift($primaryEmail)->email : $emailAddresses[0]->email; |
Retrieve the user's email addresses regardless if the email address status is set public or not
Summary by CodeRabbit