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

Add GitHub API version HTTP header #336

Merged
merged 5 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


/*
* Base URLs for GitHub
* GitHub defines.
*/
define( 'VIPGOCI_GITHUB_WEB_BASE_URL', 'https://github.com' );

Expand All @@ -35,6 +35,8 @@
define( 'VIPGOCI_GITHUB_BASE_URL', 'https://api.github.com' );
}

define( 'VIPGOCI_GITHUB_API_VERSION', '2022-11-28' );

/*
* Defines for various sizes, such as KB.
*/
Expand Down
21 changes: 21 additions & 0 deletions github-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@

declare(strict_types=1);

/**
* Add GitHub API version header to provided
* array if URL is GitHub.
*
* @param string $http_api_url Request URL to consider.
* @param array $http_headers_arr HTTP headers array.
*
* @return void
*/
function vipgoci_github_api_version_header_maybe_add(
string $http_api_url,
array &$http_headers_arr
) {
if ( true === str_starts_with(
$http_api_url,
VIPGOCI_GITHUB_BASE_URL
) ) {
$http_headers_arr[] = 'X-GitHub-Api-Version: ' . VIPGOCI_GITHUB_API_VERSION;
}
}

/**
* Fetch diffs between two commits from GitHub API,
* cache results.
Expand Down
70 changes: 53 additions & 17 deletions http-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,26 +489,36 @@ function vipgoci_http_api_fetch_url(
'vipgoci_curl_headers'
);

/*
* Set HTTP headers.
*/
$tmp_http_headers_arr = array();

if (
( is_string( $http_api_token ) ) &&
( strlen( $http_api_token ) > 0 )
) {
$tmp_http_headers_arr[] = 'Authorization: token ' . $http_api_token;

} elseif (
( is_array( $http_api_token ) ) &&
( isset( $http_api_token['wpscan_token'] ) )
) {
$tmp_http_headers_arr[] = 'Authorization: Token token=' .
$http_api_token['wpscan_token'];
}

vipgoci_github_api_version_header_maybe_add(
$http_api_url,
$tmp_http_headers_arr
);

if ( ! empty( $tmp_http_headers_arr ) ) {
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array( 'Authorization: token ' . $http_api_token )
$tmp_http_headers_arr
);
} elseif ( is_array( $http_api_token ) ) {
if ( isset( $http_api_token['wpscan_token'] ) ) {
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'Authorization: Token token=' .
$http_api_token['wpscan_token'],
)
);
}
}

vipgoci_curl_set_security_options(
Expand Down Expand Up @@ -769,7 +779,9 @@ function vipgoci_http_api_post_url(
'vipgoci_curl_headers'
);

// Construct HTTP headers to send with the request.
/*
* Set HTTP headers.
*/
$tmp_http_headers_arr = array();

if (
Expand All @@ -788,6 +800,11 @@ function vipgoci_http_api_post_url(
$tmp_http_headers_arr[] = 'Content-Type: ' . $http_content_type;
}

vipgoci_github_api_version_header_maybe_add(
$http_api_url,
$tmp_http_headers_arr
);

if ( ! empty( $tmp_http_headers_arr ) ) {
curl_setopt(
$ch,
Expand Down Expand Up @@ -1072,12 +1089,31 @@ function vipgoci_http_api_put_url(
'vipgoci_curl_headers'
);

curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array( 'Authorization: token ' . $http_api_token )
/*
* Set HTTP headers.
*/
$tmp_http_headers_arr = array();

if (
( is_string( $http_api_token ) ) &&
( strlen( $http_api_token ) > 0 )
) {
$tmp_http_headers_arr[] = 'Authorization: token ' . $http_api_token;
}

vipgoci_github_api_version_header_maybe_add(
$http_api_url,
$tmp_http_headers_arr
);

if ( ! empty( $tmp_http_headers_arr ) ) {
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
$tmp_http_headers_arr
);
}

vipgoci_curl_set_security_options(
$ch
);
Expand Down
74 changes: 74 additions & 0 deletions tests/unit/GitHubVersionHeaderMaybeAddTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Test vipgoci_github_api_version_header_maybe_add() function.
*
* @package Automattic/vip-go-ci
*/

declare(strict_types=1);

namespace Vipgoci\Tests\Unit;

use PHPUnit\Framework\TestCase;

/**
* Class that implements the testing.
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
final class GitHubVersionHeaderMaybeAddTest extends TestCase {
/**
* Setup function. Require file.
*
* @return void
*/
protected function setUp() :void {
require_once __DIR__ . '/../../defines.php';
require_once __DIR__ . '/../../github-api.php';
}

/**
* Test condition when header should not be added to array.
*
* @covers ::vipgoci_github_api_version_header_maybe_add
*
* @return void
*/
public function testVersionHeaderNoneAdded(): void {
$http_headers_arr = array();

vipgoci_github_api_version_header_maybe_add(
'http://127.0.0.1/user',
$http_headers_arr
);

$this->assertSame(
array(),
$http_headers_arr
);
}

/**
* Test condition when header should be added to array.
*
* @covers ::vipgoci_github_api_version_header_maybe_add
*
* @return void
*/
public function testVersionHeaderAdded(): void {
$http_headers_arr = array();

vipgoci_github_api_version_header_maybe_add(
VIPGOCI_GITHUB_BASE_URL . '/user',
$http_headers_arr
);

$this->assertSame(
array(
'X-GitHub-Api-Version: ' . VIPGOCI_GITHUB_API_VERSION,
),
$http_headers_arr
);
}
}