Skip to content

Instantly share code, notes, and snippets.

@bor0
Created February 20, 2023 10:21
Show Gist options
  • Save bor0/416944193a808229aa27eda9170c6152 to your computer and use it in GitHub Desktop.
Save bor0/416944193a808229aa27eda9170c6152 to your computer and use it in GitHub Desktop.
<?php
if ( empty( $argv[2] ) ) {
exit( sprintf( 'Usage: php %s <repo_name:string> <PR#:int>' . PHP_EOL, $argv[0] ) );
}
$repo = $argv[1];
$id = $argv[2];
$token = getenv( 'GH_TOKEN' );
if ( empty( $token ) ) {
exit( 'Set GitHub personal token in the GH_TOKEN env var.' . PHP_EOL );
}
function process_data( $curl_cmd, $urls ) {
foreach ( $urls as $url_data ) {
$url = $url_data['url'];
$file = $url_data['file'];
printf( 'Writing to %s...' . PHP_EOL, $file );
$result = exec( sprintf( $curl_cmd . ' ' . $url . ' > %s', $file ), $output );
if ( false === $result ) {
exit( $output );
}
}
}
$curl_cmd = sprintf( 'curl -H "Accept: application/vnd.github.v3+json" -u %s', $token );
$urls = array(
array(
'url' => sprintf( 'https://api.github.com/repos/%s/pulls/%d', $repo, $id ),
'file' => sprintf( 'pull-%d.json', $id ),
),
array(
'url' => sprintf( 'https://api.github.com/repos/%s/pulls/%d/comments', $repo, $id ),
'file' => sprintf( 'pull-%d-comments.json', $id ),
),
array(
'url' => sprintf( 'https://api.github.com/repos/%s/pulls/%d/commits', $repo, $id ),
'file' => sprintf( 'pull-%d-commits.json', $id ),
),
);
// First process get metadata
process_data( $curl_cmd, $urls );
// Next, get patch
$curl_cmd = sprintf( 'curl -H "Accept: application/vnd.github.VERSION.diff" -u %s', $token );
$urls = array(
array(
'url' => sprintf( 'https://api.github.com/repos/%s/pulls/%d', $repo, $id ),
'file' => sprintf( 'pull-%d.diff', $id ),
),
);
process_data( $curl_cmd, $urls );
@bor0
Copy link
Author

bor0 commented Feb 20, 2023

GH_TOKEN=bor0:<TOKEN> php test.php automattic/wp-calypso 36467

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