Last active
May 10, 2022 14:23
-
-
Save bor0/8127b0fe03527a8e7fe06ec8d39a8028 to your computer and use it in GitHub Desktop.
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 | |
ini_set( 'memory_limit', '4G' ); | |
define( 'GREENHOUSE_USER_ID', getenv( 'GREENHOUSE_USER_ID' ) ); | |
define( 'GREENHOUSE_TOKEN', getenv( 'GREENHOUSE_TOKEN' ) ); | |
function create_curl_handle( $page, $params = array() ) { | |
$ch = curl_init(); | |
$url = sprintf( 'https://harvest.greenhouse.io/v1/candidates?per_page=500&page=%d', $page ); | |
curl_setopt( $ch, CURLOPT_URL, $url ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 30 ); | |
curl_setopt( $ch, CURLOPT_TIMEOUT, 30 ); | |
curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 ); | |
$params[ CURLOPT_HTTPHEADER ][] = 'Authorization: Basic ' . base64_encode( GREENHOUSE_TOKEN . ':' ); | |
$params[ CURLOPT_HTTPHEADER ][] = 'On-Behalf-Of: ' . GREENHOUSE_USER_ID; | |
foreach ( $params as $key => $value ) { | |
curl_setopt( $ch, $key, $value ); | |
} | |
return $ch; | |
} | |
function gh_request( $offset, $parallel_requests ) { | |
$mh = curl_multi_init(); | |
$handles = []; | |
foreach ( range( $offset * $parallel_requests + 1, $offset * $parallel_requests + 1 + $parallel_requests ) as $page ) { | |
$handles[ $page ] = create_curl_handle( $page ); | |
} | |
foreach ( $handles as $handle ) { | |
curl_multi_add_handle( $mh, $handle ); | |
} | |
$running = null; | |
do { | |
curl_multi_exec( $mh, $running ); | |
} while ( $running ); | |
foreach ( $handles as $handle ) { | |
curl_multi_remove_handle( $mh, $handle ); | |
} | |
curl_multi_close( $mh ); | |
return $handles; | |
} | |
function has_active_application( $entry ) { | |
foreach ( $entry['applications'] as $application ) { | |
if ( 'active' === $application['status'] ) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function process_map( $handles ) { | |
return array_map( function( $handle, $page ) { | |
$response = curl_multi_getcontent( $handle ); | |
$result = json_decode( $response, true ); | |
if ( JSON_ERROR_NONE !== json_last_error() ) { | |
var_dump( "Failed to fetch " . $page ); | |
return array(); | |
// throw new Exception( 'Failed to parse returning JSON from the API' ); | |
} | |
return $result; | |
}, $handles, array_keys( $handles ) ); | |
} | |
function process_reducer( $results ) { | |
return call_user_func_array( 'array_merge', $results ); | |
} | |
$entries = []; | |
for ( $i = 0; ; $i++ ) { | |
printf( "Processing offset %d...\n", $i ); | |
$results = gh_request( $i, 50 ); | |
$results = process_map( $results ); | |
$results = process_reducer( $results ); | |
if ( empty( $results ) ) { | |
break; | |
} | |
// Only select those candidates that have a GitHub username assigned | |
$results = array_filter( $results, function( $entry ) { | |
return ! empty( $entry['keyed_custom_fields']['git_hub_username']['value'] ); | |
} ); | |
// Only retrieve the necessary fields | |
$results = array_map( function( $entry ) { | |
return [ | |
'id' => $entry['id'], | |
'github' => $entry['keyed_custom_fields']['git_hub_username']['value'], | |
'active' => has_active_application( $entry ), | |
]; | |
}, $results ); | |
// Only select those candidates that have no active applications | |
$results = array_filter( $results, function( $entry ) { | |
return ! $entry['active']; | |
} ); | |
$results = array_map( function( $entry ) { | |
unset( $entry['active'] ); // no need to display that | |
return implode( ',', $entry ); | |
}, $results ); | |
$entries = array_merge( $entries, $results ); | |
break; | |
} | |
var_dump( $entries ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment