Created
February 20, 2023 10:21
-
-
Save bor0/416944193a808229aa27eda9170c6152 to your computer and use it in GitHub Desktop.
This file contains 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 ( 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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
GH_TOKEN=bor0:<TOKEN> php test.php automattic/wp-calypso 36467