Last active
December 28, 2016 20:11
-
-
Save wturnerharris/a946f6a4a3fbe250197f24c1803a2907 to your computer and use it in GitHub Desktop.
Get commit messages from merge requests, to be run on CLI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( php_sapi_name() !== "cli" ) | |
{ | |
exit(1); | |
} | |
// Curl response headers | |
class CurlResponse | |
{ | |
var $headers; | |
function __construct () { | |
$this->headers = array(); | |
} | |
function parse_header ( $handle, $header ) | |
{ | |
$details = explode(':', $header, 2); | |
if ( count( $details ) == 2 ) | |
{ | |
$key = trim( $details[0] ); | |
$value = trim( $details[1] ); | |
$this->headers[ $key ] = $value; | |
} | |
return strlen( $header ); | |
} | |
} | |
$response = new CurlResponse(); | |
// public configurable | |
$project_id = @getopt("p:"); | |
$private_token = @getopt("t:"); | |
if ( count( $argv ) !== 3 ) | |
{ | |
fwrite(STDERR, "==================\nMissing arguments! \n================== | |
Usage: php gitlab_changelog.php -p=<GITLAB_PROJECT_ID> -t=<GITLAB_TOKEN> | |
\n"); | |
exit(1); | |
} | |
if ( empty( $project_id ) ) | |
{ | |
fwrite(STDERR, "Project ID has not been specified with -p=PROJECT-ID.\n"); | |
exit(1); | |
} | |
if ( empty( $private_token ) ) | |
{ | |
fwrite(STDERR, "Private API token has not been specified with -t=TOKEN.\n"); | |
exit(1); | |
} | |
// details | |
$domain = "https://gitlab.com/"; | |
$api = "api/v3/"; | |
$endpoint = "projects/{$project_id['p']}/merge_requests"; | |
$parameters = "?state=merged&order_by=updated_at&sort=desc&per_page=75"; | |
// setup and execute request | |
$gitlab_api_get_request = $domain . $api . $endpoint . $parameters; | |
$curl_headers = array( | |
"PRIVATE-TOKEN: {$private_token['t']}", | |
); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $gitlab_api_get_request); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_headers); | |
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array(&$response, 'parse_header')); | |
$ce = curl_exec($ch); | |
$data = json_decode( $ce ); | |
if ( !is_array( $data ) ) | |
{ | |
echo "\nFailed!"; | |
if ( !curl_errno( $ch ) ) { | |
$info = curl_getinfo($ch); | |
echo '<h2>Response code: ', curl_getinfo($ch, CURLINFO_HTTP_CODE), "</h2>\n"; | |
echo '<h2>Took ', $info['total_time'], ' seconds to send a request to ', $info['url'], "</h2>\n"; | |
echo '<h2>Executed: ', "curl --header \"{$curl_headers[0]}\" -O $gitlab_api_get_request", "</h2>\n"; | |
var_dump($response->headers, $ce, $data, $info); | |
} | |
curl_close($ch); | |
exit; | |
} else { | |
curl_close($ch); | |
} | |
// MAKEFILE | |
$changelog = "# Change Log\nAll notable changes to this project will be documented in this file.\n\n"; | |
$version = "1.x.x"; | |
foreach( $data as $object ) { | |
$desc = empty($object->description) ? $object->title : $object->description; | |
$changelog .= implode( "\n", array( | |
sprintf("## %s - %s", $version, substr( $object->updated_at, 0, 10 ) ), | |
"### CHANGED: \n", | |
(substr($desc, 0, 1) !== "-" ? "- ".$desc : $desc), | |
"\n" | |
)); | |
} | |
echo $changelog; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment