Skip to content
Open
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
2 changes: 2 additions & 0 deletions github-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@
*/

// Require necessary classes.
require_once 'traits/github-api-wrapper.php';
require_once 'models/github-project.php';
require_once 'models/github-column.php';
117 changes: 117 additions & 0 deletions models/github-column.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
/**
* XPress Github Column Model Class
*
* @package XPress
* @subpackage MVC
* @author Trasgo Furioso
* @license GPLv2
* @since 0.1.0
*/

class XPress_Github_Column_Model extends XPress_MVC_Model {
/**
* Common methods for querying Github API.
*
* @since 0.1.0
*/
use XPress_Github_API_Wrapper;

/**
* Model schema.
*
* @since 0.1.0
* @var array
*/
static protected $schema = array(
'project_id' => array(
'description' => 'Project Id',
'type' => 'string',
'required' => true,
),
'id' => array(
'description' => 'Id',
'type' => 'number',
),
'name' => array(
'description' => 'Name',
'type' => 'string',
'required' => true,
),
'url' => array(
'description' => 'Url',
'type' => 'string',
),
'project_url' => array(
'description' => 'Project url',
'type' => 'string',
),
'cards_url' => array(
'description' => 'Cards url',
'type' => 'string',
),
'created_at' => array(
'description' => 'Created at',
'type' => 'string',
),
'updated_at' => array(
'description' => 'Updated at',
'type' => 'string',
),
);

/**
* Return a model instance for a specific item.
*
* @since 0.1.0
*
* @return XPress_Github_Model instance.
*/
static function get( $id ) {
}

/**
* Return a model instance collection filtered by the params.
*
* @since 0.1.0
*
* @return array XPress_Github_Model instance collection.
*/
static function find( $params ) {
$result = array();

$url = static::construct_url( 'projects', $params['project_id'], 'columns' );

unset( $params['project_id'] );

$json = static::make_request( $url, array(
'body' => $params,
) );

foreach ( $json as $value ) {
$result[] = static::new( $value );
}

return $result;
}

/**
* Persists the current model instance.
*
* @since 0.1.0
*
* @return XPress_Github_Model instance.
*/
public function save() {
}

/**
* Deleted the current model instance.
*
* @since 0.1.0
*
* @return XPress_Github_Model instance.
*/
public function delete() {
}
}
63 changes: 8 additions & 55 deletions models/github-project.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
*/

class XPress_Github_Project_Model extends XPress_MVC_Model {
/**
* Common methods for querying Github API.
*
* @since 0.1.0
*/
use XPress_Github_API_Wrapper;

/**
* Model schema.
*
Expand Down Expand Up @@ -166,60 +173,6 @@ public function delete() {
'method' => 'DELETE',
) );

return false === $response ? false : true;
}

/**
* Make a request to Github API.
*
* @since 0.1.0
*
* @return array Response as associative array.
*/
static function make_request( $url, $params=array() ) {
$request_args = wp_parse_args( $params, array(
'method' => 'GET',
'httpversion' => '1.1',
'compress' => true,
'decompress' => true,
'timeout' => 10,
'body' => null,
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( GITHUB_AUTH_USER . ':' . GITHUB_AUTH_TOKEN ),
'Accept' => 'application/vnd.github.inertia-preview+json',
'Content-Type' => 'application/json',
),
) );

// var_dump($url);
// var_dump($request_args);

$response = wp_safe_remote_request( $url, $request_args );
$code = wp_remote_retrieve_response_code( $response );
$headers = wp_remote_retrieve_headers( $response );
$body = wp_remote_retrieve_body( $response );

// var_dump($code);
// var_dump($headers);
// var_dump($body);

if ( $code >= 200 && $code < 300 ) {
$json = json_decode( $body, true );
return $json;
} else {
return false;
}
}

/**
* Construct the request url for Github.
*
* @since 0.1.0
*
* @return string Url for request.
*/
static function construct_url( ...$parts ) {
array_unshift( $parts, 'https://api.github.com' );
return join( '/', $parts );
return empty( $response ) ? false : true;
}
}
57 changes: 57 additions & 0 deletions tests/test-github-column-model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Github Column Model Test
*
* @package XPress
* @subpackage MVC
* @author Trasgo Furioso
* @license GPLv2
* @since 0.1.0
*/

class XPress_Github_Column_Model_Test extends WP_UnitTestCase {
/**
* Test suite setup.
*/
function setUp() {
if ( ! defined( 'GITHUB_AUTH_USER' ) ) {
define( 'GITHUB_AUTH_USER', getenv( 'GITHUB_AUTH_USER' ) );
}
if ( ! defined( 'GITHUB_AUTH_TOKEN' ) ) {
define( 'GITHUB_AUTH_TOKEN', getenv( 'GITHUB_AUTH_TOKEN' ) );
}
}

/**
* Create an instance.
*/
function test_new_instance() {
$this->assertInstanceOf( XPress_Github_Column_Model::class, XPress_Github_Column_Model::new() );
}

/**
* Get a list of columns for a project.
* Columns should be XPress_Github_Column_Model instances.
*/
function test_find() {
$columns = XPress_Github_Column_Model::find( array(
'project_id' => '1275863',
) );

$this->assertInternalType( 'array', $columns );
$this->assertCount( 3, $columns );
$this->assertInstanceOf( XPress_Github_Column_Model::class, $columns[0] );
}

/**
* No matching criteria should return empty array.
*/
function test_find_404() {
$projects = XPress_Github_Column_Model::find( array(
'project_id' => '0',
) );

$this->assertInternalType( 'array', $projects );
$this->assertCount( 0, $projects );
}
}
76 changes: 76 additions & 0 deletions traits/github-api-wrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* XPress Github API Wrapper
* To be included in all models that queries the Github API.
*
* @package XPress
* @subpackage MVC
* @author Trasgo Furioso
* @license GPLv2
* @since 0.1.0
*/

trait XPress_Github_API_Wrapper {
/**
* Make a request to Github API.
*
* @since 0.1.0
*
* @return array Response as associative array.
*/
static function make_request( $url, $params=array() ) {
$request_args = wp_parse_args( $params, array(
'method' => 'GET',
'httpversion' => '1.1',
'compress' => true,
'decompress' => true,
'timeout' => 10,
'body' => null,
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( GITHUB_AUTH_USER . ':' . GITHUB_AUTH_TOKEN ),
'Accept' => 'application/vnd.github.inertia-preview+json',
'Content-Type' => 'application/json',
),
) );

// var_dump($url);
// var_dump($request_args);

$response = wp_safe_remote_request( $url, $request_args );
$code = wp_remote_retrieve_response_code( $response );
$headers = wp_remote_retrieve_headers( $response );
$body = wp_remote_retrieve_body( $response );

// var_dump($code);
// var_dump($headers);
// var_dump($body);

if ( $code >= 200 && $code < 300 ) {
if ( 204 === $code ) {
// An empty array confuses delete method.
$json = array( '204 No Content' );
} else {
$json = json_decode( $body, true );
}
}
elseif ( 404 === $code ) {
$json = array();
} else {
$json = false;
}

return $json;
}

/**
* Construct the request url for Github.
*
* @since 0.1.0
*
* @return string Url for request.
*/
static function construct_url( ...$parts ) {
array_unshift( $parts, 'https://api.github.com' );
return join( '/', $parts );
}
}