Skip to content

Instantly share code, notes, and snippets.

@jessp01
Created December 10, 2020 18:16
Show Gist options
  • Save jessp01/9dbeff91a5db894ad8d57ce6aea5e7aa to your computer and use it in GitHub Desktop.
Save jessp01/9dbeff91a5db894ad8d57ce6aea5e7aa to your computer and use it in GitHub Desktop.
Github changelog generator
#!/usr/bin/php
// This script accepts a repo, branch and a personal GH token and generates a changelog consisting of all merged pulls on said branch
// It requires PHP CLI with the cURL extension enabled.
// Best suited for projects where each release is created from a separate branch but might be useful for projects with other release schemes..
<?php
if ($argc<4){
echo __FILE__ . ' <owner> <repo> <branch> <token> '."\n";
exit (1);
}
$owner=$argv[1];
$repo=$argv[2];
$branch=$argv[3];
$token='';
if (isset($argv[4])){
$token=$argv[4];
}
function was_merged($pull_number,$token)
{
global $owner,$repo,$branch,$creds;
$url=GITHUB_API_ENDPOINT."/repos/$owner/$repo/pulls/$pull_number/merge";
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_USERAGENT,'changeloger');
$authorization = "Authorization: Bearer ".$token; // Prepare the authorisation token
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization )); // Inject the token into the header
curl_setopt($ch, CURLOPT_HEADER, 1);
/*if (isset($creds)){
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $creds);
}*/
curl_exec($ch);
$result=curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($result == 204){
return true;
}
return false;
}
define('GITHUB_API_ENDPOINT', 'https://api.github.com');
$url=GITHUB_API_ENDPOINT."/repos/$owner/$repo/pulls?state=closed&base=$branch";
$page=1;
$out="# ChangeLog for $repo-$branch\n";
while (true){
$url.="&page=$page";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch,CURLOPT_USERAGENT,'changeloger');
$authorization = "Authorization: Bearer ".$token; // Prepare the authorisation token
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization )); // Inject the token into the header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$pull_array=json_decode($result);
if (!is_array($pull_array)||empty($pull_array)){
break;
}
foreach ($pull_array as $pull){
if (was_merged($pull->number,$token)){
$out.='- ' . $pull->title .' ('.$pull->html_url.')'."\n";
}
}
$page++;
}
$changelog_file="/tmp/$owner-$repo-$branch-CHANGELOG.md";
file_put_contents($changelog_file,$out);
echo "Changelog written to $changelog_file\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment