Skip to content

Instantly share code, notes, and snippets.

@weierophinney
Created July 2, 2013 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save weierophinney/5913165 to your computer and use it in GitHub Desktop.
Save weierophinney/5913165 to your computer and use it in GitHub Desktop.
This is a version of the script I use to generate changelogs for my projects.
<?php
/**
* composer.json:
* {
* "require": {
* "zendframework/zend-http": "2.*"
* }
* }
*/
require __DIR__ . '/vendor/autoload.php';
ini_set('display_errors', true);
error_reporting(E_ALL | E_STRICT);
$token = ''; // Github API token
$user = ''; // Your user or organization
$repo = ''; // The repository you're getting the changelog for
$milestone = 0; // The milestone ID
$client = new Zend\Http\Client();
$client->setOptions(array(
'adapter' => 'Zend\Http\Client\Adapter\Curl',
));
$request = $client->getRequest();
$headers = $request->getHeaders();
$headers->addHeaderLine("Authorization", "token $token");
$client->setUri("https://api.github.com/repos/$user/$repo/issues?milestone=$milestone&state=closed&per_page=100");
$client->setMethod('GET');
$issues = array();
$done = false;
$error = false;
$i = 0;
do {
$response = $client->send();
$json = $response->getBody();
$payload = json_decode($json);
if (!is_array($payload)) {
$error = $payload;
}
if (is_array($payload)) {
$issues = array_merge($issues, $payload);
$linkHeader = $response->getHeaders()->get('Link');
if (!$linkHeader) {
$done = true;
}
if ($linkHeader) {
$links = $linkHeader->getFieldValue();
$links = explode(', ', $links);
foreach ($links as $link) {
$matches = array();
if (preg_match('#<(?P<url>.*)>; rel="next"#', $link, $matches)) {
$client->setUri($matches['url']);
}
}
}
}
$i += 1;
} while (!$done && !$error && ($i < 5));
if ($error) {
var_export($error);
exit(0);
}
echo "Total issues resolved: **" . count($issues) . "**\n";
foreach ($issues as $index => $issue) {
$issues[$issue->number] = sprintf('- [%d: %s](%s)', $issue->number, $issue->title, $issue->html_url);
unset($issues[$index]);
}
ksort($issues);
echo implode("\n", $issues) . "";
@weierophinney
Copy link
Author

I've now created a repo for this to make it even easier: https://github.com/weierophinney/changelog_generator

@TomasVotruba
Copy link

Thanks to much for the link to repo! I would not find it otherwise

@weierophinney
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment