Last active
April 19, 2019 18:26
-
-
Save gabyfle/dd07b51c39627e5038b47efe5037d028 to your computer and use it in GitHub Desktop.
Gets the latest release (includes pre-releases) name (based on Tag names) of a Github-hosted repository
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 | |
/** | |
* Parse the owner's name and the name of the repository (works only with github) | |
* | |
* @param string $repo_url the repositery url | |
* @return array [1] : the owner's name [2] : the repo name | |
*/ | |
function parse_repo_name(string $repo_url) | |
{ | |
$pattern = '/^https?:\/\/(?:www.)?github\.com\/(.*)\/(.*)/'; | |
preg_match($pattern, $repo_url, $matches); | |
return $matches; | |
} | |
/** | |
* Gets the name of the latest's release tag | |
* | |
* @param string $github_repo_link | |
* @return string the latest release name, including pre-releases | |
*/ | |
function get_latest_version(string $github_repo_link) | |
{ | |
$github_infos = parse_repo_name($github_repo_link); | |
$get_options = [ | |
'http' => [ | |
'method' => 'GET', | |
'header' => [ | |
'User-Agent: getting-releases (made by Gabyfle)' | |
] | |
] | |
]; | |
$context = stream_context_create($get_options); | |
$apiRepositoryUrl = 'https://api.github.com/repos/' . $github_infos[1] . '/' . $github_infos[2] . '/releases'; | |
$json = file_get_contents($apiRepositoryUrl, false, $context); | |
$content = json_decode($json, true); | |
$releaseName = $content[0]['name']; | |
return $releaseName; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment