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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions src/Libraries/GithubOAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@

class GithubOAuth extends AbstractOAuth
{
public static string $API_CODE_URL = 'https://github.com/login/oauth/authorize';
public static string $API_TOKEN_URL = 'https://github.com/login/oauth/access_token';
public static string $API_USER_INFO_URL = 'https://api.github.com/user';
private static string $APPLICATION_NAME = 'ShieldOAuth';
public static string $API_CODE_URL = 'https://github.com/login/oauth/authorize';
public static string $API_TOKEN_URL = 'https://github.com/login/oauth/access_token';
public static string $API_USER_INFO_URL = 'https://api.github.com/user'; // The /user API returns the user's publicly visible data or null for those that are not set
public static string $API_USER_EMAILS_URL = 'https://api.github.com/user/emails'; // The /user/emails API returns all email addresses for the user, including those that are not set public
private static string $APPLICATION_NAME = 'ShieldOAuth';
protected string $token;
protected CURLRequest $client;
protected ShieldOAuthConfig $config;
Expand Down Expand Up @@ -89,7 +90,43 @@
exit($e->getMessage());
}

return json_decode($response->getBody());
$userInfo = json_decode($response->getBody(), false);

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

return $userInfo;
}

protected function fetchUserEmailsWithToken()

Check failure on line 103 in src/Libraries/GithubOAuth.php

View workflow job for this annotation

GitHub Actions / PHP 7.4 Static Analysis

Method Datamweb\ShieldOAuth\Libraries\GithubOAuth::fetchUserEmailsWithToken() has no return type specified.

Check failure on line 103 in src/Libraries/GithubOAuth.php

View workflow job for this annotation

GitHub Actions / PHP 8.0 Static Analysis

Method Datamweb\ShieldOAuth\Libraries\GithubOAuth::fetchUserEmailsWithToken() has no return type specified.

Check failure on line 103 in src/Libraries/GithubOAuth.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 Static Analysis

Method Datamweb\ShieldOAuth\Libraries\GithubOAuth::fetchUserEmailsWithToken() has no return type specified.
{
// 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);
}
Comment on lines +103 to +120
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.


protected function getUserPrimaryEmail(array $emailAddresses): string

Check failure on line 122 in src/Libraries/GithubOAuth.php

View workflow job for this annotation

GitHub Actions / PHP 7.4 Static Analysis

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

Check failure on line 122 in src/Libraries/GithubOAuth.php

View workflow job for this annotation

GitHub Actions / PHP 8.0 Static Analysis

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

Check failure on line 122 in src/Libraries/GithubOAuth.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 Static Analysis

Method Datamweb\ShieldOAuth\Libraries\GithubOAuth::getUserPrimaryEmail() has parameter $emailAddresses with no value type specified in iterable type array.
{
// try to get the one marked as primary, otherwise grab the first one
if (! empty($emailAddresses)) {
$primaryEmail = array_filter($emailAddresses, static fn($eMail) => $eMail->primary);
$userEmail = ! empty($primaryEmail) ? array_shift($primaryEmail)->email : array_shift($emailAddresses)->email;
}
return $userEmail ?? '';
}

protected function setColumnsName(string $nameOfProcess, $userInfo): array
Expand Down
Loading