Skip to content

Instantly share code, notes, and snippets.

@bhubbard
Created November 27, 2024 21:01
Show Gist options
  • Save bhubbard/f9added4d8291c6f09f84aa06d1a0668 to your computer and use it in GitHub Desktop.
Save bhubbard/f9added4d8291c6f09f84aa06d1a0668 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: WP GitLab API
* Plugin URI: https://example.com/wp-gitlab-api
* Description: A WordPress plugin that provides an API wrapper for GitLab.
* Version: 1.0.0
* Author: Your Name
* Author URI: https://example.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-gitlab-api
* Domain Path: /languages
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WP_GitLab_API' ) ) :
/**
* Main WP_GitLab_API Class.
*
* @since 1.0.0
*/
final class WP_GitLab_API {
/**
* Version of the plugin.
*
* @var string
*/
public $version = '1.0.0';
/**
* Instance of this class.
*
* @var object
*/
protected static $instance = null;
/**
* The GitLab API base URL.
*
* @var string
*/
public $api_base_url = 'https://gitlab.com/api/v4/'; // Change if using a self-hosted instance
/**
* The GitLab private token.
*
* @var string
*/
private $private_token;
/**
* Initialize the plugin public actions.
*/
private function __construct() {
// Load plugin text domain.
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
// Set the private token.
$this->private_token = 'YOUR_PRIVATE_TOKEN'; // Replace with your actual GitLab private token
// Register any necessary hooks or actions.
}
/**
* Return an instance of this class.
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Load the plugin text domain for translation.
*/
public function load_plugin_textdomain() {
load_plugin_textdomain( 'wp-gitlab-api', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Magic getter for our object. Allows getting but not setting.
*
* @param string $field
* @return mixed
*/
public function __get( $field ) {
return $this->$field;
}
/**
* Make a request to the GitLab API.
*
* @param string $endpoint The API endpoint.
* @param string $method The HTTP method (GET, POST, PUT, DELETE).
* @param array $params The request parameters.
* @return array The API response.
*/
public function make_request( $endpoint, $method = 'GET', $params = array() ) {
$url = $this->api_base_url . $endpoint;
$args = array(
'method' => $method,
'headers' => array(
'PRIVATE-TOKEN' => $this->private_token,
),
);
if ( 'GET' === $method ) {
$url = add_query_arg( $params, $url );
} else {
$args['body'] = $params;
}
$response = wp_remote_request( $url, $args );
if ( is_wp_error( $response ) ) {
// Handle WP_Error.
error_log( 'GitLab API Error: ' . $response->get_error_message() );
return $response;
}
$status_code = wp_remote_retrieve_response_code( $response );
if ( $status_code < 200 || $status_code >= 300 ) {
// Handle API errors.
$error_message = wp_remote_retrieve_body( $response );
error_log( 'GitLab API Error (' . $status_code . '): ' . $error_message );
return new WP_Error( 'gitlab_api_error', $error_message, array( 'status_code' => $status_code ) );
}
return json_decode( wp_remote_retrieve_body( $response ) );
}
/**
* Get a list of projects.
*
* @param array $params Optional parameters (e.g., page, per_page).
* @return array The list of projects.
*/
public function get_projects( $params = array() ) {
return $this->make_request( 'projects', 'GET', $params );
}
/**
* Get a single project.
*
* @param string $project_id The ID or path of the project.
* @return array The project data.
*/
public function get_project( $project_id ) {
return $this->make_request( 'projects/' . $project_id );
}
/**
* Get the issues of a project.
*
* @param string $project_id The ID or path of the project.
* @param array $params Optional parameters (e.g., state, labels).
* @return array The project issues.
*/
public function get_project_issues( $project_id, $params = array() ) {
return $this->make_request( 'projects/' . $project_id . '/issues', 'GET', $params );
}
// Add other helper methods for specific GitLab API functionalities, e.g.,:
// public function create_issue( $project_id, $title, $description ) { ... }
// public function get_project_branches( $project_id ) { ... }
// public function get_user( $user_id ) { ... }
// ... and so on
}
endif;
/**
* Returns the main instance of WP_GitLab_API.
*
* @return WP_GitLab_API The main instance.
*/
function WP_GitLab_API() {
return WP_GitLab_API::get_instance();
}
// Get WP_GitLab_API Running.
WP_GitLab_API();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment